diff --git a/README.md b/README.md index 915a96b..e3f1b76 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,14 @@ codex mcp add limbus --env PYTHONUTF8=1 -- D:\lc-mcp\vendor\aalc\.venv\Scripts\p `超出最大尝试次数,未能进入战斗`)后仍会继续跑完并正常退出,所以 `task_status` 会把日志尾部的 `[ERROR]`/异常行放进 `errors` 字段:**看到 `succeeded` 也要先看 `errors`。** +### 实验:编队列表滑动补丁(默认关闭) + +AALC 基类允许适配器覆盖 `mouse_swipe_for_team_scroll`“以规避队伍拖拽判定”,上游只有 MuMu 实现了。 +`lc_mcp/aalc_input_patch.py` 在 Windows 前台/后台适配器上补了一个最小版本:几何与行程沿用 AALC 的 +`build_windows_scroll_swipe_plan`(不改变 Windows 翻页标定),脱离段后保持 0.1 秒、松手前停稳 0.5 秒。 +是否真有队伍被拖乱的问题尚无实机证据,所以默认不启用。验证方法:给常驻服务设置环境变量 +`LCP_TEAM_SCROLL_PATCH=1` 后重启服务,跑一次需要选队的日常任务,核对选中的队伍、编队人数以及 `task_status` 的 `errors`。 + ## 环境变量 | 变量 | 默认 | 说明 | @@ -96,6 +104,7 @@ codex mcp add limbus --env PYTHONUTF8=1 -- D:\lc-mcp\vendor\aalc\.venv\Scripts\p | `AALC_DIR` | `vendor/aalc` | 使用其他位置的 AALC 源码 | | `LCP_PORT` | 8765 | 服务/bridge 端口 | | `LCP_KEEP_SHOTS` | 30 | 截图保留张数 | +| `LCP_TEAM_SCROLL_PATCH` | 未设置 | 设为 `1` 启用实验性的编队列表滑动补丁 | ## 开发 / 测试 diff --git a/lc_mcp/aalc_engine.py b/lc_mcp/aalc_engine.py index fd2626f..108cb7a 100644 --- a/lc_mcp/aalc_engine.py +++ b/lc_mcp/aalc_engine.py @@ -43,6 +43,9 @@ def bootstrap(): from module.logger.my_log import Logger Logger() _booted = d + # 实验: 编队列表滑动补丁, 仅在 LCP_TEAM_SCROLL_PATCH=1 时安装 (见 aalc_input_patch) + from . import aalc_input_patch + aalc_input_patch.install() return d diff --git a/lc_mcp/aalc_input_patch.py b/lc_mcp/aalc_input_patch.py new file mode 100644 index 0000000..b925b13 --- /dev/null +++ b/lc_mcp/aalc_input_patch.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- +"""编队列表滑动补丁 (实验, 默认关闭; LCP_TEAM_SCROLL_PATCH=1 启用)。 + +AALC 基类注释说 ``mouse_swipe_for_team_scroll`` 可由适配器覆盖"以规避队伍拖拽判定", +上游只有 MuMu 覆盖了它, Windows 前台/后台沿用普通列表手势。是否因此误触发队伍拖拽 +尚无实机证据 (2026-10-03 的记录未在编队界面复测), 所以这里只做最小改动来验证: + +- 几何与行程完全沿用 AALC 的 ``build_windows_scroll_swipe_plan``, + 不改变 AALC 为 Windows 标定的翻页距离; +- 参照 MuMu, 脱离段后保持 0.1s 让游戏先认定为滚动, 松手前停稳 0.5s; +- 分段移动用显式等待驱动, 不经过适配器的 duration 参数 + (BackgroundInput 在 duration < 0.01s 时步数为 0, 不发任何移动事件), + 前台关闭 pyautogui 每次调用后默认的 0.1s 停顿。 +""" +import os +import time + +ENV_FLAG = 'LCP_TEAM_SCROLL_PATCH' +ESCAPE_HOLD_SECONDS = 0.1 # MUMU_TEAM_SCROLL_ESCAPE_HOLD_DURATION +SETTLE_SECONDS = 0.5 # MUMU_TEAM_SCROLL_SETTLE_DURATION +STEP_SECONDS = 0.01 # 与 AALC _mouse_move_to 的步长一致 + + +def _plan(x, y, dx, dy, duration): + from module.automation.input_handlers.scroll_swipe import build_windows_scroll_swipe_plan + plan, settle = build_windows_scroll_swipe_plan(x, y, dx, dy, duration) + return plan, max(settle, SETTLE_SECONDS) if len(plan) > 1 else 0 + + +def run_plan(plan, settle, press, move, release, sleep=None): + """按计划拖动: 每段按耗时均匀插值, 每个插值点之间显式等待。 + plan 元素为 (终点, 到达该点前的耗时); 第一段之后(脱离段)保持一小会儿。""" + sleep = sleep or time.sleep + position = plan[0][0] + press(position) + try: + for index, (point, duration) in enumerate(plan[1:]): + steps = max(1, round(duration / STEP_SECONDS)) + for k in range(1, steps + 1): + ratio = k / steps + move((position[0] + (point[0] - position[0]) * ratio, + position[1] + (point[1] - position[1]) * ratio)) + if duration > 0: + sleep(duration / steps) + position = point + if index == 0 and len(plan) > 2: + sleep(ESCAPE_HOLD_SECONDS) + if settle: + sleep(settle) + finally: + release(position) + + +def _foreground_team_scroll(self, x, y, duration=0.3, dx=0, dy=0, move_back=True): + import pyautogui + restore = self.get_mouse_position() if move_back else None + plan, settle = _plan(x, y, dx, dy, duration) + + def screen(point): + return tuple(int(round(v)) for v in self.pos_offset(*point)) + + def press(point): + pyautogui.moveTo(*screen(point), _pause=False) + pyautogui.mouseDown(_pause=False) + + run_plan(plan, settle, press, + lambda point: pyautogui.moveTo(*screen(point), _pause=False), + lambda point: pyautogui.mouseUp(*screen(point), _pause=False)) + if restore: + self.mouse_move(restore) + + +def _background_team_scroll(self, x, y, duration=0.3, dx=0, dy=0, move_back=True): + restore = self.get_mouse_position() if move_back else None + plan, settle = _plan(x, y, dx, dy, duration) + + def press(point): + self.set_mouse_pos(*point) + self.set_active() + self.mouse_down(*point) + + run_plan(plan, settle, press, + lambda point: self.set_mouse_pos(*point), + lambda point: self.mouse_up(*point)) + if restore: + self.mouse_move(restore) + + +def install(force=False): + """给 Windows 前台/后台适配器装上编队滑动 (幂等)。未设 LCP_TEAM_SCROLL_PATCH=1 时不做任何事。""" + if not force and os.environ.get(ENV_FLAG) != '1': + return [] + from module.automation.input_handlers import input as input_module + patched = [] + for name, impl in (('Input', _foreground_team_scroll), ('BackgroundInput', _background_team_scroll)): + cls = getattr(input_module, name, None) + if cls is not None and cls.__dict__.get('mouse_swipe_for_team_scroll') is not impl: + cls.mouse_swipe_for_team_scroll = impl + patched.append(name) + return patched diff --git a/tests/test_team_scroll_patch.py b/tests/test_team_scroll_patch.py new file mode 100644 index 0000000..5852ae5 --- /dev/null +++ b/tests/test_team_scroll_patch.py @@ -0,0 +1,152 @@ +# -*- coding: utf-8 -*- +"""编队滑动补丁: 几何用 AALC 真实的 scroll_swipe.py, 适配器/pyautogui 用记录调用的假对象。""" +import importlib.util +import os +import sys +import types +import unittest +from unittest import mock + +REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, REPO) + +from lc_mcp import aalc_input_patch as patch # noqa: E402 + +SCROLL_SWIPE = os.path.join(REPO, 'vendor', 'aalc', 'module', 'automation', 'input_handlers', 'scroll_swipe.py') + + +def aalc_modules(input_module=None): + """只装入 AALC 的 scroll_swipe (纯 math) 和可选的假 input 模块, 不触发 AALC 包初始化。""" + spec = importlib.util.spec_from_file_location('module.automation.input_handlers.scroll_swipe', SCROLL_SWIPE) + scroll_swipe = importlib.util.module_from_spec(spec) + spec.loader.exec_module(scroll_swipe) + mods = {name: types.ModuleType(name) for name in + ('module', 'module.automation', 'module.automation.input_handlers')} + for m in mods.values(): + m.__path__ = [] + handlers = mods['module.automation.input_handlers'] + handlers.scroll_swipe = scroll_swipe + mods['module.automation.input_handlers.scroll_swipe'] = scroll_swipe + if input_module is not None: + handlers.input = input_module + mods['module.automation.input_handlers.input'] = input_module + mods['module.automation'].input_handlers = handlers + mods['module'].automation = mods['module.automation'] + return mods, scroll_swipe + + +class FakeBackground: + def __init__(self): + self.calls = [] + + def get_mouse_position(self): + return (5, 5) + + def set_active(self): + self.calls.append(('active',)) + + def set_mouse_pos(self, x, y, **kwargs): + self.calls.append(('pos', x, y, kwargs)) + + def mouse_down(self, x, y): + self.calls.append(('down', x, y)) + + def mouse_up(self, x, y): + self.calls.append(('up', x, y)) + + def mouse_move(self, coordinate): + self.calls.append(('restore', coordinate)) + + +@unittest.skipUnless(os.path.isfile(SCROLL_SWIPE), 'AALC submodule unavailable') +class TeamScrollPatchTest(unittest.TestCase): + + def setUp(self): + self.mods, self.scroll_swipe = aalc_modules() + self.sleeps = [] + for p in (mock.patch.dict(sys.modules, self.mods), + mock.patch.object(patch.time, 'sleep', side_effect=self.sleeps.append)): + p.start() + self.addCleanup(p.stop) + + def test_background_keeps_aalc_geometry_and_actually_moves(self): + dy = -385 * 1080 / 1440 # AALC 按名称翻页的距离 + adapter = FakeBackground() + patch._background_team_scroll(adapter, 500, 600, duration=0.3, dy=dy) + moves = [c for c in adapter.calls if c[0] == 'pos'] + down = next(c for c in adapter.calls if c[0] == 'down') + up = next(c for c in adapter.calls if c[0] == 'up') + self.assertEqual(down[1:], (500, 600)) + self.assertAlmostEqual(up[2] - down[2], dy) # 行程与 AALC 的 Windows 标定一致 + self.assertGreater(len(moves), 50) # 原补丁在此为 0 个移动事件 + self.assertTrue(all(not c[3] for c in moves)) # 不经过适配器的 duration 参数 + plan, settle = self.scroll_swipe.build_windows_scroll_swipe_plan(500, 600, 0, dy, 0.3) + visited = {(round(c[1], 6), round(c[2], 6)) for c in moves} + for point, _ in plan: + self.assertIn((round(point[0], 6), round(point[1], 6)), visited) + self.assertIn(patch.ESCAPE_HOLD_SECONDS, self.sleeps) + self.assertEqual(self.sleeps[-1], patch.SETTLE_SECONDS) + moving = sum(d for _, d in plan) + self.assertAlmostEqual(sum(self.sleeps), moving + patch.ESCAPE_HOLD_SECONDS + patch.SETTLE_SECONDS) + self.assertEqual(adapter.calls[-1], ('restore', (5, 5))) + + def test_foreground_disables_pyautogui_pause(self): + calls = [] + fake = types.SimpleNamespace( + moveTo=lambda *a, **k: calls.append(('move', a, k)), + mouseDown=lambda *a, **k: calls.append(('down', a, k)), + mouseUp=lambda *a, **k: calls.append(('up', a, k))) + adapter = FakeBackground() + adapter.pos_offset = lambda x, y: (x + 100, y + 200) + with mock.patch.dict(sys.modules, {'pyautogui': fake}): + patch._foreground_team_scroll(adapter, 500, 600, duration=0.3, dy=-288, move_back=False) + self.assertTrue(all(k.get('_pause') is False for _, _, k in calls)) + self.assertEqual([c[0] for c in calls].count('down'), 1) + self.assertEqual(calls[-1][:2], ('up', (600, 512))) # 终点 (500, 312) 加窗口偏移 + # pyautogui 默认每次调用后停 0.1s; 关掉后总耗时只由计划决定 + self.assertLess(sum(self.sleeps), 2.0) + + def test_zero_distance_presses_and_releases_in_place(self): + adapter = FakeBackground() + patch._background_team_scroll(adapter, 10, 20, dy=0, move_back=False) + self.assertEqual([c[0] for c in adapter.calls], ['pos', 'active', 'down', 'up']) + self.assertEqual(self.sleeps, []) + + def test_release_happens_even_if_moving_fails(self): + adapter = FakeBackground() + adapter.set_mouse_pos = mock.Mock(side_effect=[None, RuntimeError('lost window')]) + with self.assertRaises(RuntimeError): + patch._background_team_scroll(adapter, 10, 20, dy=-200) + self.assertEqual(adapter.calls[-1][0], 'up') + + +class InstallTest(unittest.TestCase): + + def test_opt_in_and_idempotent(self): + class Input: + pass + + class BackgroundInput: + pass + + fake_input = types.ModuleType('module.automation.input_handlers.input') + fake_input.Input, fake_input.BackgroundInput = Input, BackgroundInput + mods = {name: types.ModuleType(name) for name in + ('module', 'module.automation', 'module.automation.input_handlers')} + for m in mods.values(): + m.__path__ = [] + mods['module.automation.input_handlers'].input = fake_input + mods['module.automation.input_handlers.input'] = fake_input + with mock.patch.dict(sys.modules, mods): + with mock.patch.dict(os.environ, {}, clear=True): + self.assertEqual(patch.install(), []) # 默认关闭 + self.assertFalse(hasattr(Input, 'mouse_swipe_for_team_scroll')) + with mock.patch.dict(os.environ, {'LCP_TEAM_SCROLL_PATCH': '1'}): + self.assertEqual(patch.install(), ['Input', 'BackgroundInput']) + self.assertIs(Input.mouse_swipe_for_team_scroll, patch._foreground_team_scroll) + self.assertIs(BackgroundInput.mouse_swipe_for_team_scroll, patch._background_team_scroll) + self.assertEqual(patch.install(), []) # 幂等 + + +if __name__ == '__main__': + unittest.main()