From 0028681cb49d0265767ace1583d926632e532db2 Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Mon, 1 Jan 2024 22:45:22 +0800 Subject: [PATCH] Chore: Abstract DigitCounter.is_format_matched() --- module/ocr/ocr.py | 6 +++++- tasks/dungeon/event.py | 2 +- tasks/dungeon/state.py | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/module/ocr/ocr.py b/module/ocr/ocr.py index 6c3b390e9..a099d2691 100644 --- a/module/ocr/ocr.py +++ b/module/ocr/ocr.py @@ -350,6 +350,10 @@ class DigitCounter(Ocr): def __init__(self, button: ButtonWrapper, lang='en', name=None): super().__init__(button, lang=lang, name=name) + @classmethod + def is_format_matched(cls, result) -> bool: + return '/' in result + def format_result(self, result) -> tuple[int, int, int]: """ Do OCR on a counter, such as `14/15`, and returns 14, 1, 15 @@ -360,7 +364,7 @@ class DigitCounter(Ocr): result = super().after_process(result) logger.attr(name=self.name, text=str(result)) - res = re.search(r'(\d+)/(\d+)', result) + res = re.search(r'(\d+)\s*/\s*(\d+)', result) if res: groups = [int(s) for s in res.groups()] current, total = int(groups[0]), int(groups[1]) diff --git a/tasks/dungeon/event.py b/tasks/dungeon/event.py index 2afef0e49..a9637d4e8 100644 --- a/tasks/dungeon/event.py +++ b/tasks/dungeon/event.py @@ -84,7 +84,7 @@ class DungeonEvent(UI): ocr = DoubleEventOcr(OCR_DOUBLE_EVENT_REMAIN_AT_COMBAT) for row in ocr.detect_and_ocr(self.device.image): - if '/' not in row.ocr_text: + if ocr.is_format_matched(row.ocr_text): continue remain, _, total = ocr.format_result(row.ocr_text) if total in [3, 12]: diff --git a/tasks/dungeon/state.py b/tasks/dungeon/state.py index bff6d08bf..93d29b5a9 100644 --- a/tasks/dungeon/state.py +++ b/tasks/dungeon/state.py @@ -86,7 +86,7 @@ class DungeonState(UI): continue if row.ocr_text == '+': continue - if '/' not in row.ocr_text: + if ocr.is_format_matched(row.ocr_text): continue data = ocr.format_result(row.ocr_text) if data[2] == self.config.stored.TrailblazePower.FIXED_TOTAL: