From f7f61549c14b5e2ab43358dc5214deeeb4abfefc Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:00:48 +0800 Subject: [PATCH] Refactor: Abstract BACK and CLOSE handlers --- tasks/base/main_page.py | 4 +--- tasks/base/popup.py | 35 +++++++++++++++++++++++++++++++- tasks/base/ui.py | 7 +++---- tasks/combat/combat.py | 9 ++------ tasks/daily/camera.py | 10 ++------- tasks/freebies/support_reward.py | 6 ++---- 6 files changed, 44 insertions(+), 27 deletions(-) diff --git a/tasks/base/main_page.py b/tasks/base/main_page.py index dad67ebe6..1e6ecc168 100644 --- a/tasks/base/main_page.py +++ b/tasks/base/main_page.py @@ -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 diff --git a/tasks/base/popup.py b/tasks/base/popup.py index 074140bc4..6b37324aa 100644 --- a/tasks/base/popup.py +++ b/tasks/base/popup.py @@ -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 diff --git a/tasks/base/ui.py b/tasks/base/ui.py index bbd849375..0c2f7238b 100644 --- a/tasks/base/ui.py +++ b/tasks/base/ui.py @@ -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): diff --git a/tasks/combat/combat.py b/tasks/combat/combat.py index 905656dd2..7988bd2e5 100644 --- a/tasks/combat/combat.py +++ b/tasks/combat/combat.py @@ -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}') diff --git a/tasks/daily/camera.py b/tasks/daily/camera.py index 1da1cce67..064f19bd1 100644 --- a/tasks/daily/camera.py +++ b/tasks/daily/camera.py @@ -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 diff --git a/tasks/freebies/support_reward.py b/tasks/freebies/support_reward.py index 8c9d1ef7c..600858856 100644 --- a/tasks/freebies/support_reward.py +++ b/tasks/freebies/support_reward.py @@ -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