2024-05-11 18:31:42 +00:00
|
|
|
import re
|
|
|
|
|
2024-06-03 05:49:19 +00:00
|
|
|
import cv2
|
2024-05-11 10:22:54 +00:00
|
|
|
from pponnxcr.predict_system import BoxedResult
|
|
|
|
|
2024-07-01 20:05:11 +00:00
|
|
|
from module.base.decorator import cached_property
|
2024-05-11 10:22:54 +00:00
|
|
|
from module.base.utils import area_center, area_in_area
|
2024-06-12 03:43:20 +00:00
|
|
|
from module.exception import GamePageUnknownError
|
2024-05-11 10:22:54 +00:00
|
|
|
from module.logger import logger
|
2024-05-11 18:31:42 +00:00
|
|
|
from module.ocr.ocr import Ocr, OcrWhiteLetterOnComplexBackground
|
2024-05-11 10:22:54 +00:00
|
|
|
from module.ui.scroll import AdaptiveScroll
|
2024-05-30 09:33:05 +00:00
|
|
|
from tasks.base.page import page_planner
|
2024-05-11 10:22:54 +00:00
|
|
|
from tasks.daily.synthesize import SynthesizeUI
|
|
|
|
from tasks.planner.assets.assets_planner_result import *
|
|
|
|
from tasks.planner.keywords import ITEM_CLASSES
|
2024-05-15 18:59:04 +00:00
|
|
|
from tasks.planner.keywords.classes import ItemCurrency
|
2024-05-17 18:35:13 +00:00
|
|
|
from tasks.planner.model import PlannerMixin, PlannerResultRow
|
2024-05-11 10:22:54 +00:00
|
|
|
|
|
|
|
CALCULATE_TITLE.load_search(RESULT_CHECK.search)
|
|
|
|
MATERIAL_TITLE.load_search(RESULT_CHECK.search)
|
|
|
|
DETAIL_TITLE.load_search(RESULT_CHECK.search)
|
|
|
|
|
|
|
|
|
2024-05-11 18:31:42 +00:00
|
|
|
class OcrItemName(Ocr):
|
|
|
|
def after_process(self, result):
|
2024-07-11 13:52:14 +00:00
|
|
|
result = result.replace('方相果实', '万相果实')
|
2024-05-11 18:31:42 +00:00
|
|
|
result = result.replace('念火之心', '忿火之心')
|
2024-08-28 14:42:36 +00:00
|
|
|
result = result.replace('楚天之魔', '焚天之魔')
|
2024-06-11 19:20:57 +00:00
|
|
|
result = re.sub('^火之心', '忿火之心', result)
|
2024-05-11 18:31:42 +00:00
|
|
|
result = re.sub('工造机$', '工造机杼', result)
|
2024-05-28 13:18:33 +00:00
|
|
|
result = re.sub('工造迥?轮', '工造迴轮', result)
|
2024-05-27 16:51:16 +00:00
|
|
|
result = re.sub('月狂[療撩]?牙', '月狂獠牙', result)
|
2024-06-04 08:13:52 +00:00
|
|
|
# 毁灭者的未路 思绪末屑
|
|
|
|
result = result.replace('未路', '末路')
|
|
|
|
result = result.replace('未屑', '末屑')
|
|
|
|
result = result.replace('粉未', '粉末')
|
2024-05-28 13:18:33 +00:00
|
|
|
# Error words on blank background
|
|
|
|
result = re.sub('^[國東]', '', result)
|
|
|
|
result = re.sub('時$', '', result)
|
2024-05-11 18:31:42 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class OcrPlannerResult(OcrWhiteLetterOnComplexBackground, OcrItemName):
|
2024-05-19 12:41:19 +00:00
|
|
|
min_box = (16, 20)
|
|
|
|
|
2024-07-01 20:05:11 +00:00
|
|
|
def __init__(self, lang=None):
|
|
|
|
super().__init__(OCR_RESULT, lang=lang)
|
2024-05-11 10:22:54 +00:00
|
|
|
self.limited_area = OCR_RESULT.area
|
|
|
|
self.limit_y = 720
|
|
|
|
|
2024-07-01 20:05:11 +00:00
|
|
|
def _match_result(
|
|
|
|
self,
|
|
|
|
result: str,
|
|
|
|
keyword_classes,
|
|
|
|
lang: str = None,
|
|
|
|
ignore_punctuation=True,
|
|
|
|
ignore_digit=True):
|
|
|
|
lang = self.lang
|
|
|
|
return super()._match_result(
|
|
|
|
result,
|
|
|
|
keyword_classes,
|
|
|
|
lang,
|
|
|
|
ignore_punctuation,
|
|
|
|
ignore_digit,
|
|
|
|
)
|
|
|
|
|
2024-05-11 10:22:54 +00:00
|
|
|
def filter_detected(self, result: BoxedResult) -> bool:
|
|
|
|
if not area_in_area(result.box, self.limited_area, threshold=0):
|
|
|
|
return False
|
|
|
|
if area_center(result.box)[1] > self.limit_y:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def detect_and_ocr(self, image, *args, **kwargs):
|
|
|
|
# Remove rows below DETAIL_TITLE
|
|
|
|
if DETAIL_TITLE.match_template(image):
|
|
|
|
self.limit_y = DETAIL_TITLE.button[3]
|
|
|
|
else:
|
|
|
|
self.limit_y = 720
|
|
|
|
return super().detect_and_ocr(image, *args, **kwargs)
|
|
|
|
|
|
|
|
def pre_process(self, image):
|
2024-06-03 05:49:19 +00:00
|
|
|
image = cv2.subtract((255, 255, 255, 0), image)
|
2024-05-11 10:22:54 +00:00
|
|
|
return image
|
|
|
|
|
|
|
|
|
2024-05-19 11:26:20 +00:00
|
|
|
class PlannerScan(SynthesizeUI, PlannerMixin):
|
2024-05-11 10:22:54 +00:00
|
|
|
def is_in_planner_result(self):
|
|
|
|
if self.appear(RESULT_CHECK):
|
|
|
|
return True
|
|
|
|
if self.appear(CALCULATE_TITLE):
|
|
|
|
return True
|
|
|
|
if self.appear(MATERIAL_TITLE):
|
|
|
|
return True
|
|
|
|
if self.appear(DETAIL_TITLE):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2024-07-01 20:05:11 +00:00
|
|
|
@cached_property
|
|
|
|
def planner_lang(self) -> str:
|
|
|
|
if self.config.Emulator_PackageName in ['CN-Official', 'CN-Bilibili']:
|
|
|
|
lang = 'cn'
|
|
|
|
else:
|
|
|
|
lang = self.config.LANG
|
|
|
|
if lang == 'auto':
|
|
|
|
logger.error('Language was not set before planner scan, assume it is "cn"')
|
|
|
|
lang = 'cn'
|
|
|
|
logger.attr('PlannerLang', lang)
|
|
|
|
return lang
|
|
|
|
|
2024-05-11 10:22:54 +00:00
|
|
|
def parse_planner_result_page(self) -> list[PlannerResultRow]:
|
|
|
|
"""
|
|
|
|
Pages:
|
|
|
|
in: planner result
|
|
|
|
"""
|
2024-07-01 20:05:11 +00:00
|
|
|
ocr = OcrPlannerResult(lang=self.planner_lang)
|
2024-05-11 10:22:54 +00:00
|
|
|
results = ocr.detect_and_ocr(self.device.image)
|
|
|
|
|
|
|
|
x_total = 842
|
|
|
|
x_synthesize = 965
|
|
|
|
x_demand = 1129
|
|
|
|
|
|
|
|
def x_match(result: BoxedResult, x):
|
|
|
|
rx = area_center(result.box)[0]
|
|
|
|
return x - 50 <= rx <= x + 50
|
|
|
|
|
|
|
|
def y_match(result: BoxedResult, y):
|
|
|
|
rx = area_center(result.box)[1]
|
|
|
|
return y - 15 <= rx <= y + 15
|
|
|
|
|
|
|
|
# Split columns
|
|
|
|
list_item = [r for r in results
|
|
|
|
if not r.ocr_text.isdigit() and ocr._match_result(r.ocr_text, keyword_classes=ITEM_CLASSES)]
|
|
|
|
list_number = [r for r in results if r.ocr_text.isdigit()]
|
|
|
|
list_total = [r for r in list_number if x_match(r, x_total)]
|
|
|
|
list_synthesize = [r for r in list_number if x_match(r, x_synthesize)]
|
|
|
|
list_demand = [r for r in list_number if x_match(r, x_demand)]
|
|
|
|
|
|
|
|
# Structure
|
|
|
|
out: list[PlannerResultRow] = []
|
|
|
|
for item in list_item:
|
|
|
|
y_item = area_center(item.box)[1]
|
|
|
|
total = -1
|
|
|
|
for number in list_total:
|
|
|
|
if y_match(number, y_item):
|
|
|
|
total = int(number.ocr_text)
|
|
|
|
break
|
2024-05-15 18:59:04 +00:00
|
|
|
synthesize = 0
|
2024-05-11 10:22:54 +00:00
|
|
|
for number in list_synthesize:
|
|
|
|
if y_match(number, y_item):
|
|
|
|
synthesize = int(number.ocr_text)
|
|
|
|
break
|
2024-05-15 18:59:04 +00:00
|
|
|
demand = 0
|
2024-05-11 10:22:54 +00:00
|
|
|
for number in list_demand:
|
|
|
|
if y_match(number, y_item):
|
|
|
|
demand = int(number.ocr_text)
|
|
|
|
break
|
|
|
|
item = ocr._match_result(item.ocr_text, keyword_classes=ITEM_CLASSES)
|
|
|
|
row = PlannerResultRow(
|
|
|
|
item=item,
|
|
|
|
total=total,
|
|
|
|
synthesize=synthesize,
|
|
|
|
demand=demand
|
|
|
|
)
|
|
|
|
# Validate item
|
|
|
|
# print(row)
|
|
|
|
if row.total <= 0:
|
|
|
|
logger.warning(f'Planner row with total <= 0, {row}')
|
|
|
|
continue
|
|
|
|
if row.synthesize < 0:
|
2024-05-15 18:59:04 +00:00
|
|
|
# Credits always have `synthesize`=="-"
|
2024-05-11 10:22:54 +00:00
|
|
|
if row.item.__class__ != ItemCurrency:
|
|
|
|
logger.warning(f'Planner row with synthesize < 0, {row}')
|
|
|
|
continue
|
|
|
|
if row.demand < 0:
|
|
|
|
logger.warning(f'Planner row with demand < 0, {row}')
|
|
|
|
continue
|
|
|
|
# Add
|
|
|
|
out.append(row)
|
|
|
|
|
|
|
|
logger.info(f'parse_planner_result_page: {out}')
|
|
|
|
return out
|
|
|
|
|
|
|
|
def parse_planner_result(self, skip_first_screenshot=True) -> list[PlannerResultRow]:
|
|
|
|
"""
|
|
|
|
Pages:
|
|
|
|
in: planner result
|
|
|
|
"""
|
2024-05-11 18:31:42 +00:00
|
|
|
logger.hr('Parse planner result', level=2)
|
2024-05-30 09:33:05 +00:00
|
|
|
if not self.ui_page_appear(page_planner):
|
|
|
|
logger.error('Not in page_planner, game must in the planner result page before scanning')
|
2024-06-12 03:43:20 +00:00
|
|
|
raise GamePageUnknownError
|
2024-05-30 09:33:05 +00:00
|
|
|
|
2024-05-11 10:22:54 +00:00
|
|
|
scroll = AdaptiveScroll(RESULT_SCROLL.button, name=RESULT_SCROLL.name)
|
|
|
|
scroll.drag_threshold = 0.1
|
|
|
|
scroll.edge_threshold = 0.1
|
|
|
|
scroll.parameters = {
|
|
|
|
'height': 50,
|
|
|
|
'prominence': 15,
|
|
|
|
'width': 5,
|
|
|
|
}
|
|
|
|
if not skip_first_screenshot:
|
|
|
|
self.device.screenshot()
|
|
|
|
skip_first_screenshot = False
|
|
|
|
if not scroll.at_top(main=self):
|
|
|
|
scroll.set_top(main=self)
|
|
|
|
|
|
|
|
out = []
|
|
|
|
while 1:
|
|
|
|
if skip_first_screenshot:
|
|
|
|
skip_first_screenshot = False
|
|
|
|
else:
|
|
|
|
self.device.screenshot()
|
|
|
|
|
|
|
|
# Skip first page
|
|
|
|
if self.appear(CALCULATE_TITLE):
|
|
|
|
scroll.next_page(main=self, page=0.75)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Parse
|
|
|
|
rows = self.parse_planner_result_page()
|
|
|
|
for row in rows:
|
|
|
|
if row not in out:
|
|
|
|
out.append(row)
|
|
|
|
logger.attr('PlannerResult', len(rows))
|
|
|
|
|
|
|
|
# Scroll
|
|
|
|
if scroll.at_bottom(main=self):
|
|
|
|
logger.info('Reached scroll end, stop')
|
|
|
|
break
|
|
|
|
elif self.appear(DETAIL_TITLE):
|
|
|
|
logger.info('Reached DETAIL_TITLE, stop')
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
scroll.next_page(main=self, page=0.8)
|
|
|
|
|
|
|
|
logger.hr('Planner Result')
|
|
|
|
for row in out:
|
2024-05-15 18:59:04 +00:00
|
|
|
logger.info(f'Planner item: {row.item.name}, {row.total}, {row.synthesize}, {row.demand}')
|
|
|
|
|
|
|
|
self.planner_write_results(out)
|
2024-05-11 10:22:54 +00:00
|
|
|
return out
|
|
|
|
|
2024-05-19 11:26:20 +00:00
|
|
|
def run(self):
|
|
|
|
self.device.screenshot()
|
|
|
|
self.parse_planner_result()
|
|
|
|
|
2024-05-11 10:22:54 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-05-19 11:26:20 +00:00
|
|
|
self = PlannerScan('src', task='PlannerScan')
|
2024-05-11 10:22:54 +00:00
|
|
|
self.device.screenshot()
|
|
|
|
self.parse_planner_result()
|