StarRailCopilot/tasks/dungeon/event.py

96 lines
3.2 KiB
Python
Raw Normal View History

2023-10-02 04:28:51 +00:00
import re
2023-07-10 15:36:35 +00:00
from module.logger import logger
from module.ocr.ocr import DigitCounter
from tasks.base.ui import UI
2023-08-20 16:51:10 +00:00
from tasks.dungeon.assets.assets_dungeon_event import (
DOUBLE_CALYX_EVENT_TAG,
DOUBLE_RELIC_EVENT_TAG,
2023-10-03 12:06:44 +00:00
OCR_DOUBLE_EVENT_REMAIN,
OCR_DOUBLE_EVENT_REMAIN_AT_COMBAT,
2023-08-20 16:51:10 +00:00
)
2023-07-10 15:36:35 +00:00
2023-10-02 04:28:51 +00:00
class DoubleEventOcr(DigitCounter):
def after_process(self, result):
result = super().after_process(result)
2023-10-03 12:06:44 +00:00
# re.sub as last resort, just in case
# x12 -> x/12 but x112 does not change
result = re.sub(r'(?<!\d)(\d[02-9]*)12$', r'\1/12', result)
# x112 -> x/12
2023-10-02 04:28:51 +00:00
result = re.sub(r'112$', '/12', result)
return result
2023-07-10 15:36:35 +00:00
class DungeonEvent(UI):
def has_double_calyx_event(self) -> bool:
"""
Pages:
in: page_guide, Survival_Index, nav at top
"""
has = self.image_color_count(DOUBLE_CALYX_EVENT_TAG, color=(252, 209, 123), threshold=221, count=50)
2023-08-20 16:51:10 +00:00
has |= self.image_color_count(DOUBLE_CALYX_EVENT_TAG, color=(252, 251, 140), threshold=221, count=50)
2023-07-10 15:36:35 +00:00
logger.attr('Double calyx', has)
return has
2023-08-20 16:51:10 +00:00
def has_double_relic_event(self) -> bool:
"""
Pages:
in: page_guide, Survival_Index, nav at top
"""
has = self.image_color_count(DOUBLE_RELIC_EVENT_TAG, color=(252, 209, 123), threshold=221, count=50)
has |= self.image_color_count(DOUBLE_RELIC_EVENT_TAG, color=(252, 251, 140), threshold=221, count=50)
logger.attr('Double relic', has)
return has
2023-10-03 12:06:44 +00:00
def has_double_event_at_combat(self) -> bool:
2023-07-10 15:36:35 +00:00
"""
Pages:
2023-10-03 12:06:44 +00:00
in: COMBAT_PREPARE
2023-07-10 15:36:35 +00:00
"""
2023-10-03 12:06:44 +00:00
has = self.image_color_count(
OCR_DOUBLE_EVENT_REMAIN_AT_COMBAT,
2023-11-03 06:02:54 +00:00
color=(231, 188, 103),
threshold=240, count=1000
2023-10-03 12:06:44 +00:00
)
logger.attr('Double event at combat', has)
return has
def _get_double_event_remain(self, button) -> int:
ocr = DoubleEventOcr(button)
2023-07-10 15:36:35 +00:00
remain, _, total = ocr.ocr_single_line(self.device.image)
2023-08-20 16:51:10 +00:00
if total not in [3, 12]:
2023-07-10 15:36:35 +00:00
logger.warning(f'Invalid double event remain')
remain = 0
2023-10-03 12:06:44 +00:00
return remain
def get_double_event_remain(self) -> int:
"""
Pages:
in: page_guide, Survival_Index, selected at the nav with double event
"""
remain = self._get_double_event_remain(OCR_DOUBLE_EVENT_REMAIN)
2023-07-10 15:36:35 +00:00
logger.attr('Double event remain', remain)
return remain
2023-10-03 12:06:44 +00:00
2023-11-03 06:02:54 +00:00
def get_double_event_remain_at_combat(self) -> int | None:
2023-10-03 12:06:44 +00:00
"""
Pages:
in: COMBAT_PREPARE
"""
2023-11-03 06:02:54 +00:00
if not self.has_double_event_at_combat():
logger.attr('Double event remain at combat', 0)
return 0
ocr = DoubleEventOcr(OCR_DOUBLE_EVENT_REMAIN_AT_COMBAT)
for row in ocr.detect_and_ocr(self.device.image):
if '/' not in row.ocr_text:
continue
remain, _, total = ocr.format_result(row.ocr_text)
if total in [3, 12]:
logger.attr('Double event remain at combat', remain)
return remain
logger.warning('Double event appears but failed to get remain')
logger.attr('Double event remain at combat', None)
return None