def gen_last_pit_time(file_name): if file_name != 'N/A': with open(file_name, 'r') as passings_csv: passings_obj = csv.reader(passings_csv) passings_dict = {} for passing in passings_obj: try: if 'P' in passing[3]: if passing[1] in passings_dict: if passings_dict[passing[1]] < calc_millisec( passing[7]): passings_dict[passing[1]] = calc_millisec( passing[7]) elif passing[1] != '': passings_dict[passing[1]] = calc_millisec( passing[7]) except IndexError: time.sleep(1) return gen_last_pit_time(file_name) return passings_dict else: log_print( 'Fatal Error: attempting to generate last pit time with no passings file name given' ) log_print('Ending program')
def get_restart(self): restart_input = (input('Has the race started? Y/N: ')).upper() if restart_input == 'Y': return True elif restart_input == 'N': return False else: log_print( 'Invalid Input \nPlease enter either "Y" (Yes) or "N" (No)') return self.get_restart()
def get_mod_time(self, tries_count): if tries_count > 30: log_print( 'Cannot find file, please make sure that the file is named exactly "{filename}"' .format(filename=self.passings_file_name)) self.get_passings_export() else: try: result = os.path.getmtime(self.passings_file_name) return result except FileNotFoundError: if tries_count % 5 == 0: log_print('Waiting for {filename} export...'.format( filename=self.passings_file_name)) tries_count += 1 time.sleep(2) return self.get_mod_time(tries_count)
def start_abnormal_lap_check(team_obj_dict): leader_board = get_leader_board() if len(team_obj_dict) < len(leader_board): new_driver = add_driver(team_obj_dict, leader_board) team_obj_dict[new_driver.reg_num] = new_driver log_print('{carnum} added to Lap-Checker'.format( carnum=team_obj_dict[new_driver.car_num])) for driver_key in team_obj_dict: driver = team_obj_dict[driver_key] if driver.reg_num not in leader_board: instantiate_team_lap_check() log_print('Reinstantiating TeamLapCheck') break driver_last_time = leader_board[driver.reg_num]['last_time'] if (driver_last_time != '') and (driver_last_time != 'IN PIT'): new_lap_time = calc_millisec(driver_last_time) driver.check_time(new_lap_time) time.sleep(refreshrate)
def check_time(self, last_time): self.flag = get_race_data()['flag'] # refresh to current flag status not_triggered = (not self.msg_triggered) and ( not self.drop_out_triggered) if self.flag == 'green': # ignore abnormal laps when not green self.refresh_info() not_out_lap = self.laps_since_pit > 1 # only checks laps after pit out lap if not_out_lap: best_time_ms = calc_millisec(self.best_lap_time) abnormal_short = best_time_ms + (60 * 1000) <= last_time < ( best_time_ms * 2) over_double = ( best_time_ms * 2) <= last_time < (best_time_ms * 2) + (30 * 1000) abnormal_long = (best_time_ms * 2) + ( 30 * 1000) <= last_time < (20 * 60 * 1000) - last_time if abnormal_short and not_triggered: self.message_trigger() log_print( 'Car {carnum} is 1 min over their avg laptime at {time}' .format(carnum=self.car_num, time=self.total_time)) elif over_double and not_triggered: self.message_trigger() log_print( 'Car {carnum} has a lap twice their avg at {time}'. format(carnum=self.car_num, time=self.total_time)) elif abnormal_long and not_triggered: self.message_trigger() log_print( 'Car {carnum} has an abnormally long lap at: {time}'. format(carnum=self.car_num, time=self.total_time)) elif self.normal_lap_check(): self.normal_lap() if self.drop_out_check() and not_triggered: self.drop_out() log_print( 'Car {carnum} has not hit for 20 mins, last crossing at {time}' .format(carnum=self.car_num, time=self.total_time))
def get_passings_export(self): log_print("Please export {filename}".format( filename=self.passings_file_name)) if abs(self.get_mod_time(0) - time.time()) < 3: log_print('Export Found!') time.sleep(1) else: log_print( "{filename} is too old. Deleting {filename}, wait for prompt to export" .format(filename=self.passings_file_name)) os.remove(self.passings_file_name) self.get_passings_export()
def start_driver_stint_check(driver_stint_dict): stint_info = get_stint_info() if len(driver_stint_dict) < len(stint_info): new_driver = add_driver(driver_stint_dict, stint_info) driver_stint_dict[new_driver.reg_num] = new_driver log_print('{carnum} added to Stint-Checker'.format( carnum=new_driver.car_num)) for driver_key in driver_stint_dict: driver = driver_stint_dict[driver_key] if driver.reg_num not in stint_info: log_print( 'Car {carnum} is missing from the scoreboard feed and is no longer being monitored!' .format(carnum=driver.car_num)) driver_stint_dict.pop(driver_key) # this needs testing break new_driver_info = stint_info[driver.reg_num] driver.last_time_line = new_driver_info['last_time_line'] new_lap_time = calc_millisec(new_driver_info['total_time']) if driver.last_time_line == "Start/Finish": driver.pit_msg_sent = False if driver.stint_check( new_lap_time) and not driver.over_stint_triggered: driver.over_stint() current_time_stamp = fix_time( stint_info[driver.reg_num]['total_time']) log_print( '{carnum} is nearing their 2 hour driver stint at {time}'. format(carnum=driver.car_num, time=current_time_stamp)) else: driver.pit_stop(new_driver_info['last_time_line'], new_lap_time) if not driver.pit_msg_sent: log_only('Pit Stop: {carnum} at {time}'.format( carnum=driver.car_num, time=new_driver_info['total_time'])) driver.pit_msg_sent = True time.sleep(refresh_rate)
def start_monitors(self): log_print('Initiating Monitors') while True: start_driver_stint_check(self.driver_stint_dict) start_abnormal_lap_check(self.abnormal_lap_dict)
def __init__(self, restart, passings_file_name): log_print('Populating Driver Info') self.driver_stint_dict = start_dsc_instantiation( restart, passings_file_name) self.abnormal_lap_dict = instantiate_team_lap_check()