Fix: Retry ranged character switch if the first time failed

This commit is contained in:
LmeSzinc 2024-05-08 02:54:12 +08:00
parent 043f07dbf0
commit 7d9ae07e5e
2 changed files with 25 additions and 7 deletions

View File

@ -32,6 +32,7 @@ class CharacterSwitch(UI):
characters: list[CharacterList] = [] characters: list[CharacterList] = []
character_current: CharacterList | None = None character_current: CharacterList | None = None
character_buttons: list[OcrResultButton] = [] character_buttons: list[OcrResultButton] = []
character_is_ranged: t.Optional[bool] = None
def character_update(self, skip_first_screenshot=True) -> list[CharacterList]: def character_update(self, skip_first_screenshot=True) -> list[CharacterList]:
""" """
@ -187,7 +188,7 @@ class CharacterSwitch(UI):
skip_first_screenshot: skip_first_screenshot:
Returns: Returns:
bool: If chose bool: If chose success
""" """
logger.info(f'Character choose: {character}') logger.info(f'Character choose: {character}')
if isinstance(character, int): if isinstance(character, int):
@ -220,6 +221,10 @@ class CharacterSwitch(UI):
# End # End
selected = self._update_current_character() selected = self._update_current_character()
if index in selected: if index in selected:
if len(selected) > 1:
logger.warning('Multiple selected characters found, cannot guarantee character selected')
return False
else:
logger.info('Character chose') logger.info('Character chose')
return True return True
if count > 3: if count > 3:
@ -255,22 +260,35 @@ class CharacterSwitch(UI):
logger.info('No ranged characters in team') logger.info('No ranged characters in team')
return False return False
def character_switch_to_ranged(self, update=True) -> bool: def character_switch_to_ranged(self, update=True) -> bool | None:
""" """
Args: Args:
update: If update characters before switching update: If update characters before switching
Returns: Returns:
bool: If using a ranged character now 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') logger.hr('Character switch to ranged')
if update: if update:
self.character_update() self.character_update()
character = self._get_ranged_character() character = self._get_ranged_character()
if character is True: if character is True:
self.character_is_ranged = True
return True return True
elif character is False: elif character is False:
self.character_is_ranged = False
return False return False
success = self.character_switch(character)
if success:
self.character_is_ranged = True
return True
else: else:
return self.character_switch(character) # Cannot switch, keep None to retry at next call
self.character_is_ranged = None
return None

View File

@ -285,6 +285,7 @@ class RouteLoader(RogueUI, MinimapWrapper, RouteLoader_, CharacterSwitch):
""" """
base = RouteBase(config=self.config, device=self.device, task=self.config.task.command) base = RouteBase(config=self.config, device=self.device, task=self.config.task.command)
count = 1 count = 1
self.character_is_ranged = None
while 1: while 1:
if skip_first_screenshot: if skip_first_screenshot:
skip_first_screenshot = False skip_first_screenshot = False
@ -293,7 +294,6 @@ class RouteLoader(RogueUI, MinimapWrapper, RouteLoader_, CharacterSwitch):
logger.hr(f'Route run: {count}', level=1) logger.hr(f'Route run: {count}', level=1)
base.clear_blessing() base.clear_blessing()
if count == 1:
self.character_switch_to_ranged(update=True) self.character_switch_to_ranged(update=True)
self.route_run() self.route_run()