P1-4 镜牢事件按选项表选择
- 包住 Mirror.event_handling:上游每轮挑选项前(找 unknown_event 那一步), 画面有选项框时整屏 OCR,按行把文字归到各选项;认出表里的事件就点对应选项, 没收录/选项不在画面上照常交给上游,调用结束还原 click_element - 表 lc_mcp/data/event_choices.json 10 条,取自事件速查里的直出/免费奖励; 按选项文字优先,按编号只用于第一屏、画面没变时最多重试 3 次 - 每屏选项打一次日志(含未收录事件的选项文字,便于补表) - LCP_EVENT_CHOICES=0 关闭,=<路径> 换表;表读不了记 [ERROR] 不装 - 单测执行上游真实 event_handling Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -970,3 +970,174 @@ def install_gift_preference(log=print, env=None):
|
|||||||
mirror_mod.Mirror.acquire_ego_gift = acquire_ego_gift
|
mirror_mod.Mirror.acquire_ego_gift = acquire_ego_gift
|
||||||
log('[lc_mcp] patch: 镜牢饰品选择按体系关键词排序(%s)' % os.path.basename(path))
|
log('[lc_mcp] patch: 镜牢饰品选择按体系关键词排序(%s)' % os.path.basename(path))
|
||||||
return original
|
return original
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 事件选项表
|
||||||
|
|
||||||
|
# 2026-11-14 核对: Mirror.event_handling() 不认事件, 有 "???"/直接拿饰品/优势判定就选它, 否则点第一个选项。
|
||||||
|
# 这里在上游每轮挑选项之前(它先找 event/unknown_event.png)插一步: 画面上有选项时整屏 OCR,
|
||||||
|
# 认出表里的事件就按选项文字(或第几项)点; 没认出、或表里的选项不在画面上, 交给上游原逻辑。
|
||||||
|
EVENT_CHOICES_ENV = 'LCP_EVENT_CHOICES' # 0 = 关闭; 文件路径 = 用这张表代替默认表
|
||||||
|
EVENT_CHOICES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'event_choices.json')
|
||||||
|
EVENT_OPTION = 'event/select_first_option_assets.png' # 每个选项框的左边缘, 多目标匹配得到每一项
|
||||||
|
EVENT_HOOK = 'event/unknown_event.png' # 上游每轮挑选项的第一步
|
||||||
|
EVENT_OPTION_ROW = 60 # 选项文字与选项框左边缘的最大纵向距离(1440 标定; 选项框高约 107)
|
||||||
|
EVENT_OPTION_LEFT = 20 # 选项文字至多比左边缘再靠左这么多
|
||||||
|
EVENT_TEXT_CLICKS = 5 # 一次事件里按文字最多点几次
|
||||||
|
EVENT_INDEX_TRIES = 3 # 按编号点: 只在第一屏选项, 画面没变时最多重试几次
|
||||||
|
|
||||||
|
|
||||||
|
def load_event_choices(path=EVENT_CHOICES_FILE):
|
||||||
|
"""读事件表, 返回 [{'name', 'match', 'text', 'index'}]; 格式不对抛 ValueError。"""
|
||||||
|
with open(path, encoding='utf-8') as f:
|
||||||
|
raw = json.load(f)
|
||||||
|
events = raw.get('events') if isinstance(raw, dict) else None
|
||||||
|
if not isinstance(events, list):
|
||||||
|
raise ValueError('顶层应是 {"events": [...]}')
|
||||||
|
|
||||||
|
def words(item, key):
|
||||||
|
value = item.get(key, [])
|
||||||
|
if not isinstance(value, list) or not all(isinstance(w, str) for w in value):
|
||||||
|
raise ValueError('%s 的 %s 应是字符串列表' % (item.get('name', '?'), key))
|
||||||
|
return tuple(normalize_team_name(w) for w in value if normalize_team_name(w))
|
||||||
|
|
||||||
|
rules = []
|
||||||
|
for item in events:
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
raise ValueError('events 的每一项应是对象')
|
||||||
|
name = str(item.get('name') or '?')
|
||||||
|
index = item.get('index')
|
||||||
|
if index is not None and (isinstance(index, bool) or not isinstance(index, int) or index < 1):
|
||||||
|
raise ValueError('%s 的 index 应是 >= 1 的整数' % name)
|
||||||
|
rule = {'name': name, 'match': words(item, 'match'), 'text': words(item, 'text'), 'index': index}
|
||||||
|
if not rule['match']:
|
||||||
|
raise ValueError('%s 缺少 match' % name)
|
||||||
|
if not rule['text'] and index is None:
|
||||||
|
raise ValueError('%s 至少要有 text 或 index' % name)
|
||||||
|
rules.append(rule)
|
||||||
|
return rules
|
||||||
|
|
||||||
|
|
||||||
|
def event_options(markers, lines, scale):
|
||||||
|
"""选项框左边缘(从上到下)与各自的文字: [(位置, 归一后的文字), ...]。"""
|
||||||
|
markers = sorted(markers, key=lambda m: float(m[1]))
|
||||||
|
texts = [[] for _ in markers]
|
||||||
|
for text, x, y in lines:
|
||||||
|
rows = [i for i, m in enumerate(markers)
|
||||||
|
if x >= float(m[0]) - EVENT_OPTION_LEFT * scale and abs(y - float(m[1])) <= EVENT_OPTION_ROW * scale]
|
||||||
|
if rows:
|
||||||
|
texts[min(rows, key=lambda i: abs(y - float(markers[i][1])))].append(normalize_team_name(text))
|
||||||
|
return [(m, ''.join(t)) for m, t in zip(markers, texts)]
|
||||||
|
|
||||||
|
|
||||||
|
def match_event_rule(rules, lines):
|
||||||
|
"""画面上的文字命中的第一条规则; 没有返回 None。"""
|
||||||
|
screen = [normalize_team_name(text) for text, _, _ in lines]
|
||||||
|
for rule in rules:
|
||||||
|
if any(word in text for word in rule['match'] for text in screen):
|
||||||
|
return rule
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def choose_event_option(rule, texts):
|
||||||
|
"""返回 (第几项(0 起), 'text'|'index', 理由); 选不出返回 (None, None, 理由)。"""
|
||||||
|
for word in rule['text']:
|
||||||
|
for i, text in enumerate(texts):
|
||||||
|
if word in text:
|
||||||
|
return i, 'text', '选项文字含「%s」' % word
|
||||||
|
if rule['index'] is not None:
|
||||||
|
if rule['index'] <= len(texts):
|
||||||
|
return rule['index'] - 1, 'index', '按表选第 %d 项' % rule['index']
|
||||||
|
return None, None, '表里是第 %d 项, 画面上只有 %d 项' % (rule['index'], len(texts))
|
||||||
|
return None, None, '表里的选项文字不在画面上'
|
||||||
|
|
||||||
|
|
||||||
|
def install_event_choices(log=print, env=None):
|
||||||
|
"""镜牢事件按表选项。返回原 Mirror.event_handling, 未安装返回 None。"""
|
||||||
|
env = os.environ if env is None else env
|
||||||
|
setting = (env.get(EVENT_CHOICES_ENV) or '').strip()
|
||||||
|
if setting == '0':
|
||||||
|
log('[lc_mcp] 事件选项表已关闭(%s=0), 沿用 AALC 原逻辑' % EVENT_CHOICES_ENV)
|
||||||
|
return None
|
||||||
|
path = setting or EVENT_CHOICES_FILE
|
||||||
|
try:
|
||||||
|
rules = load_event_choices(path)
|
||||||
|
except (OSError, ValueError) as e:
|
||||||
|
log('[ERROR] [lc_mcp] 事件选项表补丁跳过: %s 读不了(%s: %s)' % (path, type(e).__name__, e))
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
from tasks.mirror import mirror as mirror_mod
|
||||||
|
from module.automation import auto
|
||||||
|
from module.config import cfg
|
||||||
|
except Exception as e:
|
||||||
|
log('[lc_mcp] 事件选项表补丁跳过: 无法导入 %s' % e)
|
||||||
|
return None
|
||||||
|
original = getattr(getattr(mirror_mod, 'Mirror', None), 'event_handling', None)
|
||||||
|
if original is None:
|
||||||
|
log('[lc_mcp] 事件选项表补丁跳过: 上游没有 Mirror.event_handling')
|
||||||
|
return None
|
||||||
|
|
||||||
|
def decide(state):
|
||||||
|
"""按表点了一个选项返回 True; 其余情况返回 False 交给上游。"""
|
||||||
|
markers = auto.find_element(EVENT_OPTION, find_type='image_with_multiple_targets', threshold=0.75)
|
||||||
|
if not markers:
|
||||||
|
return False
|
||||||
|
lines = _ocr_lines(auto)
|
||||||
|
options = event_options(markers, lines, cfg.set_win_size / 1440)
|
||||||
|
texts = tuple(text for _, text in options)
|
||||||
|
state['rule'] = state['rule'] or match_event_rule(rules, lines)
|
||||||
|
rule = state['rule']
|
||||||
|
shown = ' | '.join('%d=%s' % (i + 1, text or '?') for i, text in enumerate(texts))
|
||||||
|
|
||||||
|
def once(message):
|
||||||
|
if (texts, message) not in state['logged']:
|
||||||
|
state['logged'].add((texts, message))
|
||||||
|
log(message)
|
||||||
|
|
||||||
|
if rule is None:
|
||||||
|
once('[lc_mcp] 事件选项: %s; 表里没有这个事件, 交给 AALC' % shown)
|
||||||
|
return False
|
||||||
|
i, how, reason = choose_event_option(rule, texts)
|
||||||
|
if how == 'text' and state['text_clicks'] >= EVENT_TEXT_CLICKS:
|
||||||
|
how, reason = None, '按文字已点 %d 次' % EVENT_TEXT_CLICKS
|
||||||
|
if how == 'index' and (state['signature'] not in (None, texts) or state['index_tries'] >= EVENT_INDEX_TRIES):
|
||||||
|
how, reason = None, '编号只用于第一屏选项'
|
||||||
|
if how is None:
|
||||||
|
once('[lc_mcp] 事件 %s: %s; %s, 交给 AALC' % (rule['name'], shown, reason))
|
||||||
|
return False
|
||||||
|
if how == 'text':
|
||||||
|
state['text_clicks'] += 1
|
||||||
|
else:
|
||||||
|
state['index_tries'] += 1
|
||||||
|
state['signature'] = texts
|
||||||
|
log('[lc_mcp] 事件 %s: %s; 选第 %d 项(%s)' % (rule['name'], shown, i + 1, reason))
|
||||||
|
position = options[i][0]
|
||||||
|
auto.mouse_click(position[0], position[1])
|
||||||
|
return True
|
||||||
|
|
||||||
|
def event_handling(self, *args, **kwargs):
|
||||||
|
state = {'rule': None, 'signature': None, 'index_tries': 0, 'text_clicks': 0, 'logged': set()}
|
||||||
|
real = auto.click_element
|
||||||
|
own = vars(auto).get('click_element')
|
||||||
|
|
||||||
|
def click_element(target, *a, **kw):
|
||||||
|
if target == EVENT_HOOK:
|
||||||
|
try:
|
||||||
|
if decide(state):
|
||||||
|
return True # 上游当作点到了选项: event_chance -= 1, 重新截图
|
||||||
|
except Exception as e:
|
||||||
|
log('[lc_mcp] 事件选项表出错(%s: %s), 交给 AALC' % (type(e).__name__, e))
|
||||||
|
return real(target, *a, **kw)
|
||||||
|
|
||||||
|
auto.click_element = click_element
|
||||||
|
try:
|
||||||
|
return original(self, *args, **kwargs)
|
||||||
|
finally:
|
||||||
|
if own is None:
|
||||||
|
vars(auto).pop('click_element', None)
|
||||||
|
else:
|
||||||
|
auto.click_element = own
|
||||||
|
|
||||||
|
mirror_mod.Mirror.event_handling = event_handling
|
||||||
|
log('[lc_mcp] patch: 镜牢事件按选项表选择(%s, %d 条)' % (os.path.basename(path), len(rules)))
|
||||||
|
return original
|
||||||
|
|||||||
Reference in New Issue
Block a user