StarRailCopilot/tasks/login/login.py

96 lines
3.0 KiB
Python
Raw Normal View History

2023-05-17 16:44:43 +00:00
from module.base.timer import Timer
from module.exception import GameNotRunningError
2023-05-17 16:44:43 +00:00
from module.logger import logger
from tasks.base.page import page_main
from tasks.base.ui import UI
from tasks.login.assets.assets_login import LOGIN_CONFIRM, USER_AGREEMENT_ACCEPT, LOGIN_LOADING
2023-05-17 16:44:43 +00:00
class Login(UI):
def _handle_app_login(self):
"""
Pages:
in: Any page
out: page_main
Raises:
GameStuckError:
GameTooManyClickError:
GameNotRunningError:
2023-05-17 16:44:43 +00:00
"""
logger.hr('App login')
orientation_timer = Timer(5)
startup_timer = Timer(5).start()
app_timer = Timer(5).start()
2023-05-17 16:44:43 +00:00
login_success = False
while 1:
# Watch if game alive
if app_timer.reached():
if not self.device.app_is_running():
logger.error('Game died during launch')
raise GameNotRunningError('Game not running')
app_timer.reset()
2023-05-17 16:44:43 +00:00
# Watch device rotation
if not login_success and orientation_timer.reached():
# Screen may rotate after starting an app
self.device.get_orientation()
orientation_timer.reset()
self.device.screenshot()
# End
# Game client requires at least 5s to start
# The first few frames might be captured before app_stop(), ignore them
if startup_timer.reached():
if self.ui_page_appear(page_main):
logger.info('Login to main confirm')
break
# Watch resource downloading and loading
if self.appear(LOGIN_LOADING, interval=5):
logger.info('Game resources downloading or loading')
self.device.stuck_record_clear()
2023-05-17 16:44:43 +00:00
# Login
if self.appear_then_click(LOGIN_CONFIRM):
login_success = True
2023-05-17 16:44:43 +00:00
continue
if self.appear_then_click(USER_AGREEMENT_ACCEPT):
continue
# Additional
if self.handle_popup_single():
continue
2023-09-19 02:23:32 +00:00
if self.handle_popup_confirm():
continue
if self.ui_additional():
continue
2023-05-17 16:44:43 +00:00
return True
def handle_app_login(self):
logger.info('handle_app_login')
2023-05-17 16:44:43 +00:00
self.device.screenshot_interval_set(1.0)
self.device.stuck_timer = Timer(300, count=300).start()
2023-05-17 16:44:43 +00:00
try:
self._handle_app_login()
finally:
self.device.screenshot_interval_set()
self.device.stuck_timer = Timer(60, count=60).start()
2023-05-17 16:44:43 +00:00
def app_stop(self):
logger.hr('App stop')
self.device.app_stop()
def app_start(self):
logger.hr('App start')
self.device.app_start()
self.handle_app_login()
def app_restart(self):
logger.hr('App restart')
self.device.app_stop()
self.device.app_start()
self.handle_app_login()
self.config.task_delay(server_update=True)