2023-06-19 00:39:41 +00:00
|
|
|
import re
|
2023-09-26 07:06:43 +00:00
|
|
|
from collections.abc import Iterator
|
|
|
|
from datetime import timedelta
|
2023-09-26 13:27:38 +00:00
|
|
|
from enum import Enum
|
2023-06-19 00:39:41 +00:00
|
|
|
from functools import cached_property
|
|
|
|
|
|
|
|
from module.base.timer import Timer
|
2023-09-12 10:36:24 +00:00
|
|
|
from module.exception import ScriptError
|
2023-06-19 00:39:41 +00:00
|
|
|
from module.logger import logger
|
2023-09-15 17:37:06 +00:00
|
|
|
from module.ocr.ocr import DigitCounter, Duration, Ocr
|
2023-06-19 00:39:41 +00:00
|
|
|
from module.ui.draggable_list import DraggableList
|
2023-09-26 13:27:38 +00:00
|
|
|
from tasks.assignment.assets.assets_assignment_claim import CLAIM
|
|
|
|
from tasks.assignment.assets.assets_assignment_dispatch import EMPTY_SLOT
|
2023-06-19 00:39:41 +00:00
|
|
|
from tasks.assignment.assets.assets_assignment_ui import *
|
|
|
|
from tasks.assignment.keywords import *
|
2024-05-19 13:50:00 +00:00
|
|
|
from tasks.base.assets.assets_base_page import ASSIGNMENT_CHECK
|
2023-06-19 00:39:41 +00:00
|
|
|
from tasks.base.ui import UI
|
2024-09-10 16:54:11 +00:00
|
|
|
from tasks.dungeon.ui.nav import DungeonTabSwitch
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
|
2023-09-26 13:27:38 +00:00
|
|
|
class AssignmentStatus(Enum):
|
|
|
|
CLAIMABLE = 0
|
|
|
|
DISPATCHED = 1
|
|
|
|
DISPATCHABLE = 2
|
|
|
|
LOCKED = 3
|
|
|
|
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
class AssignmentOcr(Ocr):
|
2023-09-15 17:37:06 +00:00
|
|
|
# EN has names in multiple rows
|
|
|
|
merge_thres_y = 20
|
|
|
|
merge_thres_x = 20
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
OCR_REPLACE = {
|
2023-09-12 10:36:24 +00:00
|
|
|
'cn': [
|
2024-05-19 13:50:00 +00:00
|
|
|
(KEYWORDS_ASSIGNMENT_ENTRY.Winter_Soldiers, '[黑]冬的战士们'),
|
|
|
|
(KEYWORDS_ASSIGNMENT_ENTRY.Born_to_Obey, '[牛]而服从'),
|
|
|
|
(KEYWORDS_ASSIGNMENT_ENTRY.Root_Out_the_Turpitude,
|
2023-06-19 00:39:41 +00:00
|
|
|
'根除恶[擎薯尊掌鞋]?'),
|
2024-05-19 13:50:00 +00:00
|
|
|
(KEYWORDS_ASSIGNMENT_ENTRY.Akashic_Records, '阿[未][夏复]记录'),
|
|
|
|
(KEYWORDS_ASSIGNMENT_ENTRY.Legend_of_the_Puppet_Master, '^师传说'),
|
|
|
|
(KEYWORDS_ASSIGNMENT_ENTRY.The_Wages_of_Humanity, '[赠]养人类'),
|
2024-11-15 08:44:02 +00:00
|
|
|
# (KEYWORDS_ASSIGNMENT_EVENT_ENTRY.Car_Thief, '.*的偷车贼.*'),
|
|
|
|
# (KEYWORDS_ASSIGNMENT_EVENT_ENTRY.Synesthesia_Beacon_Function_Iteration,
|
|
|
|
# '联觉信标功能[送]代'),
|
2023-09-26 07:06:43 +00:00
|
|
|
],
|
|
|
|
'en': [
|
2024-05-08 14:00:41 +00:00
|
|
|
# (KEYWORDS_ASSIGNMENT_EVENT_ENTRY.Food_Improvement_Plan.name,
|
|
|
|
# 'Food\s*[I]{0}mprovement Plan'),
|
2024-11-15 08:44:02 +00:00
|
|
|
# (KEYWORDS_ASSIGNMENT_EVENT_ENTRY.Car_Thief, '.*Car Thief.*'),
|
2023-06-19 00:39:41 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ocr_regex(self) -> re.Pattern | None:
|
|
|
|
rules = AssignmentOcr.OCR_REPLACE.get(self.lang)
|
|
|
|
if rules is None:
|
|
|
|
return None
|
2024-05-19 13:50:00 +00:00
|
|
|
return re.compile('|'.join(
|
|
|
|
f'(?P<{kw.name}>{pat})'
|
|
|
|
for kw, pat in rules
|
|
|
|
))
|
2023-06-19 00:39:41 +00:00
|
|
|
|
2023-09-17 00:09:00 +00:00
|
|
|
def filter_detected(self, result) -> bool:
|
|
|
|
# Drop duration rows
|
|
|
|
res = Duration.timedelta_regex(self.lang).search(result.ocr_text)
|
2023-09-26 07:06:43 +00:00
|
|
|
if res.group('hours') or res.group('seconds'):
|
|
|
|
return False
|
|
|
|
# Locked event assignments
|
|
|
|
locked_pattern = {
|
|
|
|
'cn': '解锁$',
|
|
|
|
'en': 'Locked$',
|
|
|
|
}[self.lang]
|
|
|
|
res = re.search(locked_pattern, result.ocr_text)
|
|
|
|
return not res
|
2023-09-17 00:09:00 +00:00
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
def after_process(self, result: str):
|
|
|
|
result = super().after_process(result)
|
2024-03-28 14:17:49 +00:00
|
|
|
# Born to ObeyCurrently Owned:7781 -> Born to Obey
|
|
|
|
for splitter in ['Currently', 'currently', '当前持有']:
|
|
|
|
try:
|
|
|
|
result = result.split(splitter)[0]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
2023-06-19 00:39:41 +00:00
|
|
|
if self.ocr_regex is None:
|
|
|
|
return result
|
|
|
|
matched = self.ocr_regex.fullmatch(result)
|
|
|
|
if matched is None:
|
|
|
|
return result
|
2023-09-26 07:06:43 +00:00
|
|
|
for keyword_class in (
|
2023-09-26 14:35:55 +00:00
|
|
|
KEYWORDS_ASSIGNMENT_ENTRY,
|
|
|
|
KEYWORDS_ASSIGNMENT_EVENT_ENTRY,
|
2023-09-26 07:06:43 +00:00
|
|
|
):
|
2024-05-19 13:50:00 +00:00
|
|
|
kw = getattr(keyword_class, matched.lastgroup, None)
|
|
|
|
if kw is not None:
|
|
|
|
matched = kw
|
2023-09-26 07:06:43 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ScriptError(f'No keyword found for {matched.lastgroup}')
|
2024-05-19 13:50:00 +00:00
|
|
|
matched = getattr(matched, self.lang)
|
2023-06-19 00:39:41 +00:00
|
|
|
logger.attr(name=f'{self.name} after_process',
|
|
|
|
text=f'{result} -> {matched}')
|
|
|
|
return matched
|
|
|
|
|
|
|
|
|
2024-06-19 19:03:07 +00:00
|
|
|
class AssignmentGroupSwitch(DungeonTabSwitch):
|
|
|
|
SEARCH_BUTTON = GROUP_SEARCH
|
|
|
|
|
|
|
|
|
|
|
|
ASSIGNMENT_GROUP_SWITCH = AssignmentGroupSwitch(
|
2023-09-26 14:35:55 +00:00
|
|
|
'AssignmentGroupSwitch',
|
|
|
|
is_selector=True
|
|
|
|
)
|
|
|
|
ASSIGNMENT_GROUP_SWITCH.add_state(
|
2024-11-15 08:44:02 +00:00
|
|
|
CURRENT_EVENT_GROUP,
|
|
|
|
check_button=SHADOW_OF_THE_RANGER_CHECK,
|
|
|
|
click_button=SHADOW_OF_THE_RANGER_CLICK
|
2023-09-26 14:35:55 +00:00
|
|
|
)
|
|
|
|
ASSIGNMENT_GROUP_SWITCH.add_state(
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.Character_Materials,
|
|
|
|
check_button=CHARACTER_MATERIALS_CHECK,
|
|
|
|
click_button=CHARACTER_MATERIALS_CLICK
|
|
|
|
)
|
|
|
|
ASSIGNMENT_GROUP_SWITCH.add_state(
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.EXP_Materials_Credits,
|
|
|
|
check_button=EXP_MATERIALS_CREDITS_CHECK,
|
|
|
|
click_button=EXP_MATERIALS_CREDITS_CLICK
|
|
|
|
)
|
|
|
|
ASSIGNMENT_GROUP_SWITCH.add_state(
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.Synthesis_Materials,
|
|
|
|
check_button=SYNTHESIS_MATERIALS_CHECK,
|
|
|
|
click_button=SYNTHESIS_MATERIALS_CLICK
|
2023-06-19 00:39:41 +00:00
|
|
|
)
|
|
|
|
ASSIGNMENT_ENTRY_LIST = DraggableList(
|
|
|
|
'AssignmentEntryList',
|
2023-09-26 07:06:43 +00:00
|
|
|
keyword_class=[AssignmentEntry, AssignmentEventEntry],
|
2023-06-19 00:39:41 +00:00
|
|
|
ocr_class=AssignmentOcr,
|
2023-09-25 06:13:11 +00:00
|
|
|
search_button=OCR_ASSIGNMENT_ENTRY_LIST,
|
2023-06-25 16:07:52 +00:00
|
|
|
check_row_order=False,
|
2023-06-19 00:39:41 +00:00
|
|
|
active_color=(40, 40, 40)
|
|
|
|
)
|
2023-09-27 02:53:37 +00:00
|
|
|
ASSIGNMENT_ENTRY_LIST.known_rows = [
|
|
|
|
kw for kc in ASSIGNMENT_ENTRY_LIST.keyword_class
|
|
|
|
for kw in kc.instances.values()
|
|
|
|
]
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssignmentUI(UI):
|
|
|
|
def goto_group(self, group: AssignmentGroup):
|
|
|
|
"""
|
|
|
|
Args:
|
2023-06-19 17:00:34 +00:00
|
|
|
group (AssignmentGroup):
|
2023-06-19 00:39:41 +00:00
|
|
|
|
2024-05-20 18:31:27 +00:00
|
|
|
Returns:
|
|
|
|
bool: If group switched
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
Examples:
|
|
|
|
self = AssignmentUI('src')
|
|
|
|
self.device.screenshot()
|
|
|
|
self.goto_group(KEYWORDS_ASSIGNMENT_GROUP.Character_Materials)
|
|
|
|
"""
|
|
|
|
logger.hr('Assignment group goto', level=3)
|
2023-09-26 14:35:55 +00:00
|
|
|
if ASSIGNMENT_GROUP_SWITCH.set(group, self):
|
2023-06-19 00:39:41 +00:00
|
|
|
self._wait_until_entry_loaded()
|
2024-05-20 10:07:39 +00:00
|
|
|
self._wait_until_correct_entry_loaded(group)
|
2024-05-20 18:31:27 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
if not ASSIGNMENT_ENTRY_LIST.cur_buttons:
|
|
|
|
ASSIGNMENT_ENTRY_LIST.load_rows(self)
|
|
|
|
return False
|
2023-06-19 00:39:41 +00:00
|
|
|
|
2023-09-26 15:11:45 +00:00
|
|
|
def goto_entry(self, entry: AssignmentEntry, insight: bool = True):
|
2023-06-19 00:39:41 +00:00
|
|
|
"""
|
|
|
|
Args:
|
2023-06-19 17:00:34 +00:00
|
|
|
entry (AssignmentEntry):
|
2023-09-26 15:11:45 +00:00
|
|
|
insight (bool): skip ocr to save time if insight is False
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
self = AssignmentUI('src')
|
|
|
|
self.device.screenshot()
|
|
|
|
self.goto_entry(KEYWORDS_ASSIGNMENT_ENTRY.Nameless_Land_Nameless_People)
|
|
|
|
"""
|
2023-09-12 10:36:24 +00:00
|
|
|
if entry.group is None:
|
|
|
|
err_msg = f'{entry} is not in any group, please inform developers if possible'
|
|
|
|
logger.warning(err_msg)
|
|
|
|
for group in self._iter_groups():
|
|
|
|
self.goto_group(group)
|
|
|
|
if ASSIGNMENT_ENTRY_LIST.select_row(entry, self):
|
|
|
|
return
|
|
|
|
raise ScriptError(err_msg)
|
|
|
|
else:
|
2024-05-20 18:31:27 +00:00
|
|
|
if self.goto_group(entry.group):
|
|
|
|
# Already insight in goto_group() - _wait_until_correct_entry_loaded()
|
|
|
|
ASSIGNMENT_ENTRY_LIST.select_row(entry, self, insight=False)
|
|
|
|
else:
|
|
|
|
ASSIGNMENT_ENTRY_LIST.select_row(entry, self, insight=insight)
|
2023-06-19 00:39:41 +00:00
|
|
|
|
2023-09-26 07:06:43 +00:00
|
|
|
def _wait_until_group_loaded(self):
|
|
|
|
skip_first_screenshot = True
|
2024-05-19 13:50:00 +00:00
|
|
|
timeout = Timer(3, count=3).start()
|
2023-09-26 07:06:43 +00:00
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
|
|
|
logger.warning('Wait group loaded timeout')
|
|
|
|
break
|
2023-09-26 15:01:13 +00:00
|
|
|
if self.image_color_count(GROUP_SEARCH, (40, 40, 40), count=25000) and \
|
|
|
|
self.image_color_count(GROUP_SEARCH, (240, 240, 240), count=8000):
|
2023-09-26 07:06:43 +00:00
|
|
|
logger.info('Group loaded')
|
|
|
|
break
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
def _wait_until_entry_loaded(self):
|
|
|
|
skip_first_screenshot = True
|
|
|
|
timeout = Timer(2, count=3).start()
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
|
|
|
logger.warning('Wait entry loaded timeout')
|
|
|
|
break
|
2024-05-21 11:02:45 +00:00
|
|
|
if self.appear(EVENT_COMPLETED):
|
|
|
|
logger.info('Event completed')
|
2024-05-20 15:49:36 +00:00
|
|
|
break
|
2024-05-19 13:50:00 +00:00
|
|
|
if self.appear(ASSIGNMENT_CHECK) and \
|
2024-06-10 08:05:57 +00:00
|
|
|
self.image_color_count(ENTRY_LOADED, (35, 35, 35), count=400):
|
2023-06-19 00:39:41 +00:00
|
|
|
logger.info('Entry loaded')
|
|
|
|
break
|
|
|
|
|
2024-05-20 10:07:39 +00:00
|
|
|
def _wait_until_correct_entry_loaded(self, group: AssignmentGroup):
|
|
|
|
skip_first_screenshot = True
|
|
|
|
timeout = Timer(3, count=3).start()
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
|
|
|
logger.warning('Wait correct entry loaded timeout')
|
|
|
|
break
|
2024-05-21 11:02:45 +00:00
|
|
|
if isinstance(group, AssignmentEventGroup) and self.appear(EVENT_COMPLETED):
|
2024-05-21 10:48:12 +00:00
|
|
|
logger.info('Correct entry loaded')
|
|
|
|
ASSIGNMENT_ENTRY_LIST.cur_buttons = []
|
2024-05-20 15:49:36 +00:00
|
|
|
break
|
2024-05-20 10:07:39 +00:00
|
|
|
|
|
|
|
ASSIGNMENT_ENTRY_LIST.load_rows(self)
|
2024-05-21 11:02:45 +00:00
|
|
|
if ASSIGNMENT_ENTRY_LIST.cur_buttons and all(
|
2024-05-20 10:07:39 +00:00
|
|
|
x.matched_keyword.group == group
|
|
|
|
for x in ASSIGNMENT_ENTRY_LIST.cur_buttons
|
|
|
|
):
|
|
|
|
logger.info('Correct entry loaded')
|
|
|
|
break
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
@property
|
|
|
|
def _limit_status(self) -> tuple[int, int, int]:
|
|
|
|
self.device.screenshot()
|
2024-05-19 13:50:00 +00:00
|
|
|
if isinstance(ASSIGNMENT_GROUP_SWITCH.get(self), AssignmentEventGroup):
|
|
|
|
ASSIGNMENT_GROUP_SWITCH.set(
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.Character_Materials, self)
|
2023-09-12 10:36:24 +00:00
|
|
|
current, remain, total = DigitCounter(
|
|
|
|
OCR_ASSIGNMENT_LIMIT).ocr_single_line(self.device.image)
|
2023-08-29 16:14:56 +00:00
|
|
|
if total and current <= total:
|
|
|
|
logger.attr('Assignment', f'{current}/{total}')
|
|
|
|
self.config.stored.Assignment.set(current, total)
|
|
|
|
else:
|
|
|
|
logger.warning(f'Invalid assignment limit: {current}/{total}')
|
|
|
|
self.config.stored.Assignment.set(0, 0)
|
|
|
|
return current, remain, total
|
2023-06-19 00:39:41 +00:00
|
|
|
|
2023-09-26 13:27:38 +00:00
|
|
|
def _check_assignment_status(self) -> AssignmentStatus:
|
|
|
|
skip_first_screenshot = True
|
|
|
|
timeout = Timer(2, count=3).start()
|
2023-09-27 04:52:37 +00:00
|
|
|
ret = AssignmentStatus.LOCKED
|
2023-09-26 13:27:38 +00:00
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
if timeout.reached():
|
2023-09-28 13:39:13 +00:00
|
|
|
logger.warning(
|
2023-09-26 13:27:38 +00:00
|
|
|
'Check assignment status timeout, assume LOCKED'
|
|
|
|
)
|
|
|
|
break
|
|
|
|
if self.appear(CLAIM):
|
2023-09-27 04:52:37 +00:00
|
|
|
ret = AssignmentStatus.CLAIMABLE
|
|
|
|
break
|
2023-09-26 13:27:38 +00:00
|
|
|
if self.appear(DISPATCHED):
|
2023-09-27 04:52:37 +00:00
|
|
|
ret = AssignmentStatus.DISPATCHED
|
|
|
|
break
|
2023-09-26 13:27:38 +00:00
|
|
|
if self.appear(EMPTY_SLOT):
|
2023-09-27 04:52:37 +00:00
|
|
|
ret = AssignmentStatus.DISPATCHABLE
|
|
|
|
break
|
2023-09-28 13:39:13 +00:00
|
|
|
if self.appear(LOCKED):
|
|
|
|
ret = AssignmentStatus.LOCKED
|
|
|
|
break
|
2023-09-27 04:52:37 +00:00
|
|
|
logger.attr('AssignmentStatus', ret.name)
|
|
|
|
return ret
|
2023-09-26 13:27:38 +00:00
|
|
|
|
2023-09-26 07:06:43 +00:00
|
|
|
def _get_assignment_time(self) -> timedelta:
|
|
|
|
return Duration(OCR_ASSIGNMENT_TIME).ocr_single_line(self.device.image)
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
def _iter_groups(self) -> Iterator[AssignmentGroup]:
|
2023-09-26 07:06:43 +00:00
|
|
|
self._wait_until_group_loaded()
|
2023-09-26 14:35:55 +00:00
|
|
|
for state in ASSIGNMENT_GROUP_SWITCH.state_list:
|
|
|
|
check = state['check_button']
|
|
|
|
click = state['click_button']
|
2023-09-26 15:01:13 +00:00
|
|
|
if self.appear(check) or self.appear(click):
|
2023-09-26 14:35:55 +00:00
|
|
|
yield state['state']
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
def _iter_entries(self) -> Iterator[AssignmentEntry]:
|
|
|
|
"""
|
|
|
|
Iterate entries from top to bottom
|
|
|
|
"""
|
2024-05-20 13:27:55 +00:00
|
|
|
# load_rows is done in goto_group already
|
2023-09-26 07:06:43 +00:00
|
|
|
# Freeze ocr results here
|
|
|
|
yield from [
|
|
|
|
button.matched_keyword
|
|
|
|
for button in ASSIGNMENT_ENTRY_LIST.cur_buttons
|
|
|
|
]
|