StarRailCopilot/tasks/base/ui.py
2023-05-18 00:44:43 +08:00

176 lines
5.3 KiB
Python

from module.base.decorator import run_once
from module.base.timer import Timer
from module.exception import GameNotRunningError, GamePageUnknownError
from module.logger import logger
from tasks.base.page import Page, page_main
from tasks.base.popup import PopupHandler
class UI(PopupHandler):
ui_current: Page
def ui_page_appear(self, page):
"""
Args:
page (Page):
"""
return self.appear(page.check_button)
def ui_get_current_page(self, skip_first_screenshot=True):
"""
Args:
skip_first_screenshot:
Returns:
Page:
Raises:
GameNotRunningError:
GamePageUnknownError:
"""
logger.info("UI get current page")
@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()
@run_once
def rotation_check():
self.device.get_orientation()
timeout = Timer(10, count=20).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:
self.device.screenshot()
# End
if timeout.reached():
break
# Known pages
for page in Page.iter_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.ui_additional():
timeout.reset()
continue
app_check()
minicap_check()
rotation_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 Page.iter_pages()]}")
logger.warning('Supported page: Any page with a "HOME" button on the upper-right')
logger.critical("Please switch to a supported page before starting SRC")
raise GamePageUnknownError
def ui_goto(self, destination, skip_first_screenshot=True):
"""
Args:
destination (Page):
skip_first_screenshot:
"""
# Create connection
Page.init_connection(destination)
self.interval_clear(list(Page.iter_check_buttons()))
logger.hr(f"UI goto {destination}")
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
# Destination page
if self.ui_page_appear(destination):
logger.info(f'Page arrive: {destination}')
break
# Other pages
clicked = False
for page in Page.iter_pages():
if page.parent is None or page.check_button is None:
continue
if self.appear(page.check_button, interval=5):
logger.info(f'Page switch: {page} -> {page.parent}')
button = page.links[page.parent]
self.device.click(button)
self.ui_button_interval_reset(button)
clicked = True
break
if clicked:
continue
# Additional
if self.ui_additional():
continue
# Reset connection
Page.clear_connection()
def ui_ensure(self, destination, skip_first_screenshot=True):
"""
Args:
destination (Page):
skip_first_screenshot:
Returns:
bool: If UI switched.
"""
logger.hr("UI ensure")
self.ui_get_current_page(skip_first_screenshot=skip_first_screenshot)
if self.ui_current == destination:
logger.info("Already at %s" % destination)
return False
else:
logger.info("Goto %s" % destination)
self.ui_goto(destination, skip_first_screenshot=True)
return True
def ui_goto_main(self):
return self.ui_ensure(destination=page_main)
def ui_additional(self) -> bool:
"""
Handle all possible popups during UI switching.
Returns:
If handled any popup.
"""
if self.handle_reward():
return True
if self.handle_battle_pass_notification():
return True
def ui_button_interval_reset(self, button):
"""
Reset interval of some button to avoid mistaken clicks
Args:
button (Button):
"""
pass