63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""引擎接口 + 平台无关的文字匹配。
|
|
|
|
引擎 = "看屏幕 + 动手" 的最小集合, 坐标一律是游戏窗口客户区截图里的像素坐标。
|
|
实现: AalcEngine(Windows, 调 AALC) / FakeEngine(测试)。
|
|
"""
|
|
import re
|
|
|
|
_WS = re.compile(r'\s+')
|
|
|
|
|
|
def norm(s):
|
|
"""匹配用归一化: 去全部空白 + 小写 (OCR 常把 "Hong Lu" 识别成 "HongLu" 或反之)。"""
|
|
return _WS.sub('', str(s)).lower()
|
|
|
|
|
|
def find_hits(text, items):
|
|
"""在 OCR items 里找包含 text 的项, 返回中心点列表。完全相等的排前面。"""
|
|
key = norm(text)
|
|
if not key:
|
|
return []
|
|
exact, partial = [], []
|
|
for it in items:
|
|
t = norm(it['text'])
|
|
if key not in t:
|
|
continue
|
|
hit = {'text': it['text'], 'x': it['x'] + it['w'] // 2, 'y': it['y'] + it['h'] // 2}
|
|
(exact if t == key else partial).append(hit)
|
|
return exact + partial
|
|
|
|
|
|
class Engine:
|
|
"""所有方法失败时抛异常, 由协议层转成 isError 结果。"""
|
|
|
|
def screenshot(self):
|
|
"""截图并落盘。返回 {'path': str, 'w': int, 'h': int}。"""
|
|
raise NotImplementedError
|
|
|
|
def ocr(self):
|
|
"""截图 + OCR。返回 (shot, items); items = [{text, x, y, w, h, score}]。"""
|
|
raise NotImplementedError
|
|
|
|
def find_image(self, name, threshold=0.8, model='normal'):
|
|
"""在当前画面找 AALC 模板图 (如 'battle/turn_assets.png')。返回 (x, y) 或 None。"""
|
|
raise NotImplementedError
|
|
|
|
def list_images(self, prefix=''):
|
|
"""列出可用模板图的相对路径。"""
|
|
raise NotImplementedError
|
|
|
|
def click(self, x, y, times=1):
|
|
raise NotImplementedError
|
|
|
|
def keypress(self, key):
|
|
raise NotImplementedError
|
|
|
|
def drag(self, from_x, from_y, to_x, to_y, duration=1.0):
|
|
"""保持左键按下,以指定秒数移动到终点后松开。"""
|
|
raise NotImplementedError
|
|
|
|
def type_text(self, text):
|
|
raise NotImplementedError
|