2023-09-15 05:29:22 +00:00
|
|
|
import re
|
2023-10-22 17:49:48 +00:00
|
|
|
|
2023-09-15 05:29:22 +00:00
|
|
|
import module.config.server as server
|
|
|
|
from module.config.server import VALID_LANG
|
|
|
|
from module.exception import RequestHumanTakeover, ScriptError
|
|
|
|
from module.logger import logger
|
2023-11-10 13:36:53 +00:00
|
|
|
from module.ocr.ocr import OcrWhiteLetterOnComplexBackground
|
2024-05-13 04:36:11 +00:00
|
|
|
from tasks.base.assets.assets_base_main_page import OCR_MAP_NAME
|
|
|
|
from tasks.base.page import Page, page_main
|
2023-09-24 09:43:12 +00:00
|
|
|
from tasks.base.popup import PopupHandler
|
2023-09-15 05:29:22 +00:00
|
|
|
from tasks.map.keywords import KEYWORDS_MAP_PLANE, MapPlane
|
|
|
|
|
|
|
|
|
2023-10-22 17:49:48 +00:00
|
|
|
class OcrPlaneName(OcrWhiteLetterOnComplexBackground):
|
2023-09-15 05:29:22 +00:00
|
|
|
def after_process(self, result):
|
|
|
|
# RobotSettlement1
|
2023-09-15 17:37:06 +00:00
|
|
|
result = re.sub(r'-[Ii1]$', '', result)
|
2023-09-27 15:49:56 +00:00
|
|
|
result = re.sub(r'I$', '', result)
|
2023-09-15 05:29:22 +00:00
|
|
|
result = re.sub(r'\d+$', '', result)
|
2023-09-25 04:24:29 +00:00
|
|
|
# Herta's OfficeY/
|
|
|
|
result = re.sub(r'Y/?$', '', result)
|
2023-09-27 15:49:56 +00:00
|
|
|
# Stargazer Navatia -> Stargazer Navalia
|
|
|
|
result = result.replace('avatia', 'avalia')
|
2023-10-02 09:15:03 +00:00
|
|
|
# DomainiRespite
|
|
|
|
result = result.replace('omaini', 'omain')
|
2023-10-03 09:59:51 +00:00
|
|
|
# Domain=Combat
|
|
|
|
result = result.replace('=', '')
|
2023-10-04 17:58:14 +00:00
|
|
|
# Domain--Occunrence
|
|
|
|
# Domain'--Occurence
|
|
|
|
# Domain-Qccurrence
|
|
|
|
result = result.replace('cunr', 'cur').replace('uren', 'urren').replace('Qcc', 'Occ')
|
2023-10-07 17:15:15 +00:00
|
|
|
# Domain-Elit
|
2023-10-18 13:01:48 +00:00
|
|
|
# Domain--Etite
|
2023-10-07 17:15:15 +00:00
|
|
|
result = re.sub(r'[Ee]lit$', 'Elite', result)
|
2023-10-18 13:01:48 +00:00
|
|
|
result = result.replace('tite', 'lite')
|
2023-09-15 05:29:22 +00:00
|
|
|
|
2023-10-04 17:58:14 +00:00
|
|
|
# 区域-战
|
|
|
|
result = re.sub(r'区域.*战$', '区域战斗', result)
|
2023-11-17 16:21:23 +00:00
|
|
|
# 区域-事
|
2024-05-13 17:37:27 +00:00
|
|
|
result = re.sub(r'区域.*[事件]$', '区域事件', result)
|
2024-03-27 11:40:55 +00:00
|
|
|
# 区域-战
|
|
|
|
result = re.sub(r'区域.*交$', '区域交易', result)
|
2024-05-13 18:10:46 +00:00
|
|
|
# 区域-战
|
|
|
|
result = re.sub(r'区域.*[精英]$', '区域精英', result)
|
2023-10-04 17:58:14 +00:00
|
|
|
# 区域-事伴, 区域-事祥
|
2023-10-20 18:06:23 +00:00
|
|
|
result = re.sub(r'事[伴祥]', '事件', result)
|
2023-10-04 17:58:14 +00:00
|
|
|
# 医域-战斗
|
|
|
|
result = result.replace('医域', '区域')
|
2023-10-20 18:06:23 +00:00
|
|
|
# 区域-战半, 区域-战头, 区域-战头书
|
2023-11-28 16:31:04 +00:00
|
|
|
result = re.sub(r'战[半头卒三]', '战斗', result)
|
2023-10-20 18:06:23 +00:00
|
|
|
# 区域一战斗
|
|
|
|
result = re.sub(r'区域[\-—-一=]', '区域-', result)
|
2023-09-24 09:43:12 +00:00
|
|
|
# 累塔的办公室
|
|
|
|
result = result.replace('累塔', '黑塔')
|
2023-09-27 15:49:56 +00:00
|
|
|
if '星港' in result:
|
|
|
|
result = '迴星港'
|
2024-05-08 13:46:53 +00:00
|
|
|
result = result.replace('太司', '太卜司')
|
2024-06-30 16:01:50 +00:00
|
|
|
# IRadiantFeldspar
|
|
|
|
result = re.sub('[Ii1|]\s*Radiant', 'Radiant', result)
|
2023-09-24 09:43:12 +00:00
|
|
|
|
2023-10-02 09:15:03 +00:00
|
|
|
result = result.replace(' ', '')
|
|
|
|
|
2023-09-15 05:29:22 +00:00
|
|
|
return super().after_process(result)
|
|
|
|
|
|
|
|
|
2023-09-24 09:43:12 +00:00
|
|
|
class MainPage(PopupHandler):
|
2023-09-15 05:29:22 +00:00
|
|
|
# Same as BigmapPlane class
|
|
|
|
# Current plane
|
|
|
|
plane: MapPlane = KEYWORDS_MAP_PLANE.Herta_ParlorCar
|
|
|
|
|
|
|
|
_lang_checked = False
|
2023-12-08 14:00:11 +00:00
|
|
|
_lang_check_success = True
|
2023-09-15 05:29:22 +00:00
|
|
|
|
2023-10-22 17:49:48 +00:00
|
|
|
def update_plane(self, lang=None) -> MapPlane | None:
|
2023-10-01 08:26:07 +00:00
|
|
|
"""
|
|
|
|
Pages:
|
|
|
|
in: page_main
|
|
|
|
"""
|
|
|
|
if lang is None:
|
|
|
|
lang = server.lang
|
|
|
|
ocr = OcrPlaneName(OCR_MAP_NAME, lang=lang)
|
|
|
|
result = ocr.ocr_single_line(self.device.image)
|
|
|
|
# Try to match
|
|
|
|
keyword = ocr._match_result(result, keyword_classes=MapPlane, lang=lang)
|
|
|
|
if keyword is not None:
|
|
|
|
self.plane = keyword
|
|
|
|
logger.attr('CurrentPlane', keyword)
|
|
|
|
return keyword
|
|
|
|
# Try to remove suffix
|
|
|
|
for suffix in range(1, 5):
|
|
|
|
keyword = ocr._match_result(result[:-suffix], keyword_classes=MapPlane, lang=lang)
|
|
|
|
if keyword is not None:
|
|
|
|
self.plane = keyword
|
|
|
|
logger.attr('CurrentPlane', keyword)
|
|
|
|
return keyword
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2023-10-22 17:49:48 +00:00
|
|
|
def check_lang_from_map_plane(self) -> str | None:
|
2023-09-15 05:29:22 +00:00
|
|
|
logger.info('check_lang_from_map_plane')
|
|
|
|
lang_unknown = self.config.Emulator_GameLanguage == 'auto'
|
|
|
|
|
|
|
|
if lang_unknown:
|
|
|
|
lang_list = VALID_LANG
|
|
|
|
else:
|
|
|
|
# Try current lang first
|
|
|
|
lang_list = [server.lang] + [lang for lang in VALID_LANG if lang != server.lang]
|
|
|
|
|
|
|
|
for lang in lang_list:
|
|
|
|
logger.info(f'Try ocr in lang {lang}')
|
2023-10-22 17:49:48 +00:00
|
|
|
keyword = self.update_plane(lang)
|
2023-09-15 05:29:22 +00:00
|
|
|
if keyword is not None:
|
|
|
|
logger.info(f'check_lang_from_map_plane matched lang: {lang}')
|
|
|
|
if lang_unknown or lang != server.lang:
|
|
|
|
self.config.Emulator_GameLanguage = lang
|
2023-09-15 16:05:32 +00:00
|
|
|
server.set_lang(lang)
|
2023-12-08 14:00:11 +00:00
|
|
|
MainPage._lang_checked = True
|
|
|
|
MainPage._lang_check_success = True
|
2023-09-15 05:29:22 +00:00
|
|
|
return lang
|
|
|
|
|
|
|
|
if lang_unknown:
|
|
|
|
logger.critical('Cannot detect in-game text language, please set it to 简体中文 or English')
|
|
|
|
raise RequestHumanTakeover
|
|
|
|
else:
|
|
|
|
logger.warning(f'Cannot detect in-game text language, assume current lang={server.lang} is correct')
|
2023-12-08 14:00:11 +00:00
|
|
|
MainPage._lang_checked = True
|
|
|
|
MainPage._lang_check_success = False
|
2023-09-15 05:29:22 +00:00
|
|
|
return server.lang
|
|
|
|
|
|
|
|
def handle_lang_check(self, page: Page):
|
2023-09-15 15:53:14 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
page:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: If checked
|
|
|
|
"""
|
2023-09-15 05:29:22 +00:00
|
|
|
if MainPage._lang_checked:
|
2023-09-15 15:53:14 +00:00
|
|
|
return False
|
2023-09-15 05:29:22 +00:00
|
|
|
if page != page_main:
|
2023-09-15 15:53:14 +00:00
|
|
|
return False
|
2023-09-15 05:29:22 +00:00
|
|
|
|
|
|
|
self.check_lang_from_map_plane()
|
2023-09-15 15:53:14 +00:00
|
|
|
return True
|
2023-09-15 05:29:22 +00:00
|
|
|
|
|
|
|
def acquire_lang_checked(self):
|
2023-09-15 15:53:14 +00:00
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
bool: If checked
|
|
|
|
"""
|
2023-09-15 05:29:22 +00:00
|
|
|
if MainPage._lang_checked:
|
2023-09-15 15:53:14 +00:00
|
|
|
return False
|
2023-09-15 05:29:22 +00:00
|
|
|
|
|
|
|
logger.info('acquire_lang_checked')
|
|
|
|
try:
|
|
|
|
self.ui_goto(page_main)
|
|
|
|
except AttributeError:
|
|
|
|
logger.critical('Method ui_goto() not found, class MainPage must be inherited by class UI')
|
|
|
|
raise ScriptError
|
|
|
|
|
|
|
|
self.handle_lang_check(page=page_main)
|
2023-09-15 15:53:14 +00:00
|
|
|
return True
|