From 724610404a009d1fe5464380cac8856b616c05bd Mon Sep 17 00:00:00 2001 From: Zebartin <1500012724@pku.edu.cn> Date: Tue, 20 Jun 2023 01:00:34 +0800 Subject: [PATCH] Opt: Reduce use of ocr --- tasks/assignment/assignment.py | 37 ++++++++++++++++++---------- tasks/assignment/claim.py | 4 +-- tasks/assignment/dispatch.py | 11 ++++----- tasks/assignment/keywords/classes.py | 4 +-- tasks/assignment/ui.py | 5 ++-- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/tasks/assignment/assignment.py b/tasks/assignment/assignment.py index 9c7000b1b..c4dabdeb8 100644 --- a/tasks/assignment/assignment.py +++ b/tasks/assignment/assignment.py @@ -8,10 +8,13 @@ from tasks.assignment.assets.assets_assignment_ui import (DISPATCHED, OCR_ASSIGNMENT_TIME) from tasks.assignment.claim import AssignmentClaim from tasks.assignment.keywords import * -from tasks.base.page import page_assignment +from tasks.base.page import page_assignment, page_menu +from tasks.daily.synthesize import SynthesizeUI -class Assignment(AssignmentClaim): +class Assignment(AssignmentClaim, SynthesizeUI): + dispatched: dict[AssignmentEntry, datetime] = dict() + def run(self, assignments: list[AssignmentEntry] = None, duration: int = None): if assignments is None: assignments = [AssignmentEntry.find( @@ -19,21 +22,21 @@ class Assignment(AssignmentClaim): if duration is None: duration = self.config.Assignment_Duration + self.ensure_scroll_top(page_menu) self.ui_ensure(page_assignment) # Iterate in user-specified order, return undispatched ones undispatched = list(self._check_inlist(assignments, duration)) - _, _, total = self._limit_status + remain = self._check_all() # There are unchecked assignments - if total > len(self.dispatched): - self._check_all() - _, remain, _ = self._limit_status + if remain > 0: for assignment in undispatched[:remain]: self.goto_entry(assignment) - self.dispatch(assignment, duration, check_limit=False) + self.dispatch(assignment, duration) if remain < len(undispatched): - logger.warning( - f'The following assignments can not be dispatched due to limit: {", ".join([x.name for x in undispatched])}') - self._dispatch_remain(duration, remain - len(undispatched)) + logger.warning('The following assignments can not be dispatched due to limit: ' + f'{", ".join([x.name for x in undispatched[remain:]])}') + elif remain > len(undispatched): + self._dispatch_remain(duration, remain - len(undispatched)) # Scheduler delay = min(self.dispatched.values()) @@ -53,6 +56,7 @@ class Assignment(AssignmentClaim): logger.hr('Assignment check inlist', level=2) logger.info( f'User specified assignments: {", ".join([x.name for x in assignments])}') + _, remain, _ = self._limit_status for assignment in assignments: self.goto_entry(assignment) if self.appear(CLAIM): @@ -63,8 +67,10 @@ class Assignment(AssignmentClaim): OCR_ASSIGNMENT_TIME).ocr_single_line(self.device.image) continue if self.appear(EMPTY_SLOT): - dispatched = self.dispatch(assignment, duration) - if not dispatched: + if remain > 0: + self.dispatch(assignment, duration) + remain -= 1 + else: yield assignment def _check_all(self): @@ -76,6 +82,9 @@ class Assignment(AssignmentClaim): Break when a dispatchable assignment is encountered """ logger.hr('Assignment check all', level=2) + _, remain, total = self._limit_status + if total == len(self.dispatched): + return remain for group in self._iter_groups(): self.goto_group(group) entries = self._iter_entries() @@ -86,6 +95,7 @@ class Assignment(AssignmentClaim): self.goto_entry(assignment) if self.appear(CLAIM): self.claim(assignment, None, should_redispatch=False) + remain += 1 continue if self.appear(DISPATCHED): self.dispatched[assignment] = datetime.now() + Duration( @@ -93,6 +103,7 @@ class Assignment(AssignmentClaim): continue if self.appear(EMPTY_SLOT): break + return remain def _dispatch_remain(self, duration: int, remain: int): """ @@ -120,7 +131,7 @@ class Assignment(AssignmentClaim): if assignment in self.dispatched: continue self.goto_entry(assignment) - self.dispatch(assignment, duration, check_limit=False) + self.dispatch(assignment, duration) remain -= 1 if remain <= 0: return diff --git a/tasks/assignment/claim.py b/tasks/assignment/claim.py index baeb26264..57b485ccc 100644 --- a/tasks/assignment/claim.py +++ b/tasks/assignment/claim.py @@ -19,7 +19,7 @@ class AssignmentClaim(AssignmentDispatch): Pages: in: CLAIM - out: DISPATCHED(succeed) or EMPTY_SLOT(fail) + out: DISPATCHED or EMPTY_SLOT """ redispatched = False skip_first_screenshot = True @@ -50,7 +50,7 @@ class AssignmentClaim(AssignmentDispatch): continue # Re-select duration and dispatch if should_redispatch and not redispatched: - self.dispatch(assignment, duration_expected, check_limit=False) + self.dispatch(assignment, duration_expected) def _is_duration_expected(self, duration: int) -> bool: """ diff --git a/tasks/assignment/dispatch.py b/tasks/assignment/dispatch.py index 99ba0cf0a..6c089f9c2 100644 --- a/tasks/assignment/dispatch.py +++ b/tasks/assignment/dispatch.py @@ -20,25 +20,24 @@ ASSIGNMENT_DURATION_SWITCH.add_state('20', DURATION_20) class AssignmentDispatch(AssignmentUI): dispatched: dict[AssignmentEntry, datetime] = dict() - def dispatch(self, assignment: AssignmentEntry, duration: int, check_limit: bool = True) -> bool: + def dispatch(self, assignment: AssignmentEntry, duration: int): """ + Dispatch assignment. + Should be called only when limit is checked + Args: assignment (AssignmentEntry): duration (int): user specified duration - check_limit (bool): Pages: in: EMPTY_SLOT - out: DISPATCHED(succeed) or EMPTY_SLOT(fail) + out: DISPATCHED """ - if check_limit and self._limit_status[1] == 0: - return False self._select_characters() self._select_duration(duration) self._confirm_assignment(CONFIRM_ASSIGNMENT) self.dispatched[assignment] = datetime.now() + \ timedelta(hours=duration) - return True def _select_characters(self): """ diff --git a/tasks/assignment/keywords/classes.py b/tasks/assignment/keywords/classes.py index 363021e44..25e11167c 100644 --- a/tasks/assignment/keywords/classes.py +++ b/tasks/assignment/keywords/classes.py @@ -5,13 +5,13 @@ from typing import ClassVar from module.ocr.keyword import Keyword -@dataclass +@dataclass(repr=False) class AssignmentGroup(Keyword): instances: ClassVar = {} entries: tuple[AssignmentEntry] = () -@dataclass +@dataclass(repr=False) class AssignmentEntry(Keyword): instances: ClassVar = {} group: AssignmentGroup = None diff --git a/tasks/assignment/ui.py b/tasks/assignment/ui.py index 7eec695fc..0649cab63 100644 --- a/tasks/assignment/ui.py +++ b/tasks/assignment/ui.py @@ -98,14 +98,13 @@ class AssignmentUI(UI): def goto_group(self, group: AssignmentGroup): """ Args: - group: AssignmentGroup + group (AssignmentGroup): Examples: self = AssignmentUI('src') self.device.screenshot() self.goto_group(KEYWORDS_ASSIGNMENT_GROUP.Character_Materials) """ - self.ui_ensure(page_assignment) logger.hr('Assignment group goto', level=3) if ASSIGNMENT_TOP_SWITCH.set(group, main=self): self._wait_until_entry_loaded() @@ -113,7 +112,7 @@ class AssignmentUI(UI): def goto_entry(self, entry: AssignmentEntry): """ Args: - entry: AssignmentEntry + entry (AssignmentEntry): Examples: self = AssignmentUI('src')