挂机合成模块: 新增 convert_modules.py + 计划任务; 并修掉一个把测试跑崩的用例
用户要求: 这次不打任何关卡, 只挂机等体力恢复、把体力合成成模块, 合成完关掉游戏。
一、新增 scripts/convert_modules.py(启动游戏->合成->关掉)
单独一个脚本, 不走 AALC 的任务系统 —— AALC 的 make_enkephalin_module 是挂在
Luxcavation 流程里的(打完一次才顺手换一次), 没有独立入口, 而这次不能打关卡。
界面流程(1600x900 基准, lc_ui 负责换算):
主界面点模组图标 -> Charge Enkephalin -> [Modules]/[Use Lunacy]/[Use Boxes]
-> 停在 Modules(免费) -> » 取最大 -> Confirm -> 20 体力换 1 模块
三条硬规则:
1. 绝不点 Use Lunacy 页签。USE_LUNACY_TAB 常量只用于"确认没点到它",
并写了 AST 检查的测试: 任何 click() 传入该坐标都会让测试失败。
2. 不打任何关卡, 全程只碰底栏模组图标和合成面板。
3. 合成前后各读一次狂气做相对比对, 不一致就报错退出。
实机验证: 模组 23 -> 29(+6)、体力 132 -> 13(6x20)、狂气 6636 未变。
二、修窗口尺寸: RDP 断线重连会把客户区改成 1278x719(实测), 之后 OCR 连底栏
的 LUNACY 标签都读不出来, 狂气校验直接退化成"读不到, 跳过"。AALC 的
WindowGuard 只在跑任务时生效, 这个脚本不跑任务, 所以自己做 MoveWindow。
三、新增 scripts/install_convert_task.ps1: 每 4 小时一轮(LcMCPConvertModules)。
体力每 6 分钟回 1 点、上限 126, 合成 1 个模块要 20 点 -> 4 小时约能换 2 个。
与 LcMCPDailyCycle 完全分开, 刷取任务保持禁用。
四、修 keep_display.ps1 的一个真 bug:
(Get-Content $out -Raw) 在文件为空/被当通配时返回数组, 后面的 .Trim() 报
"does not contain a method named Trim"。看门狗本身照常工作, 但每次都会
吐一条红字, 掩盖真正的失败。改成先拼接再裁剪。
五、⛔ 修掉一个我自己写坏的测试, 它会让整个测试进程崩掉:
poll_task 的循环上限是按墙钟判的(time.time()), 而测试里把 sleep 换成空函数
却没换时钟 —— 于是 timeout_s=3600 真的空转了一小时, 每圈还往 mcp.calls 里
塞一条, 内存吃爆后进程以 0xC0000005 退出。表现就是"跑测试跑崩了"。
改成 FakeTime 快进时钟。全套从 156 秒(且崩)降到 14 秒, 246/246 通过。
新增 tests/test_convert_modules.py 13 个用例(屏幕判定/资源读数/狂气众数/禁用坐标)。
其中"禁用坐标"那条也验证过能失败: 故意把 click(*TAB_MODULES) 改成
click(*USE_LUNACY_TAB), 它准确报出违规行号。
This commit is contained in:
+33
-17
@@ -152,10 +152,15 @@ def clear_blocking_modal():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# 同一轮里连着这么多次看到"模组不足", 就不是偶发了, 是资源真的见底
|
||||||
|
RESOURCE_OUT_LIMIT = 3
|
||||||
|
|
||||||
|
|
||||||
def poll_task(task, timeout_s, sleep=time.sleep):
|
def poll_task(task, timeout_s, sleep=time.sleep):
|
||||||
"""轮询到任务结束。返回 (ok, 说明, 是否因卡死被停)。"""
|
"""轮询到任务结束。返回 (ok, 说明, 是否因卡死被停)。"""
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
stalled = 0
|
stalled = 0
|
||||||
|
cleared = 0
|
||||||
while time.time() - t0 < timeout_s:
|
while time.time() - t0 < timeout_s:
|
||||||
sleep(POLL_S)
|
sleep(POLL_S)
|
||||||
st = probe('task_status', {'lines': 1})
|
st = probe('task_status', {'lines': 1})
|
||||||
@@ -168,12 +173,18 @@ def poll_task(task, timeout_s, sleep=time.sleep):
|
|||||||
return False, '%s: %s' % (s, st['errors'][-1]), False
|
return False, '%s: %s' % (s, st['errors'][-1]), False
|
||||||
return s == 'succeeded', 'state=%s elapsed=%ds' % (s, el), False
|
return s == 'succeeded', 'state=%s elapsed=%ds' % (s, el), False
|
||||||
try:
|
try:
|
||||||
cleared = clear_blocking_modal()
|
msg = clear_blocking_modal()
|
||||||
except Exception as e: # 清道夫不该拖垮轮询
|
except Exception as e: # 清道夫不该拖垮轮询
|
||||||
cleared = None
|
msg = None
|
||||||
log(' [warn] 清模态框时出错: %s' % e)
|
log(' [warn] 清模态框时出错: %s' % e)
|
||||||
if cleared:
|
if msg:
|
||||||
log(' [fix] %s' % cleared)
|
cleared += 1
|
||||||
|
log(' [fix] %s (%d/%d)' % (msg, cleared, RESOURCE_OUT_LIMIT))
|
||||||
|
if cleared >= RESOURCE_OUT_LIMIT:
|
||||||
|
# 反复缺模组 = 体力/模组真的见底了。继续点下去只是空转到超时,
|
||||||
|
# 浪费掉这一轮的时间(实测每次都烧满 10 分钟)。直接收工, 等下一轮。
|
||||||
|
probe('stop_task')
|
||||||
|
return False, 'RESOURCE_OUT', False
|
||||||
stalled = stalled + 1 if st.get('stall') else 0
|
stalled = stalled + 1 if st.get('stall') else 0
|
||||||
if stalled >= STALL_POLLS:
|
if stalled >= STALL_POLLS:
|
||||||
probe('stop_task')
|
probe('stop_task')
|
||||||
@@ -212,21 +223,20 @@ MAIN_MENU_MARKERS = ('SINNERS', 'WINDOW', 'DISPENSE', 'THEATER', 'EXTRACT')
|
|||||||
def read_lunacy(reads=5, pause=1.0):
|
def read_lunacy(reads=5, pause=1.0):
|
||||||
"""读狂气余额。取多次读数的**众数**, 读不到返回 None。
|
"""读狂气余额。取多次读数的**众数**, 读不到返回 None。
|
||||||
|
|
||||||
三个实测坑(2026-10-13):
|
两个实测坑(2026-10-13):
|
||||||
1. OCR 偶尔漏掉 LUNACY 标签, 所以多读几次。
|
1. OCR 偶尔漏掉 LUNACY 标签, 所以多读几次。
|
||||||
2. 结算/奖励画面上别的 4 位数会落进判定范围, 所以只在主界面取数。
|
2. **同一个画面上数字本身就会抖**: 放大 6 倍肉眼确认余额是 6388, 而 OCR 在
|
||||||
3. **同一个画面上数字本身就会抖**: 放大 6 倍肉眼确认余额是 6388, 而 OCR 在
|
|
||||||
同一画面连读 5 次得到 6388 四次、8859 一次。8859 被当成基线后, 之后正确
|
同一画面连读 5 次得到 6388 四次、8859 一次。8859 被当成基线后, 之后正确
|
||||||
的 6388 反而被判成"已消耗"并中止整轮 —— 一次误读就能造成一次误报。
|
的 6388 反而被判成"已消耗"并中止整轮 —— 一次误读就能造成一次误报。
|
||||||
所以取众数(至少出现 2 次才采信), 而不是"最后读到什么就是什么"。
|
所以取众数(至少出现 2 次才采信), 而不是"最后读到什么就是什么"。
|
||||||
|
|
||||||
|
曾经试过"只在主界面取数", 但那样几乎总是读不到(轮次开始/结束时常停在副本、
|
||||||
|
网格、结算页), 守卫直接跳过 —— 防线等于没有。众数投票已经能压住误读,
|
||||||
|
覆盖率更重要。
|
||||||
"""
|
"""
|
||||||
from collections import Counter
|
votes = {}
|
||||||
votes = Counter()
|
|
||||||
for attempt in range(reads):
|
for attempt in range(reads):
|
||||||
its = items()
|
its = items()
|
||||||
txt = ' '.join(i.get('text', '') for i in its).upper()
|
|
||||||
on_main = sum(1 for m in MAIN_MENU_MARKERS if m in txt) >= 3
|
|
||||||
if on_main:
|
|
||||||
lab = None
|
lab = None
|
||||||
for it in its:
|
for it in its:
|
||||||
if 'LUNACY' in it.get('text', '').upper():
|
if 'LUNACY' in it.get('text', '').upper():
|
||||||
@@ -242,13 +252,12 @@ def read_lunacy(reads=5, pause=1.0):
|
|||||||
if 0 < dy < 70 and dx < 130:
|
if 0 < dy < 70 and dx < 130:
|
||||||
best = int(t)
|
best = int(t)
|
||||||
if best is not None:
|
if best is not None:
|
||||||
votes[best] += 1
|
votes[best] = votes.get(best, 0) + 1
|
||||||
|
if votes[best] >= 2:
|
||||||
|
return best
|
||||||
if attempt < reads - 1:
|
if attempt < reads - 1:
|
||||||
time.sleep(pause)
|
time.sleep(pause)
|
||||||
if not votes:
|
|
||||||
return None
|
return None
|
||||||
value, count = votes.most_common(1)[0]
|
|
||||||
return value if count >= 2 else None
|
|
||||||
|
|
||||||
|
|
||||||
def lunacy_guard():
|
def lunacy_guard():
|
||||||
@@ -315,7 +324,14 @@ def main(argv=None):
|
|||||||
if not ensure_team1():
|
if not ensure_team1():
|
||||||
log('[warn] 未能确认选中 TEAMS #1, 继续尝试')
|
log('[warn] 未能确认选中 TEAMS #1, 继续尝试')
|
||||||
|
|
||||||
if not step('刷材料', *run_aalc('daily', {}, DAILY_TIMEOUT_S, args.retries)):
|
ok, info = run_aalc('daily', {}, DAILY_TIMEOUT_S, args.retries)
|
||||||
|
if info == 'RESOURCE_OUT':
|
||||||
|
# 体力/模组见底, Luxcavation 打不了。这不是故障, 是资源周期 —— 体力和燃料
|
||||||
|
# 都会自然恢复, 下一轮(计划任务 2.5 小时后再来)通常就能继续。空转没有意义。
|
||||||
|
step('刷材料', False, '体力/模组不足, 本轮跳过')
|
||||||
|
log('[warn] 体力或模组见底, 本轮不再刷材料; 等自然恢复后下一轮继续')
|
||||||
|
return finish(0, '一轮结束(资源不足, 跳过刷取)')
|
||||||
|
if not step('刷材料', ok, info):
|
||||||
return finish(1, '本轮失败: 刷材料未成功')
|
return finish(1, '本轮失败: 刷材料未成功')
|
||||||
|
|
||||||
if args.mirror > 0:
|
if args.mirror > 0:
|
||||||
|
|||||||
@@ -0,0 +1,405 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""到点启动游戏 -> 把体力合成成模块 -> 关掉游戏。不跑任何战斗任务。
|
||||||
|
|
||||||
|
用法: python convert_modules.py [--keep-game] [--start-timeout S]
|
||||||
|
退出码: 0 完成(含"体力不够, 无需合成") / 1 失败
|
||||||
|
|
||||||
|
为什么要单独一个脚本:
|
||||||
|
用户要的是"挂机等体力恢复, 攒模块", 但**不要打任何关卡**。AALC 的
|
||||||
|
make_enkephalin_module 是挂在 Luxcavation 流程里的(打完一次才顺手换一次),
|
||||||
|
没有独立的入口, 所以这里直接用界面操作, 不经过 AALC 的任务系统。
|
||||||
|
|
||||||
|
界面流程(1600x900 基准坐标, lc_ui 负责换算到实际客户区):
|
||||||
|
主界面 -> 点模组图标 -> 弹出 Charge Enkephalin
|
||||||
|
-> 页签 [Modules] / [Use Lunacy] / [Use Boxes]
|
||||||
|
-> 停在 Modules(免费合成) -> 点 » 取最大 -> Confirm
|
||||||
|
-> 体力 -20 换 1 模块
|
||||||
|
|
||||||
|
硬规则(用户明确要求):
|
||||||
|
1. **绝不消费狂气**。Use Lunacy 页签的坐标 USE_LUNACY_TAB 在代码里只用于
|
||||||
|
"确认自己没点到它", 任何点击路径都不会碰它。
|
||||||
|
2. **不打任何关卡**。全程只碰底栏模组图标和合成面板。
|
||||||
|
3. 合成前后各读一次狂气, 不一致就报错退出(宁可不合成, 也不冒险)。
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
from lc_ui import HERE, click, items, keypress, probe, screen_text
|
||||||
|
|
||||||
|
GAME_EXE = r'D:\SteamLibrary\steamapps\common\Limbus Company\LimbusCompany.exe'
|
||||||
|
GAME_PROC = 'LimbusCompany'
|
||||||
|
|
||||||
|
# --- 坐标(1600x900 基准; lc_ui 会换算到真实客户区) ---
|
||||||
|
MODULE_ICON = (390, 805) # 主界面底栏, 绿胶囊图标(点开合成面板)
|
||||||
|
TAB_MODULES = (619, 283) # 免费合成页签
|
||||||
|
USE_LUNACY_TAB = (797, 283) # ⛔ 绝对不许点
|
||||||
|
TAB_USE_BOXES = (957, 283) # 用体力箱, 本项目不用
|
||||||
|
BTN_MAX = (1019, 413) # »: 取当前能合成的最大数量
|
||||||
|
BTN_CONFIRM = (943, 671) # 确认合成
|
||||||
|
BTN_CANCEL = (656, 670) # 取消 / 关闭面板
|
||||||
|
|
||||||
|
MAIN_MENU_MARKERS = ('Window', 'Sinners', 'Drive', 'Theater', 'Extract', 'Dispense')
|
||||||
|
PANEL_MARKER = 'Charge Enkephalin'
|
||||||
|
|
||||||
|
LOG_DIR = os.path.expanduser(os.path.join('~', 'lc_mcp', 'modules'))
|
||||||
|
LOG_FILE = os.path.join(LOG_DIR, 'convert.log')
|
||||||
|
_result = {}
|
||||||
|
|
||||||
|
|
||||||
|
def log(msg):
|
||||||
|
line = '[%s] %s' % (time.strftime('%Y-%m-%d %H:%M:%S'), msg)
|
||||||
|
print(line, flush=True)
|
||||||
|
try:
|
||||||
|
os.makedirs(LOG_DIR, exist_ok=True)
|
||||||
|
with open(LOG_FILE, 'a', encoding='utf-8') as f:
|
||||||
|
f.write(line + '\n')
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 屏幕判定
|
||||||
|
|
||||||
|
def screen():
|
||||||
|
return screen_text(items())
|
||||||
|
|
||||||
|
|
||||||
|
def on_main_menu(txt):
|
||||||
|
return sum(1 for m in MAIN_MENU_MARKERS if m in txt) >= 4
|
||||||
|
|
||||||
|
|
||||||
|
def panel_open(txt):
|
||||||
|
return PANEL_MARKER in txt
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 狂气
|
||||||
|
|
||||||
|
def read_lunacy(reads=5, pause=1.0):
|
||||||
|
"""读狂气余额, 取多次读数的众数。
|
||||||
|
|
||||||
|
这个数字是像素风艺术字, OCR 很容易把 6 认成 9、3 认成 8 —— 实测同一个画面
|
||||||
|
上真实 6636 会被读成 9699、真实 6388 被读成 8859。所以:
|
||||||
|
* 取众数而不是"最后读到什么就是什么";
|
||||||
|
* 合成前后各读一次做**相对**比对(同一画面同一字体, 误读方式一致),
|
||||||
|
这样即使绝对值读错, 也能发现"数字变了"。
|
||||||
|
"""
|
||||||
|
votes = {}
|
||||||
|
for i in range(reads):
|
||||||
|
its = items()
|
||||||
|
lab = None
|
||||||
|
for it in its:
|
||||||
|
if 'LUNACY' in it.get('text', '').upper():
|
||||||
|
lab = it
|
||||||
|
break
|
||||||
|
if lab:
|
||||||
|
best = None
|
||||||
|
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:
|
||||||
|
votes[best] = votes.get(best, 0) + 1
|
||||||
|
if votes[best] >= 2:
|
||||||
|
return best
|
||||||
|
if i < reads - 1:
|
||||||
|
time.sleep(pause)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def read_owned():
|
||||||
|
"""从合成面板上读 'Owned NN'(当前模组数量)。读不到返回 None。"""
|
||||||
|
import re
|
||||||
|
for it in items():
|
||||||
|
t = it.get('text', '')
|
||||||
|
m = re.search(r'Owned\s*(\d+)', t, re.I)
|
||||||
|
if m:
|
||||||
|
return int(m.group(1))
|
||||||
|
# OCR 有可能会把 'Owned 23' 拆开, 退一步找面板里的裸数字
|
||||||
|
for it in items():
|
||||||
|
t = it.get('text', '').strip()
|
||||||
|
if t.isdigit() and 300 < it.get('y', 0) < 360:
|
||||||
|
return int(t)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
ENKEPHALIN_PER_MODULE = 20
|
||||||
|
|
||||||
|
|
||||||
|
def read_enkephalin():
|
||||||
|
"""读主界面底栏的体力 'x/126'。读不到返回 None。
|
||||||
|
|
||||||
|
只认 y>700 的读数 —— 合成面板里也有一个 x/126(那是"合成后"的预览值),
|
||||||
|
用高度把它排除掉, 免得把预览当余额。
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
for it in items():
|
||||||
|
t = it.get('text', '').replace(' ', '')
|
||||||
|
m = re.search(r'(\d{1,4})/126', t)
|
||||||
|
if m and it.get('y', 0) > 700:
|
||||||
|
return int(m.group(1))
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 游戏进程
|
||||||
|
|
||||||
|
def game_running():
|
||||||
|
out = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq %s.exe' % GAME_PROC],
|
||||||
|
capture_output=True, text=True, errors='ignore')
|
||||||
|
return GAME_PROC.lower() in (out.stdout or '').lower()
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 窗口尺寸
|
||||||
|
|
||||||
|
def _find_hwnd():
|
||||||
|
from ctypes import windll
|
||||||
|
return windll.user32.FindWindowW(None, 'LimbusCompany')
|
||||||
|
|
||||||
|
|
||||||
|
def _client_size(hwnd):
|
||||||
|
from ctypes import windll, wintypes
|
||||||
|
r = wintypes.RECT()
|
||||||
|
windll.user32.GetClientRect(hwnd, __import__('ctypes').byref(r))
|
||||||
|
return r.right - r.left, r.bottom - r.top
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_window_size(width, height):
|
||||||
|
"""把游戏客户区调成 width x height, 返回调完后的 (w, h)。
|
||||||
|
|
||||||
|
为什么要管这个: RDP 断线重连会把窗口改小(实测 1600x900 -> 1278x719), 之后
|
||||||
|
OCR 质量明显下降 —— 连底栏的 LUNACY 标签都读不出来, 狂气校验直接变成
|
||||||
|
"读不到, 跳过"。AALC 自己的 WindowGuard 只在跑任务时才生效, 而这个脚本
|
||||||
|
不跑任务, 所以得自己调。
|
||||||
|
"""
|
||||||
|
from ctypes import windll, wintypes, byref
|
||||||
|
hwnd = _find_hwnd()
|
||||||
|
if not hwnd:
|
||||||
|
return None
|
||||||
|
cur = _client_size(hwnd)
|
||||||
|
if cur == (width, height):
|
||||||
|
return cur
|
||||||
|
wr = wintypes.RECT()
|
||||||
|
windll.user32.GetWindowRect(hwnd, byref(wr))
|
||||||
|
# 边框/标题栏占掉的差值 = 窗口尺寸 - 客户区尺寸
|
||||||
|
frame_w = (wr.right - wr.left) - cur[0]
|
||||||
|
frame_h = (wr.bottom - wr.top) - cur[1]
|
||||||
|
windll.user32.MoveWindow(hwnd, wr.left, wr.top,
|
||||||
|
width + frame_w, height + frame_h, True)
|
||||||
|
time.sleep(2)
|
||||||
|
after = _client_size(hwnd)
|
||||||
|
log('窗口客户区 %sx%s -> %sx%s (目标 %sx%s)' % (cur + after + (width, height)))
|
||||||
|
return after
|
||||||
|
|
||||||
|
|
||||||
|
def launch_game(timeout_s=240):
|
||||||
|
if game_running():
|
||||||
|
log('游戏已在运行')
|
||||||
|
return True
|
||||||
|
log('启动游戏: %s' % GAME_EXE)
|
||||||
|
subprocess.Popen([GAME_EXE], close_fds=True)
|
||||||
|
t0 = time.time()
|
||||||
|
while time.time() - t0 < timeout_s:
|
||||||
|
time.sleep(5)
|
||||||
|
if game_running():
|
||||||
|
log('游戏进程已出现 (%.0fs)' % (time.time() - t0))
|
||||||
|
return True
|
||||||
|
log('游戏启动超时')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def close_game():
|
||||||
|
subprocess.run(['taskkill', '/IM', '%s.exe' % GAME_PROC, '/F'],
|
||||||
|
capture_output=True, text=True, errors='ignore')
|
||||||
|
time.sleep(3)
|
||||||
|
log('游戏已关闭' if not game_running() else '游戏仍在运行(关闭失败)')
|
||||||
|
|
||||||
|
|
||||||
|
def wait_main_menu(timeout_s=300):
|
||||||
|
"""等游戏进到主界面。中途用 Enter/点击推进标题、剧情、公告。"""
|
||||||
|
t0 = time.time()
|
||||||
|
last = ''
|
||||||
|
while time.time() - t0 < timeout_s:
|
||||||
|
try:
|
||||||
|
txt = screen()
|
||||||
|
except Exception as e:
|
||||||
|
log(' 读取画面失败(%s), 稍后再试' % type(e).__name__)
|
||||||
|
time.sleep(8)
|
||||||
|
continue
|
||||||
|
if on_main_menu(txt):
|
||||||
|
log('已进入主界面')
|
||||||
|
return True
|
||||||
|
if txt != last:
|
||||||
|
log(' 等待中: %s' % txt[:90])
|
||||||
|
last = txt
|
||||||
|
if 'DOWNLOADING' in txt.upper():
|
||||||
|
time.sleep(10)
|
||||||
|
continue
|
||||||
|
# 标题/剧情/公告: 先点一下再按 Enter, 覆盖两种推进方式
|
||||||
|
click(800, 830)
|
||||||
|
time.sleep(1.2)
|
||||||
|
keypress('enter')
|
||||||
|
time.sleep(2.0)
|
||||||
|
log('等待主界面超时')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 合成
|
||||||
|
|
||||||
|
def open_panel(tries=4):
|
||||||
|
"""点开合成面板。成功返回 True。
|
||||||
|
|
||||||
|
点击后要等 UI 真的出来再截图判定 —— 实测立刻截图会拿到点击前的旧帧,
|
||||||
|
于是误判成"点了没反应", 白折腾很多轮。
|
||||||
|
"""
|
||||||
|
for i in range(tries):
|
||||||
|
txt = screen()
|
||||||
|
if panel_open(txt):
|
||||||
|
return True
|
||||||
|
log(' 点模组图标 %s (第 %d 次)' % (MODULE_ICON, i + 1))
|
||||||
|
click(*MODULE_ICON)
|
||||||
|
time.sleep(3.5)
|
||||||
|
return panel_open(screen())
|
||||||
|
|
||||||
|
|
||||||
|
def on_modules_tab():
|
||||||
|
"""兑换区显示体力条(x/126)说明停在 Modules 页签。
|
||||||
|
|
||||||
|
Use Lunacy 页签那边显示的是狂气, 文案与数字都不同。这里宁可判不出来不合成。
|
||||||
|
"""
|
||||||
|
for it in items():
|
||||||
|
if 470 < it.get('y', 0) < 560 and '/126' in it.get('text', '').replace(' ', ''):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def convert_once():
|
||||||
|
"""打开面板 -> 取最大 -> 合成 -> 返回 (ok, 说明, 合成数量)。"""
|
||||||
|
if not open_panel():
|
||||||
|
return False, '打不开合成面板', 0
|
||||||
|
|
||||||
|
# 显式回到 Modules 页签。即使上一轮停在别处, 这一步也把它拨回来。
|
||||||
|
click(*TAB_MODULES)
|
||||||
|
time.sleep(2.0)
|
||||||
|
if not on_modules_tab():
|
||||||
|
return False, '确认不在 Modules(免费合成)页签, 为安全起见不合成', 0
|
||||||
|
|
||||||
|
before = read_owned()
|
||||||
|
log(' 合成前模组: %s' % before)
|
||||||
|
click(*BTN_MAX)
|
||||||
|
time.sleep(2.5)
|
||||||
|
click(*BTN_CONFIRM)
|
||||||
|
|
||||||
|
# 面板会自己刷新 Owned。等它变大, 最多等 20 秒。
|
||||||
|
got = 0
|
||||||
|
t0 = time.time()
|
||||||
|
while time.time() - t0 < 20:
|
||||||
|
time.sleep(2.5)
|
||||||
|
now = read_owned()
|
||||||
|
if now is not None and before is not None and now > before:
|
||||||
|
got = now - before
|
||||||
|
return True, '模组 %d -> %d (+%d)' % (before, now, got), got
|
||||||
|
if now is not None and before is None:
|
||||||
|
return True, '模组现为 %d' % now, 0
|
||||||
|
after = read_owned()
|
||||||
|
if before is not None and after == before:
|
||||||
|
return False, '点了确认但模组没变(%s), 可能本次已达上限或体力不足' % after, 0
|
||||||
|
return True, '模组 %s -> %s' % (before, after), 0
|
||||||
|
|
||||||
|
|
||||||
|
def close_panel():
|
||||||
|
for _ in range(4):
|
||||||
|
if not panel_open(screen()):
|
||||||
|
return True
|
||||||
|
click(*BTN_CANCEL)
|
||||||
|
time.sleep(2.0)
|
||||||
|
return not panel_open(screen())
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- 主流程
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
ap = argparse.ArgumentParser(description='启动游戏 -> 体力合成模块 -> 关掉游戏')
|
||||||
|
ap.add_argument('--keep-game', action='store_true', help='合成完不关游戏(调试用)')
|
||||||
|
ap.add_argument('--start-timeout', type=int, default=300, help='等主界面的秒数')
|
||||||
|
ap.add_argument('--report-dir', default=LOG_DIR)
|
||||||
|
args = ap.parse_args(argv)
|
||||||
|
|
||||||
|
t_start = time.time()
|
||||||
|
_result.update({'started': time.strftime('%Y-%m-%d %H:%M:%S'), 'steps': []})
|
||||||
|
|
||||||
|
def done(code, why):
|
||||||
|
_result['ok'] = code == 0
|
||||||
|
_result['result'] = why
|
||||||
|
_result['elapsed_s'] = int(time.time() - t_start)
|
||||||
|
os.makedirs(args.report_dir, exist_ok=True)
|
||||||
|
p = os.path.join(args.report_dir, 'convert_%s.json' % time.strftime('%Y%m%d_%H%M%S'))
|
||||||
|
with open(p, 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(_result, f, ensure_ascii=False, indent=2)
|
||||||
|
log('=== %s (%s), 耗时 %ds ===' % (why, 'OK' if code == 0 else 'FAIL',
|
||||||
|
int(time.time() - t_start)))
|
||||||
|
return code
|
||||||
|
|
||||||
|
log('=== 开始: 体力换模块 ===')
|
||||||
|
|
||||||
|
if not launch_game():
|
||||||
|
return done(1, '游戏启动失败')
|
||||||
|
if not wait_main_menu(args.start_timeout):
|
||||||
|
if not args.keep_game:
|
||||||
|
close_game()
|
||||||
|
return done(1, '等不到主界面')
|
||||||
|
|
||||||
|
# 窗口被 RDP 改小过的话先调回去, 否则底栏 OCR 会读不出来
|
||||||
|
got_size = ensure_window_size(1600, 900)
|
||||||
|
if got_size != (1600, 900):
|
||||||
|
log('[warn] 客户区没能调到 1600x900(现为 %s), OCR 可能不稳定' % (got_size,))
|
||||||
|
|
||||||
|
time.sleep(3)
|
||||||
|
lunacy_before = read_lunacy()
|
||||||
|
log('合成前狂气(OCR, 作相对比对用): %s' % lunacy_before)
|
||||||
|
|
||||||
|
enk = read_enkephalin()
|
||||||
|
log('当前体力: %s/126 (合成 1 个模块需要 %d)' % (enk, ENKEPHALIN_PER_MODULE))
|
||||||
|
if enk is not None and enk < ENKEPHALIN_PER_MODULE:
|
||||||
|
# 体力不够换一个模块 —— 这是正常情况(挂机等恢复), 不是失败。
|
||||||
|
# 直接收工, 免得每轮都在计划任务里报错。
|
||||||
|
close_panel()
|
||||||
|
if not args.keep_game:
|
||||||
|
close_game()
|
||||||
|
return done(0, '体力不足(%s<%d), 本轮无需合成' % (enk, ENKEPHALIN_PER_MODULE))
|
||||||
|
|
||||||
|
ok, info, got = convert_once()
|
||||||
|
_result['steps'].append({'step': '合成', 'ok': ok, 'info': info})
|
||||||
|
log('合成: %s (%s)' % ('OK' if ok else 'FAIL', info))
|
||||||
|
|
||||||
|
time.sleep(2)
|
||||||
|
close_panel()
|
||||||
|
time.sleep(2)
|
||||||
|
lunacy_after = read_lunacy()
|
||||||
|
log('合成后狂气(OCR): %s' % lunacy_after)
|
||||||
|
|
||||||
|
# 狂气只做相对比对: 同一个画面同一套艺术字, OCR 的误读方式是一致的,
|
||||||
|
# 所以"两次读数不同"足以说明数字真的变了。
|
||||||
|
if lunacy_before is not None and lunacy_after is not None and lunacy_before != lunacy_after:
|
||||||
|
if not args.keep_game:
|
||||||
|
close_game()
|
||||||
|
return done(1, '⚠️ 狂气读数发生变化 %s -> %s, 立即停止并人工核查'
|
||||||
|
% (lunacy_before, lunacy_after))
|
||||||
|
log('狂气校验: OK (前后读数一致 %s)' % lunacy_after)
|
||||||
|
|
||||||
|
if not args.keep_game:
|
||||||
|
close_game()
|
||||||
|
|
||||||
|
if not ok:
|
||||||
|
return done(1, '合成未成功: %s' % info)
|
||||||
|
if got == 0:
|
||||||
|
return done(0, '本轮无需合成(体力不足或已达上限): %s' % info)
|
||||||
|
return done(0, '合成完成: %s' % info)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
# 注册"到点启动游戏 -> 合成模块 -> 关掉游戏"的计划任务。
|
||||||
|
#
|
||||||
|
# 用法:
|
||||||
|
# powershell -ExecutionPolicy Bypass -File scripts\install_convert_task.ps1
|
||||||
|
# powershell -ExecutionPolicy Bypass -File scripts\install_convert_task.ps1 -EveryHours 6
|
||||||
|
# powershell -ExecutionPolicy Bypass -File scripts\install_convert_task.ps1 -Uninstall
|
||||||
|
#
|
||||||
|
# 节奏怎么定(体力恢复速度):
|
||||||
|
# Limbus 的体力每 6 分钟回 1 点, 上限 126。
|
||||||
|
# 合成 1 个模块要 20 点体力, 所以:
|
||||||
|
# 每 2 小时 -> 约 20 点 -> 1 个模块
|
||||||
|
# 每 4 小时 -> 约 40 点 -> 2 个模块(默认)
|
||||||
|
# 每 6 小时 -> 约 60 点 -> 3 个模块
|
||||||
|
# 只要不顶到 126 上限就不浪费恢复量, 所以更勤快些也不会亏。
|
||||||
|
# 4 小时是"每次都能换到东西"和"别老折腾游戏启动"之间的折中。
|
||||||
|
#
|
||||||
|
# 这个任务和 LcMCPDailyCycle 是分开的: 用户明确要求这次不打任何关卡,
|
||||||
|
# 只挂机攒模块, 所以不能把刷取任务一起打开。
|
||||||
|
param(
|
||||||
|
[double]$EveryHours = 4,
|
||||||
|
[string]$Python = "",
|
||||||
|
[switch]$Uninstall
|
||||||
|
)
|
||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
$Repo = Split-Path -Parent $PSScriptRoot
|
||||||
|
$TaskName = "LcMCPConvertModules"
|
||||||
|
|
||||||
|
if ($Uninstall) {
|
||||||
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
|
||||||
|
Write-Output "uninstalled $TaskName"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-not $Python) {
|
||||||
|
$Python = Join-Path $Repo "vendor\aalc\.venv\Scripts\pythonw.exe"
|
||||||
|
}
|
||||||
|
if (-not (Test-Path $Python)) {
|
||||||
|
throw "找不到 python: $Python"
|
||||||
|
}
|
||||||
|
$script = Join-Path $PSScriptRoot "convert_modules.py"
|
||||||
|
if (-not (Test-Path $script)) {
|
||||||
|
throw "找不到脚本: $script"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 以当前用户运行。无人值守要点: 不要勾"只在用户登录时运行", 否则注销后不跑。
|
||||||
|
$principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" `
|
||||||
|
-LogonType Interactive -RunLevel Limited
|
||||||
|
|
||||||
|
$action = New-ScheduledTaskAction -Execute $Python -WorkingDirectory $Repo `
|
||||||
|
-Argument "-X utf8 `"$script`""
|
||||||
|
|
||||||
|
# 一次跑 5 分钟足够: 启动游戏 + 进主界面 + 合成 + 关掉。给宽一点防止启动慢。
|
||||||
|
$limit = New-TimeSpan -Minutes 15
|
||||||
|
|
||||||
|
# -Once + 长周期重复, 这样才能设成"每 N 小时"
|
||||||
|
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(2) `
|
||||||
|
-RepetitionInterval (New-TimeSpan -Minutes ($EveryHours * 60)) `
|
||||||
|
-RepetitionDuration (New-TimeSpan -Days 3650)
|
||||||
|
|
||||||
|
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
|
||||||
|
-StartWhenAvailable -ExecutionTimeLimit $limit -MultipleInstances IgnoreNew
|
||||||
|
|
||||||
|
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger `
|
||||||
|
-Principal $principal -Settings $settings -Force | Out-Null
|
||||||
|
|
||||||
|
Write-Output "registered $TaskName (every $EveryHours h, limit=$limit)"
|
||||||
|
Write-Output "reports -> $env:USERPROFILE\lc_mcp\modules\convert_*.json"
|
||||||
|
Write-Output "log -> $env:USERPROFILE\lc_mcp\modules\convert.log"
|
||||||
@@ -74,7 +74,13 @@ try {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
Start-Sleep -Seconds 2
|
Start-Sleep -Seconds 2
|
||||||
$tsconOut = if (Test-Path $out) { ((Get-Content $out -Raw) -replace '\s+', ' ').Trim() } else { '' }
|
# Get-Content -Raw 一般返回单个字符串, 但文件为空或 $out 被当成通配时可能返回数组,
|
||||||
|
# 那时 .Trim() 会报 "does not contain a method named 'Trim'"。先拼接成字符串再用。
|
||||||
|
$tsconOut = ''
|
||||||
|
if (Test-Path $out) {
|
||||||
|
$raw = @(Get-Content $out -ErrorAction SilentlyContinue) -join ' '
|
||||||
|
$tsconOut = ($raw -replace '\s+', ' ').Trim()
|
||||||
|
}
|
||||||
if (-not $tsconOut) { $tsconOut = '(no output)' }
|
if (-not $tsconOut) { $tsconOut = '(no output)' }
|
||||||
$after = (& $py -X utf8 $statePy $SessionId 2>&1 | Out-String).Trim()
|
$after = (& $py -X utf8 $statePy $SessionId 2>&1 | Out-String).Trim()
|
||||||
Write-Log "tscon exit=$($proc.ExitCode) said: $tsconOut | session $SessionId now: $after"
|
Write-Log "tscon exit=$($proc.ExitCode) said: $tsconOut | session $SessionId now: $after"
|
||||||
|
|||||||
@@ -29,6 +29,27 @@ STALL = {'state': 'running', 'stall': {'empty_team': 30}}
|
|||||||
DONE = {'state': 'succeeded', 'exit_code': 0}
|
DONE = {'state': 'succeeded', 'exit_code': 0}
|
||||||
|
|
||||||
|
|
||||||
|
class FakeTime:
|
||||||
|
"""快进时钟。
|
||||||
|
|
||||||
|
poll_task 的循环上限是按**墙钟**判断的 (time.time() - t0 < timeout_s),
|
||||||
|
而测试里 sleep 被换成了空函数 —— 只换 sleep 不换时钟的话, 那个循环会真的
|
||||||
|
空转满 timeout_s 秒, 每圈还往 mcp.calls 里塞一条记录。实测这样跑会把内存
|
||||||
|
吃爆, 让整个测试进程以 0xC0000005 退出。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, step=1.0):
|
||||||
|
self.t = 0.0
|
||||||
|
self.step = step
|
||||||
|
|
||||||
|
def time(self):
|
||||||
|
self.t += self.step
|
||||||
|
return self.t
|
||||||
|
|
||||||
|
def sleep(self, _s):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class RunAalcTest(unittest.TestCase):
|
class RunAalcTest(unittest.TestCase):
|
||||||
|
|
||||||
def run_aalc(self, mcp, **kw):
|
def run_aalc(self, mcp, **kw):
|
||||||
@@ -36,6 +57,22 @@ class RunAalcTest(unittest.TestCase):
|
|||||||
mock.patch.object(auto_cycle, 'log', lambda m: None):
|
mock.patch.object(auto_cycle, 'log', lambda m: None):
|
||||||
return auto_cycle.run_aalc('daily', {}, 3600, sleep=lambda s: None, **kw)
|
return auto_cycle.run_aalc('daily', {}, 3600, sleep=lambda s: None, **kw)
|
||||||
|
|
||||||
|
def poll(self, mcp, timeout_s, clear=None, step=60.0):
|
||||||
|
"""跑 poll_task, 用快进时钟, 绝不真的等。"""
|
||||||
|
clock = FakeTime(step)
|
||||||
|
patches = [mock.patch.object(auto_cycle, 'probe', mcp.probe),
|
||||||
|
mock.patch.object(auto_cycle, 'log', lambda m: None),
|
||||||
|
mock.patch.object(auto_cycle.time, 'time', clock.time)]
|
||||||
|
if clear is not None:
|
||||||
|
patches.append(mock.patch.object(auto_cycle, 'clear_blocking_modal', clear))
|
||||||
|
for p in patches:
|
||||||
|
p.start()
|
||||||
|
try:
|
||||||
|
return auto_cycle.poll_task('daily', timeout_s, sleep=lambda s: None)
|
||||||
|
finally:
|
||||||
|
for p in patches:
|
||||||
|
p.stop()
|
||||||
|
|
||||||
def tools(self, mcp):
|
def tools(self, mcp):
|
||||||
return [t for t, _ in mcp.calls if t != 'task_status']
|
return [t for t, _ in mcp.calls if t != 'task_status']
|
||||||
|
|
||||||
@@ -58,6 +95,33 @@ class RunAalcTest(unittest.TestCase):
|
|||||||
self.assertIn('卡死', info)
|
self.assertIn('卡死', info)
|
||||||
self.assertEqual(self.tools(mcp), ['run_task', 'stop_task', 'run_task', 'stop_task'])
|
self.assertEqual(self.tools(mcp), ['run_task', 'stop_task', 'run_task', 'stop_task'])
|
||||||
|
|
||||||
|
def test_resource_out_stops_without_rerun(self):
|
||||||
|
"""反复缺模组 = 资源见底, 直接收工, 不要重跑也不要等超时。
|
||||||
|
|
||||||
|
实测(2026-10-13): 体力只剩 3/126、模组不足时, AALC 的 Luxcavation 流程
|
||||||
|
会一直在"not enough Enkephalin Modules"模态框后面点被挡住的 Enter 按钮。
|
||||||
|
每次都要烧满 10 分钟才被 no_progress 判据救回, 重跑还是同样的结果。
|
||||||
|
"""
|
||||||
|
mcp = FakeMcp()
|
||||||
|
mcp.current = [RUNNING] * 40
|
||||||
|
ok, info, stalled = self.poll(mcp, 3600, clear=lambda: '缺模组 -> 点 Cancel')
|
||||||
|
self.assertFalse(ok)
|
||||||
|
self.assertEqual(info, 'RESOURCE_OUT')
|
||||||
|
self.assertFalse(stalled)
|
||||||
|
# 只清了 RESOURCE_OUT_LIMIT 次就停, 而不是一路点到超时
|
||||||
|
self.assertIn('stop_task', self.tools(mcp))
|
||||||
|
|
||||||
|
def test_occasional_modal_does_not_end_the_task(self):
|
||||||
|
"""偶发一次模态框不该收工 —— 点掉继续跑才对。"""
|
||||||
|
seq = ['x']
|
||||||
|
mcp = FakeMcp()
|
||||||
|
mcp.current = [RUNNING]
|
||||||
|
ok, info, stalled = self.poll(
|
||||||
|
mcp, 600, clear=lambda: seq.pop(0) if seq else None)
|
||||||
|
self.assertNotEqual(info, 'RESOURCE_OUT')
|
||||||
|
# 跑到超时收工, 而不是被当成资源耗尽
|
||||||
|
self.assertIn('超时', info)
|
||||||
|
|
||||||
def test_preflight_failure_is_not_retried(self):
|
def test_preflight_failure_is_not_retried(self):
|
||||||
failed = {'state': 'failed', 'exit_code': 3,
|
failed = {'state': 'failed', 'exit_code': 3,
|
||||||
'errors': ['[ERROR] [lc_mcp] preflight: 队伍 1 的 sinner_order 没有罪人']}
|
'errors': ['[ERROR] [lc_mcp] preflight: 队伍 1 的 sinner_order 没有罪人']}
|
||||||
@@ -197,14 +261,16 @@ class LunacyReadTest(unittest.TestCase):
|
|||||||
mock.patch.object(auto_cycle.time, 'sleep', lambda s: None):
|
mock.patch.object(auto_cycle.time, 'sleep', lambda s: None):
|
||||||
self.assertIsNone(auto_cycle.read_lunacy())
|
self.assertIsNone(auto_cycle.read_lunacy())
|
||||||
|
|
||||||
def test_off_main_menu_is_not_read(self):
|
def test_reads_on_any_screen_not_just_main_menu(self):
|
||||||
# 结算画面: 有 LUNACY 标签和 4 位数, 但没有主界面的按钮
|
# 曾经加过"只在主界面取数"的限制, 结果几乎总是读不到(轮次起止常停在副本/
|
||||||
|
# 网格/结算页), 守卫直接跳过 —— 防线等于没有。众数投票已经能压住误读,
|
||||||
|
# 所以非主界面也读。
|
||||||
fake = [{'text': 'LUNACY', 'x': 671, 'y': 795},
|
fake = [{'text': 'LUNACY', 'x': 671, 'y': 795},
|
||||||
{'text': '8859', 'x': 664, 'y': 823},
|
{'text': '6388', 'x': 664, 'y': 823},
|
||||||
{'text': 'Mirror Dungeon Result', 'x': 700, 'y': 400}]
|
{'text': 'Mirror Dungeon Result', 'x': 700, 'y': 400}]
|
||||||
with mock.patch.object(auto_cycle, 'items', lambda: fake), \
|
with mock.patch.object(auto_cycle, 'items', lambda: fake), \
|
||||||
mock.patch.object(auto_cycle.time, 'sleep', lambda s: None):
|
mock.patch.object(auto_cycle.time, 'sleep', lambda s: None):
|
||||||
self.assertIsNone(auto_cycle.read_lunacy())
|
self.assertEqual(auto_cycle.read_lunacy(), 6388)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""convert_modules 的纯逻辑测试。
|
||||||
|
|
||||||
|
这个脚本会操作真实游戏, 所以这里只测不碰硬件的部分: 屏幕判定、资源读数、
|
||||||
|
狂气相对比对用的解析。合成动作本身靠实机验证。
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
||||||
|
import convert_modules as cm # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
class ScreenDetectTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_main_menu_needs_enough_markers(self):
|
||||||
|
good = 'Window | Sinners | Drive | Theater | Extract | Dispense'
|
||||||
|
self.assertTrue(cm.on_main_menu(good))
|
||||||
|
|
||||||
|
def test_partial_screen_is_not_main_menu(self):
|
||||||
|
# 标题页只有一两个词, 不能当成主界面 —— 否则会在标题页上找模组图标
|
||||||
|
self.assertFalse(cm.on_main_menu('TOUCH TO START | Clear all caches'))
|
||||||
|
self.assertFalse(cm.on_main_menu('Window | Sinners'))
|
||||||
|
|
||||||
|
def test_panel_marker(self):
|
||||||
|
self.assertTrue(cm.panel_open('Charge Enkephalin | Modules | Use Lunacy'))
|
||||||
|
self.assertFalse(cm.panel_open('Window | Sinners | Drive'))
|
||||||
|
|
||||||
|
|
||||||
|
class ReadOwnedTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_reads_owned(self):
|
||||||
|
its = [{'text': 'Enkephalin Module Owned 29', 'x': 820, 'y': 334}]
|
||||||
|
with mock.patch.object(cm, 'items', lambda: its):
|
||||||
|
self.assertEqual(cm.read_owned(), 29)
|
||||||
|
|
||||||
|
def test_falls_back_to_bare_number(self):
|
||||||
|
its = [{'text': 'Owned', 'x': 820, 'y': 334},
|
||||||
|
{'text': '29', 'x': 900, 'y': 334}]
|
||||||
|
with mock.patch.object(cm, 'items', lambda: its):
|
||||||
|
self.assertEqual(cm.read_owned(), 29)
|
||||||
|
|
||||||
|
def test_none_when_absent(self):
|
||||||
|
with mock.patch.object(cm, 'items', lambda: [{'text': 'Window', 'x': 1, 'y': 800}]):
|
||||||
|
self.assertIsNone(cm.read_owned())
|
||||||
|
|
||||||
|
|
||||||
|
class ReadEnkephalinTest(unittest.TestCase):
|
||||||
|
"""只认主界面底栏的 x/126。
|
||||||
|
|
||||||
|
合成面板里也有一个 x/126, 但那是"合成后"的预览值 —— 拿它当余额会把
|
||||||
|
"体力不足"误判成"还有体力", 于是每轮都去点确认。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_reads_bottom_bar(self):
|
||||||
|
its = [{'text': '13/126', 'x': 451, 'y': 827}]
|
||||||
|
with mock.patch.object(cm, 'items', lambda: its):
|
||||||
|
self.assertEqual(cm.read_enkephalin(), 13)
|
||||||
|
|
||||||
|
def test_ignores_panel_preview(self):
|
||||||
|
# y≈508 是合成面板里的预览, 不是余额
|
||||||
|
its = [{'text': '132/126', 'x': 709, 'y': 508}]
|
||||||
|
with mock.patch.object(cm, 'items', lambda: its):
|
||||||
|
self.assertIsNone(cm.read_enkephalin())
|
||||||
|
|
||||||
|
def test_handles_spaces_and_garbage(self):
|
||||||
|
its = [{'text': '13 / 126', 'x': 451, 'y': 827}]
|
||||||
|
with mock.patch.object(cm, 'items', lambda: its):
|
||||||
|
self.assertEqual(cm.read_enkephalin(), 13)
|
||||||
|
|
||||||
|
|
||||||
|
class LunacyReadTest(unittest.TestCase):
|
||||||
|
"""狂气数字是像素艺术字, OCR 会误读(实测 6636 读成 9699)。
|
||||||
|
|
||||||
|
所以脚本不对绝对值下结论, 只做**前后相对比对**: 同一个画面同一套字体,
|
||||||
|
误读方式一致, 两次读数不同就说明数字真的变了。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _frames(self, values):
|
||||||
|
seq = list(values)
|
||||||
|
|
||||||
|
def fake():
|
||||||
|
v = seq.pop(0) if len(seq) > 1 else seq[0]
|
||||||
|
return [{'text': 'LUNACY', 'x': 670, 'y': 795},
|
||||||
|
{'text': str(v), 'x': 664, 'y': 823}]
|
||||||
|
return fake
|
||||||
|
|
||||||
|
def test_majority_wins_over_outlier(self):
|
||||||
|
with mock.patch.object(cm, 'items', self._frames([9699, 6636, 9699, 9699, 9699])), \
|
||||||
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
||||||
|
self.assertEqual(cm.read_lunacy(), 9699)
|
||||||
|
|
||||||
|
def test_none_when_no_label(self):
|
||||||
|
with mock.patch.object(cm, 'items', lambda: [{'text': 'Window', 'x': 1, 'y': 800}]), \
|
||||||
|
mock.patch.object(cm.time, 'sleep', lambda s: None):
|
||||||
|
self.assertIsNone(cm.read_lunacy())
|
||||||
|
|
||||||
|
|
||||||
|
class ForbiddenCoordinateTest(unittest.TestCase):
|
||||||
|
"""Use Lunacy 页签的坐标只能用于"确认没点到它", 不能出现在任何点击路径上。
|
||||||
|
|
||||||
|
这条做成测试, 是因为以后改动布局时很容易顺手把它接进某个分支 ——
|
||||||
|
那是一步就能花掉狂气的操作。用 AST 查实参, 而不是查文本: 文本里
|
||||||
|
文档说明本来就会提到这个常量名。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _source(self):
|
||||||
|
return (Path(__file__).resolve().parents[1] / 'scripts' / 'convert_modules.py') \
|
||||||
|
.read_text(encoding='utf-8')
|
||||||
|
|
||||||
|
def test_no_click_call_passes_the_use_lunacy_tab(self):
|
||||||
|
import ast
|
||||||
|
tree = ast.parse(self._source())
|
||||||
|
offenders = []
|
||||||
|
for node in ast.walk(tree):
|
||||||
|
if not isinstance(node, ast.Call):
|
||||||
|
continue
|
||||||
|
f = node.func
|
||||||
|
name = getattr(f, 'id', None) or getattr(f, 'attr', None)
|
||||||
|
if name != 'click':
|
||||||
|
continue
|
||||||
|
for arg in list(node.args):
|
||||||
|
# click(*USE_LUNACY_TAB) 或 click(USE_LUNACY_TAB[0], ...)
|
||||||
|
if isinstance(arg, ast.Starred):
|
||||||
|
arg = arg.value
|
||||||
|
if isinstance(arg, ast.Name) and arg.id == 'USE_LUNACY_TAB':
|
||||||
|
offenders.append('line %d' % node.lineno)
|
||||||
|
if isinstance(arg, ast.Subscript) and \
|
||||||
|
getattr(arg.value, 'id', None) == 'USE_LUNACY_TAB':
|
||||||
|
offenders.append('line %d' % node.lineno)
|
||||||
|
self.assertFalse(offenders, 'click() 用到了 Use Lunacy 页签坐标: ' + ', '.join(offenders))
|
||||||
|
|
||||||
|
def test_the_constant_still_exists_and_is_where_we_think(self):
|
||||||
|
"""万一布局变了, 这条会提醒重新量坐标, 而不是让上面那条静静地失效。"""
|
||||||
|
self.assertEqual(cm.USE_LUNACY_TAB, (797, 283))
|
||||||
|
self.assertLess(cm.TAB_MODULES[0], cm.USE_LUNACY_TAB[0]) # Modules 在左
|
||||||
|
self.assertLess(cm.USE_LUNACY_TAB[0], cm.TAB_USE_BOXES[0]) # Use Boxes 在右
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user