2023-07-09 10:09:30 +00:00
|
|
|
import datetime
|
2023-08-02 11:07:46 +00:00
|
|
|
import re
|
2023-07-09 10:09:30 +00:00
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from module.base.timer import Timer
|
|
|
|
from module.base.utils import get_color
|
2023-07-09 10:09:30 +00:00
|
|
|
from module.config.utils import get_server_next_update
|
2023-06-19 13:57:19 +00:00
|
|
|
from module.logger.logger import logger
|
2023-08-02 11:07:46 +00:00
|
|
|
from module.ocr.ocr import Digit, Duration, Ocr
|
|
|
|
from module.ocr.utils import split_and_pair_buttons
|
|
|
|
from module.ui.scroll import Scroll
|
2023-06-19 13:57:19 +00:00
|
|
|
from module.ui.switch import Switch
|
2023-07-19 05:36:48 +00:00
|
|
|
from tasks.base.assets.assets_base_page import BATTLE_PASS_CHECK, MAIN_GOTO_BATTLE_PASS
|
2023-07-17 05:40:58 +00:00
|
|
|
from tasks.base.page import page_battle_pass, page_main
|
2023-06-19 13:57:19 +00:00
|
|
|
from tasks.base.ui import UI
|
|
|
|
from tasks.battle_pass.assets.assets_battle_pass import *
|
2023-08-02 11:07:46 +00:00
|
|
|
from tasks.battle_pass.keywords import *
|
2023-06-19 13:57:19 +00:00
|
|
|
|
|
|
|
SWITCH_BATTLE_PASS_TAB = Switch('BattlePassTab', is_selector=True)
|
|
|
|
SWITCH_BATTLE_PASS_TAB.add_state(
|
|
|
|
KEYWORD_BATTLE_PASS_TAB.Rewards,
|
|
|
|
check_button=REWARDS_CHECK,
|
|
|
|
click_button=REWARDS_CLICK
|
|
|
|
)
|
|
|
|
SWITCH_BATTLE_PASS_TAB.add_state(
|
|
|
|
KEYWORD_BATTLE_PASS_TAB.Missions,
|
|
|
|
check_button=MISSIONS_CHECK,
|
|
|
|
click_button=MISSIONS_CLICK
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-02 11:07:46 +00:00
|
|
|
class BattlePassMissionTab(Switch):
|
|
|
|
def get(self, main):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
main (ModuleBase):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: state name or 'unknown'.
|
|
|
|
"""
|
|
|
|
for data in self.state_list:
|
|
|
|
if main.image_color_count(data['check_button'], color=(250, 233, 153)):
|
|
|
|
return data['state']
|
|
|
|
|
|
|
|
return 'unknown'
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_BATTLE_PASS_MISSION_TAB = BattlePassMissionTab('BattlePassMissionTab', is_selector=True)
|
|
|
|
SWITCH_BATTLE_PASS_MISSION_TAB.add_state(
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.Today_Missions,
|
|
|
|
check_button=TODAY_MISSION_CLICK,
|
|
|
|
click_button=TODAY_MISSION_CLICK
|
|
|
|
)
|
|
|
|
SWITCH_BATTLE_PASS_MISSION_TAB.add_state(
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.This_Week_Missions,
|
|
|
|
check_button=WEEK_MISSION_CLICK,
|
|
|
|
click_button=WEEK_MISSION_CLICK
|
|
|
|
)
|
|
|
|
SWITCH_BATTLE_PASS_MISSION_TAB.add_state(
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.This_Period_Missions,
|
|
|
|
check_button=PERIOD_MISSION_CLICK,
|
|
|
|
click_button=PERIOD_MISSION_CLICK
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BattlePassQuestOcr(Ocr):
|
|
|
|
def after_process(self, result):
|
|
|
|
result = super().after_process(result)
|
2023-09-08 14:23:57 +00:00
|
|
|
if self.lang == 'cn':
|
2023-08-02 11:07:46 +00:00
|
|
|
result = re.sub("[jJ]", "」", result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
class BattlePassUI(UI):
|
2023-11-17 04:37:16 +00:00
|
|
|
MAX_LEVEL = 70
|
2023-07-08 04:09:23 +00:00
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
def _battle_pass_wait_rewards_loaded(self, skip_first_screenshot=True):
|
|
|
|
timeout = Timer(2, count=4).start()
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
|
|
|
logger.warning('Wait rewards tab loaded timeout')
|
|
|
|
break
|
|
|
|
if self.appear(REWARDS_LOADED):
|
|
|
|
logger.info('Rewards tab loaded')
|
|
|
|
break
|
|
|
|
|
2023-08-02 11:07:46 +00:00
|
|
|
def _battle_pass_wait_missions_loaded(self, skip_first_screenshot=True, has_scroll=True):
|
2023-06-19 13:57:19 +00:00
|
|
|
timeout = Timer(2, count=4).start()
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
|
|
|
logger.warning('Wait missions tab loaded timeout')
|
|
|
|
break
|
2023-08-02 11:07:46 +00:00
|
|
|
if has_scroll:
|
|
|
|
if self.appear(MISSION_PAGE_SCROLL):
|
|
|
|
logger.info('Rewards tab loaded')
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
color = get_color(self.device.image, MISSIONS_LOADED.area)
|
|
|
|
if np.mean(color) > 128:
|
|
|
|
logger.info('Missions tab loaded')
|
|
|
|
break
|
2023-06-19 13:57:19 +00:00
|
|
|
|
|
|
|
def battle_pass_goto(self, state: KEYWORD_BATTLE_PASS_TAB):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
state:
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
self = BattlePassUI('alas')
|
|
|
|
self.device.screenshot()
|
|
|
|
self.battle_pass_goto(KEYWORDS_DUNGEON_TAB.Missions)
|
|
|
|
self.battle_pass_goto(KEYWORDS_DUNGEON_TAB.Rewards)
|
|
|
|
"""
|
|
|
|
self.ui_ensure(page_battle_pass)
|
|
|
|
if SWITCH_BATTLE_PASS_TAB.set(state, main=self):
|
|
|
|
logger.info(f'Tab goto {state}, wait until loaded')
|
|
|
|
if state == KEYWORD_BATTLE_PASS_TAB.Missions:
|
2023-08-02 11:07:46 +00:00
|
|
|
self._battle_pass_wait_missions_loaded(has_scroll=False)
|
2023-06-19 13:57:19 +00:00
|
|
|
if state == KEYWORD_BATTLE_PASS_TAB.Rewards:
|
|
|
|
self._battle_pass_wait_rewards_loaded()
|
|
|
|
|
2023-08-02 11:07:46 +00:00
|
|
|
def battle_pass_mission_tab_goto(self, state: KEYWORD_BATTLE_PASS_MISSION_TAB):
|
|
|
|
self.battle_pass_goto(KEYWORD_BATTLE_PASS_TAB.Missions)
|
|
|
|
if SWITCH_BATTLE_PASS_MISSION_TAB.set(state, main=self):
|
|
|
|
logger.info(f'Tab goto {state}, wait until loaded')
|
|
|
|
if state == KEYWORD_BATTLE_PASS_MISSION_TAB.Today_Missions:
|
|
|
|
self._battle_pass_wait_missions_loaded(has_scroll=False)
|
|
|
|
if state == KEYWORD_BATTLE_PASS_MISSION_TAB.This_Week_Missions:
|
|
|
|
self._battle_pass_wait_missions_loaded()
|
|
|
|
if state == KEYWORD_BATTLE_PASS_MISSION_TAB.This_Period_Missions:
|
|
|
|
self._battle_pass_wait_missions_loaded()
|
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
def handle_choose_gifts(self, interval=5):
|
|
|
|
"""
|
|
|
|
Popup when you have purchase Nameless Glory
|
|
|
|
|
|
|
|
Args:
|
|
|
|
interval:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
If handled
|
|
|
|
"""
|
|
|
|
if self.appear_then_click(CLOSE_CHOOSE_GIFT, interval=interval):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _claim_exp(self, skip_first_screenshot=True):
|
2023-06-26 11:16:19 +00:00
|
|
|
logger.hr('Claim EXP', level=1)
|
2023-06-19 13:57:19 +00:00
|
|
|
self.battle_pass_goto(KEYWORD_BATTLE_PASS_TAB.Missions)
|
2023-06-23 01:40:42 +00:00
|
|
|
claimed = False
|
2023-06-19 13:57:19 +00:00
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if not self.appear(EXP_CLAIM_ALL):
|
2023-06-26 11:16:19 +00:00
|
|
|
logger.info('No more EXP to claim')
|
2023-06-19 13:57:19 +00:00
|
|
|
break
|
|
|
|
if self.appear_then_click(EXP_CLAIM_ALL):
|
2023-06-23 01:40:42 +00:00
|
|
|
claimed = True
|
2023-06-19 13:57:19 +00:00
|
|
|
logger.info("All EXP claimed")
|
|
|
|
continue
|
2023-06-26 11:16:19 +00:00
|
|
|
|
|
|
|
logger.attr('EXP claimed', claimed)
|
2023-06-23 01:40:42 +00:00
|
|
|
return claimed
|
2023-06-19 13:57:19 +00:00
|
|
|
|
|
|
|
def _claim_rewards(self, skip_first_screenshot=True):
|
2023-06-26 11:16:19 +00:00
|
|
|
logger.hr('Claim rewards', level=1)
|
2023-06-19 13:57:19 +00:00
|
|
|
self.battle_pass_goto(KEYWORD_BATTLE_PASS_TAB.Rewards)
|
2023-11-17 13:54:35 +00:00
|
|
|
timeout = Timer(5, count=15).start()
|
2023-06-19 13:57:19 +00:00
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
2023-11-06 14:30:43 +00:00
|
|
|
if self.reward_appear():
|
2023-06-26 11:16:19 +00:00
|
|
|
logger.info('Get reward')
|
|
|
|
break
|
|
|
|
if self.appear(CLOSE_CHOOSE_GIFT):
|
|
|
|
logger.info('Got reward but have gift to choose')
|
2023-06-19 13:57:19 +00:00
|
|
|
break
|
2023-11-17 13:54:35 +00:00
|
|
|
if timeout.reached():
|
|
|
|
logger.warning('Claim reward timeout, no rewards to claim')
|
|
|
|
break
|
2023-09-15 16:05:32 +00:00
|
|
|
if self.appear_then_click(REWARDS_CLAIM_ALL, interval=2):
|
2023-11-17 13:54:35 +00:00
|
|
|
timeout.reset()
|
2023-06-19 13:57:19 +00:00
|
|
|
continue
|
|
|
|
|
2023-11-17 13:54:35 +00:00
|
|
|
logger.info('Close reward popup')
|
2023-06-19 13:57:19 +00:00
|
|
|
skip_first_screenshot = True
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if self.appear(BATTLE_PASS_CHECK):
|
|
|
|
logger.info("Claiming rewards complete")
|
|
|
|
break
|
|
|
|
if self.handle_choose_gifts():
|
|
|
|
logger.info("You have unclaimed gift to choose")
|
|
|
|
continue
|
|
|
|
if self.handle_reward():
|
|
|
|
continue
|
|
|
|
|
2023-07-09 10:09:30 +00:00
|
|
|
def _get_battle_pass_level(self) -> int:
|
2023-06-23 01:40:42 +00:00
|
|
|
digit = Digit(OCR_LEVEL)
|
|
|
|
return digit.ocr_single_line(self.device.image)
|
|
|
|
|
2023-07-09 10:09:30 +00:00
|
|
|
def _get_battle_pass_end(self) -> datetime.datetime:
|
|
|
|
remain = Duration(OCR_REMAINING_TIME).ocr_single_line(self.device.image)
|
|
|
|
future = get_server_next_update(self.config.Scheduler_ServerUpdate)
|
|
|
|
future += datetime.timedelta(days=remain.days)
|
|
|
|
return future
|
2023-07-08 04:09:23 +00:00
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
def claim_battle_pass_rewards(self):
|
|
|
|
"""
|
2023-07-09 10:09:30 +00:00
|
|
|
Returns:
|
|
|
|
int: Current battle pass level
|
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
Examples:
|
|
|
|
self = BattlePassUI('alas')
|
|
|
|
self.device.screenshot()
|
2023-06-23 01:40:42 +00:00
|
|
|
self.claim_battle_pass_rewards()
|
2023-06-19 13:57:19 +00:00
|
|
|
"""
|
2023-09-12 20:26:36 +00:00
|
|
|
logger.hr('Quest recognise', level=1)
|
2023-08-30 17:06:24 +00:00
|
|
|
with self.config.multi_set():
|
|
|
|
previous_level = self._get_battle_pass_level()
|
|
|
|
self.config.stored.BattlePassLevel.set(previous_level)
|
|
|
|
quests = self.battle_pass_quests_recognition(
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.Today_Missions, has_scroll=False)
|
|
|
|
self.config.stored.BattlePassTodayQuest.write_quests(quests)
|
2023-07-08 04:09:23 +00:00
|
|
|
if previous_level == self.MAX_LEVEL:
|
|
|
|
return previous_level
|
2023-08-30 17:06:24 +00:00
|
|
|
|
2023-06-23 01:40:42 +00:00
|
|
|
claimed_exp = self._claim_exp()
|
2023-07-08 04:09:23 +00:00
|
|
|
current_level = self._get_battle_pass_level()
|
2023-08-30 17:06:24 +00:00
|
|
|
self.config.stored.BattlePassLevel.set(current_level)
|
2023-07-08 04:09:23 +00:00
|
|
|
if claimed_exp and current_level > previous_level:
|
2023-06-23 01:40:42 +00:00
|
|
|
logger.info("Upgraded, go to claim rewards")
|
|
|
|
self._claim_rewards()
|
2023-07-08 04:09:23 +00:00
|
|
|
return current_level
|
2023-06-19 13:57:19 +00:00
|
|
|
|
2023-08-02 11:07:46 +00:00
|
|
|
def ocr_single_page(self):
|
|
|
|
"""
|
|
|
|
Returns incomplete quests only
|
|
|
|
"""
|
|
|
|
ocr = BattlePassQuestOcr(OCR_BATTLE_PASS_QUEST)
|
|
|
|
results = ocr.matched_ocr(self.device.image, [BattlePassQuest, BattlePassQuestState])
|
|
|
|
|
|
|
|
def completed_state(state):
|
|
|
|
return state != KEYWORD_BATTLE_PASS_QUEST_STATE.Navigate
|
|
|
|
|
|
|
|
return [incomplete_quest for incomplete_quest, _ in
|
|
|
|
split_and_pair_buttons(results, split_func=completed_state, relative_area=(0, 0, 800, 100))]
|
|
|
|
|
2023-08-30 17:06:24 +00:00
|
|
|
def battle_pass_quests_recognition(
|
|
|
|
self,
|
|
|
|
page: KEYWORD_BATTLE_PASS_MISSION_TAB,
|
|
|
|
has_scroll=True,
|
|
|
|
) -> list[BattlePassQuest]:
|
2023-08-02 11:07:46 +00:00
|
|
|
"""
|
|
|
|
Args:
|
2023-08-30 17:06:24 +00:00
|
|
|
page: One of the followings:
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.Today_Missions
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.This_Week_Missions
|
|
|
|
KEYWORD_BATTLE_PASS_MISSION_TAB.This_Period_Missions
|
2023-08-02 11:07:46 +00:00
|
|
|
has_scroll: need to scroll to recognize all quests
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
"""
|
|
|
|
logger.info(f"Recognizing battle pass quests at {page}")
|
|
|
|
self.battle_pass_mission_tab_goto(page)
|
|
|
|
if not has_scroll:
|
|
|
|
results = self.ocr_single_page()
|
2023-08-30 17:34:16 +00:00
|
|
|
results = [result.matched_keyword for result in results]
|
2023-08-02 11:07:46 +00:00
|
|
|
else:
|
|
|
|
scroll = Scroll(MISSION_PAGE_SCROLL, color=(198, 198, 198))
|
|
|
|
scroll.set_top(main=self)
|
|
|
|
results = self.ocr_single_page()
|
|
|
|
while not scroll.at_bottom(main=self):
|
|
|
|
scroll.next_page(main=self)
|
|
|
|
results += [result for result in self.ocr_single_page() if result not in results]
|
|
|
|
results = [result.matched_keyword for result in results]
|
|
|
|
return results
|
|
|
|
|
2023-07-29 08:42:34 +00:00
|
|
|
def has_battle_pass_entrance(self, skip_first_screenshot=True):
|
|
|
|
"""
|
|
|
|
Pages:
|
|
|
|
in: page_main
|
|
|
|
"""
|
|
|
|
timeout = Timer(0.5, count=2).start()
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
|
|
|
break
|
|
|
|
|
|
|
|
if self.appear(MAIN_GOTO_BATTLE_PASS, similarity=0.75):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2023-10-09 04:16:03 +00:00
|
|
|
logger.info('No battle pass entrance, probably a gap between two periods')
|
2023-07-29 08:42:34 +00:00
|
|
|
return False
|
|
|
|
|
2023-06-19 13:57:19 +00:00
|
|
|
def run(self):
|
2023-07-17 05:40:58 +00:00
|
|
|
self.ui_ensure(page_main)
|
2023-07-29 08:42:34 +00:00
|
|
|
if not self.has_battle_pass_entrance():
|
2023-10-09 04:16:03 +00:00
|
|
|
self.config.stored.BattlePassTodayQuest.set(0)
|
2023-07-17 05:40:58 +00:00
|
|
|
self.config.task_delay(server_update=True)
|
|
|
|
self.config.task_stop()
|
|
|
|
|
|
|
|
self.ui_goto(page_battle_pass)
|
2023-07-08 04:09:23 +00:00
|
|
|
current_level = self.claim_battle_pass_rewards()
|
|
|
|
if current_level == self.MAX_LEVEL:
|
2023-07-09 10:09:30 +00:00
|
|
|
self.config.task_delay(target=self._get_battle_pass_end())
|
2023-07-08 04:09:23 +00:00
|
|
|
else:
|
|
|
|
self.config.task_delay(server_update=True)
|