Fix: Confirm unknown ui page; 5 seconds timeout (#956)

This commit is contained in:
LmeSzinc 2022-03-07 21:55:11 +08:00
parent 526d85bf14
commit dbfbd294ff
6 changed files with 93 additions and 39 deletions

View File

@ -77,6 +77,10 @@ class AzurLaneAutoScript:
self.config.task_call('Restart')
self.device.sleep(10)
return False
except GamePageUnknownError:
logger.critical('Game page unknown')
self.save_error_log()
exit(1)
except ScriptError as e:
logger.critical(e)
logger.critical('This is likely to be a mistake of developers, but sometimes just random issues')

View File

@ -113,6 +113,7 @@ def function_drop(rate=0.5, default=None):
70% possibility:
INFO | Click (1091, 628) @ REWARD_GOTO_MAIN
"""
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
@ -134,3 +135,33 @@ def function_drop(rate=0.5, default=None):
return wrapper
return decorate
def run_once(f):
"""
Run a function only once, no matter how many times it has been called.
Examples:
@run_once
def my_function(foo, bar):
return foo + bar
while 1:
my_function()
Examples:
def my_function(foo, bar):
return foo + bar
action = run_once(my_function)
while 1:
action()
"""
def wrapper(*args, **kwargs):
if not wrapper.has_run:
wrapper.has_run = True
return f(*args, **kwargs)
wrapper.has_run = False
return wrapper

View File

@ -4,11 +4,11 @@ from module.combat.level import LevelOcr
from module.equipment.assets import *
from module.equipment.equipment_change import EquipmentChange
from module.equipment.fleet_equipment import OCR_FLEET_INDEX
from module.exception import CampaignEnd
from module.map.assets import FLEET_PREPARATION, MAP_PREPARATION
from module.ocr.ocr import Digit
from module.retire.dock import *
from module.ui.page import page_fleet, page_main
from module.exception import CampaignEnd
from module.ui.page import page_fleet
SIM_VALUE = 0.95
@ -60,7 +60,7 @@ class GemsFarming(CampaignRun, Dock, EquipmentChange):
"""
Enter GEMS_FLEET_1 page
"""
self.ui_ensure(page_fleet)
self.ui_goto(page_fleet)
self.ui_ensure_index(self.config.Fleet_Fleet1, letter=OCR_FLEET_INDEX,
next_button=FLEET_NEXT, prev_button=FLEET_PREV, skip_first_screenshot=True)
@ -96,8 +96,6 @@ class GemsFarming(CampaignRun, Dock, EquipmentChange):
self.equipment_take_on(index_list=index_list)
self.ui_ensure(page_main)
def vanguard_change(self):
"""
Change vanguard and vanguard's equipment
@ -119,7 +117,6 @@ class GemsFarming(CampaignRun, Dock, EquipmentChange):
self._equip_take_off_one()
self.equipment_take_on()
self.ui_ensure(page_main)
def _ship_change_confirm(self, button):

View File

@ -200,7 +200,6 @@ class CampaignRun(UI):
elif self.campaign.is_in_auto_search_menu():
logger.info('In auto search menu, skip ensure_campaign_ui.')
else:
self.ui_get_current_page()
self.campaign.ensure_campaign_ui(
name=self.stage,
mode=mode

View File

@ -45,6 +45,10 @@ class GameNotRunningError(Exception):
pass
class GamePageUnknownError(Exception):
pass
class RequestHumanTakeover(Exception):
# Request human takeover
# Alas is unable to handle such error, probably because of wrong settings.

View File

@ -1,7 +1,8 @@
from module.base.button import Button
from module.base.timer import Timer
from module.base.decorator import run_once
from module.combat.assets import *
from module.exception import GameNotRunningError, RequestHumanTakeover
from module.exception import GameNotRunningError, GamePageUnknownError
from module.handler.assets import *
from module.handler.info_handler import InfoHandler
from module.logger import logger
@ -92,41 +93,59 @@ class UI(InfoHandler):
Returns:
Page:
"""
if not skip_first_screenshot or not hasattr(self.device, 'image') or self.device.image is None:
self.device.screenshot()
logger.info('UI get current page')
# Known pages
for page in self.ui_pages:
if page.check_button is None:
continue
if self.ui_page_appear(page=page):
logger.attr('UI', page.name)
self.ui_current = page
return page
# Unknown page but able to handle
logger.info('Unknown ui page')
if self.appear_then_click(GOTO_MAIN, offset=(20, 20)) or self.ui_additional():
logger.info('Goto page_main')
self.ui_current = page_unknown
self.ui_goto(page_main, skip_first_screenshot=True)
# Unknown page, need manual switching
if hasattr(self, 'ui_current'):
logger.warning(f'Unrecognized ui_current, using previous: {self.ui_current}')
else:
logger.info('Unable to goto page_main')
logger.attr('EMULATOR__SCREENSHOT_METHOD', self.config.Emulator_ScreenshotMethod)
logger.attr('EMULATOR__CONTROL_METHOD', self.config.Emulator_ControlMethod)
logger.attr('SERVER', self.config.SERVER)
logger.warning('Starting from current page is not supported')
logger.warning(f'Supported page: {[str(page) for page in self.ui_pages]}')
logger.warning(f'Supported page: Any page with a "HOME" button on the upper-right')
@run_once
def app_check():
if not self.device.app_is_running():
raise GameNotRunningError('Game not running')
@run_once
def minicap_check():
if self.config.Emulator_ControlMethod == 'uiautomator2':
self.device.uninstall_minicap()
timeout = Timer(5, count=10).start()
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
if not hasattr(self.device, 'image') or self.device.image is None:
self.device.screenshot()
else:
logger.critical('Please switch to a supported page before starting Alas')
raise RequestHumanTakeover
self.device.screenshot()
# End
if timeout.reached():
break
# Known pages
for page in self.ui_pages:
if page.check_button is None:
continue
if self.ui_page_appear(page=page):
logger.attr('UI', page.name)
self.ui_current = page
return page
# Unknown page but able to handle
logger.info('Unknown ui page')
if self.appear_then_click(GOTO_MAIN, offset=(20, 20), interval=2) or self.ui_additional():
timeout.reset()
continue
app_check()
minicap_check()
# Unknown page, need manual switching
logger.warning('Unknown ui page')
logger.attr('EMULATOR__SCREENSHOT_METHOD', self.config.Emulator_ScreenshotMethod)
logger.attr('EMULATOR__CONTROL_METHOD', self.config.Emulator_ControlMethod)
logger.attr('SERVER', self.config.SERVER)
logger.warning('Starting from current page is not supported')
logger.warning(f'Supported page: {[str(page) for page in self.ui_pages]}')
logger.warning(f'Supported page: Any page with a "HOME" button on the upper-right')
logger.critical('Please switch to a supported page before starting Alas')
raise GamePageUnknownError
def ui_goto(self, destination, offset=(20, 20), confirm_wait=0, skip_first_screenshot=True):
"""