按 logs-raw 查清两次 5 小时多的空转, 都是上游循环没有上限、不报错、也不再打里程碑日志: - 11-21 14:01 起 5h20m: 游戏改版后驱动菜单的镜牢入口 home/mirror_dungeons_assets.png 最高 0.41 (11-18 之前每轮 1.00), 大厅 <-> 驱动来回点 2500 多轮; 上游 V1.6.2 c0e4926 换的正是这张图 - 11-17 00:45 起 5h40m: 全员阵亡后 Mirror.re_start() 的 while True, 画面 5.5 小时一帧没变 install_progress_watchdog: AALC logger 上挂 handler 记最后一条「开始执行/结束执行」, 任务主线程的 match_template / take_screenshot 里检查, 超时抛 TaskAbort(退出码 4, finally 照常写回配置), 记 [ERROR] 和 decisions progress/stall。LCP_PROGRESS_STALL_MIN 可调, 0 关闭。 20 份原始日志回放: 17 轮正常运行最长空档约 20 分钟、不会误停; 三次卡住都在最后一条之后 40 分钟停下。 单测: 779 passed(新增 test_progress_watchdog 10 例)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
11 KiB
Python
243 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""子进程入口: 跑 AALC 现成的任务流程。
|
|
|
|
用法: python -m lc_mcp.aalc_task <task> [json_params]
|
|
python -m lc_mcp.aalc_task --restore <file> 写回被强杀任务临时覆盖的配置
|
|
放在子进程里跑, 停止 = 杀进程树; 不会像 QThread.terminate 那样留下被占用的锁。
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from . import killswitch, preflight
|
|
from .aalc_engine import bootstrap
|
|
from .aalc_patches import (TaskAbort, WindowGuard, driver_click_tasks, headless_hdr_warning,
|
|
install_back_button_result, install_default_battle_team, install_event_choices,
|
|
install_exact_team_match, install_gift_confirm_guard, install_gift_preference,
|
|
install_image_error_guard, install_key_hold, install_progress_watchdog,
|
|
install_rabbit_hole, install_robust_set_foreground, install_shop_audit, install_shop_by_name,
|
|
install_system_team_selection, install_team_screen_guard,
|
|
install_theme_pack_audit, install_theme_pack_refresh_click, install_theme_pack_systems,
|
|
install_typed_keyboard_road, load_rabbit_hole, mirror_progress, stop_driver_click)
|
|
from .decisions import decisions
|
|
from .game_lock import GameBusyError, exclusive_game
|
|
|
|
# 任务 -> {参数名: AALC 配置项}; 参数只在本次运行内生效
|
|
TASKS = {
|
|
'all': {}, # 完全按 config.yaml 里勾选的任务跑, 等价于 AALC 界面点"开始"
|
|
'daily': {'exp': 'set_EXP_count', 'thread': 'set_thread_count'},
|
|
'reward': {},
|
|
'enkephalin': {'times': 'set_lunacy_to_enkephalin'},
|
|
'mirror': {'count': 'set_mirror_count'},
|
|
}
|
|
# 任务 -> {选项名: 允许的值}; 不对应 AALC 配置项, 只决定本次装哪些补丁
|
|
TASK_OPTIONS = {
|
|
'mirror': {'strategy': ('rabbit_hole',)}, # rabbit_hole: 兔子洞速刷, 见 lc_mcp/data/rabbit_hole.json
|
|
}
|
|
# 用户给了镜牢次数时一并临时关闭的配置: 开着无限刷牢, AALC 会把次数改成 9999
|
|
EXPLICIT_COUNT_OVERRIDES = {'mirror': {'count': {'infinite_dungeons': False}}}
|
|
RESTORE_ENV = 'LCP_RESTORE_FILE'
|
|
PREFLIGHT_EXIT = 3
|
|
INCOMPLETE_EXIT = 4 # AALC 正常返回, 但镜牢完成局数少于应打局数
|
|
|
|
|
|
def log(msg):
|
|
print(msg, flush=True)
|
|
|
|
|
|
def apply_overrides(cfg, task, params, restore_path=None):
|
|
"""临时覆盖本次运行的次数, 返回原值。
|
|
|
|
AALC 运行中会整份保存配置(如镜牢后 rotate_team_queue), 覆盖值会被写进 config.yaml。
|
|
所以先把原值落盘: 进程被 stop_task 强杀、finally 来不及执行时, TaskRunner 据此写回。
|
|
"""
|
|
overrides = {TASKS[task][name]: int(value) for name, value in params.items()}
|
|
for name in params:
|
|
overrides.update(EXPLICIT_COUNT_OVERRIDES.get(task, {}).get(name, {}))
|
|
originals = {key: cfg.get_value(key) for key in overrides}
|
|
if originals and restore_path:
|
|
tmp = restore_path + '.tmp'
|
|
with open(tmp, 'w', encoding='utf-8') as f:
|
|
json.dump(originals, f, ensure_ascii=False)
|
|
os.replace(tmp, restore_path)
|
|
for key, value in overrides.items():
|
|
if key == 'infinite_dungeons' and originals.get(key):
|
|
log('[lc_mcp] 本次按指定次数运行镜牢, 临时关闭无限刷牢, 结束后恢复')
|
|
cfg.unsaved_set_value(key, value)
|
|
return originals
|
|
|
|
|
|
def mirror_result(task, progress):
|
|
"""按 AALC 实际完成局数给出退出码; 退出码 0 不再代表"一局没打"。"""
|
|
if progress.total is None:
|
|
if task != 'mirror':
|
|
return 0 # all 没勾选镜牢
|
|
log('[ERROR] [lc_mcp] 镜牢任务结束, 但没收到 AALC 的镜牢进度, 不能确认完成局数')
|
|
return INCOMPLETE_EXIT
|
|
log('[lc_mcp] 镜牢完成 %d/%d 局' % (progress.finished, progress.total))
|
|
if progress.finished < progress.total:
|
|
log('[ERROR] [lc_mcp] 镜牢只完成 %d/%d 局: AALC 正常返回, 但没有打完'
|
|
'(常见原因: 没有适用当前难度的队伍、队列为空)' % (progress.finished, progress.total))
|
|
return INCOMPLETE_EXIT
|
|
return 0
|
|
|
|
|
|
def restore(cfg, originals, restore_path=None):
|
|
for key, value in originals.items():
|
|
cfg.set_value(key, value)
|
|
if originals:
|
|
cfg.flush()
|
|
if restore_path and os.path.exists(restore_path):
|
|
os.remove(restore_path)
|
|
|
|
|
|
def restore_file(path, cfg=None):
|
|
with open(path, encoding='utf-8') as f:
|
|
originals = json.load(f)
|
|
if cfg is None:
|
|
bootstrap()
|
|
from module.config import cfg
|
|
restore(cfg, originals, path)
|
|
|
|
|
|
def run(task, params):
|
|
try:
|
|
with exclusive_game():
|
|
return _run(task, params)
|
|
except GameBusyError as e:
|
|
log('[ERROR] [lc_mcp] %s' % e)
|
|
return PREFLIGHT_EXIT
|
|
|
|
|
|
def split_options(task, params):
|
|
"""把任务选项(TASK_OPTIONS)从参数里拆出来, 返回 (次数类参数, 选项)。"""
|
|
allowed = TASK_OPTIONS.get(task, {})
|
|
params = dict(params)
|
|
options = {k: params.pop(k) for k in list(params) if k in allowed}
|
|
return params, options
|
|
|
|
|
|
def _run(task, params):
|
|
params, options = split_options(task, params)
|
|
rabbit = None
|
|
if options.get('strategy') == 'rabbit_hole':
|
|
try:
|
|
rabbit = load_rabbit_hole()
|
|
except (OSError, ValueError) as e:
|
|
log('[ERROR] [lc_mcp] 兔子洞配置读不了: %s' % e)
|
|
return PREFLIGHT_EXIT
|
|
bootstrap()
|
|
from module.automation import auto
|
|
from module.config import cfg
|
|
from module.game_and_screen import screen
|
|
from tasks.base import script_task_scheme as scheme
|
|
from tasks.base.retry_monitor import retry_monitor
|
|
from tasks.battle import battle
|
|
from utils.path_manager import path_manager
|
|
|
|
with killswitch.listening(log=log, on_stop=lambda: stop_driver_click(auto)):
|
|
problems = preflight.check(preflight.collect(cfg), task,
|
|
desktop=None if cfg.simulator else preflight.desktop_size())
|
|
if problems:
|
|
for p in problems:
|
|
log('[ERROR] [lc_mcp] preflight: %s' % p)
|
|
return PREFLIGHT_EXIT
|
|
|
|
decisions.open() # TaskRunner 给的 .decisions.jsonl; 直接运行时不写
|
|
install_exact_team_match(auto, log)
|
|
install_robust_set_foreground(screen, log=log)
|
|
install_progress_watchdog(log=log) # 40 分钟没有「开始执行/结束执行」就结束(11-17/11-21 各空转 5 小时多)
|
|
if not cfg.simulator:
|
|
install_key_hold(log=log) # 前台按键带扫描码、各模式按住再松开(ISSUES I-05)
|
|
# 日常本(经验/纽本)不翻队伍列表: 队伍是自定义名字时按名字找必然失败,
|
|
# 白等 60 秒还会把界面滚乱, 后面 to_battle 就找不到 "进入战斗" 了。
|
|
if task in ('daily', 'all'):
|
|
install_default_battle_team(scheme, log=log)
|
|
# 镜牢: 编队是自定义名字(体系名), AALC 只会找 "编队#N"; 改成按体系名点选。
|
|
if task in ('mirror', 'all'):
|
|
install_back_button_result(log=log) # 上游 V1.6.2 拿 click_element 返回的 True 当坐标, 首次引导页 TypeError
|
|
install_system_team_selection(log=log, names=rabbit['team_names'] if rabbit else None)
|
|
install_team_screen_guard(log=log)
|
|
install_gift_confirm_guard(log=log) # 「获得E.G.O饰品」点不关时换落点/结束任务(ISSUES I-06)
|
|
install_image_error_guard(log=log) # 截图连续识别失败即结束任务(防窗口没了还空转)
|
|
install_typed_keyboard_road(log=log)
|
|
install_gift_preference(log=log)
|
|
install_event_choices(log=log)
|
|
install_shop_audit(log=log)
|
|
install_shop_by_name(log=log) # 装在审计之后(包在外层), 审计才看得到按名字点的那一下
|
|
if rabbit: # 卡包按体系加分(BACKLOG R-01); 兔子洞再给攻略路线上的卡包加分
|
|
install_theme_pack_systems(log=log, route=rabbit['theme_packs'], route_name='兔子洞路线')
|
|
else:
|
|
install_theme_pack_systems(log=log)
|
|
install_theme_pack_refresh_click(log=log) # 刷新卡包按 OCR 点(上游模板点不中)
|
|
install_theme_pack_audit(log=log) # 装在加分之后(包在外层), 审计记得到加分
|
|
if rabbit:
|
|
install_rabbit_hole(log=log, config=rabbit)
|
|
guard = WindowGuard(screen, cfg, log=log)
|
|
guard.start()
|
|
restore_path = os.environ.get(RESTORE_ENV)
|
|
originals = apply_overrides(cfg, task, params, restore_path)
|
|
with driver_click_tasks(scheme, auto, screen, cfg, log=log), \
|
|
mirror_progress(scheme) as progress:
|
|
try:
|
|
if task == 'all':
|
|
with headless_hdr_warning(scheme, log=log):
|
|
scheme.script_task()
|
|
return mirror_result(task, progress)
|
|
# 与 script_task() 相同的前置步骤
|
|
scheme.init_game()
|
|
path_manager.initialize_paths()
|
|
auto.clear_img_cache()
|
|
retry_monitor.start()
|
|
get_reward = None
|
|
if auto.click_element('battle/turn_assets.png', take_screenshot=True):
|
|
print('[lc_mcp] 检测到正在战斗, 先打完', flush=True)
|
|
get_reward = battle.fight()
|
|
{
|
|
'daily': scheme.Daily_task_wrapper(get_reward=get_reward),
|
|
'reward': scheme.to_get_reward,
|
|
'enkephalin': scheme.Buy_enkephalin,
|
|
'mirror': scheme.Mirror_task,
|
|
}[task]()
|
|
return mirror_result(task, progress)
|
|
except TaskAbort as e:
|
|
log('[ERROR] [lc_mcp] 任务中止: %s' % e)
|
|
return INCOMPLETE_EXIT
|
|
finally:
|
|
guard.stop()
|
|
retry_monitor.stop()
|
|
restore(cfg, originals, restore_path)
|
|
|
|
|
|
def main(argv):
|
|
if len(argv) == 3 and argv[1] == '--restore':
|
|
restore_file(argv[2])
|
|
print('[lc_mcp] restored config from %s' % argv[2], flush=True)
|
|
return 0
|
|
if len(argv) < 2 or argv[1] not in TASKS:
|
|
print('usage: python -m lc_mcp.aalc_task <%s> [json]' % '|'.join(TASKS), file=sys.stderr)
|
|
return 2
|
|
task = argv[1]
|
|
params = json.loads(argv[2]) if len(argv) > 2 else {}
|
|
options = TASK_OPTIONS.get(task, {})
|
|
unknown = set(params) - set(TASKS[task]) - set(options)
|
|
if unknown:
|
|
print('unknown params for %s: %s' % (task, ', '.join(sorted(unknown))), file=sys.stderr)
|
|
return 2
|
|
bad = {k: v for k, v in params.items() if k in options and v not in options[k]}
|
|
if bad:
|
|
print('bad option for %s: %s' % (task, ', '.join('%s=%r' % kv for kv in sorted(bad.items()))),
|
|
file=sys.stderr)
|
|
return 2
|
|
print('[lc_mcp] task %s start %s' % (task, json.dumps(params)), flush=True)
|
|
code = run(task, params)
|
|
if code:
|
|
print('[lc_mcp] task %s aborted (exit %s)' % (task, code), flush=True)
|
|
return code
|
|
print('[lc_mcp] task %s done' % task, flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main(sys.argv))
|