StarRailCopilot/tasks/rogue/bleesing/curio.py

158 lines
5.5 KiB
Python
Raw Normal View History

2023-08-12 07:15:25 +00:00
import re
import numpy as np
from module.base.filter import MultiLangFilter
2023-08-12 07:15:25 +00:00
from module.base.timer import Timer
from module.base.utils import get_color
from module.logger import logger
from module.ocr.ocr import Ocr, OcrResultButton
2023-10-05 10:52:55 +00:00
from tasks.rogue.assets.assets_rogue_curio import CURIO_ENFORCE, OCR_ROGUE_CURIO
from tasks.rogue.assets.assets_rogue_ui import BLESSING_CONFIRM, BLESSING_ENHANCED, BLESSING_OBTAINED
2023-09-11 15:24:41 +00:00
from tasks.rogue.bleesing.preset import CURIO_PRESET_1
from tasks.rogue.bleesing.selector import RogueSelector
from tasks.rogue.bleesing.utils import get_regex_from_keyword_name, parse_name
2023-10-05 10:52:55 +00:00
from tasks.rogue.keywords import RogueCurio
CURIO_FILTER_ATTR = tuple()
CURIO_ATTR_NAME = 'curio_name'
2023-09-10 16:38:32 +00:00
patt = get_regex_from_keyword_name(RogueCurio, CURIO_ATTR_NAME)
CURIO_FILTER_ATTR += (CURIO_ATTR_NAME,)
2023-08-22 19:00:45 +00:00
CURIO_FILTER_PRESET = ('random', 'unrecorded')
2023-09-10 16:38:32 +00:00
FILTER_REGEX = re.compile(patt)
CURIO_FILTER = MultiLangFilter(FILTER_REGEX, CURIO_FILTER_ATTR, CURIO_FILTER_PRESET)
2023-08-12 07:15:25 +00:00
class RogueCurioOcr(Ocr):
merge_thres_y = 40
2023-08-12 07:15:25 +00:00
def after_process(self, result):
result = super().after_process(result)
2023-09-12 10:39:23 +00:00
if self.lang == 'cn':
2023-08-12 07:15:25 +00:00
replace_pattern_dict = {
"": "",
2023-08-14 20:16:31 +00:00
"漂灭": "湮灭",
2023-09-10 16:38:32 +00:00
"殷子": "骰子",
2023-08-12 07:15:25 +00:00
}
for pattern, replace in replace_pattern_dict.items():
result = re.sub(pattern, replace, result)
return result
class RogueCurioSelector(RogueSelector):
def recognition(self):
self.ocr_results = []
2023-08-12 07:15:25 +00:00
ocr = RogueCurioOcr(OCR_ROGUE_CURIO)
results = ocr.matched_ocr(self.main.device.image, RogueCurio)
2023-08-12 07:15:25 +00:00
expect_num = 3
if len(results) != expect_num:
logger.warning(f"The OCR result does not match the curio count. "
f"Expect {expect_num}, but recognized {len(results)} only.")
self.ocr_results = results
2023-08-12 07:15:25 +00:00
return results
def ui_select(self, target: OcrResultButton | None, skip_first_screenshot=True):
2023-08-12 07:15:25 +00:00
def is_curio_selected():
return np.mean(get_color(self.main.device.image, tuple(target.area))) > 60 # shiny background
2023-08-12 07:15:25 +00:00
def is_select_curio_complete():
"""
Case 1: back to main page
2023-08-14 20:16:31 +00:00
Case 2: event page
Case 3: is_page_choose_blessing()
Case 4: BLESSING_ENHANCED
Case 5: BLESSING_OBTAINED
2023-08-12 07:15:25 +00:00
"""
if self.main.is_in_main():
logger.info("Main page checked")
return True
if self.main.is_page_event():
logger.info("Event page checked")
return True
if self.main.is_page_choose_blessing():
logger.info("is_page_choose_blessing() checked")
return True
if self.main.appear(BLESSING_ENHANCED):
logger.info("BLESSING_ENHANCED checked")
return True
if self.main.appear(BLESSING_OBTAINED):
logger.info("BLESSING_OBTAINED checked")
return True
return False
2023-08-12 07:15:25 +00:00
enforce = False
if not target:
enforce = True
2023-08-12 07:15:25 +00:00
interval = Timer(1)
# start -> selected
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.main.device.screenshot()
2023-08-12 07:15:25 +00:00
if is_curio_selected():
if enforce:
logger.info("Curio selected (enforce)")
else:
logger.info(f"Curio {target} selected")
2023-08-12 07:15:25 +00:00
break
if interval.reached():
if enforce:
self.main.device.click(CURIO_ENFORCE)
2023-08-12 07:15:25 +00:00
else:
self.main.device.click(target)
2023-08-12 07:15:25 +00:00
interval.reset()
skip_first_screenshot = True
# Avoid double-clicking
interval = Timer(3, count=6)
2023-08-12 07:15:25 +00:00
# selected -> confirm
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.main.device.screenshot()
2023-08-12 07:15:25 +00:00
if is_select_curio_complete():
break
if interval.reached():
self.main.device.click(BLESSING_CONFIRM)
2023-08-12 07:15:25 +00:00
interval.reset()
def try_select(self, option: OcrResultButton | str):
if option == 'random':
target = np.random.choice(self.ocr_results)
self.ui_select(target)
return True
2023-08-22 19:00:45 +00:00
if option == 'unrecorded':
for result in self.ocr_results:
if self.main.is_unrecorded(result, (0, -720, 300, 0)):
self.ui_select(result)
return True
return False
if isinstance(option, OcrResultButton):
self.ui_select(option)
return True
return False
def load_filter(self):
filter_ = CURIO_FILTER
2023-08-22 19:00:45 +00:00
string = ""
2023-09-10 15:48:24 +00:00
match self.main.config.RogueCurio_PresetCurioFilter:
2023-08-22 19:00:45 +00:00
case 'preset-1':
string = CURIO_PRESET_1
case 'custom':
2023-09-10 15:48:24 +00:00
string = self.main.config.RogueCurio_CustomCurioFilter
2023-08-22 19:00:45 +00:00
string = parse_name(string)
2023-09-10 15:48:24 +00:00
match self.main.config.RogueCurio_CurioSelectionStrategy:
2023-08-22 19:00:45 +00:00
case 'unrecorded-first':
string = 'unrecorded > ' + string
case 'before-random':
string = string.replace('random', 'unrecorded > random')
filter_.load(string)
self.filter_ = filter_