P1-5 镜牢选队容忍 OCR 误读与队名后缀
- 队名先归一:去空白、NFKC 全角转半角、改回实测误读(震颐→震颤、沉论→沉沦、编#→编队#) - 精确匹配优先;否则接受这一屏唯一的「名字+后缀」(流血队、流血(X)),多条不点 - 顶栏确认只认精确,避免把 流血2 当成 流血 - 没命中时日志打印每屏看到的名字,点了没变时打印顶栏读到的文字 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+87
-26
@@ -16,6 +16,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
import unicodedata
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
TEAM_TARGET = re.compile(r'(?:TEAMS|TFAMS|编队)#(\d+)', re.IGNORECASE)
|
TEAM_TARGET = re.compile(r'(?:TEAMS|TFAMS|编队)#(\d+)', re.IGNORECASE)
|
||||||
@@ -509,56 +510,116 @@ def _scroll_team_list(auto, direction, times=6, distance=250):
|
|||||||
time.sleep(0.35)
|
time.sleep(0.35)
|
||||||
|
|
||||||
|
|
||||||
def _header_is(auto, name):
|
# OCR 实测误读(2026-11-14): 震颤→震颐, 沉沦→沉论, 编队#16→编#16。归一化时按子串改回来。
|
||||||
"""顶栏当前队伍名就是 name。"""
|
TEAM_OCR_FIXES = (('震颐', '震颤'), ('沉论', '沉沦'), ('编#', '编队#'))
|
||||||
|
TEAM_PREFIX_MIN = 2 # 前缀匹配的队名至少两个字, 免得单字名把一片都吃进来
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_team_name(text):
|
||||||
|
"""去空白、全角转半角(NFKC: #→#、()→()、全角空格)、改回常见误读。"""
|
||||||
|
text = ''.join(unicodedata.normalize('NFKC', str(text)).split())
|
||||||
|
for wrong, right in TEAM_OCR_FIXES:
|
||||||
|
text = text.replace(wrong, right)
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def team_name_match(text, names):
|
||||||
|
"""'exact' = 归一后等于某个名字; 'prefix' = 某个名字后面带了后缀(流血队、流血(X)); 否则 None。"""
|
||||||
|
text = normalize_team_name(text)
|
||||||
|
wanted = [normalize_team_name(name) for name in names]
|
||||||
|
if text in wanted:
|
||||||
|
return 'exact'
|
||||||
|
if any(len(name) >= TEAM_PREFIX_MIN and text.startswith(name) for name in wanted):
|
||||||
|
return 'prefix'
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def pick_team_item(items, names):
|
||||||
|
"""从列表条目 [(文本, x, y)] 里挑要点的那条, 返回 (条目, 理由) 或 (None, 理由)。
|
||||||
|
|
||||||
|
精确匹配优先, 多个名字时按 names 的顺序; 没有精确匹配才看前缀, 且这一屏只能有一条,
|
||||||
|
两条以上宁可不点(点错队比没选上更糟: 后面整局都用错的队)。
|
||||||
|
"""
|
||||||
|
for name in names:
|
||||||
|
for item in items:
|
||||||
|
if team_name_match(item[0], (name,)) == 'exact':
|
||||||
|
return item, '精确匹配 %s' % name
|
||||||
|
prefixed = {}
|
||||||
|
for item in items:
|
||||||
|
if team_name_match(item[0], names) == 'prefix':
|
||||||
|
prefixed.setdefault(normalize_team_name(item[0]), item)
|
||||||
|
if len(prefixed) == 1:
|
||||||
|
return next(iter(prefixed.values())), '前缀匹配 %s' % '/'.join(names)
|
||||||
|
if prefixed:
|
||||||
|
return None, '前缀匹配到多条(%s), 不点' % '/'.join(sorted(prefixed))
|
||||||
|
return None, ''
|
||||||
|
|
||||||
|
|
||||||
|
def _list_names(items):
|
||||||
|
"""这一屏列表里的名字, 从上到下(失败日志用)。"""
|
||||||
|
return [normalize_team_name(text) for text, x, y in sorted(items, key=lambda item: item[2])]
|
||||||
|
|
||||||
|
|
||||||
|
def _header_texts(auto):
|
||||||
|
"""顶栏区域读到的文字; 截图失败返回 None。"""
|
||||||
if auto.take_screenshot() is None:
|
if auto.take_screenshot() is None:
|
||||||
return False
|
return None
|
||||||
return any(text == name and _in_header(x, y) for text, x, y in _team_ocr(auto))
|
return [text for text, x, y in _team_ocr(auto) if _in_header(x, y)]
|
||||||
|
|
||||||
|
|
||||||
def _wait_header(auto, name, tries=3, delay=1.2):
|
def _header_is(auto, names):
|
||||||
|
"""顶栏当前队伍名(归一后)恰好是 names 之一。只认精确: 顶栏是 "流血2" 时不能当成 "流血"。"""
|
||||||
|
return any(team_name_match(text, names) == 'exact' for text in _header_texts(auto) or ())
|
||||||
|
|
||||||
|
|
||||||
|
def _wait_header(auto, names, tries=3, delay=1.2):
|
||||||
"""点完之后顶栏要过一两帧才刷新, 多等几次。"""
|
"""点完之后顶栏要过一两帧才刷新, 多等几次。"""
|
||||||
for _ in range(tries):
|
for _ in range(tries):
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
if _header_is(auto, name):
|
if _header_is(auto, names):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _select_team_by_name(auto, names, log=print, rounds=4):
|
def _select_team_by_name(auto, names, log=print, rounds=4):
|
||||||
"""在编队列表里按名字点选。成功返回 True, 没找到返回 False。"""
|
"""在编队列表里按名字点选。成功返回 True, 没找到返回 False。
|
||||||
|
|
||||||
|
名字先归一化(空白/全半角/常见误读); 精确匹配优先, 否则接受唯一的"名字+后缀"。
|
||||||
|
没找到时把每一屏看到的名字打进日志。
|
||||||
|
"""
|
||||||
if auto.take_screenshot() is None:
|
if auto.take_screenshot() is None:
|
||||||
return False
|
return False
|
||||||
if not auto.find_element('teams/identify_assets.png'):
|
if not auto.find_element('teams/identify_assets.png'):
|
||||||
log('[lc_mcp] 不在编队界面, 跳过按名字选队')
|
log('[lc_mcp] 不在编队界面, 跳过按名字选队')
|
||||||
return False
|
return False
|
||||||
for name in names: # 已经是这支队就不用动列表
|
current = [normalize_team_name(t) for t in _header_texts(auto) or ()
|
||||||
if _header_is(auto, name):
|
if team_name_match(t, names) == 'exact']
|
||||||
log('[lc_mcp] patch: 当前已经是编队 %s, 无需切换' % name)
|
if current: # 已经是这支队就不用动列表; 带后缀的队名要回列表里确认唯一后再点
|
||||||
return True
|
log('[lc_mcp] patch: 当前已经是编队 %s, 无需切换' % current[0])
|
||||||
|
return True
|
||||||
auto.mouse_click(1, 1)
|
auto.mouse_click(1, 1)
|
||||||
time.sleep(0.3)
|
time.sleep(0.3)
|
||||||
_scroll_team_list(auto, +1) # 先回到顶部
|
_scroll_team_list(auto, +1) # 先回到顶部
|
||||||
for round_index in range(rounds):
|
for round_index in range(1, rounds + 1):
|
||||||
if auto.take_screenshot() is None:
|
if auto.take_screenshot() is None:
|
||||||
|
log('[lc_mcp] 第 %d 屏截图失败' % round_index)
|
||||||
continue
|
continue
|
||||||
items = _team_ocr(auto)
|
items = [item for item in _team_ocr(auto) if _in_list(item[1], item[2])]
|
||||||
hit = None
|
hit, reason = pick_team_item(items, names)
|
||||||
for name in names:
|
|
||||||
for text, x, y in items:
|
|
||||||
if text == name and _in_list(x, y):
|
|
||||||
hit = (name, x, y)
|
|
||||||
break
|
|
||||||
if hit:
|
|
||||||
break
|
|
||||||
if hit:
|
if hit:
|
||||||
name, x, y = hit
|
text, x, y = hit
|
||||||
log('[lc_mcp] 编队列表里找到 %s @(%d,%d)' % (name, x, y))
|
label = normalize_team_name(text)
|
||||||
|
log('[lc_mcp] 编队列表里找到 %s @(%d,%d), %s' % (label, x, y, reason))
|
||||||
auto.mouse_action_with_pos([x, y], offset=False)
|
auto.mouse_action_with_pos([x, y], offset=False)
|
||||||
if _wait_header(auto, name):
|
if _wait_header(auto, (label,)):
|
||||||
log('[lc_mcp] patch: 已按体系名选中编队 %s' % name)
|
log('[lc_mcp] patch: 已按体系名选中编队 %s' % label)
|
||||||
return True
|
return True
|
||||||
log('[lc_mcp] 点了 %s 但顶栏没变, 继续往下翻' % name)
|
log('[lc_mcp] 点了 %s 但顶栏没变(顶栏读到: %s), 继续往下翻'
|
||||||
|
% (label, '/'.join(normalize_team_name(t) for t in _header_texts(auto) or ()) or '无'))
|
||||||
|
else:
|
||||||
|
log('[lc_mcp] 第 %d 屏没有 %s%s; 这一屏的名字: %s'
|
||||||
|
% (round_index, '/'.join(names), ', ' + reason if reason else '',
|
||||||
|
'/'.join(_list_names(items)) or '(空)'))
|
||||||
_scroll_team_list(auto, -1, times=3)
|
_scroll_team_list(auto, -1, times=3)
|
||||||
log('[lc_mcp] 编队列表里没找到 %s(共 %d 轮)' % ('/'.join(names), rounds))
|
log('[lc_mcp] 编队列表里没找到 %s(共 %d 轮)' % ('/'.join(names), rounds))
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user