1
0
mirror of https://github.com/sui-feng-cb/AzurLaneAutoScript1.git synced 2026-07-13 02:54:25 +08:00

Refactor: reorganize exercise.run() for clearer structure

This commit is contained in:
positnuec
2026-07-08 20:47:12 +08:00
parent 47414bdf5f
commit c615e17d8f

View File

@@ -206,52 +206,38 @@ class Exercise(ExerciseCombat):
return preserve, admiral_interval
def run(self):
self.ui_ensure(page_exercise)
server_update = self.config.Scheduler_ServerUpdate
def _should_force_clear(self, remain_time, admiral_interval):
"""Decide whether to clear all exercise attempts this run."""
if admiral_interval is None or not remain_time:
return False
self.opponent_change_count = self._get_opponent_change_count()
logger.attr("Change_opponent_count", self.opponent_change_count)
logger.attr('Exercise_ExerciseStrategy', self.config.Exercise_ExerciseStrategy)
self.preserve, admiral_interval = self._get_exercise_strategy()
remain_hours = int(remain_time.total_seconds() // 3600)
admiral_start, admiral_end = admiral_interval
remain_time = OCR_PERIOD_REMAIN.ocr(self.device.image)
logger.info(f'Exercise period remain: {remain_time}')
if self._get_thursday_strategy(remain_hours):
logger.info('Reach Thursday, using all attempts')
return True
if admiral_start > remain_hours >= admiral_end:
logger.info('Reach set time for admiral trial, using all attempts')
return True
if remain_hours < 6:
logger.info('Exercise period remain less than 6 hours, using all attempts')
return True
if admiral_interval is not None and remain_time:
admiral_start, admiral_end = admiral_interval
return False
if self._get_thursday_strategy(int(remain_time.total_seconds() // 3600)):
logger.info('Reach Thursday for admiral trial, using all attempts.')
self.preserve = 0
forced_run = True
elif admiral_start > int(remain_time.total_seconds() // 3600) >= admiral_end: # set time for getting admiral
logger.info('Reach set time for admiral trial, using all attempts.')
self.preserve = 0
forced_run = True
elif int(remain_time.total_seconds() // 3600) < 6: # if not set to "sun18", still depleting at sunday 18pm.
logger.info('Exercise period remain less than 6 hours, using all attempts.')
self.preserve = 0
forced_run = True
else:
logger.info(f'Preserve {self.preserve} exercise')
forced_run = False
else:
forced_run = False
def _should_delay(self, next_update, forced_clear):
"""Check whether to delay exercise to the configured time before next update."""
if forced_clear:
return False
seconds_until_update = (next_update - datetime.datetime.now()).seconds
return seconds_until_update > 3600 * self.config.Exercise_DelayUntilHoursBeforeNextUpdate
# Delay task to the configured time
if ((get_server_next_update(server_update) - datetime.datetime.now()).seconds >
3600 * self.config.Exercise_DelayUntilHoursBeforeNextUpdate)\
and not forced_run:
logger.warning(f'Exercise should run at {self.config.Exercise_DelayUntilHoursBeforeNextUpdate} '
f'hours before next update. Delay task to it.')
run = False
else:
run = True
while run:
def _exercise(self, preserve):
"""Consume exercise attempts until remain <= preserve or refresh exhausted."""
while 1:
self.remain = OCR_EXERCISE_REMAIN.ocr(self.device.image)
if self.remain <= self.preserve:
if self.remain <= preserve:
break
logger.hr(f'Exercise remain {self.remain}', level=1)
@@ -263,19 +249,42 @@ class Exercise(ExerciseCombat):
logger.info('New opponent exhausted')
break
def run(self):
self.ui_ensure(page_exercise)
server_update = self.config.Scheduler_ServerUpdate
self.opponent_change_count = self._get_opponent_change_count()
logger.attr("Change_opponent_count", self.opponent_change_count)
logger.attr('Exercise_ExerciseStrategy', self.config.Exercise_ExerciseStrategy)
self.preserve, admiral_interval = self._get_exercise_strategy()
remain_time = self._get_exercise_reset_remain()
logger.info(f'Exercise period remain: {remain_time}')
forced_clear = self._should_force_clear(remain_time, admiral_interval)
if forced_clear:
self.preserve = 0
next_update = get_server_next_update(server_update)
should_delay = self._should_delay(next_update, forced_clear)
if should_delay:
logger.warning(f'Exercise should run at {self.config.Exercise_DelayUntilHoursBeforeNextUpdate} '
f'hours before next update. Delay task to it.')
else:
self._exercise(self.preserve)
# self.equipment_take_off_when_finished()
# Scheduler
with self.config.multi_set():
self.config.set_record(Exercise_OpponentRefreshValue=self.opponent_change_count)
if self.remain <= self.preserve or self.opponent_change_count >= 5:
next_run = get_server_next_update(server_update) \
- datetime.timedelta(hours=self.config.Exercise_DelayUntilHoursBeforeNextUpdate)
next_run = next_update - datetime.timedelta(hours=self.config.Exercise_DelayUntilHoursBeforeNextUpdate)
now = datetime.datetime.now()
if next_run < now or run:
if next_run >= now or should_delay:
minutes_to_delay = int((next_run - now).total_seconds() / 60 + 1)
self.config.task_delay(minute=minutes_to_delay)
else:
self.config.task_delay(server_update=True)
return
minutes_to_delay = int((next_run - now).total_seconds() / 60 + 1)
self.config.task_delay(minute=minutes_to_delay)
else:
self.config.task_delay(success=False)