Fix: Simplify cloud login calls, remove nested retries

This commit is contained in:
LmeSzinc 2024-07-15 23:41:37 +08:00
parent 00f1d04694
commit f93445b70c
3 changed files with 47 additions and 55 deletions

View File

@ -61,7 +61,9 @@ class UI(MainPage):
def cloud_login(): def cloud_login():
if self.config.is_cloud_game: if self.config.is_cloud_game:
from tasks.login.login import Login from tasks.login.login import Login
Login(config=self.config, device=self.device).cloud_login() login = Login(config=self.config, device=self.device)
self.device.dump_hierarchy()
login.cloud_try_enter_game()
timeout = Timer(10, count=20).start() timeout = Timer(10, count=20).start()
while 1: while 1:

View File

@ -72,7 +72,7 @@ class XPath:
悬浮窗及侧边栏元素 悬浮窗及侧边栏元素
""" """
# 悬浮窗 # 悬浮窗
FLOAT_WINDOW = '//*[@class="android.widget.ImageView"]' FLOAT_WINDOW = '//*[@package="com.miHoYo.cloudgames.hkrpg" and @class="android.widget.ImageView"]'
# 退出按钮,返回登录页面 # 退出按钮,返回登录页面
FLOAT_EXIT = '//*[@resource-id="com.miHoYo.cloudgames.hkrpg:id/iv_exit"]' FLOAT_EXIT = '//*[@resource-id="com.miHoYo.cloudgames.hkrpg:id/iv_exit"]'
# 弹出侧边栏的 节点信息 # 弹出侧边栏的 节点信息
@ -342,13 +342,16 @@ class LoginAndroidCloud(ModuleBase):
logger.attr('Net state', None) logger.attr('Net state', None)
return False return False
def cloud_ensure_ingame(self): def cloud_enter_game(self):
""" """
Note that cloud game needs to be started before calling,
hierarchy needs to be updated before calling
Pages: Pages:
in: Any in: Any page in cloud game
out: page_main out: page_main
""" """
logger.hr('Cloud ensure ingame', level=1) logger.hr('Cloud enter game', level=1)
with self.config.multi_set(): with self.config.multi_set():
if self.config.Emulator_GameClient != 'cloud_android': if self.config.Emulator_GameClient != 'cloud_android':
@ -358,48 +361,27 @@ class LoginAndroidCloud(ModuleBase):
if self.config.Optimization_WhenTaskQueueEmpty != 'close_game': if self.config.Optimization_WhenTaskQueueEmpty != 'close_game':
self.config.Optimization_WhenTaskQueueEmpty = 'close_game' self.config.Optimization_WhenTaskQueueEmpty = 'close_game'
for _ in range(3): if self.appear(XPath.START_GAME):
if self.device.app_is_running(): logger.info('Cloud game is in main page')
logger.info('Cloud game is already running') self._cloud_get_remain()
self.device.dump_hierarchy() self._cloud_enter()
return True
if self.appear(XPath.START_GAME): elif self.appear(XPath.FLOAT_WINDOW):
logger.info('Cloud game is in main page') logger.info('Cloud game is in game')
self._cloud_get_remain() return True
self._cloud_enter() elif self.appear(XPath.FLOAT_DELAY):
return True logger.info('Cloud game is in game with float window expanded')
elif self.appear(XPath.FLOAT_WINDOW): self._cloud_setting_exit()
logger.info('Cloud game is in game') return True
return True elif self.appear(XPath.POPUP_CONFIRM):
elif self.appear(XPath.FLOAT_DELAY): logger.info('Cloud game have a popup')
logger.info('Cloud game is in game with float window expanded') self._cloud_enter()
self._cloud_setting_exit() return True
return True else:
elif self.appear(XPath.POPUP_CONFIRM): self._cloud_start()
logger.info('Cloud game have a popup') self._cloud_get_remain()
self._cloud_enter() self._cloud_enter()
return True return True
else:
try:
self._cloud_start()
except GameNotRunningError:
continue
self._cloud_get_remain()
self._cloud_enter()
return True
else:
logger.info('Cloud game is not running')
self.device.app_start()
try:
self._cloud_start()
except GameNotRunningError:
continue
self._cloud_get_remain()
self._cloud_enter()
return True
logger.error('Failed to enter cloud game after 3 trials')
return False
def is_in_cloud_page(self): def is_in_cloud_page(self):
if self.appear(XPath.START_GAME): if self.appear(XPath.START_GAME):
@ -418,15 +400,16 @@ class LoginAndroidCloud(ModuleBase):
logger.info('Not in cloud page') logger.info('Not in cloud page')
return False return False
def cloud_login(self): def cloud_try_enter_game(self):
""" """
Note that hierarchy needs to be updated before calling
Pages: Pages:
in: Any page in cloud game in: Any page in cloud game
out: page_main out: page_main
""" """
self.device.dump_hierarchy()
if self.is_in_cloud_page(): if self.is_in_cloud_page():
self.cloud_ensure_ingame() self.cloud_enter_game()
return True return True
return False return False
@ -522,5 +505,7 @@ class LoginAndroidCloud(ModuleBase):
if __name__ == '__main__': if __name__ == '__main__':
self = LoginAndroidCloud('src') self = LoginAndroidCloud('src')
self.cloud_login() self.device.app_start()
self.device.dump_hierarchy()
self.cloud_enter_game()
self.cloud_keep_alive() self.cloud_keep_alive()

View File

@ -90,18 +90,23 @@ class Login(UI, LoginAndroidCloud):
def app_start(self): def app_start(self):
logger.hr('App start') logger.hr('App start')
self.device.app_start()
if self.config.is_cloud_game: if self.config.is_cloud_game:
self.cloud_ensure_ingame() self.device.dump_hierarchy()
self.cloud_enter_game()
else: else:
self.device.app_start()
self.handle_app_login() self.handle_app_login()
def app_restart(self): def app_restart(self):
logger.hr('App restart') logger.hr('App restart')
self.device.app_stop() self.device.app_stop()
self.device.app_start()
if self.config.is_cloud_game: if self.config.is_cloud_game:
self.cloud_ensure_ingame() self.device.dump_hierarchy()
self.cloud_enter_game()
else: else:
self.device.app_start()
self.handle_app_login() self.handle_app_login()
self.config.task_delay(server_update=True) self.config.task_delay(server_update=True)