|
| 1 | +"""巅峰对决特殊处理模块。 |
| 2 | +
|
| 3 | +提供巅峰对决相关的自定义识别功能,包括战斗力解析和对手选择逻辑。 |
| 4 | +""" |
| 5 | + |
| 6 | +from maa.agent.agent_server import AgentServer |
| 7 | +from maa.custom_recognition import CustomRecognition |
| 8 | +from maa.context import Context |
| 9 | + |
| 10 | +from agent.customs.utils import Prompter, CounterManager |
| 11 | +from agent.customs.maahelper import RecoHelper, ParamAnalyzer |
| 12 | + |
| 13 | + |
| 14 | +# ==================== 辅助函数 ==================== |
| 15 | + |
| 16 | + |
| 17 | +def parse_power(text: str) -> int: |
| 18 | + """解析战斗力文本。 |
| 19 | +
|
| 20 | + 从识别结果中提取战斗力数值,支持全角/半角符号处理。 |
| 21 | + OCR可能将千分位分隔符识别为逗号或小数点,均会被正确处理。 |
| 22 | +
|
| 23 | + 参数: |
| 24 | + text:包含战斗力信息的文本,格式如 "战斗力:12,345" 或 "战斗力:140.489" |
| 25 | +
|
| 26 | + 返回: |
| 27 | + 战斗力数值(整数)。如果解析失败则返回无穷大 |
| 28 | +
|
| 29 | + 示例: |
| 30 | + >>> parse_power("战斗力:12,345") |
| 31 | + 12345 |
| 32 | + >>> parse_power("战斗力:140.489") |
| 33 | + 140489 |
| 34 | + >>> parse_power("战斗力:10000") |
| 35 | + 10000 |
| 36 | + """ |
| 37 | + # 移除所有非数字字符(战斗力、冒号、逗号、小数点等) |
| 38 | + num_str = "".join(c for c in text if c.isdigit()) |
| 39 | + |
| 40 | + # 尝试转换为整数 |
| 41 | + try: |
| 42 | + return int(num_str) |
| 43 | + except ValueError: |
| 44 | + # 解析失败时返回无穷大,确保不会被选中 |
| 45 | + return float("inf") |
| 46 | + |
| 47 | + |
| 48 | +# ==================== 自定义识别类 ==================== |
| 49 | + |
| 50 | + |
| 51 | +@AgentServer.custom_recognition("pick_opponent") |
| 52 | +class PickOpponent(CustomRecognition): |
| 53 | + """选择对手识别器。 |
| 54 | +
|
| 55 | + 在巅峰对决中选择战斗力最低的对手。 |
| 56 | + 通过识别所有对手的战斗力,自动选择数值最小的目标。 |
| 57 | + """ |
| 58 | + |
| 59 | + def analyze( |
| 60 | + self, |
| 61 | + context: Context, |
| 62 | + argv: CustomRecognition.AnalyzeArg, |
| 63 | + ) -> CustomRecognition.AnalyzeResult: |
| 64 | + """执行对手选择分析。 |
| 65 | +
|
| 66 | + 识别所有可见对手的战斗力,并返回战斗力最低的目标位置。 |
| 67 | +
|
| 68 | + 参数: |
| 69 | + context:MaaFramework 上下文 |
| 70 | + argv:自定义识别参数 |
| 71 | +
|
| 72 | + 返回: |
| 73 | + 战斗力最低对手的识别结果,包含位置信息 |
| 74 | + """ |
| 75 | + try: |
| 76 | + # 识别所有对手的战斗力文本 |
| 77 | + rh = RecoHelper(context, argv).recognize("巅峰对决_识别战斗力") |
| 78 | + if rh.hit: |
| 79 | + results = rh.filtered_results |
| 80 | + # 选择战斗力最低的对手 |
| 81 | + min_result = min(results, key=lambda res: parse_power(res.text)) |
| 82 | + return RecoHelper.rt(min_result) |
| 83 | + |
| 84 | + return RecoHelper.NoResult |
| 85 | + except Exception as e: |
| 86 | + return Prompter.error("选择对手", e, reco_detail=True) |
0 commit comments