Skip to content

Commit

Permalink
auto accel while farming (#48)
Browse files Browse the repository at this point in the history
* running logic update

* auto accel while farming
  • Loading branch information
Juice-XIJ authored Jun 15, 2022
1 parent c8206a9 commit 8c9567d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
81 changes: 55 additions & 26 deletions forza.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -376,42 +406,41 @@ 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)

# fdp is not car information
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)
except BaseException as e:
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')
1 change: 1 addition & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 8c9567d

Please sign in to comment.