From 7d9ae07e5eec2a150a1bc2cf36748538e35f53b0 Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Wed, 8 May 2024 02:54:12 +0800 Subject: [PATCH] Fix: Retry ranged character switch if the first time failed --- tasks/character/switch.py | 28 +++++++++++++++++++++++----- tasks/rogue/route/loader.py | 4 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/tasks/character/switch.py b/tasks/character/switch.py index 6c625d9e9..7b36b6806 100644 --- a/tasks/character/switch.py +++ b/tasks/character/switch.py @@ -32,6 +32,7 @@ class CharacterSwitch(UI): characters: list[CharacterList] = [] character_current: CharacterList | None = None character_buttons: list[OcrResultButton] = [] + character_is_ranged: t.Optional[bool] = None def character_update(self, skip_first_screenshot=True) -> list[CharacterList]: """ @@ -187,7 +188,7 @@ class CharacterSwitch(UI): skip_first_screenshot: Returns: - bool: If chose + bool: If chose success """ logger.info(f'Character choose: {character}') if isinstance(character, int): @@ -220,8 +221,12 @@ class CharacterSwitch(UI): # End selected = self._update_current_character() if index in selected: - logger.info('Character chose') - return True + if len(selected) > 1: + logger.warning('Multiple selected characters found, cannot guarantee character selected') + return False + else: + logger.info('Character chose') + return True if count > 3: logger.warning('Failed to choose character, assume chose') return False @@ -255,22 +260,35 @@ class CharacterSwitch(UI): logger.info('No ranged characters in team') return False - def character_switch_to_ranged(self, update=True) -> bool: + def character_switch_to_ranged(self, update=True) -> bool | None: """ Args: update: If update characters before switching Returns: bool: If using a ranged character now + or None if failed to switch """ + if self.character_is_ranged is not None: + return self.character_is_ranged + logger.hr('Character switch to ranged') if update: self.character_update() character = self._get_ranged_character() if character is True: + self.character_is_ranged = True return True elif character is False: + self.character_is_ranged = False return False + + success = self.character_switch(character) + if success: + self.character_is_ranged = True + return True else: - return self.character_switch(character) + # Cannot switch, keep None to retry at next call + self.character_is_ranged = None + return None diff --git a/tasks/rogue/route/loader.py b/tasks/rogue/route/loader.py index cd9d2a197..7fa9e4f86 100644 --- a/tasks/rogue/route/loader.py +++ b/tasks/rogue/route/loader.py @@ -285,6 +285,7 @@ class RouteLoader(RogueUI, MinimapWrapper, RouteLoader_, CharacterSwitch): """ base = RouteBase(config=self.config, device=self.device, task=self.config.task.command) count = 1 + self.character_is_ranged = None while 1: if skip_first_screenshot: skip_first_screenshot = False @@ -293,8 +294,7 @@ class RouteLoader(RogueUI, MinimapWrapper, RouteLoader_, CharacterSwitch): logger.hr(f'Route run: {count}', level=1) base.clear_blessing() - if count == 1: - self.character_switch_to_ranged(update=True) + self.character_switch_to_ranged(update=True) self.route_run() # if not success: