Skip to content

Commit a5f9933

Browse files
authored
feat: add get_campaign_remains (#483)
1 parent 6c6bcbc commit a5f9933

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

autowsgr/ui/map/data.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ def parse_map_title(text: str) -> MapIdentity | None:
315315
CLICK_DIFFICULTY: tuple[float, float] = (0.800, 0.130)
316316
"""切换难度。"""
317317

318+
CAMPAIGN_REMAIN_CROP: tuple[float, float, float, float] = (0.08, 0.82, 0.25, 0.92)
319+
"""战役剩余次数 OCR 裁剪区域 (第一列战役的底部文本区)。"""
320+
318321

319322
# ── 出征地图节点切换 ──
320323

autowsgr/ui/map/panels/campaign.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import re
56
import time
67
from dataclasses import dataclass
78
from typing import TYPE_CHECKING
@@ -11,6 +12,7 @@
1112
from autowsgr.ui.map.base import BaseMapPage
1213
from autowsgr.ui.map.data import (
1314
CAMPAIGN_POSITIONS,
15+
CAMPAIGN_REMAIN_CROP,
1416
CLICK_DIFFICULTY,
1517
DIFFICULTY_EASY_COLOR,
1618
DIFFICULTY_HARD_COLOR,
@@ -38,6 +40,36 @@ class CampaignPanelMixin(BaseMapPage):
3840

3941
# ── 查询 ─────────────────────────────────────────────────────────────
4042

43+
def get_campaign_remains(self) -> tuple[int, int] | None:
44+
"""获取战役剩余次数。
45+
46+
Returns
47+
-------
48+
tuple[int, int] | None
49+
返回 (剩余次数, 总次数),如 (0, 8)。如果识别失败返回 None。
50+
"""
51+
self.ensure_panel(MapPanel.BATTLE)
52+
time.sleep(0.5)
53+
screen = self._ctrl.screenshot()
54+
img = PixelChecker.crop(screen, *CAMPAIGN_REMAIN_CROP)
55+
56+
ocr = self._ocr
57+
if ocr is None:
58+
_log.warning('[UI] 无法获取战役次数: 未提供 OCR 引擎')
59+
return None
60+
61+
results = ocr.recognize(img)
62+
text = ''.join([r.text for r in results]).replace(' ', '')
63+
m = re.search(r'(\d+)[/|](\d+)', text)
64+
if m:
65+
remains = int(m.group(1))
66+
total = int(m.group(2))
67+
_log.info(f'[UI] 战役次数识别成功: {remains}/{total}')
68+
return remains, total
69+
70+
_log.warning(f'[UI] 战役次数识别失败: OCR 结果未匹配 ({text})')
71+
return None
72+
4173
def recognize_difficulty(self) -> str | None:
4274
"""通过检测难度按钮颜色识别当前难度。
4375

0 commit comments

Comments
 (0)