Refactor: Abstract BACK and CLOSE handlers

This commit is contained in:
LmeSzinc 2023-10-18 12:00:48 +08:00
parent 9f77cb1f32
commit f7f61549c1
6 changed files with 44 additions and 27 deletions

View File

@ -187,9 +187,7 @@ class MainPage(PopupHandler):
self.device.click(CLOSE)
clicked = True
continue
if self.appear(page_gacha.check_button, interval=2):
logger.info(f'{page_gacha} -> {CLOSE}')
self.device.click(CLOSE)
if self.handle_ui_close(page_gacha.check_button, interval=2):
continue
if self.appear_then_click(ROGUE_LEAVE_FOR_NOW, interval=2):
clicked = True

View File

@ -1,10 +1,11 @@
from module.base.base import ModuleBase
from module.logger import logger
from tasks.base.assets.assets_base_page import BACK, CLOSE
from tasks.base.assets.assets_base_popup import *
class PopupHandler(ModuleBase):
def handle_reward(self, interval=5, click_button=None) -> bool:
def handle_reward(self, interval=5, click_button: ButtonWrapper = None) -> bool:
"""
Args:
interval:
@ -96,3 +97,35 @@ class PopupHandler(ModuleBase):
return True
return False
def handle_ui_close(self, appear_button: ButtonWrapper, interval=2) -> bool:
"""
Args:
appear_button: Click if button appears
interval:
Returns:
If handled.
"""
if self.appear(appear_button, interval=interval):
logger.info(f'{appear_button} -> {CLOSE}')
self.device.click(CLOSE)
return True
return False
def handle_ui_back(self, appear_button: ButtonWrapper, interval=2) -> bool:
"""
Args:
appear_button: Click if button appears
interval:
Returns:
If handled.
"""
if self.appear(appear_button, interval=interval):
logger.info(f'{appear_button} -> {BACK}')
self.device.click(BACK)
return True
return False

View File

@ -4,7 +4,7 @@ from module.base.timer import Timer
from module.exception import GameNotRunningError, GamePageUnknownError
from module.logger import logger
from module.ocr.ocr import Ocr
from tasks.base.assets.assets_base_page import CLOSE, MAP_EXIT
from tasks.base.assets.assets_base_page import MAP_EXIT
from tasks.base.main_page import MainPage
from tasks.base.page import Page, page_main
from tasks.combat.assets.assets_combat_finish import COMBAT_EXIT
@ -307,9 +307,8 @@ class UI(MainPage):
return True
if self.handle_monthly_card_reward():
return True
if self.appear(COMBAT_PREPARE, interval=5):
logger.info(f'UI additional: {COMBAT_PREPARE} -> {CLOSE}')
self.device.click(CLOSE)
if self.handle_ui_close(COMBAT_PREPARE, interval=5):
return True
if self.appear_then_click(COMBAT_EXIT, interval=5):
return True
if self.appear_then_click(INFO_CLOSE, interval=5):

View File

@ -1,6 +1,5 @@
from module.base.decorator import run_once
from module.logger import logger
from tasks.base.assets.assets_base_page import CLOSE
from tasks.combat.assets.assets_combat_finish import COMBAT_AGAIN, COMBAT_EXIT
from tasks.combat.assets.assets_combat_prepare import COMBAT_PREPARE
from tasks.combat.assets.assets_combat_team import COMBAT_TEAM_PREPARE, COMBAT_TEAM_SUPPORT
@ -294,13 +293,9 @@ class Combat(CombatInteract, CombatPrepare, CombatState, CombatTeam, CombatSuppo
break
# Click
if self.appear(COMBAT_PREPARE, interval=2):
logger.info(f'{COMBAT_PREPARE} -> {CLOSE}')
self.device.click(CLOSE)
if self.handle_ui_close(COMBAT_PREPARE, interval=2):
continue
if self.appear(COMBAT_TEAM_PREPARE, interval=2):
logger.info(f'{COMBAT_TEAM_PREPARE} -> {CLOSE}')
self.device.click(CLOSE)
if self.handle_ui_close(COMBAT_TEAM_PREPARE, interval=2):
continue
if self.appear(COMBAT_AGAIN, interval=2):
logger.info(f'{COMBAT_AGAIN} -> {COMBAT_EXIT}')

View File

@ -1,5 +1,4 @@
from module.logger import logger
from tasks.base.assets.assets_base_page import CLOSE
from tasks.base.page import page_camera
from tasks.base.ui import UI
from tasks.daily.assets.assets_daily_camera import PICTURE_TAKEN, TAKE_PICTURE
@ -42,12 +41,7 @@ class CameraUI(UI):
if self.is_in_main():
logger.info('Back to camera main page')
break
if self.appear(PICTURE_TAKEN, interval=1):
self.device.click(CLOSE)
logger.info(f'{PICTURE_TAKEN} -> {CLOSE}')
self.interval_reset(TAKE_PICTURE)
if self.handle_ui_close(PICTURE_TAKEN, interval=1):
continue
if self.appear(TAKE_PICTURE, interval=1):
self.device.click(CLOSE)
logger.info(f'{TAKE_PICTURE} -> {CLOSE}')
if self.handle_ui_close(TAKE_PICTURE, interval=1):
continue

View File

@ -1,6 +1,6 @@
from module.base.timer import Timer
from module.logger import logger
from tasks.base.assets.assets_base_page import CLOSE, MENU_CHECK
from tasks.base.assets.assets_base_page import MENU_CHECK
from tasks.base.assets.assets_base_popup import GET_REWARD
from tasks.base.page import page_menu
from tasks.base.ui import UI
@ -97,9 +97,7 @@ class SupportReward(UI):
if self.appear(MENU_CHECK):
return True
if self.appear(IN_PROFILE, interval=2):
logger.info(f'{IN_PROFILE} -> {CLOSE}')
self.device.click(CLOSE)
if self.handle_ui_close(IN_PROFILE, interval=2):
continue
if self.handle_reward(click_button=CAN_GET_REWARD):
# # Avoid clicking on some other buttons