Fix: Handle reward on complex background

This commit is contained in:
LmeSzinc 2023-11-06 22:30:43 +08:00
parent 4adfa00148
commit e1468db85d
8 changed files with 118 additions and 31 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -32,13 +32,22 @@ GET_LIGHT_CONE = ButtonWrapper(
)
GET_REWARD = ButtonWrapper(
name='GET_REWARD',
share=Button(
file='./assets/share/base/popup/GET_REWARD.png',
area=(623, 95, 657, 119),
search=(603, 75, 677, 139),
color=(145, 131, 99),
button=(741, 495, 1071, 644),
),
share=[
Button(
file='./assets/share/base/popup/GET_REWARD.png',
area=(625, 95, 655, 119),
search=(605, 75, 675, 139),
color=(213, 0, 0),
button=(741, 495, 1071, 644),
),
Button(
file='./assets/share/base/popup/GET_REWARD.2.png',
area=(625, 144, 655, 168),
search=(605, 124, 675, 188),
color=(226, 0, 0),
button=(625, 144, 655, 168),
),
],
)
MONTHLY_CARD_GET_ITEM = ButtonWrapper(
name='MONTHLY_CARD_GET_ITEM',

View File

@ -1,10 +1,22 @@
from typing import Callable
from module.base.base import ModuleBase
from module.base.utils import color_similarity_2d
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 reward_appear(self) -> bool:
for button in GET_REWARD.buttons:
image = self.image_crop(button.search, copy=False)
image = color_similarity_2d(image, color=(203, 181, 132))
if button.match_template(image, direct_match=True):
return True
return False
def handle_reward(self, interval=5, click_button: ButtonWrapper = None) -> bool:
"""
Args:
interval:
@ -13,16 +25,26 @@ class PopupHandler(ModuleBase):
Returns:
If handled.
"""
# Same as ModuleBase.match_template()
self.device.stuck_record_add(GET_REWARD)
if interval and not self.interval_is_reached(GET_REWARD, interval=interval):
return False
appear = self.reward_appear()
if click_button is None:
if self.appear_then_click(GET_REWARD, interval=interval):
return True
if appear:
self.device.click(GET_REWARD)
else:
if self.appear(GET_REWARD, interval=interval):
if appear:
logger.info(f'{GET_REWARD} -> {click_button}')
self.device.click(click_button)
return True
return False
if appear and interval:
self.interval_reset(GET_REWARD, interval=interval)
return appear
def handle_battle_pass_notification(self, interval=5) -> bool:
"""
@ -113,3 +135,66 @@ class PopupHandler(ModuleBase):
return True
return False
def handle_get_character(self, interval=2) -> bool:
"""
Popup when getting a character from rogue rewards.
Args:
interval:
Returns:
If handled.
"""
if self.appear(GET_CHARACTER, interval=interval):
logger.info(f'{GET_CHARACTER} -> {GET_REWARD}')
self.device.click(GET_REWARD)
return True
return False
def handle_ui_close(self, appear_button: ButtonWrapper | Callable, interval=2) -> bool:
"""
Args:
appear_button: Click if button appears
interval:
Returns:
If handled.
"""
if callable(appear_button):
if self.interval_is_reached(appear_button, interval=interval) and appear_button():
logger.info(f'{appear_button.__name__} -> {CLOSE}')
self.device.click(CLOSE)
self.interval_reset(appear_button, interval=interval)
return True
else:
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 | Callable, interval=2) -> bool:
"""
Args:
appear_button: Click if button appears
interval:
Returns:
If handled.
"""
if callable(appear_button):
if self.interval_is_reached(appear_button, interval=interval) and appear_button():
logger.info(f'{appear_button.__name__} -> {BACK}')
self.device.click(BACK)
self.interval_reset(appear_button, interval=interval)
return True
else:
if self.appear(appear_button, interval=interval):
logger.info(f'{appear_button} -> {BACK}')
self.device.click(BACK)
return True
return False

View File

@ -12,7 +12,6 @@ from module.ocr.utils import split_and_pair_buttons
from module.ui.scroll import Scroll
from module.ui.switch import Switch
from tasks.base.assets.assets_base_page import BATTLE_PASS_CHECK, MAIN_GOTO_BATTLE_PASS
from tasks.base.assets.assets_base_popup import GET_REWARD
from tasks.base.page import page_battle_pass, page_main
from tasks.base.ui import UI
from tasks.battle_pass.assets.assets_battle_pass import *
@ -187,7 +186,7 @@ class BattlePassUI(UI):
else:
self.device.screenshot()
if self.appear(GET_REWARD):
if self.reward_appear():
logger.info('Get reward')
break
if self.appear(CLOSE_CHOOSE_GIFT):

View File

@ -1,11 +1,9 @@
from module.ocr.ocr import *
from module.ui.scroll import Scroll
from tasks.base.assets.assets_base_page import MENU_CHECK, SYNTHESIZE_CHECK
from tasks.base.assets.assets_base_popup import GET_REWARD
from tasks.base.assets.assets_base_page import MENU_CHECK, MENU_SCROLL, SYNTHESIZE_CHECK
from tasks.base.assets.assets_base_popup import POPUP_CONFIRM
from tasks.base.page import Page, page_menu, page_synthesize
from tasks.base.ui import UI
from tasks.base.assets.assets_base_page import MENU_SCROLL
from tasks.base.assets.assets_base_popup import POPUP_CONFIRM
from tasks.daily.assets.assets_daily_synthesize_consumable import *
from tasks.daily.assets.assets_daily_synthesize_material import *
@ -157,7 +155,7 @@ class SynthesizeUI(UI):
else:
self.device.screenshot()
if self.appear(GET_REWARD):
if self.reward_appear():
logger.info('Synthesize consumable completed')
break
# Synthesize confirm
@ -175,7 +173,7 @@ class SynthesizeUI(UI):
logger.info('Synthesize consumables page appear')
break
# Go back to the previous page
if self.appear_then_click(GET_REWARD):
if self.handle_reward(interval=2):
logger.info('Click on the blank space to back to synthesize page')
continue

View File

@ -1,7 +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_popup import GET_REWARD
from tasks.base.assets.assets_base_page import MENU_CHECK
from tasks.base.page import page_menu
from tasks.base.ui import UI
from tasks.freebies.assets.assets_freebies_support_reward import (
@ -53,7 +52,7 @@ class SupportReward(UI):
"""
Pages:
in: PROFILE
out: GET_REWARD
out: reward_appear()
"""
logger.info('Getting reward')
claimed = False
@ -68,7 +67,7 @@ class SupportReward(UI):
if not claimed and empty.reached():
logger.info('No reward')
break
if self.appear(GET_REWARD):
if self.reward_appear():
logger.info('Got reward')
break
if timeout.reached():
@ -83,7 +82,7 @@ class SupportReward(UI):
def _goto_menu(self):
"""
Pages:
in: PROFILE or GET_REWARD
in: PROFILE or reward_appear
out: MENU
"""
skip_first_screenshot = False
@ -97,9 +96,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

View File

@ -1,7 +1,6 @@
from module.base.timer import Timer
from module.logger import logger
from tasks.base.assets.assets_base_page import CLOSE
from tasks.base.assets.assets_base_popup import GET_REWARD
from tasks.item.assets.assets_item_relics import *
from tasks.item.keywords import KEYWORD_ITEM_TAB
from tasks.item.ui import ItemUI
@ -96,7 +95,7 @@ class RelicsUI(ItemUI):
else:
self.device.screenshot()
if self.appear(GET_REWARD):
if self.reward_appear():
logger.info("Relic salvaged")
break
if self.appear_then_click(SALVAGE, interval=2):