2023-08-12 17:04:54 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
from module.logger import logger
|
|
|
|
from module.ocr.keyword import Keyword
|
|
|
|
from module.ocr.ocr import OcrResultButton
|
|
|
|
|
|
|
|
|
|
|
|
class RogueSelector:
|
|
|
|
"""
|
|
|
|
An Interface used in blessing, curio, and other ui selection in rogue
|
|
|
|
"""
|
2023-08-14 19:35:15 +00:00
|
|
|
|
2024-11-12 11:28:37 +00:00
|
|
|
def __init__(self, main: "RogueUI"):
|
2023-08-14 19:35:15 +00:00
|
|
|
self.main = main
|
2023-08-20 16:27:41 +00:00
|
|
|
self.filter_ = None
|
|
|
|
self.ocr_results = []
|
2023-08-12 17:04:54 +00:00
|
|
|
|
|
|
|
def recognition(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def ui_select(self, target: OcrResultButton | None, skip_first_screenshot=True):
|
|
|
|
...
|
|
|
|
|
|
|
|
def try_select(self, option: OcrResultButton | str):
|
|
|
|
...
|
|
|
|
|
|
|
|
def load_filter(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
def perform_selection(self, priority):
|
|
|
|
if not self.ocr_results:
|
|
|
|
logger.warning('No blessing recognized, randomly choose one')
|
|
|
|
self.ui_select(None)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not len(priority):
|
|
|
|
logger.info('No blessing project satisfies current filter, randomly choose one')
|
|
|
|
choose = np.random.choice(self.ocr_results)
|
|
|
|
self.ui_select(choose)
|
|
|
|
return False
|
|
|
|
|
|
|
|
for option in priority:
|
|
|
|
logger.info(f"Try to choose option: {option}")
|
|
|
|
if self.try_select(option):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logger.info(f"Can not choose option: {option}")
|
|
|
|
|
2023-09-11 10:56:19 +00:00
|
|
|
def apply_filter(self) -> list[Keyword | str]:
|
2023-08-12 17:04:54 +00:00
|
|
|
def match_ocr_result(matched_keyword: Keyword):
|
|
|
|
for result in self.ocr_results:
|
|
|
|
if result.matched_keyword == matched_keyword:
|
|
|
|
return result
|
|
|
|
return None
|
|
|
|
|
|
|
|
if self.filter_:
|
|
|
|
keywords = [result.matched_keyword for result in self.ocr_results]
|
|
|
|
priority = self.filter_.apply(keywords)
|
|
|
|
priority = [option if isinstance(option, str) else match_ocr_result(option) for option in priority]
|
|
|
|
else:
|
|
|
|
logger.warning("No filter loaded, use random instead")
|
|
|
|
priority = ['random']
|
2023-09-11 10:56:19 +00:00
|
|
|
priority = self.after_process(priority)
|
|
|
|
return priority
|
|
|
|
|
|
|
|
def after_process(self, priority) -> list[Keyword | str]:
|
|
|
|
"""
|
|
|
|
Usage: add extra logics to modify the priority
|
|
|
|
"""
|
|
|
|
return priority
|
|
|
|
|
|
|
|
def recognize_and_select(self):
|
|
|
|
self.recognition()
|
|
|
|
self.load_filter()
|
|
|
|
priority = self.apply_filter()
|
2023-08-12 17:04:54 +00:00
|
|
|
logger.info(f"Priority: {priority}")
|
|
|
|
self.perform_selection(priority)
|