为"反复刷镜牢+经验、不消费狂气"的持续运行做的两处加固: 1. on_teams_screen() 判定漏判 (scripts/auto_cycle.py) 实测 OCR 把 "SIN COST" 输出成 "SINICOST"(无空格), 而判定写死了带空格的 'SIN COST', 于是 ensure_team1() 以为不在 TEAMS 页, 去点 Sinners 按钮空转, 每轮都报 "未能确认选中 TEAMS #1"。改为去空格后再比, 并同时接受 ALLIDENTITIES / ALLE.G.O 等 OCR 变体。 2. read_lunacy() 增加重试 (scripts/auto_cycle.py) 狂气校验是"不许消费"的唯一防线, 但 OCR 偶尔漏掉 LUNACY 标签, 旧代码直接 返回 None 并静默放行("读不到狂气余额, 跳过校验") —— 等于没校验。 改为重试 2 次(每次间隔 2 秒)。 3. 计划任务支持持续刷取 (scripts/install_unattended.ps1) 新增 -EveryHours N: 每 N 小时重复一轮, 而不是每天一次。 顺带修掉两处过时注释("每 5 分钟" -> 1 分钟, 实际早已是 1 分钟)。 4. 补 BOM (scripts/install_unattended.ps1, install_logon_task.ps1) Windows PowerShell 5.1 对无 BOM 的 UTF-8 按 GBK 解析, 中文串会乱码报错 (10-04 坑 6)。keep_display.ps1 本来就有, 这两个安装脚本漏了。 注意: 改动 install_unattended.ps1 会让文件 LF 变 CRLF, 属正常。 新增测试 6 个(on_teams_screen 4 个 + read_lunacy 2 个), 全套 213/213 通过。
308 lines
12 KiB
Python
308 lines
12 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""一轮自动推进: 刷材料 -> (镜牢) -> 升级最弱人格 -> 同步 -> 报告。
|
|
|
|
用法: python auto_cycle.py [--mirror N] [--target LV] [--retries N] [--report-dir DIR]
|
|
退出码: 0 正常完成一轮 / 1 失败
|
|
|
|
每轮在 report-dir(默认 ~/lc_mcp/cycles)写一份 cycle_<时间>.log 与同名 .json 报告,
|
|
计划任务无人值守跑完后据此核对。
|
|
|
|
设计: 只做"一轮",不做无限循环——由外层(goal 轮次)负责重复与判断,
|
|
避免无人监督地长时间空转。每轮输出结构化报告供上层核对。
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
from lc_ui import HERE, PY, click, drag, items, keypress, probe, run
|
|
|
|
LEVELUP = os.path.join(HERE, 'levelup.py')
|
|
# 手动抽卡花掉狂气后, 删掉这个文件重新记基线, 否则每轮都会中止
|
|
LUNACY_BASELINE = os.path.expanduser(os.path.join('~', 'lc_mcp', 'lunacy_baseline.txt'))
|
|
|
|
# 导航
|
|
BACK = (85, 75) # 只在 Sinners 内部退层用, 主界面上按它会进剧情
|
|
BTN_SINNERS = (1130, 805) # 主界面底部 Sinners
|
|
TAB_ALL_IDENTITIES = (1472, 245) # TEAMS 页 -> All Identities
|
|
|
|
# AALC 任务轮询上限
|
|
DAILY_TIMEOUT_S = 45 * 60
|
|
MIRROR_TIMEOUT_S = 70 * 60 # 每局镜牢; 10-04 首次一局 39 分钟
|
|
POLL_S = 60
|
|
# task_status 连续几次报 stall 就判定卡死并停任务
|
|
STALL_POLLS = 2
|
|
# 卡死后重跑次数。AALC 任务开头会自己回主界面, 重跑通常就能脱困
|
|
STALL_RETRIES = 1
|
|
DEFAULT_REPORT_DIR = os.path.expanduser(os.path.join('~', 'lc_mcp', 'cycles'))
|
|
|
|
_log_file = None
|
|
|
|
|
|
def log(msg):
|
|
line = '[%s] %s' % (time.strftime('%H:%M:%S'), msg)
|
|
print(line, flush=True)
|
|
if _log_file:
|
|
with open(_log_file, 'a', encoding='utf-8') as f:
|
|
f.write(line + '\n')
|
|
|
|
|
|
def goto_identities():
|
|
"""从任意位置尽力走到 罪人 -> Identities 网格。
|
|
|
|
只点 Sinners 按钮, 绝不点左上返回键——主界面上的返回键会进入剧情(Drive),
|
|
实测会掉进过场动画, 需要额外恢复。
|
|
"""
|
|
for attempt in range(5):
|
|
its = items()
|
|
txt = ' | '.join(i.get('text', '') for i in its)
|
|
if 'IDENTITIES' in txt and 'Facades' in txt:
|
|
return True
|
|
if 'TEAMS #1' in txt or 'SIN COST' in txt or 'All Idensities' in txt:
|
|
click(*TAB_ALL_IDENTITIES)
|
|
time.sleep(3)
|
|
continue
|
|
if 'LUXCAVATION' in txt.upper() or 'MIRROR' in txt.upper():
|
|
# 在副本/关卡菜单里, 先回主界面
|
|
keypress('esc')
|
|
time.sleep(2)
|
|
continue
|
|
click(*BTN_SINNERS)
|
|
time.sleep(3.5)
|
|
its = items()
|
|
txt = ' | '.join(i.get('text', '') for i in its)
|
|
return 'IDENTITIES' in txt and 'Facades' in txt
|
|
|
|
|
|
def on_teams_screen(txt):
|
|
"""TEAMS 界面的判定。
|
|
|
|
实测 OCR 会把 "SIN COST" 输出成 "SINICOST"(无空格), 直接匹配 'SIN COST'
|
|
会漏判 —— 于是 ensure_team1() 以为不在 TEAMS 页, 去点 Sinners 按钮空转,
|
|
最后报 "未能确认选中 TEAMS #1"。所以先去空格再比。
|
|
"""
|
|
t = txt.replace(' ', '').upper()
|
|
return ('SINCOST' in t or 'SINICOST' in t
|
|
or 'ALLIDENTITIES' in t or 'ALLE.G.0' in t or 'ALLE.G.O' in t)
|
|
|
|
|
|
def on_grid(txt):
|
|
return 'IDENTITIES' in txt and 'Facades' in txt
|
|
|
|
|
|
def ensure_team1():
|
|
"""进 TEAMS 界面, 把预设列表滚到顶并选中 TEAMS #1。
|
|
|
|
实测坑: 这个账号有 40 个队伍预设, 而 AALC 的选队搜索只朝列表下方扫
|
|
(dy 为负), 列表一旦停在底部就永远找不到 TEAMS #1, 日志表现为
|
|
"找不到队伍 # 1" 然后整轮空转。所以每轮开跑前先把列表拉回顶部。
|
|
"""
|
|
for _ in range(5):
|
|
its = items()
|
|
txt = ' | '.join(i.get('text', '') for i in its)
|
|
if on_teams_screen(txt):
|
|
break
|
|
if on_grid(txt):
|
|
click(*BACK) # 网格 -> TEAMS, 仍在 Sinners 内, 安全
|
|
time.sleep(3)
|
|
continue
|
|
click(*BTN_SINNERS)
|
|
time.sleep(3.5)
|
|
|
|
for _ in range(10):
|
|
its = items()
|
|
cand = [i for i in its
|
|
if i.get('text', '').strip().replace(' ', '') in
|
|
('TEAMS#1', 'TFAMS#1') and i.get('x', 999) < 320]
|
|
if cand:
|
|
top = min(cand, key=lambda i: i.get('y', 0))
|
|
click(top['x'], top['y'])
|
|
time.sleep(2.5)
|
|
return True
|
|
# 向下拖拽 = 列表内容下移 = 露出更靠前的预设
|
|
drag(190, 400, 190, 700)
|
|
time.sleep(2)
|
|
return False
|
|
|
|
|
|
def poll_task(task, timeout_s, sleep=time.sleep):
|
|
"""轮询到任务结束。返回 (ok, 说明, 是否因卡死被停)。"""
|
|
t0 = time.time()
|
|
stalled = 0
|
|
while time.time() - t0 < timeout_s:
|
|
sleep(POLL_S)
|
|
st = probe('task_status', {'lines': 1})
|
|
s = st.get('state')
|
|
el = int(time.time() - t0)
|
|
if s != 'running':
|
|
log('%s 结束: state=%s elapsed=%ds exit=%s' % (task, s, el, st.get('exit_code')))
|
|
# AALC 单次战斗失败后仍会 exit 0, succeeded 还要看 errors
|
|
if st.get('errors'):
|
|
return False, '%s: %s' % (s, st['errors'][-1]), False
|
|
return s == 'succeeded', 'state=%s elapsed=%ds' % (s, el), False
|
|
stalled = stalled + 1 if st.get('stall') else 0
|
|
if stalled >= STALL_POLLS:
|
|
probe('stop_task')
|
|
return False, '卡死(%s), 已停止任务 elapsed=%ds' % (st['stall'], el), True
|
|
if el % 600 < POLL_S:
|
|
log(' ...%s running %ds' % (task, el))
|
|
probe('stop_task')
|
|
return False, '超时 %ds' % timeout_s, False
|
|
|
|
|
|
def run_aalc(task, params, timeout_s, retries=STALL_RETRIES, sleep=time.sleep):
|
|
"""跑一个 AALC 任务; 卡死时停掉重跑, 最多 retries 次。返回 (ok, 说明)。"""
|
|
info = ''
|
|
for attempt in range(retries + 1):
|
|
if attempt:
|
|
log('%s 卡死后重跑 (%d/%d)' % (task, attempt, retries))
|
|
sleep(10)
|
|
r = probe('run_task', dict(params, task=task))
|
|
if not r.get('ok'):
|
|
return False, 'run_task 失败: %s' % r.get('error', r)
|
|
log('%s 已启动 %s, 开始轮询' % (task, params or ''))
|
|
ok, info, stalled = poll_task(task, timeout_s, sleep=sleep)
|
|
if ok or not stalled:
|
|
return ok, info
|
|
return False, info
|
|
|
|
|
|
def report_grid():
|
|
out = run([PY, '-X', 'utf8', LEVELUP, 'list'])
|
|
return out.strip()
|
|
|
|
|
|
def read_lunacy(retries=2, pause=2.0):
|
|
"""从当前画面读狂气余额(LUNACY 标签右下方的数字)。读不到返回 None。
|
|
|
|
OCR 偶尔会漏掉 LUNACY 这个标签(实测同一画面一次抓到、一次没抓到), 而狂气
|
|
校验是"不许消费"的唯一防线 —— 静默跳过等于没校验, 所以这里重试几次。
|
|
"""
|
|
best = None
|
|
for attempt in range(retries + 1):
|
|
its = items()
|
|
lab = None
|
|
for it in its:
|
|
if 'LUNACY' in it.get('text', '').upper():
|
|
lab = it
|
|
break
|
|
if lab:
|
|
for it in its:
|
|
t = it.get('text', '').strip()
|
|
if t.isdigit() and len(t) >= 3:
|
|
dy = it.get('y', 0) - lab.get('y', 0)
|
|
dx = abs(it.get('x', 0) - lab.get('x', 0))
|
|
if 0 < dy < 70 and dx < 130:
|
|
best = int(t)
|
|
if best is not None:
|
|
return best
|
|
if attempt < retries:
|
|
time.sleep(pause)
|
|
return best
|
|
|
|
|
|
def lunacy_guard():
|
|
"""狂气只许不降。用户明确要求狂气只能用于抽卡。
|
|
|
|
返回 (ok, 说明)。首次运行写入基线。
|
|
"""
|
|
cur = read_lunacy()
|
|
if cur is None:
|
|
return True, '读不到狂气余额, 跳过校验'
|
|
base = None
|
|
if os.path.exists(LUNACY_BASELINE):
|
|
try:
|
|
base = int(open(LUNACY_BASELINE, encoding='utf-8').read().strip())
|
|
except ValueError:
|
|
base = None
|
|
if base is None:
|
|
with open(LUNACY_BASELINE, 'w', encoding='utf-8') as f:
|
|
f.write(str(cur))
|
|
return True, '记录狂气基线 %d' % cur
|
|
if cur < base:
|
|
return False, '狂气从 %d 降到 %d —— 已消耗, 立即停止' % (base, cur)
|
|
if cur > base:
|
|
with open(LUNACY_BASELINE, 'w', encoding='utf-8') as f:
|
|
f.write(str(cur))
|
|
return True, '狂气 %d -> %d (增加, 更新基线)' % (base, cur)
|
|
return True, '狂气 %d 未变' % cur
|
|
|
|
|
|
def main(argv=None):
|
|
global _log_file
|
|
ap = argparse.ArgumentParser(description='一轮: 刷材料 -> 镜牢 -> 升级/同步')
|
|
ap.add_argument('--mirror', type=int, default=0, help='本轮镜牢局数, 默认 0')
|
|
ap.add_argument('--target', type=int, default=34, help='把队内人格升到的等级')
|
|
ap.add_argument('--retries', type=int, default=STALL_RETRIES, help='AALC 卡死后重跑次数')
|
|
ap.add_argument('--report-dir', default=DEFAULT_REPORT_DIR)
|
|
args = ap.parse_args(argv)
|
|
|
|
os.makedirs(args.report_dir, exist_ok=True)
|
|
base = os.path.join(args.report_dir, 'cycle_' + time.strftime('%Y%m%d_%H%M%S'))
|
|
_log_file = base + '.log'
|
|
t_start = time.time()
|
|
steps = []
|
|
|
|
def step(name, ok, info):
|
|
steps.append({'step': name, 'ok': ok, 'info': info, 'at': time.strftime('%H:%M:%S')})
|
|
log('%s: %s (%s)' % (name, 'OK' if ok else 'FAIL', info))
|
|
return ok
|
|
|
|
def finish(code, why):
|
|
log('=== %s, 耗时 %ds ===' % (why, int(time.time() - t_start)))
|
|
report = {'ok': code == 0, 'result': why, 'started': time.strftime(
|
|
'%Y-%m-%d %H:%M:%S', time.localtime(t_start)), 'elapsed_s': int(time.time() - t_start),
|
|
'args': vars(args), 'steps': steps, 'log': _log_file}
|
|
with open(base + '.json', 'w', encoding='utf-8') as f:
|
|
json.dump(report, f, ensure_ascii=False, indent=2)
|
|
return code
|
|
|
|
log('=== 一轮开始 ===')
|
|
if not step('狂气校验', *lunacy_guard()):
|
|
return finish(1, '中止: 狂气被消耗, 按用户要求不得使用')
|
|
|
|
# 先把预设列表拉回顶部并选中 TEAMS #1——AALC 只朝列表下方找, 停在底部会找不到
|
|
if not ensure_team1():
|
|
log('[warn] 未能确认选中 TEAMS #1, 继续尝试')
|
|
|
|
if not step('刷材料', *run_aalc('daily', {}, DAILY_TIMEOUT_S, args.retries)):
|
|
return finish(1, '本轮失败: 刷材料未成功')
|
|
|
|
if args.mirror > 0:
|
|
# 镜牢掉落自我碎片, 同步到 IV 阶要用
|
|
ok, info = run_aalc('mirror', {'count': args.mirror},
|
|
MIRROR_TIMEOUT_S * args.mirror, args.retries)
|
|
if not step('镜牢 x%d' % args.mirror, ok, info):
|
|
log('[warn] 镜牢未完成, 继续升级')
|
|
|
|
# AALC 任务结束后在主界面, 这里才是能读到 LUNACY 的时机
|
|
time.sleep(8)
|
|
if not step('狂气校验(刷后)', *lunacy_guard()):
|
|
return finish(1, '中止: 狂气被消耗, 按用户要求不得使用')
|
|
|
|
if not goto_identities():
|
|
step('进入 Identities 网格', False, '找不到网格')
|
|
return finish(1, '本轮失败: 无法进入 Identities 网格')
|
|
log('升级前:\n' + report_grid())
|
|
|
|
log('对队内低于 Lv.%d 的人格升级 + 同步' % args.target)
|
|
# 用 progress 而非 weakest: 网格 OCR 会把 LV14 读成 Lv.1,
|
|
# progress 会逐个打开详情页以真实等级判断目标。
|
|
out = run([PY, '-X', 'utf8', LEVELUP, 'progress', str(args.target), '--uptie'])
|
|
lines = out.strip().splitlines()
|
|
for line in lines:
|
|
log(' ' + line)
|
|
step('升级/同步', not any('FAIL' in l for l in lines),
|
|
'; '.join(l.strip() for l in lines if '升级:' in l or '同步:' in l) or '无需升级')
|
|
|
|
time.sleep(5)
|
|
if not goto_identities():
|
|
step('回到网格', False, '升级后无法回到网格')
|
|
return finish(1, '本轮失败: 升级后无法回到网格')
|
|
log('升级后:\n' + report_grid())
|
|
return finish(0, '一轮结束')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|