Fix background MCP input and add mirror dungeon feedback records
This commit is contained in:
+36
-2
@@ -125,11 +125,26 @@ class AalcEngine(Engine):
|
|||||||
def find_image(self, name, threshold=0.8, model='normal'):
|
def find_image(self, name, threshold=0.8, model='normal'):
|
||||||
if name not in self.list_images(name):
|
if name not in self.list_images(name):
|
||||||
raise ValueError('没有模板图 %r (用 list_images 查)' % name)
|
raise ValueError('没有模板图 %r (用 list_images 查)' % name)
|
||||||
self._grab(gray=True)
|
img = self._grab(gray=True)
|
||||||
|
# 模板按 2560x1440 制作。配置中的目标窗口尺寸不一定等于
|
||||||
|
# 当前客户区;临时按实际 16:9 画面缩放,不调整窗口或保存配置。
|
||||||
|
old_size = self.cfg.set_win_size
|
||||||
|
# 保留亚像素比例:例如 1278 宽对应 718.875 高,取整会改变
|
||||||
|
# 缩放采样,使小图标的匹配置信度明显下降。
|
||||||
|
match_size = max(1, min(img.height, img.width * 9 / 16))
|
||||||
|
changed = match_size != old_size
|
||||||
|
try:
|
||||||
|
if changed:
|
||||||
|
self.auto.clear_img_cache()
|
||||||
|
self.cfg.unsaved_set_value('set_win_size', match_size)
|
||||||
pos = self.auto.find_image_element(name, threshold, model=model)
|
pos = self.auto.find_image_element(name, threshold, model=model)
|
||||||
|
finally:
|
||||||
|
if changed:
|
||||||
|
self.cfg.unsaved_set_value('set_win_size', old_size)
|
||||||
|
self.auto.clear_img_cache()
|
||||||
return (int(pos[0]), int(pos[1])) if pos else None
|
return (int(pos[0]), int(pos[1])) if pos else None
|
||||||
|
|
||||||
# ---- 动手 (AALC 输入后端, 由 config.yaml win_input_type 决定: 默认后台 PostMessage) ----
|
# ---- 动手 (AALC 输入后端,由 config.yaml 决定后台消息/前台输入) ----
|
||||||
def click(self, x, y, times=1):
|
def click(self, x, y, times=1):
|
||||||
self.auto.mouse_click(int(x), int(y), times=int(times))
|
self.auto.mouse_click(int(x), int(y), times=int(times))
|
||||||
|
|
||||||
@@ -137,7 +152,26 @@ class AalcEngine(Engine):
|
|||||||
k = str(key).lower()
|
k = str(key).lower()
|
||||||
if k not in self.key_names:
|
if k not in self.key_names:
|
||||||
raise ValueError('AALC 不支持按键 %r, 可用: %s' % (key, ' '.join(self.key_names)))
|
raise ValueError('AALC 不支持按键 %r, 可用: %s' % (key, ' '.join(self.key_names)))
|
||||||
|
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
|
||||||
|
# Unity 可能在同一帧收到 down/up,导致快捷键被吞掉。
|
||||||
|
handler = self.auto.input_handler
|
||||||
|
self.auto.wait_pause()
|
||||||
|
handler.set_active()
|
||||||
|
handler.key_down(k)
|
||||||
|
try:
|
||||||
|
time.sleep(0.12)
|
||||||
|
finally:
|
||||||
|
handler.key_up(k)
|
||||||
|
else:
|
||||||
self.auto.key_press(k)
|
self.auto.key_press(k)
|
||||||
|
|
||||||
|
def drag(self, from_x, from_y, to_x, to_y, duration=1.0):
|
||||||
|
img = self._grab(gray=False)
|
||||||
|
for x, y in ((from_x, from_y), (to_x, to_y)):
|
||||||
|
if not (0 <= x < img.width and 0 <= y < img.height):
|
||||||
|
raise ValueError('拖动坐标超出游戏客户区 %sx%s' % (img.width, img.height))
|
||||||
|
self.auto.mouse_drag(int(from_x), int(from_y), drag_time=float(duration),
|
||||||
|
dx=int(to_x) - int(from_x), dy=int(to_y) - int(from_y))
|
||||||
|
|
||||||
def type_text(self, text):
|
def type_text(self, text):
|
||||||
self.auto.input_text(str(text))
|
self.auto.input_text(str(text))
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ def answer_offline(reason, stdin, stdout):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# The protocol is bytes, but Chinese diagnostics still use stderr text.
|
||||||
|
# Windows may otherwise encode errors as cp936 for a UTF-8 MCP client.
|
||||||
|
if hasattr(sys.stderr, 'reconfigure'):
|
||||||
|
sys.stderr.reconfigure(encoding='utf-8')
|
||||||
stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
|
stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
|
||||||
try:
|
try:
|
||||||
sock, rfile = connect()
|
sock, rfile = connect()
|
||||||
|
|||||||
@@ -54,5 +54,9 @@ class Engine:
|
|||||||
def keypress(self, key):
|
def keypress(self, key):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def drag(self, from_x, from_y, to_x, to_y, duration=1.0):
|
||||||
|
"""保持左键按下,以指定秒数移动到终点后松开。"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def type_text(self, text):
|
def type_text(self, text):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -33,5 +33,8 @@ class FakeEngine(Engine):
|
|||||||
def keypress(self, key):
|
def keypress(self, key):
|
||||||
self.actions.append(('key', key))
|
self.actions.append(('key', key))
|
||||||
|
|
||||||
|
def drag(self, from_x, from_y, to_x, to_y, duration=1.0):
|
||||||
|
self.actions.append(('drag', from_x, from_y, to_x, to_y, duration))
|
||||||
|
|
||||||
def type_text(self, text):
|
def type_text(self, text):
|
||||||
self.actions.append(('type', text))
|
self.actions.append(('type', text))
|
||||||
|
|||||||
+13
-1
@@ -1,6 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""MCP (JSON-RPC 2.0) 处理: 与传输方式无关, handle(msg) -> 响应 dict 或 None(通知)。"""
|
"""MCP (JSON-RPC 2.0) 处理: 与传输方式无关, handle(msg) -> 响应 dict 或 None(通知)。"""
|
||||||
import json
|
import json
|
||||||
|
import math
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ from .engine import find_hits
|
|||||||
|
|
||||||
MAX_WAIT = 300.0
|
MAX_WAIT = 300.0
|
||||||
WAIT_INTERVAL = 1.0
|
WAIT_INTERVAL = 1.0
|
||||||
INPUT_TOOLS = {'click', 'click_text', 'click_image', 'keypress', 'type_text'}
|
INPUT_TOOLS = {'click', 'click_text', 'click_image', 'keypress', 'type_text', 'drag'}
|
||||||
TASK_TOOLS = {'run_task', 'task_status', 'stop_task'}
|
TASK_TOOLS = {'run_task', 'task_status', 'stop_task'}
|
||||||
|
|
||||||
|
|
||||||
@@ -46,6 +47,10 @@ TOOLS = [
|
|||||||
'inputSchema': _obj({'x': _NUM, 'y': _NUM, 'times': {'type': 'number', 'default': 1}}, ['x', 'y'])},
|
'inputSchema': _obj({'x': _NUM, 'y': _NUM, 'times': {'type': 'number', 'default': 1}}, ['x', 'y'])},
|
||||||
{'name': 'keypress', 'description': '按一次键: a-z 0-9 enter esc space tab shift ctrl alt up down left right',
|
{'name': 'keypress', 'description': '按一次键: a-z 0-9 enter esc space tab shift ctrl alt up down left right',
|
||||||
'inputSchema': _obj({'key': _STR}, ['key'])},
|
'inputSchema': _obj({'key': _STR}, ['key'])},
|
||||||
|
{'name': 'drag', 'description': '在游戏客户区保持左键按下并缓慢拖动,适用于卡包下拉;duration 是移动秒数,0.1–10,默认 1。先 get_state 核对坐标',
|
||||||
|
'inputSchema': _obj({'from_x': _NUM, 'from_y': _NUM, 'to_x': _NUM, 'to_y': _NUM,
|
||||||
|
'duration': {'type': 'number', 'default': 1.0, 'minimum': 0.1, 'maximum': 10}},
|
||||||
|
['from_x', 'from_y', 'to_x', 'to_y'])},
|
||||||
{'name': 'type_text', 'description': '向游戏窗口输入文本', 'inputSchema': _obj({'text': _STR}, ['text'])},
|
{'name': 'type_text', 'description': '向游戏窗口输入文本', 'inputSchema': _obj({'text': _STR}, ['text'])},
|
||||||
{'name': 'run_task', 'description': (
|
{'name': 'run_task', 'description': (
|
||||||
'后台运行 AALC 现成的完整任务流程(会自动启动游戏、处理战斗/掉线重试)。立即返回, 用 task_status 看进度。'
|
'后台运行 AALC 现成的完整任务流程(会自动启动游戏、处理战斗/掉线重试)。立即返回, 用 task_status 看进度。'
|
||||||
@@ -157,6 +162,13 @@ class Server:
|
|||||||
if name == 'keypress':
|
if name == 'keypress':
|
||||||
e.keypress(str(a['key']))
|
e.keypress(str(a['key']))
|
||||||
return {'ok': True, 'key': str(a['key'])}
|
return {'ok': True, 'key': str(a['key'])}
|
||||||
|
if name == 'drag':
|
||||||
|
duration = float(a.get('duration', 1.0))
|
||||||
|
if not math.isfinite(duration) or not 0.1 <= duration <= 10:
|
||||||
|
raise ValueError('duration must be between 0.1 and 10 seconds')
|
||||||
|
points = [int(a[k]) for k in ('from_x', 'from_y', 'to_x', 'to_y')]
|
||||||
|
e.drag(*points, duration=duration)
|
||||||
|
return {'ok': True, 'from': points[:2], 'to': points[2:], 'duration': duration}
|
||||||
if name == 'type_text':
|
if name == 'type_text':
|
||||||
e.type_text(str(a['text']))
|
e.type_text(str(a['text']))
|
||||||
return {'ok': True, 'typed': str(a['text'])}
|
return {'ok': True, 'typed': str(a['text'])}
|
||||||
|
|||||||
+34
-3
@@ -74,8 +74,8 @@ def serve_lines(server, readline, write):
|
|||||||
write(json.dumps(resp, ensure_ascii=False) + '\n')
|
write(json.dumps(resp, ensure_ascii=False) + '\n')
|
||||||
|
|
||||||
|
|
||||||
def run_stdio(server):
|
def run_stdio(server, out=None):
|
||||||
out = sys.stdout
|
out = sys.stdout if out is None else out
|
||||||
sys.stdout = sys.stderr # AALC 或第三方库的 print 不能混进协议流
|
sys.stdout = sys.stderr # AALC 或第三方库的 print 不能混进协议流
|
||||||
|
|
||||||
def write(s):
|
def write(s):
|
||||||
@@ -87,6 +87,13 @@ def run_stdio(server):
|
|||||||
class _Handler(socketserver.StreamRequestHandler):
|
class _Handler(socketserver.StreamRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
try:
|
||||||
|
self._handle_session()
|
||||||
|
except (ConnectionResetError, BrokenPipeError):
|
||||||
|
# MCP 客户端关闭/重载时可能直接断开 TCP,不是服务故障。
|
||||||
|
return
|
||||||
|
|
||||||
|
def _handle_session(self):
|
||||||
rfile, wfile = self.rfile, self.wfile
|
rfile, wfile = self.rfile, self.wfile
|
||||||
first = rfile.readline(256).decode('utf-8', 'replace').strip()
|
first = rfile.readline(256).decode('utf-8', 'replace').strip()
|
||||||
if not (first.startswith('AUTH ') and hmac.compare_digest(first[5:], self.server.token)):
|
if not (first.startswith('AUTH ') and hmac.compare_digest(first[5:], self.server.token)):
|
||||||
@@ -122,9 +129,21 @@ def main(argv=None):
|
|||||||
ap.add_argument('--host', default='127.0.0.1')
|
ap.add_argument('--host', default='127.0.0.1')
|
||||||
ap.add_argument('--port', type=int, default=DEFAULT_PORT)
|
ap.add_argument('--port', type=int, default=DEFAULT_PORT)
|
||||||
args = ap.parse_args(argv)
|
args = ap.parse_args(argv)
|
||||||
|
# pythonw.exe 没有标准流;登录任务仍需要可写的日志流。
|
||||||
|
_configure_streams()
|
||||||
|
protocol_out = None
|
||||||
|
if args.stdio:
|
||||||
|
if sys.stdin is None:
|
||||||
|
raise RuntimeError('--stdio requires python.exe, not pythonw.exe')
|
||||||
|
for stream in (sys.stdin, sys.stdout, sys.stderr):
|
||||||
|
if hasattr(stream, 'reconfigure'):
|
||||||
|
stream.reconfigure(encoding='utf-8')
|
||||||
|
protocol_out = sys.stdout
|
||||||
|
# AALC 导入期间也会输出,不能等引擎初始化后才隔离 stdout。
|
||||||
|
sys.stdout = sys.stderr
|
||||||
mcp = build_server(fake=args.fake)
|
mcp = build_server(fake=args.fake)
|
||||||
if args.stdio:
|
if args.stdio:
|
||||||
return run_stdio(mcp)
|
return run_stdio(mcp, protocol_out)
|
||||||
if args.host not in ('127.0.0.1', '::1', 'localhost'):
|
if args.host not in ('127.0.0.1', '::1', 'localhost'):
|
||||||
log('WARNING: 监听 %s 会把游戏控制权暴露到网络上, 建议走 SSH 隧道' % args.host)
|
log('WARNING: 监听 %s 会把游戏控制权暴露到网络上, 建议走 SSH 隧道' % args.host)
|
||||||
srv = make_tcp(mcp, args.host, args.port, load_token(create=True))
|
srv = make_tcp(mcp, args.host, args.port, load_token(create=True))
|
||||||
@@ -132,5 +151,17 @@ def main(argv=None):
|
|||||||
srv.serve_forever()
|
srv.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
|
def _configure_streams():
|
||||||
|
if sys.stdout is not None and sys.stderr is not None:
|
||||||
|
return None
|
||||||
|
os.makedirs(HOME, exist_ok=True)
|
||||||
|
stream = open(os.path.join(HOME, 'server.log'), 'a', encoding='utf-8', buffering=1)
|
||||||
|
if sys.stderr is None:
|
||||||
|
sys.stderr = stream
|
||||||
|
if sys.stdout is None:
|
||||||
|
sys.stdout = stream
|
||||||
|
return stream
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user