From 8c9567d23f9de9d5909f4562ce67b4317374daf6 Mon Sep 17 00:00:00 2001 From: Juice-XIJ Date: Tue, 14 Jun 2022 22:36:31 -0700 Subject: [PATCH] auto accel while farming (#48) * running logic update * auto accel while farming --- forza.py | 81 ++++++++++++++++++++++++++++++++++++++------------------ gui.py | 1 + 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/forza.py b/forza.py index cade3bb..5c34aa1 100644 --- a/forza.py +++ b/forza.py @@ -75,6 +75,11 @@ def __init__(self, threadPool: ThreadPoolExecutor, logger: Logger = None, packet self.last_upshift = time.time() self.last_downshift = time.time() + # === exp farm setting === + self.reset_car = 0 + self.reset_timer = time.time() + self.break_timer = time.time() + def test_gear(self, update_car_gui_func=None): """collect gear information @@ -152,13 +157,16 @@ def analyze(self, performance_profile: bool = True, is_gui: bool = False): finally: self.logger.debug(f'{self.analyze.__name__} ended') - def __update_forza_info(self, fdp: ForzaDataPacket, update_tree_func=lambda *args: None, dump: bool = True): + def __update_forza_info(self, fdp: ForzaDataPacket, update_tree_func=lambda *args: None, dump: bool = True, first_load: bool = False): """update forza info while running + # try to load config if: + # self.ordinal != fdp.car_ordinal or self.car_perf != fdp.car_performance_index or self.car_class != fdp.car_class or self.car_drivetrain != fdp.drivetrain_type + Args: fdp (ForzaDataPacket): datapackage """ - if len(self.shift_point) <= 0 or self.ordinal != fdp.car_ordinal or self.car_perf != fdp.car_performance_index or self.car_class != fdp.car_class or self.car_drivetrain != fdp.drivetrain_type: + if first_load or self.ordinal != fdp.car_ordinal or self.car_perf != fdp.car_performance_index or self.car_class != fdp.car_class or self.car_drivetrain != fdp.drivetrain_type: self.ordinal = fdp.car_ordinal self.car_perf = fdp.car_performance_index self.car_class = fdp.car_class @@ -167,8 +175,12 @@ def __update_forza_info(self, fdp: ForzaDataPacket, update_tree_func=lambda *arg if dump: res = self.__try_auto_load_config(fdp) + if not res: + self.shift_point = {} + if update_tree_func is not None: self.threadPool.submit(update_tree_func) + return res else: return True @@ -270,13 +282,9 @@ def shifting(self, iteration, fdp): [int]: iteration """ gear = fdp.gear - if fdp.speed > 0.1 and gear >= self.minGear: + if len(self.shift_point) > 0 and fdp.speed > 0.1 and gear >= self.minGear: iteration = iteration + 1 - # exp or sp farming to avoid afk detection - if self.farming and iteration % 800 == 0: - self.threadPool.submit(keyboard_helper.press_brake, self) - # prepare shifting params slip = (fdp.tire_slip_ratio_RL + fdp.tire_slip_ratio_RR) / 2 f_slip = (fdp.tire_slip_ratio_FL + fdp.tire_slip_ratio_FR) / 2 @@ -365,6 +373,28 @@ def __down_shift(self, speed, target_down_speed, slips, iteration, gear, fdp): self.logger.debug(f'[{iteration}] down shift triggered. speed < target down speed ({speed} < {target_down_speed}), slips {slips}') gear_helper.down_shift_handle(gear, self) + def __exp_farming_setup(self, fdp): + """exp farming setup + + Args: + fdp (ForzaDataPacket): datapackage + """ + # enable reset car if exp or sp farming is True + if self.farming and fdp.car_ordinal > 0 and fdp.speed < 20 and time.time() - self.reset_time > 10: + self.reset_car = self.reset_car + 1 + # reset car position + if self.reset_car == 200: + self.reset_car = 0 + self.threadPool.submit(keyboard_helper.resetcar, self) + self.reset_time = time.time() + else: + self.reset_car = 0 + + # exp or sp farming to avoid afk detection, 30s interval + if self.farming and time.time() - self.break_timer > 30: + self.threadPool.submit(keyboard_helper.press_brake, self) + self.break_timer = time.time() + def run(self, update_tree_func=lambda *args: None, update_car_gui_func=lambda *args: None): """run the auto shifting @@ -376,8 +406,14 @@ def run(self, update_tree_func=lambda *args: None, update_car_gui_func=lambda *a self.logger.debug(f'{self.run.__name__} started') helper.create_socket(self) iteration = -1 - reset_car = 0 - reset_time = time.time() + self.reset_car = 0 + self.reset_time = time.time() + refresh_time = time.time() + first_load = True + + if self.farming: + keyboard_helper.pressdown_str('w') + while self.isRunning: fdp = helper.nextFdp(self.server_socket, self.packet_format) @@ -385,27 +421,17 @@ def run(self, update_tree_func=lambda *args: None, update_car_gui_func=lambda *a if fdp is None or fdp.car_ordinal <= 0: continue - if update_car_gui_func is not None and iteration % 10 == 0: + # UI refresh, every 0.1s + if update_car_gui_func is not None and time.time() - refresh_time > 0.1: self.threadPool.submit(update_car_gui_func, fdp) + refresh_time = time.time() - # try to load config if: - # 1. self.shift_point is empty - # or - # 2. fdp.car_ordinal is different from self.ordinal => means car switched - if not self.__update_forza_info(fdp, update_tree_func): - return + # load car config + self.__update_forza_info(fdp, update_tree_func, first_load=first_load) + first_load = False # enable reset car if exp or sp farming is True - if self.farming and fdp.car_ordinal > 0 and fdp.speed < 20 and time.time() - reset_time > 10: - reset_car = reset_car + 1 - # reset car position - if reset_car == 200: - reset_car = 0 - self.threadPool.submit(keyboard_helper.resetcar, self) - reset_time = time.time() - continue - else: - reset_car = 0 + self.__exp_farming_setup(fdp) # shifting iteration = self.shifting(iteration, fdp) @@ -413,5 +439,8 @@ def run(self, update_tree_func=lambda *args: None, update_car_gui_func=lambda *a self.logger.exception(e) finally: self.isRunning = False + if self.farming: + keyboard_helper.release_str('w') + helper.close_socket(self) self.logger.debug(f'{self.run.__name__} finished') diff --git a/gui.py b/gui.py index 3fe9f32..0794ca0 100644 --- a/gui.py +++ b/gui.py @@ -161,6 +161,7 @@ def text_update(self, lang_index): def update_tree(self): """Update shift point tree """ + key = 0 for key, value in self.forza5.shift_point.items(): self.treeview.item(self.speed_tree[key], values=round(value['speed'], 3)) self.treeview.item(self.rpm_tree[key], values=round(value['rpmo'], 3))