aram-mayhem-dsh 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.zh-CN.txt +61 -54
- package/LICENSE +27 -27
- package/README.md +234 -194
- package/THIRD_PARTY_NOTICES.md +50 -50
- package/client.js +973 -829
- package/config/settings.json +57 -57
- package/cordis.patch.yml +5 -6
- package/data/README.md +9 -9
- package/docs/DETAIL.md +3 -3
- package/docs/DETAIL.zh.md +3 -3
- package/docs/preview.svg +63 -63
- package/icon.svg +23 -23
- package/index.js +544 -439
- package/install.ps1 +63 -63
- package/locale/en.json +7 -7
- package/locale/zh.json +7 -7
- package/package.json +72 -72
- package/requirements-core.txt +6 -6
- package/requirements-dsh.txt +8 -8
- package/scripts/desktop_overlay.py +1305 -1305
- package/scripts/dsh_service.py +999 -999
- package/src/__init__.py +3 -3
- package/src/app.py +462 -462
- package/src/core/__init__.py +1 -1
- package/src/core/event_bus.py +56 -56
- package/src/core/events.py +28 -28
- package/src/core/hotkeys.py +166 -166
- package/src/core/logging_setup.py +44 -44
- package/src/core/paths.py +44 -44
- package/src/core/settings.py +271 -271
- package/src/data/__init__.py +1 -1
- package/src/data/fetcher.py +239 -239
- package/src/data/matcher.py +534 -534
- package/src/data/models.py +287 -287
- package/src/data/schema.py +128 -128
- package/src/data/scoring.py +127 -127
- package/src/data/store.py +543 -543
- package/src/lcu/__init__.py +1 -1
- package/src/lcu/connector.py +316 -316
- package/src/lcu/listener.py +507 -507
- package/src/main.py +76 -76
- package/src/ui/__init__.py +1 -1
- package/src/ui/augment_panel.py +276 -276
- package/src/ui/build_panel.py +319 -319
- package/src/ui/champ_panel.py +236 -236
- package/src/ui/overlay_window.py +631 -631
- package/src/ui/theme.py +298 -298
- package/src/ui/widgets.py +408 -408
- package/src/vision/__init__.py +1 -1
- package/src/vision/capture.py +249 -249
- package/src/vision/ocr_engine.py +122 -122
- package/src/vision/scanner.py +272 -272
|
@@ -1,1305 +1,1305 @@
|
|
|
1
|
-
"""Always-on-top desktop icon for the DSH ARAM: Mayhem plugin.
|
|
2
|
-
|
|
3
|
-
Collapsed: a 68 px hex icon. Click to expand into a compact overlay with
|
|
4
|
-
champion, recommended augments and build data. The window is independent of
|
|
5
|
-
the DSH page; it polls the local service at 127.0.0.1:8123.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from __future__ import annotations
|
|
9
|
-
|
|
10
|
-
import argparse
|
|
11
|
-
import ctypes
|
|
12
|
-
import json
|
|
13
|
-
import logging
|
|
14
|
-
import math
|
|
15
|
-
import os
|
|
16
|
-
import sys
|
|
17
|
-
import time
|
|
18
|
-
from pathlib import Path
|
|
19
|
-
from typing import Any
|
|
20
|
-
from urllib.parse import quote
|
|
21
|
-
|
|
22
|
-
from PySide6.QtCore import QEvent, QLockFile, QPoint, QRectF, Qt, QTimer, QUrl, Signal
|
|
23
|
-
from PySide6.QtGui import (
|
|
24
|
-
QColor,
|
|
25
|
-
QFont,
|
|
26
|
-
QIcon,
|
|
27
|
-
QLinearGradient,
|
|
28
|
-
QPainter,
|
|
29
|
-
QPainterPath,
|
|
30
|
-
QPen,
|
|
31
|
-
QPixmap,
|
|
32
|
-
)
|
|
33
|
-
from PySide6.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
|
|
34
|
-
from PySide6.QtWidgets import (
|
|
35
|
-
QApplication,
|
|
36
|
-
QButtonGroup,
|
|
37
|
-
QFrame,
|
|
38
|
-
QGraphicsDropShadowEffect,
|
|
39
|
-
QHBoxLayout,
|
|
40
|
-
QLabel,
|
|
41
|
-
QLineEdit,
|
|
42
|
-
QPushButton,
|
|
43
|
-
QScrollArea,
|
|
44
|
-
QStackedWidget,
|
|
45
|
-
QVBoxLayout,
|
|
46
|
-
QWidget,
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
ROOT = Path(__file__).resolve().parents[1]
|
|
50
|
-
if str(ROOT) not in sys.path:
|
|
51
|
-
sys.path.insert(0, str(ROOT))
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def _enable_dpi_awareness() -> None:
|
|
55
|
-
"""DPI-aware coordinates without importing the optional OCR/Pillow stack."""
|
|
56
|
-
if os.name != "nt":
|
|
57
|
-
return
|
|
58
|
-
try:
|
|
59
|
-
ctypes.windll.shcore.SetProcessDpiAwareness(2)
|
|
60
|
-
except (AttributeError, OSError):
|
|
61
|
-
try:
|
|
62
|
-
ctypes.windll.user32.SetProcessDPIAware()
|
|
63
|
-
except (AttributeError, OSError):
|
|
64
|
-
pass
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
log = logging.getLogger("aram-overlay")
|
|
68
|
-
|
|
69
|
-
TIER_COLORS = {"T0": "#D9B86C", "T1": "#5BD2C0", "T2": "#7FA6D9", "T3": "#8895A8"}
|
|
70
|
-
TIER_BG = {"T0": "#2A2315", "T1": "#112924", "T2": "#152238", "T3": "#1B2330"}
|
|
71
|
-
RARITY = {
|
|
72
|
-
"prismatic": {
|
|
73
|
-
"fill": "qlineargradient(x1:0,y1:0,x2:1,y2:0, stop:0 #5CE1E6, stop:0.5 #B388FF, stop:1 #FFD166)",
|
|
74
|
-
"color": "#1A1408",
|
|
75
|
-
"border": "#F0D79A",
|
|
76
|
-
},
|
|
77
|
-
"gold": {"fill": "#2A2315", "color": "#E8C77A", "border": "#C8AA6E"},
|
|
78
|
-
"silver": {"fill": "#202733", "color": "#D7DEE8", "border": "#AEBACB"},
|
|
79
|
-
}
|
|
80
|
-
COLORS = {
|
|
81
|
-
"bg": "#0A111D",
|
|
82
|
-
"surface": "#111C2C",
|
|
83
|
-
"surface2": "#17243A",
|
|
84
|
-
"border": "#263A55",
|
|
85
|
-
"text": "#E8F0FB",
|
|
86
|
-
"muted": "#8CA2BF",
|
|
87
|
-
"gold": "#D9B86C",
|
|
88
|
-
"gold2": "#F0D79A",
|
|
89
|
-
"teal": "#5BD2C0",
|
|
90
|
-
"red": "#FF7185",
|
|
91
|
-
"green": "#62D19A",
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
STYLE = f"""
|
|
95
|
-
* {{
|
|
96
|
-
font-family: "Microsoft YaHei UI", "Microsoft YaHei", "Segoe UI", sans-serif;
|
|
97
|
-
color: {COLORS["text"]};
|
|
98
|
-
}}
|
|
99
|
-
QWidget#Root {{ background: transparent; }}
|
|
100
|
-
QFrame#Shell {{
|
|
101
|
-
background: qlineargradient(x1:0,y1:0,x2:1,y2:1,
|
|
102
|
-
stop:0 #0D1727, stop:0.55 #0B1422, stop:1 #101B2C);
|
|
103
|
-
border: 1px solid #2A405E;
|
|
104
|
-
border-radius: 16px;
|
|
105
|
-
}}
|
|
106
|
-
QFrame#Card {{
|
|
107
|
-
background: #111C2C;
|
|
108
|
-
border: 1px solid #1E2F47;
|
|
109
|
-
border-radius: 11px;
|
|
110
|
-
}}
|
|
111
|
-
QFrame#Row {{
|
|
112
|
-
background: #101B2A;
|
|
113
|
-
border: 1px solid #1C2C43;
|
|
114
|
-
border-radius: 9px;
|
|
115
|
-
}}
|
|
116
|
-
QFrame#Row:hover {{
|
|
117
|
-
background: #142238;
|
|
118
|
-
border: 1px solid #2A4262;
|
|
119
|
-
}}
|
|
120
|
-
QLabel#Title {{ font-size: 16px; font-weight: 700; }}
|
|
121
|
-
QLabel#Sub {{ font-size: 9px; color: {COLORS["muted"]}; }}
|
|
122
|
-
QLabel#Muted {{ color: {COLORS["muted"]}; font-size: 10px; }}
|
|
123
|
-
QLabel#HeroName {{ font-size: 18px; font-weight: 700; }}
|
|
124
|
-
QLabel#HeroTitle {{ font-size: 10px; color: {COLORS["muted"]}; }}
|
|
125
|
-
QLabel#BigRate {{ font-size: 24px; font-weight: 700; color: {COLORS["gold2"]}; }}
|
|
126
|
-
QLabel#Section {{ font-size: 12px; font-weight: 700; }}
|
|
127
|
-
QLabel#Toast {{
|
|
128
|
-
background: #1B2A40; border: 1px solid #3A567A; border-radius: 7px;
|
|
129
|
-
padding: 6px 9px; font-size: 10px;
|
|
130
|
-
}}
|
|
131
|
-
QPushButton#Tab {{
|
|
132
|
-
background: transparent; border: none;
|
|
133
|
-
border-bottom: 2px solid transparent;
|
|
134
|
-
color: {COLORS["muted"]}; padding: 7px 10px 6px; font-size: 12px;
|
|
135
|
-
}}
|
|
136
|
-
QPushButton#Tab:checked {{
|
|
137
|
-
color: {COLORS["gold2"]}; border-bottom: 2px solid {COLORS["gold"]};
|
|
138
|
-
}}
|
|
139
|
-
QPushButton#Icon {{
|
|
140
|
-
background: transparent; border: 1px solid transparent;
|
|
141
|
-
border-radius: 7px; color: {COLORS["muted"]}; font-size: 14px; font-weight: 700;
|
|
142
|
-
}}
|
|
143
|
-
QPushButton#Icon:hover {{ background: #1B2A40; border: 1px solid #2A405E; }}
|
|
144
|
-
QPushButton#Gold {{
|
|
145
|
-
background: qlineargradient(x1:0,y1:0,x2:1,y2:0, stop:0 #D9B86C, stop:1 #E8CF96);
|
|
146
|
-
border: none; border-radius: 8px; color: #1B160A;
|
|
147
|
-
font-size: 11px; font-weight: 700; padding: 7px 12px;
|
|
148
|
-
}}
|
|
149
|
-
QPushButton#Ghost {{
|
|
150
|
-
background: #152236; border: 1px solid #2A405E; border-radius: 8px;
|
|
151
|
-
color: {COLORS["text"]}; font-size: 11px; padding: 6px 10px;
|
|
152
|
-
}}
|
|
153
|
-
QPushButton#Link {{
|
|
154
|
-
background: transparent; border: none; color: {COLORS["muted"]};
|
|
155
|
-
font-size: 10px; padding: 0; text-align: left;
|
|
156
|
-
}}
|
|
157
|
-
QLineEdit {{
|
|
158
|
-
background: #101B2A; border: 1px solid #2A405E; border-radius: 7px;
|
|
159
|
-
padding: 5px 8px; font-size: 11px; color: {COLORS["text"]};
|
|
160
|
-
}}
|
|
161
|
-
QCheckBox {{ font-size: 10px; color: {COLORS["muted"]}; spacing: 4px; }}
|
|
162
|
-
QScrollArea {{ background: transparent; border: none; }}
|
|
163
|
-
QScrollArea > QWidget > QWidget {{ background: transparent; }}
|
|
164
|
-
QScrollBar:vertical {{ background: transparent; width: 6px; margin: 2px; }}
|
|
165
|
-
QScrollBar::handle:vertical {{ background: #2B405D; min-height: 24px; border-radius: 3px; }}
|
|
166
|
-
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{ height: 0; }}
|
|
167
|
-
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{ background: transparent; }}
|
|
168
|
-
QToolTip {{
|
|
169
|
-
background: #152236; color: {COLORS["text"]}; border: 1px solid #345071;
|
|
170
|
-
padding: 4px 6px; font-size: 10px;
|
|
171
|
-
}}
|
|
172
|
-
"""
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
def _pct(value: Any, digits: int = 1) -> str:
|
|
176
|
-
try:
|
|
177
|
-
return f"{float(value) * 100:.{digits}f}%"
|
|
178
|
-
except (TypeError, ValueError):
|
|
179
|
-
return "--"
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
def _num(value: Any) -> str:
|
|
183
|
-
try:
|
|
184
|
-
return f"{int(value):,}"
|
|
185
|
-
except (TypeError, ValueError):
|
|
186
|
-
return "--"
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
def _clear(layout: QVBoxLayout) -> None:
|
|
190
|
-
while layout.count():
|
|
191
|
-
item = layout.takeAt(0)
|
|
192
|
-
if item is None:
|
|
193
|
-
continue
|
|
194
|
-
widget = item.widget()
|
|
195
|
-
if widget is not None:
|
|
196
|
-
widget.deleteLater()
|
|
197
|
-
child = item.layout()
|
|
198
|
-
if child is not None:
|
|
199
|
-
_clear(child)
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
def _label(text: str, name: str = "Muted", *, wrap: bool = False, align: Any = None) -> QLabel:
|
|
203
|
-
label = QLabel(str(text))
|
|
204
|
-
label.setObjectName(name)
|
|
205
|
-
label.setWordWrap(wrap)
|
|
206
|
-
if align is not None:
|
|
207
|
-
label.setAlignment(align)
|
|
208
|
-
return label
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
def _tier_badge(tier: str) -> QLabel:
|
|
212
|
-
color = TIER_COLORS.get(tier, TIER_COLORS["T2"])
|
|
213
|
-
bg = TIER_BG.get(tier, TIER_BG["T2"])
|
|
214
|
-
badge = QLabel(tier or "T2")
|
|
215
|
-
badge.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
216
|
-
badge.setFixedHeight(17)
|
|
217
|
-
badge.setStyleSheet(
|
|
218
|
-
f"background: {bg}; color: {color}; border: 1px solid {color}66; "
|
|
219
|
-
"border-radius: 4px; font-size: 9px; font-weight: 700; padding: 1px 4px;"
|
|
220
|
-
)
|
|
221
|
-
return badge
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
def _chip(text: str, color: str | None = None) -> QLabel:
|
|
225
|
-
chip = QLabel(str(text))
|
|
226
|
-
color = color or COLORS["muted"]
|
|
227
|
-
chip.setStyleSheet(
|
|
228
|
-
f"background: {color}14; color: {color}; border: 1px solid {color}44; "
|
|
229
|
-
"border-radius: 4px; font-size: 9px; padding: 1px 5px;"
|
|
230
|
-
)
|
|
231
|
-
return chip
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
def _rarity_badge(rarity: str, label: str) -> QLabel:
|
|
235
|
-
"""In-game augment rarity accents: silver, gold, prismatic."""
|
|
236
|
-
theme = RARITY.get(str(rarity), RARITY["gold"])
|
|
237
|
-
badge = QLabel(label or str(rarity or "?"))
|
|
238
|
-
badge.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
239
|
-
badge.setStyleSheet(
|
|
240
|
-
f"background: {theme['fill']}; color: {theme['color']}; "
|
|
241
|
-
f"border: 1px solid {theme['border']}; border-radius: 4px; "
|
|
242
|
-
"font-size: 9px; font-weight: 700; padding: 1px 5px;"
|
|
243
|
-
)
|
|
244
|
-
return badge
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
def _avatar(name: str, tier: str = "T2", size: int = 40) -> QLabel:
|
|
248
|
-
palette = ["#365E7A", "#64518F", "#8B6038", "#34736B", "#7D4E60"]
|
|
249
|
-
color = palette[sum(ord(ch) for ch in (name or "?")) % len(palette)]
|
|
250
|
-
label = QLabel((name or "?")[:1])
|
|
251
|
-
label.setFixedSize(size, size)
|
|
252
|
-
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
253
|
-
label.setStyleSheet(
|
|
254
|
-
f"background: {color}; color: white; border: 2px solid {TIER_COLORS.get(tier, '#7FA6D9')}; "
|
|
255
|
-
f"border-radius: {size // 2}px; font-size: {max(12, int(size * 0.38))}px; font-weight: 700;"
|
|
256
|
-
)
|
|
257
|
-
return label
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
class DragHeader(QFrame):
|
|
261
|
-
def __init__(self, window: FloatingOverlay) -> None:
|
|
262
|
-
super().__init__()
|
|
263
|
-
self._window = window
|
|
264
|
-
self._origin: QPoint | None = None
|
|
265
|
-
self._window_origin: QPoint | None = None
|
|
266
|
-
|
|
267
|
-
def mousePressEvent(self, event):
|
|
268
|
-
if event.button() == Qt.MouseButton.LeftButton:
|
|
269
|
-
self._origin = event.globalPosition().toPoint()
|
|
270
|
-
self._window_origin = self._window.pos()
|
|
271
|
-
super().mousePressEvent(event)
|
|
272
|
-
|
|
273
|
-
def mouseMoveEvent(self, event):
|
|
274
|
-
if self._origin is not None and event.buttons() & Qt.MouseButton.LeftButton:
|
|
275
|
-
self._window.move(self._window_origin + event.globalPosition().toPoint() - self._origin)
|
|
276
|
-
super().mouseMoveEvent(event)
|
|
277
|
-
|
|
278
|
-
def mouseReleaseEvent(self, event):
|
|
279
|
-
self._origin = None
|
|
280
|
-
self._window_origin = None
|
|
281
|
-
super().mouseReleaseEvent(event)
|
|
282
|
-
|
|
283
|
-
def mouseDoubleClickEvent(self, event):
|
|
284
|
-
if event.button() == Qt.MouseButton.LeftButton:
|
|
285
|
-
self._window.collapse()
|
|
286
|
-
super().mouseDoubleClickEvent(event)
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
class IconWidget(QWidget):
|
|
290
|
-
clicked = Signal()
|
|
291
|
-
|
|
292
|
-
def __init__(self, overlay: FloatingOverlay) -> None:
|
|
293
|
-
super().__init__()
|
|
294
|
-
self._overlay = overlay
|
|
295
|
-
self._press: QPoint | None = None
|
|
296
|
-
self._origin: QPoint | None = None
|
|
297
|
-
self._moved = False
|
|
298
|
-
self.setFixedSize(68, 68)
|
|
299
|
-
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
300
|
-
self.setToolTip("海斗助手 · 点击展开")
|
|
301
|
-
|
|
302
|
-
def set_status(self, connected: bool, phase: str = "") -> None:
|
|
303
|
-
self._connected = connected
|
|
304
|
-
self._phase = phase
|
|
305
|
-
self.update()
|
|
306
|
-
|
|
307
|
-
def paintEvent(self, event):
|
|
308
|
-
painter = QPainter(self)
|
|
309
|
-
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
310
|
-
rect = QRectF(5, 5, 58, 58)
|
|
311
|
-
path = QPainterPath()
|
|
312
|
-
cx, cy, radius = rect.center().x(), rect.center().y(), 27
|
|
313
|
-
for index in range(6):
|
|
314
|
-
angle = math.radians(60 * index - 30)
|
|
315
|
-
point = QPoint(int(cx + radius * math.cos(angle)), int(cy + radius * math.sin(angle)))
|
|
316
|
-
if index == 0:
|
|
317
|
-
path.moveTo(point)
|
|
318
|
-
else:
|
|
319
|
-
path.lineTo(point)
|
|
320
|
-
path.closeSubpath()
|
|
321
|
-
gradient = QLinearGradient(rect.topLeft(), rect.bottomRight())
|
|
322
|
-
gradient.setColorAt(0, QColor("#E9D39A"))
|
|
323
|
-
gradient.setColorAt(1, QColor("#A87A32"))
|
|
324
|
-
painter.setPen(QPen(QColor("#3A2D16"), 1.5))
|
|
325
|
-
painter.setBrush(gradient)
|
|
326
|
-
painter.drawPath(path)
|
|
327
|
-
painter.setPen(QColor("#1A1408"))
|
|
328
|
-
font = QFont(self.font())
|
|
329
|
-
font.setPixelSize(24)
|
|
330
|
-
font.setWeight(QFont.Weight.Bold)
|
|
331
|
-
painter.setFont(font)
|
|
332
|
-
painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, "海")
|
|
333
|
-
connected = getattr(self, "_connected", False)
|
|
334
|
-
painter.setPen(QPen(QColor("#0A111D"), 2))
|
|
335
|
-
painter.setBrush(QColor(COLORS["green"] if connected else COLORS["red"]))
|
|
336
|
-
painter.drawEllipse(QRectF(50, 50, 12, 12))
|
|
337
|
-
painter.end()
|
|
338
|
-
|
|
339
|
-
def mousePressEvent(self, event):
|
|
340
|
-
if event.button() == Qt.MouseButton.LeftButton:
|
|
341
|
-
self._press = event.globalPosition().toPoint()
|
|
342
|
-
self._origin = self._overlay.pos()
|
|
343
|
-
self._moved = False
|
|
344
|
-
super().mousePressEvent(event)
|
|
345
|
-
|
|
346
|
-
def mouseMoveEvent(self, event):
|
|
347
|
-
if self._press is not None and event.buttons() & Qt.MouseButton.LeftButton:
|
|
348
|
-
delta = event.globalPosition().toPoint() - self._press
|
|
349
|
-
if abs(delta.x()) + abs(delta.y()) > 3:
|
|
350
|
-
self._moved = True
|
|
351
|
-
self._overlay.move(self._origin + delta)
|
|
352
|
-
super().mouseMoveEvent(event)
|
|
353
|
-
|
|
354
|
-
def mouseReleaseEvent(self, event):
|
|
355
|
-
if event.button() == Qt.MouseButton.LeftButton:
|
|
356
|
-
if not self._moved:
|
|
357
|
-
self.clicked.emit()
|
|
358
|
-
self._press = None
|
|
359
|
-
super().mouseReleaseEvent(event)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
class BenchRow(QFrame):
|
|
363
|
-
clicked = Signal(int)
|
|
364
|
-
|
|
365
|
-
def __init__(self, summary: dict[str, Any], my: dict[str, Any] | None, active: bool) -> None:
|
|
366
|
-
super().__init__()
|
|
367
|
-
self._cid = int(summary["champion"]["id"])
|
|
368
|
-
self.setObjectName("Row")
|
|
369
|
-
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
370
|
-
if active:
|
|
371
|
-
self.setStyleSheet("QFrame#Row { border: 1px solid #D9B86C; background: #17243A; border-radius: 9px; }")
|
|
372
|
-
layout = QHBoxLayout(self)
|
|
373
|
-
layout.setContentsMargins(8, 6, 8, 6)
|
|
374
|
-
layout.setSpacing(7)
|
|
375
|
-
c = summary["champion"]
|
|
376
|
-
layout.addWidget(_avatar(c.get("name", ""), c.get("tier", "T2"), 30))
|
|
377
|
-
body = QVBoxLayout()
|
|
378
|
-
body.setSpacing(1)
|
|
379
|
-
title = QHBoxLayout()
|
|
380
|
-
title.setSpacing(4)
|
|
381
|
-
title.addWidget(_label(c.get("name", ""), "Section"))
|
|
382
|
-
title.addWidget(_tier_badge(c.get("tier", "T2")))
|
|
383
|
-
title.addStretch(1)
|
|
384
|
-
body.addLayout(title)
|
|
385
|
-
aug = (summary.get("top_augments") or [{}])[0].get("augment", {}).get("name")
|
|
386
|
-
body.addWidget(_label(f"首选 {aug}" if aug else "暂无英雄样本", "Muted"))
|
|
387
|
-
layout.addLayout(body, 1)
|
|
388
|
-
right = QVBoxLayout()
|
|
389
|
-
right.setSpacing(1)
|
|
390
|
-
rate = QLabel(c.get("win_rate_label", "--"))
|
|
391
|
-
rate.setStyleSheet("font-size: 13px; font-weight: 700;")
|
|
392
|
-
rate.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
393
|
-
right.addWidget(rate)
|
|
394
|
-
if my:
|
|
395
|
-
try:
|
|
396
|
-
delta = float(c.get("win_rate", 0)) - float(my["champion"].get("win_rate", 0))
|
|
397
|
-
text = f"{delta * 100:+.1f} vs 我的"
|
|
398
|
-
color = COLORS["green"] if delta >= 0 else COLORS["red"]
|
|
399
|
-
except (TypeError, ValueError):
|
|
400
|
-
text = "海斗胜率"
|
|
401
|
-
color = COLORS["muted"]
|
|
402
|
-
else:
|
|
403
|
-
text = "海斗胜率"
|
|
404
|
-
color = COLORS["muted"]
|
|
405
|
-
delta_label = QLabel(text)
|
|
406
|
-
delta_label.setStyleSheet(f"font-size: 9px; color: {color};")
|
|
407
|
-
delta_label.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
408
|
-
right.addWidget(delta_label)
|
|
409
|
-
layout.addLayout(right)
|
|
410
|
-
|
|
411
|
-
def mouseReleaseEvent(self, event):
|
|
412
|
-
if event.button() == Qt.MouseButton.LeftButton:
|
|
413
|
-
self.clicked.emit(self._cid)
|
|
414
|
-
super().mouseReleaseEvent(event)
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
class FloatingOverlay(QWidget):
|
|
418
|
-
def __init__(
|
|
419
|
-
self,
|
|
420
|
-
root: Path,
|
|
421
|
-
api: str,
|
|
422
|
-
*,
|
|
423
|
-
start_expanded: bool = False,
|
|
424
|
-
persist_pref: bool = False,
|
|
425
|
-
) -> None:
|
|
426
|
-
super().__init__()
|
|
427
|
-
self.root = root
|
|
428
|
-
self.api_base = api.rstrip("/")
|
|
429
|
-
self.persist_pref = persist_pref
|
|
430
|
-
self.snap: dict[str, Any] | None = None
|
|
431
|
-
self.scan: dict[str, Any] | None = None
|
|
432
|
-
self.preview: dict[str, Any] | None = None
|
|
433
|
-
self.service_error = ""
|
|
434
|
-
self.scan_error = ""
|
|
435
|
-
self.tab = "champ"
|
|
436
|
-
self.expanded = False
|
|
437
|
-
self.scanning = False
|
|
438
|
-
self._expanded_at = 0.0
|
|
439
|
-
self._signature: Any = None
|
|
440
|
-
self._search_busy = False
|
|
441
|
-
self._state_path = root / "config" / "overlay_state.json"
|
|
442
|
-
self._pid_path = root / "data" / "cache" / "overlay.pid"
|
|
443
|
-
self._lock_path = root / "data" / "cache" / "overlay.lock"
|
|
444
|
-
self._state_path.parent.mkdir(parents=True, exist_ok=True)
|
|
445
|
-
self._pid_path.parent.mkdir(parents=True, exist_ok=True)
|
|
446
|
-
self.setWindowTitle("海斗助手")
|
|
447
|
-
self.setObjectName("Root")
|
|
448
|
-
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
|
|
449
|
-
self.setWindowFlags(
|
|
450
|
-
Qt.WindowType.FramelessWindowHint
|
|
451
|
-
| Qt.WindowType.WindowStaysOnTopHint
|
|
452
|
-
| Qt.WindowType.Tool
|
|
453
|
-
)
|
|
454
|
-
self.setStyleSheet(STYLE)
|
|
455
|
-
self.setWindowIcon(_make_icon())
|
|
456
|
-
self._api = ApiClient(self.api_base, self)
|
|
457
|
-
self._api.snapshot_ready.connect(self._on_snapshot)
|
|
458
|
-
self._api.snapshot_failed.connect(self._on_snapshot_error)
|
|
459
|
-
self._api.scan_ready.connect(self._on_scan)
|
|
460
|
-
self._api.scan_failed.connect(self._on_scan_error)
|
|
461
|
-
self._api.champion_ready.connect(self._on_champion)
|
|
462
|
-
self._api.request_failed.connect(self._on_request_error)
|
|
463
|
-
|
|
464
|
-
self.stack = QStackedWidget()
|
|
465
|
-
outer = QVBoxLayout(self)
|
|
466
|
-
outer.setContentsMargins(0, 0, 0, 0)
|
|
467
|
-
self.stack.addWidget(self._build_icon_page())
|
|
468
|
-
self.stack.addWidget(self._build_panel_page())
|
|
469
|
-
outer.addWidget(self.stack)
|
|
470
|
-
self._restore_position()
|
|
471
|
-
self._apply_mode(False, start_expanded)
|
|
472
|
-
self._poll = QTimer(self)
|
|
473
|
-
self._poll.setInterval(2200)
|
|
474
|
-
self._poll.timeout.connect(self._api.get_snapshot)
|
|
475
|
-
self._poll.start()
|
|
476
|
-
# Some games (including League's borderless fullscreen window) are
|
|
477
|
-
# themselves WS_EX_TOPMOST and can end up above a Qt tool window when
|
|
478
|
-
# they regain focus. Re-assert topmost without activating our window.
|
|
479
|
-
self._top_timer = QTimer(self)
|
|
480
|
-
self._top_timer.setInterval(1200)
|
|
481
|
-
self._top_timer.timeout.connect(self._raise_topmost)
|
|
482
|
-
self._top_timer.start()
|
|
483
|
-
QTimer.singleShot(120, self._api.get_snapshot)
|
|
484
|
-
if start_expanded:
|
|
485
|
-
QTimer.singleShot(200, self.expand)
|
|
486
|
-
|
|
487
|
-
# ------------------------------------------------------------------- UI
|
|
488
|
-
def _build_icon_page(self) -> QWidget:
|
|
489
|
-
page = QWidget()
|
|
490
|
-
page.setObjectName("Root")
|
|
491
|
-
layout = QVBoxLayout(page)
|
|
492
|
-
layout.setContentsMargins(0, 0, 0, 0)
|
|
493
|
-
self.icon = IconWidget(self)
|
|
494
|
-
self.icon.clicked.connect(self.expand)
|
|
495
|
-
layout.addWidget(self.icon, 0, Qt.AlignmentFlag.AlignCenter)
|
|
496
|
-
return page
|
|
497
|
-
|
|
498
|
-
def _build_panel_page(self) -> QWidget:
|
|
499
|
-
page = QWidget()
|
|
500
|
-
page.setObjectName("Root")
|
|
501
|
-
layout = QVBoxLayout(page)
|
|
502
|
-
layout.setContentsMargins(0, 0, 0, 0)
|
|
503
|
-
self.shell = QFrame()
|
|
504
|
-
self.shell.setObjectName("Shell")
|
|
505
|
-
shadow = QGraphicsDropShadowEffect(self.shell)
|
|
506
|
-
shadow.setBlurRadius(28)
|
|
507
|
-
shadow.setOffset(0, 5)
|
|
508
|
-
shadow.setColor(QColor(0, 0, 0, 150))
|
|
509
|
-
self.shell.setGraphicsEffect(shadow)
|
|
510
|
-
layout.addWidget(self.shell)
|
|
511
|
-
body = QVBoxLayout(self.shell)
|
|
512
|
-
body.setContentsMargins(0, 0, 0, 0)
|
|
513
|
-
body.setSpacing(0)
|
|
514
|
-
|
|
515
|
-
header = DragHeader(self)
|
|
516
|
-
header.setFixedHeight(48)
|
|
517
|
-
head = QHBoxLayout(header)
|
|
518
|
-
head.setContentsMargins(12, 6, 8, 4)
|
|
519
|
-
head.setSpacing(7)
|
|
520
|
-
mark = QLabel("海")
|
|
521
|
-
mark.setFixedSize(26, 26)
|
|
522
|
-
mark.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
523
|
-
mark.setStyleSheet(
|
|
524
|
-
"background: qlineargradient(x1:0,y1:0,x2:1,y2:1, stop:0 #E9D39A, stop:1 #A87A32); "
|
|
525
|
-
"color: #1A1408; border-radius: 7px; font-weight: 700;"
|
|
526
|
-
)
|
|
527
|
-
head.addWidget(mark)
|
|
528
|
-
title_box = QVBoxLayout()
|
|
529
|
-
title_box.setSpacing(0)
|
|
530
|
-
title = _label("海斗助手", "Title")
|
|
531
|
-
title_box.addWidget(title)
|
|
532
|
-
self.phase_label = _label("等待客户端", "Sub")
|
|
533
|
-
title_box.addWidget(self.phase_label)
|
|
534
|
-
head.addLayout(title_box)
|
|
535
|
-
head.addStretch(1)
|
|
536
|
-
self.collapse_btn = QPushButton("收起")
|
|
537
|
-
self.collapse_btn.setObjectName("Icon")
|
|
538
|
-
self.collapse_btn.setFixedSize(38, 24)
|
|
539
|
-
self.collapse_btn.setStyleSheet("font-size: 10px; font-weight: 600;")
|
|
540
|
-
self.collapse_btn.setToolTip("缩小为屏幕图标(Esc)")
|
|
541
|
-
self.collapse_btn.clicked.connect(self.collapse)
|
|
542
|
-
head.addWidget(self.collapse_btn)
|
|
543
|
-
close = QPushButton("✕")
|
|
544
|
-
close.setObjectName("Icon")
|
|
545
|
-
close.setFixedSize(24, 24)
|
|
546
|
-
close.setToolTip("退出")
|
|
547
|
-
close.clicked.connect(self.close)
|
|
548
|
-
head.addWidget(close)
|
|
549
|
-
for child in header.findChildren(QLabel):
|
|
550
|
-
child.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
|
|
551
|
-
body.addWidget(header)
|
|
552
|
-
|
|
553
|
-
tabs = QFrame()
|
|
554
|
-
tab_layout = QHBoxLayout(tabs)
|
|
555
|
-
tab_layout.setContentsMargins(9, 0, 9, 0)
|
|
556
|
-
tab_layout.setSpacing(2)
|
|
557
|
-
self.tab_group = QButtonGroup(self)
|
|
558
|
-
self.tab_buttons: list[QPushButton] = []
|
|
559
|
-
for index, (key, text) in enumerate((("champ", "英雄"), ("augment", "推荐海克斯"), ("build", "出装"))):
|
|
560
|
-
button = QPushButton(text)
|
|
561
|
-
button.setObjectName("Tab")
|
|
562
|
-
button.setCheckable(True)
|
|
563
|
-
button.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
564
|
-
button.clicked.connect(lambda checked=False, k=key: self._select_tab(k))
|
|
565
|
-
self.tab_group.addButton(button, index)
|
|
566
|
-
self.tab_buttons.append(button)
|
|
567
|
-
tab_layout.addWidget(button)
|
|
568
|
-
tab_layout.addStretch(1)
|
|
569
|
-
body.addWidget(tabs)
|
|
570
|
-
|
|
571
|
-
self.scroll = QScrollArea()
|
|
572
|
-
self.scroll.setWidgetResizable(True)
|
|
573
|
-
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
|
574
|
-
self.content = QWidget()
|
|
575
|
-
self.content_layout = QVBoxLayout(self.content)
|
|
576
|
-
self.content_layout.setContentsMargins(11, 8, 11, 8)
|
|
577
|
-
self.content_layout.setSpacing(8)
|
|
578
|
-
self.scroll.setWidget(self.content)
|
|
579
|
-
body.addWidget(self.scroll, 1)
|
|
580
|
-
|
|
581
|
-
self.footer = QLabel("")
|
|
582
|
-
self.footer.setObjectName("Muted")
|
|
583
|
-
self.footer.setContentsMargins(12, 4, 12, 8)
|
|
584
|
-
self.footer.setWordWrap(True)
|
|
585
|
-
body.addWidget(self.footer)
|
|
586
|
-
self._select_tab("champ")
|
|
587
|
-
return page
|
|
588
|
-
|
|
589
|
-
# --------------------------------------------------------------- window
|
|
590
|
-
def _apply_mode(self, expanded: bool, start_expanded: bool = False) -> None:
|
|
591
|
-
self.expanded = expanded
|
|
592
|
-
if expanded:
|
|
593
|
-
self._expanded_at = time.monotonic()
|
|
594
|
-
self.stack.setCurrentIndex(1)
|
|
595
|
-
self.setMinimumSize(350, 480)
|
|
596
|
-
self.setMaximumSize(500, 900)
|
|
597
|
-
self.resize(390, 620)
|
|
598
|
-
self._clamp_to_screen()
|
|
599
|
-
self._raise_topmost()
|
|
600
|
-
self._api.get_snapshot()
|
|
601
|
-
self._rebuild(force=True)
|
|
602
|
-
else:
|
|
603
|
-
self.stack.setCurrentIndex(0)
|
|
604
|
-
self.setMinimumSize(68, 68)
|
|
605
|
-
self.setMaximumSize(68, 68)
|
|
606
|
-
self.resize(68, 68)
|
|
607
|
-
self._snap_right()
|
|
608
|
-
self._raise_topmost()
|
|
609
|
-
|
|
610
|
-
def _snap_right(self) -> None:
|
|
611
|
-
"""Collapsed icon always hugs the right edge, never the middle."""
|
|
612
|
-
screen = self.screen() or QApplication.primaryScreen()
|
|
613
|
-
if screen is None:
|
|
614
|
-
return
|
|
615
|
-
area = screen.availableGeometry()
|
|
616
|
-
margin = 14
|
|
617
|
-
top_offset = 96
|
|
618
|
-
x = area.right() - self.width() - margin
|
|
619
|
-
y = area.top() + top_offset
|
|
620
|
-
y = max(area.top() + margin, min(y, area.bottom() - self.height() - margin))
|
|
621
|
-
self.move(x, y)
|
|
622
|
-
|
|
623
|
-
def showEvent(self, event):
|
|
624
|
-
super().showEvent(event)
|
|
625
|
-
QTimer.singleShot(0, self._raise_topmost)
|
|
626
|
-
|
|
627
|
-
def _raise_topmost(self) -> None:
|
|
628
|
-
if os.name != "nt":
|
|
629
|
-
return
|
|
630
|
-
try:
|
|
631
|
-
hwnd = int(self.winId())
|
|
632
|
-
# HWND_TOPMOST + SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE
|
|
633
|
-
ctypes.windll.user32.SetWindowPos(
|
|
634
|
-
hwnd, -1, 0, 0, 0, 0, 0x0002 | 0x0001 | 0x0010
|
|
635
|
-
)
|
|
636
|
-
except (OSError, ValueError):
|
|
637
|
-
pass
|
|
638
|
-
|
|
639
|
-
def changeEvent(self, event):
|
|
640
|
-
# Clicking into the game (or any other app) should get the overlay out
|
|
641
|
-
# of the way: collapse to the 68 px icon automatically.
|
|
642
|
-
if (
|
|
643
|
-
event.type() == QEvent.Type.WindowDeactivate
|
|
644
|
-
and self.expanded
|
|
645
|
-
and time.monotonic() - self._expanded_at > 0.35
|
|
646
|
-
):
|
|
647
|
-
QTimer.singleShot(80, self._collapse_if_inactive)
|
|
648
|
-
super().changeEvent(event)
|
|
649
|
-
|
|
650
|
-
def _collapse_if_inactive(self) -> None:
|
|
651
|
-
if self.expanded and not self.isActiveWindow():
|
|
652
|
-
self.collapse()
|
|
653
|
-
|
|
654
|
-
def expand(self) -> None:
|
|
655
|
-
if self.expanded:
|
|
656
|
-
return
|
|
657
|
-
self._apply_mode(True)
|
|
658
|
-
self.raise_()
|
|
659
|
-
self.activateWindow()
|
|
660
|
-
|
|
661
|
-
def collapse(self) -> None:
|
|
662
|
-
if not self.expanded:
|
|
663
|
-
return
|
|
664
|
-
self._apply_mode(False)
|
|
665
|
-
self._save_position()
|
|
666
|
-
|
|
667
|
-
def toggle(self) -> None:
|
|
668
|
-
self.collapse() if self.expanded else self.expand()
|
|
669
|
-
|
|
670
|
-
def _clamp_to_screen(self) -> None:
|
|
671
|
-
screen = QApplication.primaryScreen()
|
|
672
|
-
if not screen:
|
|
673
|
-
return
|
|
674
|
-
available = screen.availableGeometry()
|
|
675
|
-
x = max(available.left(), min(self.x(), available.right() - self.width()))
|
|
676
|
-
y = max(available.top(), min(self.y(), available.bottom() - self.height()))
|
|
677
|
-
self.move(x, y)
|
|
678
|
-
|
|
679
|
-
def _restore_position(self) -> None:
|
|
680
|
-
x = y = None
|
|
681
|
-
try:
|
|
682
|
-
data = json.loads(self._state_path.read_text(encoding="utf-8"))
|
|
683
|
-
if isinstance(data, dict):
|
|
684
|
-
x, y = data.get("x"), data.get("y")
|
|
685
|
-
except (OSError, ValueError):
|
|
686
|
-
pass
|
|
687
|
-
screen = QApplication.primaryScreen()
|
|
688
|
-
available = screen.availableGeometry() if screen else None
|
|
689
|
-
if (
|
|
690
|
-
isinstance(x, int)
|
|
691
|
-
and isinstance(y, int)
|
|
692
|
-
and available is not None
|
|
693
|
-
and available.intersects(QRectF(x, y, 68, 68).toRect())
|
|
694
|
-
):
|
|
695
|
-
self.move(max(available.left(), min(x, available.right() - 68)),
|
|
696
|
-
max(available.top(), min(y, available.bottom() - 68)))
|
|
697
|
-
return
|
|
698
|
-
if available is not None:
|
|
699
|
-
self.move(available.right() - 90, available.top() + 26)
|
|
700
|
-
else:
|
|
701
|
-
self.move(80, 80)
|
|
702
|
-
|
|
703
|
-
def _save_position(self) -> None:
|
|
704
|
-
try:
|
|
705
|
-
self._state_path.write_text(
|
|
706
|
-
json.dumps({"x": self.x(), "y": self.y()}, ensure_ascii=False),
|
|
707
|
-
encoding="utf-8",
|
|
708
|
-
)
|
|
709
|
-
except OSError:
|
|
710
|
-
pass
|
|
711
|
-
|
|
712
|
-
def closeEvent(self, event):
|
|
713
|
-
self._save_position()
|
|
714
|
-
self._poll.stop()
|
|
715
|
-
if self.persist_pref:
|
|
716
|
-
try:
|
|
717
|
-
pref = self.root / "config" / "overlay_pref.json"
|
|
718
|
-
pref.parent.mkdir(parents=True, exist_ok=True)
|
|
719
|
-
pref.write_text(json.dumps({"enabled": False}, ensure_ascii=False), encoding="utf-8")
|
|
720
|
-
except OSError:
|
|
721
|
-
pass
|
|
722
|
-
try:
|
|
723
|
-
self._pid_path.unlink()
|
|
724
|
-
except OSError:
|
|
725
|
-
pass
|
|
726
|
-
event.accept()
|
|
727
|
-
app = QApplication.instance()
|
|
728
|
-
if app is not None:
|
|
729
|
-
app.quit()
|
|
730
|
-
|
|
731
|
-
def keyPressEvent(self, event):
|
|
732
|
-
if event.key() == Qt.Key.Key_Escape and self.expanded:
|
|
733
|
-
self.collapse()
|
|
734
|
-
else:
|
|
735
|
-
super().keyPressEvent(event)
|
|
736
|
-
|
|
737
|
-
# ---------------------------------------------------------------- events
|
|
738
|
-
def _on_snapshot(self, data: dict[str, Any]) -> None:
|
|
739
|
-
self.snap = data
|
|
740
|
-
self.service_error = ""
|
|
741
|
-
lcu = data.get("lcu") or {}
|
|
742
|
-
self.icon.set_status(bool(lcu.get("connected")), str(lcu.get("phase", "")))
|
|
743
|
-
phase = {
|
|
744
|
-
"ChampSelect": "选人阶段", "InProgress": "对局中", "Reconnect": "重连中",
|
|
745
|
-
"Lobby": "大厅", "None": "空闲", "Disconnected": "等待客户端",
|
|
746
|
-
}.get(str(lcu.get("phase") or "Disconnected"), str(lcu.get("phase") or ""))
|
|
747
|
-
self.phase_label.setText(
|
|
748
|
-
f"{lcu.get('summoner') or 'LCU'} · {phase}" if lcu.get("connected") else "等待英雄联盟客户端"
|
|
749
|
-
)
|
|
750
|
-
self._refresh_preview()
|
|
751
|
-
self._rebuild()
|
|
752
|
-
|
|
753
|
-
def _on_snapshot_error(self, message: str) -> None:
|
|
754
|
-
self.service_error = message
|
|
755
|
-
self.icon.set_status(False)
|
|
756
|
-
self.phase_label.setText("本地服务未连接")
|
|
757
|
-
self._rebuild(force=True)
|
|
758
|
-
|
|
759
|
-
def _on_scan(self, data: dict[str, Any]) -> None:
|
|
760
|
-
self.scanning = False
|
|
761
|
-
self._rebuild(force=True)
|
|
762
|
-
if not data.get("ok"):
|
|
763
|
-
self.scan_error = str(data.get("error") or "识别失败")
|
|
764
|
-
self.scan = None
|
|
765
|
-
else:
|
|
766
|
-
self.scan = data
|
|
767
|
-
self.scan_error = ""
|
|
768
|
-
self._rebuild(force=True)
|
|
769
|
-
|
|
770
|
-
def _on_scan_error(self, message: str) -> None:
|
|
771
|
-
self.scanning = False
|
|
772
|
-
self.scan_error = message
|
|
773
|
-
self._rebuild(force=True)
|
|
774
|
-
|
|
775
|
-
def _on_champion(self, data: dict[str, Any]) -> None:
|
|
776
|
-
self._search_busy = False
|
|
777
|
-
self.preview = data.get("summary")
|
|
778
|
-
self.tab = "champ"
|
|
779
|
-
self._select_tab("champ")
|
|
780
|
-
self._rebuild(force=True)
|
|
781
|
-
|
|
782
|
-
def _on_request_error(self, message: str) -> None:
|
|
783
|
-
self._search_busy = False
|
|
784
|
-
self.scan_error = message
|
|
785
|
-
self._rebuild(force=True)
|
|
786
|
-
|
|
787
|
-
def _refresh_preview(self) -> None:
|
|
788
|
-
if not self.preview:
|
|
789
|
-
return
|
|
790
|
-
cid = self.preview.get("champion", {}).get("id")
|
|
791
|
-
for summary in (self.snap or {}).get("bench", []):
|
|
792
|
-
if summary.get("champion", {}).get("id") == cid:
|
|
793
|
-
self.preview = summary
|
|
794
|
-
return
|
|
795
|
-
if (self.snap or {}).get("my_champion", {}).get("champion", {}).get("id") == cid:
|
|
796
|
-
self.preview = (self.snap or {}).get("my_champion")
|
|
797
|
-
return
|
|
798
|
-
|
|
799
|
-
def _active(self) -> dict[str, Any] | None:
|
|
800
|
-
return self.preview or (self.snap or {}).get("my_champion")
|
|
801
|
-
|
|
802
|
-
def _signature_now(self) -> Any:
|
|
803
|
-
snap = self.snap or {}
|
|
804
|
-
lcu = snap.get("lcu") or {}
|
|
805
|
-
my = snap.get("my_champion") or {}
|
|
806
|
-
return (
|
|
807
|
-
self.expanded,
|
|
808
|
-
self.tab,
|
|
809
|
-
self.service_error,
|
|
810
|
-
self.scan_error,
|
|
811
|
-
self.scan.get("at") if self.scan else None,
|
|
812
|
-
self.scanning,
|
|
813
|
-
lcu.get("connected"),
|
|
814
|
-
lcu.get("phase"),
|
|
815
|
-
(my.get("champion") or {}).get("id"),
|
|
816
|
-
tuple(item.get("champion", {}).get("id") for item in snap.get("bench", [])),
|
|
817
|
-
(self.preview or {}).get("champion", {}).get("id"),
|
|
818
|
-
(snap.get("meta") or {}).get("patch"),
|
|
819
|
-
)
|
|
820
|
-
|
|
821
|
-
def _select_tab(self, key: str) -> None:
|
|
822
|
-
self.tab = key
|
|
823
|
-
for button, (tab_key, _) in zip(self.tab_buttons, (("champ", ""), ("augment", ""), ("build", ""))):
|
|
824
|
-
button.setChecked(tab_key == key)
|
|
825
|
-
self._rebuild(force=True)
|
|
826
|
-
|
|
827
|
-
def _rebuild(self, force: bool = False) -> None:
|
|
828
|
-
if not self.expanded:
|
|
829
|
-
return
|
|
830
|
-
signature = self._signature_now()
|
|
831
|
-
if not force and signature == self._signature:
|
|
832
|
-
return
|
|
833
|
-
self._signature = signature
|
|
834
|
-
_clear(self.content_layout)
|
|
835
|
-
if self.service_error:
|
|
836
|
-
self._service_error_view()
|
|
837
|
-
elif self.snap is None:
|
|
838
|
-
self.content_layout.addWidget(_label("正在连接本地服务…", "Muted", wrap=True))
|
|
839
|
-
elif self.tab == "champ":
|
|
840
|
-
self._champ_view()
|
|
841
|
-
elif self.tab == "augment":
|
|
842
|
-
self._augment_view()
|
|
843
|
-
else:
|
|
844
|
-
self._build_view()
|
|
845
|
-
meta = (self.snap or {}).get("meta") or {}
|
|
846
|
-
self.footer.setText(
|
|
847
|
-
f"{meta.get('patch', '--')} · {meta.get('source', '--')} · {meta.get('license', '--')}"
|
|
848
|
-
+ (" · 桌面图标" if self.expanded else "")
|
|
849
|
-
)
|
|
850
|
-
self.content_layout.addStretch(1)
|
|
851
|
-
|
|
852
|
-
def _service_error_view(self) -> None:
|
|
853
|
-
card = QFrame()
|
|
854
|
-
card.setObjectName("Card")
|
|
855
|
-
layout = QVBoxLayout(card)
|
|
856
|
-
layout.setContentsMargins(12, 14, 12, 14)
|
|
857
|
-
layout.setSpacing(6)
|
|
858
|
-
layout.addWidget(_label("本地服务未连接", "Section", align=Qt.AlignmentFlag.AlignCenter))
|
|
859
|
-
layout.addWidget(_label(self.service_error, "Muted", wrap=True, align=Qt.AlignmentFlag.AlignCenter))
|
|
860
|
-
button = QPushButton("重试")
|
|
861
|
-
button.setObjectName("Ghost")
|
|
862
|
-
button.clicked.connect(self._api.get_snapshot)
|
|
863
|
-
layout.addWidget(button, 0, Qt.AlignmentFlag.AlignCenter)
|
|
864
|
-
self.content_layout.addWidget(card)
|
|
865
|
-
|
|
866
|
-
def _champ_view(self) -> None:
|
|
867
|
-
snap = self.snap or {}
|
|
868
|
-
my = snap.get("my_champion")
|
|
869
|
-
active = self._active()
|
|
870
|
-
if active:
|
|
871
|
-
self.content_layout.addWidget(self._hero_card(active, bool(self.preview)))
|
|
872
|
-
else:
|
|
873
|
-
empty = QFrame()
|
|
874
|
-
empty.setObjectName("Card")
|
|
875
|
-
layout = QVBoxLayout(empty)
|
|
876
|
-
layout.setContentsMargins(12, 16, 12, 16)
|
|
877
|
-
layout.addWidget(_label("等待英雄", "Section", align=Qt.AlignmentFlag.AlignCenter))
|
|
878
|
-
layout.addWidget(_label("连接客户端后自动读取你的英雄;也可以搜索英雄名或 ID。", "Muted", wrap=True, align=Qt.AlignmentFlag.AlignCenter))
|
|
879
|
-
self.content_layout.addWidget(empty)
|
|
880
|
-
|
|
881
|
-
search = QHBoxLayout()
|
|
882
|
-
self.search_input = QLineEdit()
|
|
883
|
-
self.search_input.setPlaceholderText("搜索英雄,如 亚索 / 157")
|
|
884
|
-
self.search_input.returnPressed.connect(self._search_champion)
|
|
885
|
-
search.addWidget(self.search_input, 1)
|
|
886
|
-
button = QPushButton("查询")
|
|
887
|
-
button.setObjectName("Ghost")
|
|
888
|
-
button.clicked.connect(self._search_champion)
|
|
889
|
-
search.addWidget(button)
|
|
890
|
-
self.content_layout.addLayout(search)
|
|
891
|
-
|
|
892
|
-
bench = snap.get("bench") or []
|
|
893
|
-
if bench:
|
|
894
|
-
self.content_layout.addWidget(_label(f"备战席 · {len(bench)} 位", "Section"))
|
|
895
|
-
for item in bench:
|
|
896
|
-
row = BenchRow(item, my, bool(self.preview and self.preview.get("champion", {}).get("id") == item.get("champion", {}).get("id")))
|
|
897
|
-
row.clicked.connect(self._preview_bench)
|
|
898
|
-
self.content_layout.addWidget(row)
|
|
899
|
-
else:
|
|
900
|
-
self.content_layout.addWidget(_label("暂无备战席数据;进入海克斯大乱斗选人阶段后显示。", "Muted", wrap=True))
|
|
901
|
-
self.content_layout.addWidget(_label("统计为部分对局样本,相关不等于因果。", "Muted", wrap=True))
|
|
902
|
-
|
|
903
|
-
def _hero_card(self, summary: dict[str, Any], preview: bool) -> QFrame:
|
|
904
|
-
card = QFrame()
|
|
905
|
-
card.setObjectName("Card")
|
|
906
|
-
layout = QVBoxLayout(card)
|
|
907
|
-
layout.setContentsMargins(11, 10, 11, 10)
|
|
908
|
-
layout.setSpacing(7)
|
|
909
|
-
c = summary["champion"]
|
|
910
|
-
top = QHBoxLayout()
|
|
911
|
-
top.setSpacing(9)
|
|
912
|
-
top.addWidget(_avatar(c.get("name", ""), c.get("tier", "T2"), 44))
|
|
913
|
-
info = QVBoxLayout()
|
|
914
|
-
info.setSpacing(1)
|
|
915
|
-
row = QHBoxLayout()
|
|
916
|
-
row.setSpacing(5)
|
|
917
|
-
row.addWidget(_label(c.get("name", ""), "HeroName"))
|
|
918
|
-
row.addWidget(_tier_badge(c.get("tier", "T2")))
|
|
919
|
-
row.addStretch(1)
|
|
920
|
-
info.addLayout(row)
|
|
921
|
-
info.addWidget(_label(f"{c.get('title', '')} · {summary.get('style_label', '均衡强化')}", "HeroTitle"))
|
|
922
|
-
if preview:
|
|
923
|
-
btn = QPushButton("返回我的英雄")
|
|
924
|
-
btn.setObjectName("Link")
|
|
925
|
-
btn.clicked.connect(self._reset_preview)
|
|
926
|
-
info.addWidget(btn)
|
|
927
|
-
top.addLayout(info, 1)
|
|
928
|
-
rate = QVBoxLayout()
|
|
929
|
-
rate.setSpacing(0)
|
|
930
|
-
value = QLabel(c.get("win_rate_label", "--"))
|
|
931
|
-
value.setObjectName("BigRate")
|
|
932
|
-
value.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
933
|
-
rate.addWidget(value)
|
|
934
|
-
rank = QLabel(f"第 {c.get('rank', '--')} 名")
|
|
935
|
-
rank.setObjectName("Muted")
|
|
936
|
-
rank.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
937
|
-
rate.addWidget(rank)
|
|
938
|
-
top.addLayout(rate)
|
|
939
|
-
layout.addLayout(top)
|
|
940
|
-
metrics = QHBoxLayout()
|
|
941
|
-
metrics.setSpacing(12)
|
|
942
|
-
for text, label in (
|
|
943
|
-
(c.get("pick_rate_label", "--"), "选取率"),
|
|
944
|
-
(_num(c.get("games")), "样本"),
|
|
945
|
-
(c.get("tier", "--"), "评级"),
|
|
946
|
-
):
|
|
947
|
-
box = QVBoxLayout()
|
|
948
|
-
box.setSpacing(0)
|
|
949
|
-
v = QLabel(str(text))
|
|
950
|
-
v.setStyleSheet("font-size: 12px; font-weight: 700;")
|
|
951
|
-
box.addWidget(v)
|
|
952
|
-
box.addWidget(_label(label, "Muted"))
|
|
953
|
-
metrics.addLayout(box)
|
|
954
|
-
metrics.addStretch(1)
|
|
955
|
-
layout.addLayout(metrics)
|
|
956
|
-
return card
|
|
957
|
-
|
|
958
|
-
def _augment_view(self) -> None:
|
|
959
|
-
active = self._active()
|
|
960
|
-
if not active:
|
|
961
|
-
self.content_layout.addWidget(_label("先选择英雄,再查看推荐海克斯。", "Muted", wrap=True))
|
|
962
|
-
return
|
|
963
|
-
c = active["champion"]
|
|
964
|
-
header = QFrame()
|
|
965
|
-
header.setObjectName("Card")
|
|
966
|
-
layout = QHBoxLayout(header)
|
|
967
|
-
layout.setContentsMargins(9, 7, 9, 7)
|
|
968
|
-
layout.setSpacing(7)
|
|
969
|
-
layout.addWidget(_avatar(c.get("name", ""), c.get("tier", "T2"), 30))
|
|
970
|
-
info = QVBoxLayout()
|
|
971
|
-
info.setSpacing(1)
|
|
972
|
-
info.addWidget(_label(c.get("name", ""), "Section"))
|
|
973
|
-
info.addWidget(_label(f"当前英雄 · 胜率 {c.get('win_rate_label', '--')} · {c.get('tier', '--')}", "Muted"))
|
|
974
|
-
layout.addLayout(info, 1)
|
|
975
|
-
button = QPushButton("扫描当前海克斯")
|
|
976
|
-
button.setObjectName("Gold")
|
|
977
|
-
button.setEnabled(not self.scanning)
|
|
978
|
-
button.clicked.connect(self._scan)
|
|
979
|
-
layout.addWidget(button)
|
|
980
|
-
self.content_layout.addWidget(header)
|
|
981
|
-
if self.scan_error:
|
|
982
|
-
self.content_layout.addWidget(_label(self.scan_error, "Muted", wrap=True))
|
|
983
|
-
if self.scanning:
|
|
984
|
-
self.content_layout.addWidget(_label("正在识别游戏窗口中的卡片…", "Muted", wrap=True))
|
|
985
|
-
if self.scan and self.scan.get("evaluations"):
|
|
986
|
-
self.content_layout.addWidget(_label("本局识别", "Section"))
|
|
987
|
-
for item in self.scan["evaluations"]:
|
|
988
|
-
self.content_layout.addWidget(self._augment_card(item))
|
|
989
|
-
recommendations = active.get("top_augments") or []
|
|
990
|
-
self.content_layout.addWidget(_label(f"版本推荐 · {len(recommendations)} 个", "Section"))
|
|
991
|
-
for item in recommendations:
|
|
992
|
-
self.content_layout.addWidget(self._augment_card(item))
|
|
993
|
-
if not recommendations:
|
|
994
|
-
self.content_layout.addWidget(_label("该英雄暂无足够样本。", "Muted", wrap=True))
|
|
995
|
-
self.content_layout.addWidget(_label("推荐分 = 英雄专属胜率(向全服收缩)+ 全服强度;低样本会被压低。", "Muted", wrap=True))
|
|
996
|
-
|
|
997
|
-
def _augment_card(self, item: dict[str, Any]) -> QFrame:
|
|
998
|
-
card = QFrame()
|
|
999
|
-
card.setObjectName("Card")
|
|
1000
|
-
best = bool(item.get("is_best"))
|
|
1001
|
-
if best:
|
|
1002
|
-
card.setStyleSheet("QFrame#Card { border: 1px solid #D9B86C; background: #1A1F2B; border-radius: 10px; }")
|
|
1003
|
-
layout = QVBoxLayout(card)
|
|
1004
|
-
layout.setContentsMargins(9, 7, 9, 7)
|
|
1005
|
-
layout.setSpacing(4)
|
|
1006
|
-
c = item.get("augment") or {}
|
|
1007
|
-
top = QHBoxLayout()
|
|
1008
|
-
top.setSpacing(5)
|
|
1009
|
-
rank = QLabel(str(item.get("rank", "")))
|
|
1010
|
-
rank.setFixedSize(20, 20)
|
|
1011
|
-
rank.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
1012
|
-
rank.setStyleSheet(
|
|
1013
|
-
f"background: {TIER_BG.get(item.get('tier', 'T2'), '#152238')}; "
|
|
1014
|
-
f"color: {TIER_COLORS.get(item.get('tier', 'T2'), '#7FA6D9')}; "
|
|
1015
|
-
"border-radius: 10px; font-size: 9px; font-weight: 700;"
|
|
1016
|
-
)
|
|
1017
|
-
top.addWidget(rank)
|
|
1018
|
-
top.addWidget(_label(c.get("name", ""), "Section"))
|
|
1019
|
-
top.addWidget(_rarity_badge(c.get("rarity", "gold"), c.get("rarity_label", "")))
|
|
1020
|
-
top.addWidget(_tier_badge(item.get("tier", "T2")))
|
|
1021
|
-
top.addStretch(1)
|
|
1022
|
-
score = QLabel(str(round(float(item.get("score", 0)))))
|
|
1023
|
-
score.setStyleSheet("font-size: 17px; font-weight: 700; color: #F0D79A;")
|
|
1024
|
-
top.addWidget(score)
|
|
1025
|
-
layout.addLayout(top)
|
|
1026
|
-
metrics = QHBoxLayout()
|
|
1027
|
-
metrics.setSpacing(8)
|
|
1028
|
-
metrics.addWidget(_label(f"胜率 {item.get('win_rate_label', '--')}", "Muted"))
|
|
1029
|
-
delta = item.get("delta", 0)
|
|
1030
|
-
try:
|
|
1031
|
-
delta_color = COLORS["green"] if float(delta) >= 0 else COLORS["red"]
|
|
1032
|
-
except (TypeError, ValueError):
|
|
1033
|
-
delta_color = COLORS["muted"]
|
|
1034
|
-
d = QLabel(f"差值 {item.get('delta_label', '--')}pp")
|
|
1035
|
-
d.setStyleSheet(f"font-size: 9px; color: {delta_color};")
|
|
1036
|
-
metrics.addWidget(d)
|
|
1037
|
-
metrics.addWidget(_label(f"选取 {item.get('pick_rate_label', '--')}", "Muted"))
|
|
1038
|
-
metrics.addWidget(_label(f"样本 {_num(item.get('games'))}", "Muted"))
|
|
1039
|
-
metrics.addStretch(1)
|
|
1040
|
-
layout.addLayout(metrics)
|
|
1041
|
-
tags = item.get("tags") or []
|
|
1042
|
-
if tags:
|
|
1043
|
-
tag_row = QHBoxLayout()
|
|
1044
|
-
tag_row.setSpacing(3)
|
|
1045
|
-
for tag in tags[:4]:
|
|
1046
|
-
tag_row.addWidget(_chip(str(tag), COLORS["gold2"] if "首选" in str(tag) else COLORS["muted"]))
|
|
1047
|
-
tag_row.addStretch(1)
|
|
1048
|
-
layout.addLayout(tag_row)
|
|
1049
|
-
return card
|
|
1050
|
-
|
|
1051
|
-
def _build_view(self) -> None:
|
|
1052
|
-
active = self._active()
|
|
1053
|
-
if not active:
|
|
1054
|
-
self.content_layout.addWidget(_label("先选择英雄,再查看出装。", "Muted", wrap=True))
|
|
1055
|
-
return
|
|
1056
|
-
c = active["champion"]
|
|
1057
|
-
build = active.get("build")
|
|
1058
|
-
if not build:
|
|
1059
|
-
self.content_layout.addWidget(_label(f"{c.get('name', '')} 暂无出装数据。", "Muted", wrap=True))
|
|
1060
|
-
return
|
|
1061
|
-
self.content_layout.addWidget(_label(f"{c.get('name', '')} · {c.get('tier', '')} · {c.get('win_rate_label', '')}", "Section"))
|
|
1062
|
-
self._item_section("出门装", build.get("starting_items") or [])
|
|
1063
|
-
self.content_layout.addWidget(_label("核心三件套", "Section"))
|
|
1064
|
-
for index, core in enumerate(build.get("core_builds") or [], 1):
|
|
1065
|
-
card = QFrame()
|
|
1066
|
-
card.setObjectName("Card")
|
|
1067
|
-
layout = QVBoxLayout(card)
|
|
1068
|
-
layout.setContentsMargins(8, 6, 8, 6)
|
|
1069
|
-
layout.setSpacing(3)
|
|
1070
|
-
head = QHBoxLayout()
|
|
1071
|
-
head.addWidget(_label("首选" if index == 1 else f"方案 {index}", "Section"))
|
|
1072
|
-
head.addStretch(1)
|
|
1073
|
-
head.addWidget(_label(f"{core.get('win_rate_label', '--')} 胜率", "Muted"))
|
|
1074
|
-
head.addWidget(_label(f"{_num(core.get('games'))} 局", "Muted"))
|
|
1075
|
-
layout.addLayout(head)
|
|
1076
|
-
names = " → ".join(item.get("name", "") for item in core.get("items") or [])
|
|
1077
|
-
layout.addWidget(_label(names, "HeroTitle", wrap=True))
|
|
1078
|
-
self.content_layout.addWidget(card)
|
|
1079
|
-
self._item_section("鞋子", build.get("boots") or [])
|
|
1080
|
-
if build.get("spells"):
|
|
1081
|
-
self.content_layout.addWidget(_label("召唤师技能: " + " + ".join(build.get("spells") or []), "Muted", wrap=True))
|
|
1082
|
-
destiny = build.get("destiny")
|
|
1083
|
-
self.content_layout.addWidget(_label("命运之武器", "Section"))
|
|
1084
|
-
if destiny:
|
|
1085
|
-
names = " / ".join(item.get("name", "") for item in destiny.get("items") or [])
|
|
1086
|
-
self.content_layout.addWidget(_label(f"{names} · {destiny.get('win_rate_label', '--')} · {_num(destiny.get('games'))} 局", "Muted", wrap=True))
|
|
1087
|
-
else:
|
|
1088
|
-
self.content_layout.addWidget(_label("暂无达标样本", "Muted"))
|
|
1089
|
-
skill = " → ".join(build.get("skill_priority") or []) or "暂无"
|
|
1090
|
-
self.content_layout.addWidget(_label(f"加点:{skill}(通用模板)", "Muted", wrap=True))
|
|
1091
|
-
if build.get("notes"):
|
|
1092
|
-
self.content_layout.addWidget(_label(build["notes"], "Muted", wrap=True))
|
|
1093
|
-
|
|
1094
|
-
def _item_section(self, title: str, items: list[dict[str, Any]]) -> None:
|
|
1095
|
-
self.content_layout.addWidget(_label(title, "Section"))
|
|
1096
|
-
row = QHBoxLayout()
|
|
1097
|
-
row.setSpacing(5)
|
|
1098
|
-
for item in items:
|
|
1099
|
-
chip = _chip(item.get("name", ""), COLORS["teal"])
|
|
1100
|
-
chip.setToolTip(item.get("name", ""))
|
|
1101
|
-
row.addWidget(chip)
|
|
1102
|
-
row.addStretch(1)
|
|
1103
|
-
self.content_layout.addLayout(row)
|
|
1104
|
-
|
|
1105
|
-
# --------------------------------------------------------------- actions
|
|
1106
|
-
def _scan(self) -> None:
|
|
1107
|
-
if self.scanning:
|
|
1108
|
-
return
|
|
1109
|
-
active = self._active()
|
|
1110
|
-
champion = (active or {}).get("champion", {}).get("id")
|
|
1111
|
-
if not champion:
|
|
1112
|
-
my = (self.snap or {}).get("my_champion") or {}
|
|
1113
|
-
champion = my.get("champion", {}).get("id")
|
|
1114
|
-
self.scanning = True
|
|
1115
|
-
self.scan_error = ""
|
|
1116
|
-
self._rebuild(force=True)
|
|
1117
|
-
self._api.scan(champion)
|
|
1118
|
-
|
|
1119
|
-
def _search_champion(self) -> None:
|
|
1120
|
-
if self._search_busy:
|
|
1121
|
-
return
|
|
1122
|
-
query = self.search_input.text().strip() if hasattr(self, "search_input") else ""
|
|
1123
|
-
if not query:
|
|
1124
|
-
return
|
|
1125
|
-
self._search_busy = True
|
|
1126
|
-
self.scan_error = ""
|
|
1127
|
-
self._api.get_champion(query)
|
|
1128
|
-
|
|
1129
|
-
def _preview_bench(self, champion_id: int) -> None:
|
|
1130
|
-
for item in (self.snap or {}).get("bench", []):
|
|
1131
|
-
if int(item.get("champion", {}).get("id", 0)) == int(champion_id):
|
|
1132
|
-
self.preview = item
|
|
1133
|
-
self._rebuild(force=True)
|
|
1134
|
-
return
|
|
1135
|
-
|
|
1136
|
-
def _reset_preview(self) -> None:
|
|
1137
|
-
self.preview = None
|
|
1138
|
-
self._rebuild(force=True)
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
class ApiClient(QWidget):
|
|
1142
|
-
"""Network helper; QWidget gives the signals an object in the GUI thread."""
|
|
1143
|
-
|
|
1144
|
-
snapshot_ready = Signal(dict)
|
|
1145
|
-
snapshot_failed = Signal(str)
|
|
1146
|
-
scan_ready = Signal(dict)
|
|
1147
|
-
scan_failed = Signal(str)
|
|
1148
|
-
champion_ready = Signal(dict)
|
|
1149
|
-
request_failed = Signal(str)
|
|
1150
|
-
|
|
1151
|
-
def __init__(self, base: str, parent: QWidget | None = None) -> None:
|
|
1152
|
-
super().__init__(parent)
|
|
1153
|
-
self.base = base
|
|
1154
|
-
self.manager = QNetworkAccessManager(self)
|
|
1155
|
-
self._snapshot_pending = False
|
|
1156
|
-
self._scan_pending = False
|
|
1157
|
-
|
|
1158
|
-
def _request(self, path: str, method: str = "get", body: dict[str, Any] | None = None) -> QNetworkReply:
|
|
1159
|
-
request = QNetworkRequest(QUrl(self.base + path))
|
|
1160
|
-
request.setTransferTimeout(4000 if path != "/scan" else 180000)
|
|
1161
|
-
if method == "get":
|
|
1162
|
-
return self.manager.get(request)
|
|
1163
|
-
request.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "application/json")
|
|
1164
|
-
return self.manager.post(request, json.dumps(body or {}).encode("utf-8"))
|
|
1165
|
-
|
|
1166
|
-
def get_snapshot(self) -> None:
|
|
1167
|
-
if self._snapshot_pending:
|
|
1168
|
-
return
|
|
1169
|
-
self._snapshot_pending = True
|
|
1170
|
-
reply = self._request("/snapshot")
|
|
1171
|
-
reply.finished.connect(lambda: self._snapshot_done(reply))
|
|
1172
|
-
|
|
1173
|
-
def _snapshot_done(self, reply: QNetworkReply) -> None:
|
|
1174
|
-
self._snapshot_pending = False
|
|
1175
|
-
try:
|
|
1176
|
-
data = json.loads(bytes(reply.readAll()).decode("utf-8") or "{}")
|
|
1177
|
-
if reply.error() != QNetworkReply.NetworkError.NoError and not data:
|
|
1178
|
-
raise RuntimeError(reply.errorString() or "服务不可达")
|
|
1179
|
-
self.snapshot_ready.emit(data)
|
|
1180
|
-
except Exception as exc:
|
|
1181
|
-
self.snapshot_failed.emit(str(exc))
|
|
1182
|
-
finally:
|
|
1183
|
-
reply.deleteLater()
|
|
1184
|
-
|
|
1185
|
-
def scan(self, champion_id: int | None) -> None:
|
|
1186
|
-
if self._scan_pending:
|
|
1187
|
-
return
|
|
1188
|
-
self._scan_pending = True
|
|
1189
|
-
reply = self._request("/scan", "post", {"champion": champion_id})
|
|
1190
|
-
reply.finished.connect(lambda: self._scan_done(reply))
|
|
1191
|
-
|
|
1192
|
-
def _scan_done(self, reply: QNetworkReply) -> None:
|
|
1193
|
-
self._scan_pending = False
|
|
1194
|
-
try:
|
|
1195
|
-
data = json.loads(bytes(reply.readAll()).decode("utf-8") or "{}")
|
|
1196
|
-
if reply.error() != QNetworkReply.NetworkError.NoError and not data:
|
|
1197
|
-
raise RuntimeError(reply.errorString() or "扫描请求失败")
|
|
1198
|
-
self.scan_ready.emit(data)
|
|
1199
|
-
except Exception as exc:
|
|
1200
|
-
self.scan_failed.emit(str(exc))
|
|
1201
|
-
finally:
|
|
1202
|
-
reply.deleteLater()
|
|
1203
|
-
|
|
1204
|
-
def get_champion(self, query: str) -> None:
|
|
1205
|
-
reply = self._request(f"/champion?champion={quote(str(query))}&top=5")
|
|
1206
|
-
reply.finished.connect(lambda: self._champion_done(reply))
|
|
1207
|
-
|
|
1208
|
-
def _champion_done(self, reply: QNetworkReply) -> None:
|
|
1209
|
-
try:
|
|
1210
|
-
data = json.loads(bytes(reply.readAll()).decode("utf-8") or "{}")
|
|
1211
|
-
if not data.get("ok"):
|
|
1212
|
-
raise RuntimeError(data.get("error") or reply.errorString() or "未找到英雄")
|
|
1213
|
-
self.champion_ready.emit(data)
|
|
1214
|
-
except Exception as exc:
|
|
1215
|
-
self.request_failed.emit(str(exc))
|
|
1216
|
-
finally:
|
|
1217
|
-
reply.deleteLater()
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
def _make_icon() -> QIcon:
|
|
1221
|
-
pixmap = QPixmap(64, 64)
|
|
1222
|
-
pixmap.fill(Qt.GlobalColor.transparent)
|
|
1223
|
-
painter = QPainter(pixmap)
|
|
1224
|
-
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
1225
|
-
path = QPainterPath()
|
|
1226
|
-
cx, cy, radius = 32, 32, 26
|
|
1227
|
-
for index in range(6):
|
|
1228
|
-
angle = math.radians(60 * index - 30)
|
|
1229
|
-
point = QPoint(int(cx + radius * math.cos(angle)), int(cy + radius * math.sin(angle)))
|
|
1230
|
-
path.moveTo(point) if index == 0 else path.lineTo(point)
|
|
1231
|
-
path.closeSubpath()
|
|
1232
|
-
gradient = QLinearGradient(4, 4, 60, 60)
|
|
1233
|
-
gradient.setColorAt(0, QColor("#E9D39A"))
|
|
1234
|
-
gradient.setColorAt(1, QColor("#A87A32"))
|
|
1235
|
-
painter.setPen(QPen(QColor("#3A2D16"), 2))
|
|
1236
|
-
painter.setBrush(gradient)
|
|
1237
|
-
painter.drawPath(path)
|
|
1238
|
-
painter.setPen(QColor("#1A1408"))
|
|
1239
|
-
painter.setFont(QFont("Microsoft YaHei UI", 20, QFont.Weight.Bold))
|
|
1240
|
-
painter.drawText(QRectF(0, 0, 64, 64), Qt.AlignmentFlag.AlignCenter, "海")
|
|
1241
|
-
painter.end()
|
|
1242
|
-
return QIcon(pixmap)
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
def main(argv: list[str] | None = None) -> int:
|
|
1246
|
-
parser = argparse.ArgumentParser(description="ARAM: Mayhem desktop floating icon")
|
|
1247
|
-
parser.add_argument("--root", default=str(ROOT))
|
|
1248
|
-
parser.add_argument("--api", default="http://127.0.0.1:8123")
|
|
1249
|
-
parser.add_argument("--pid-file", default="")
|
|
1250
|
-
parser.add_argument("--expanded", action="store_true")
|
|
1251
|
-
parser.add_argument("--tab", choices=("champ", "augment", "build"), default="champ")
|
|
1252
|
-
parser.add_argument("--screenshot", default="")
|
|
1253
|
-
parser.add_argument("--quit-after", type=float, default=0.0)
|
|
1254
|
-
args = parser.parse_args(argv)
|
|
1255
|
-
root = Path(args.root).resolve()
|
|
1256
|
-
root.joinpath("logs").mkdir(parents=True, exist_ok=True)
|
|
1257
|
-
logging.basicConfig(
|
|
1258
|
-
level=logging.INFO,
|
|
1259
|
-
format="%(asctime)s %(levelname)-7s %(name)s: %(message)s",
|
|
1260
|
-
handlers=[logging.FileHandler(root / "logs" / "overlay.log", encoding="utf-8")],
|
|
1261
|
-
)
|
|
1262
|
-
_enable_dpi_awareness()
|
|
1263
|
-
app = QApplication.instance() or QApplication(sys.argv[:1])
|
|
1264
|
-
app.setStyle("Fusion")
|
|
1265
|
-
app.setQuitOnLastWindowClosed(False)
|
|
1266
|
-
(root / "data" / "cache").mkdir(parents=True, exist_ok=True)
|
|
1267
|
-
lock = QLockFile(str(root / "data" / "cache" / "overlay.lock"))
|
|
1268
|
-
lock.setStaleLockTime(30_000)
|
|
1269
|
-
if not lock.tryLock(100):
|
|
1270
|
-
log.info("another overlay instance is already running")
|
|
1271
|
-
return 0
|
|
1272
|
-
pid_path = Path(args.pid_file) if args.pid_file else root / "data" / "cache" / "overlay.pid"
|
|
1273
|
-
pid_path.parent.mkdir(parents=True, exist_ok=True)
|
|
1274
|
-
pid_path.write_text(str(os.getpid()), encoding="utf-8")
|
|
1275
|
-
overlay = FloatingOverlay(
|
|
1276
|
-
root,
|
|
1277
|
-
args.api,
|
|
1278
|
-
start_expanded=args.expanded,
|
|
1279
|
-
persist_pref=bool(args.pid_file),
|
|
1280
|
-
)
|
|
1281
|
-
overlay.show()
|
|
1282
|
-
if args.tab != "champ":
|
|
1283
|
-
QTimer.singleShot(300, lambda: overlay._select_tab(args.tab))
|
|
1284
|
-
if args.screenshot:
|
|
1285
|
-
def capture() -> None:
|
|
1286
|
-
path = Path(args.screenshot)
|
|
1287
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
1288
|
-
overlay.grab().save(str(path))
|
|
1289
|
-
log.info("screenshot saved to %s", path)
|
|
1290
|
-
QTimer.singleShot(1800, capture)
|
|
1291
|
-
if args.quit_after:
|
|
1292
|
-
QTimer.singleShot(int(args.quit_after * 1000), overlay.close)
|
|
1293
|
-
try:
|
|
1294
|
-
result = app.exec()
|
|
1295
|
-
finally:
|
|
1296
|
-
try:
|
|
1297
|
-
pid_path.unlink()
|
|
1298
|
-
except OSError:
|
|
1299
|
-
pass
|
|
1300
|
-
lock.unlock()
|
|
1301
|
-
return int(result)
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
if __name__ == "__main__":
|
|
1305
|
-
raise SystemExit(main())
|
|
1
|
+
"""Always-on-top desktop icon for the DSH ARAM: Mayhem plugin.
|
|
2
|
+
|
|
3
|
+
Collapsed: a 68 px hex icon. Click to expand into a compact overlay with
|
|
4
|
+
champion, recommended augments and build data. The window is independent of
|
|
5
|
+
the DSH page; it polls the local service at 127.0.0.1:8123.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import ctypes
|
|
12
|
+
import json
|
|
13
|
+
import logging
|
|
14
|
+
import math
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
import time
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any
|
|
20
|
+
from urllib.parse import quote
|
|
21
|
+
|
|
22
|
+
from PySide6.QtCore import QEvent, QLockFile, QPoint, QRectF, Qt, QTimer, QUrl, Signal
|
|
23
|
+
from PySide6.QtGui import (
|
|
24
|
+
QColor,
|
|
25
|
+
QFont,
|
|
26
|
+
QIcon,
|
|
27
|
+
QLinearGradient,
|
|
28
|
+
QPainter,
|
|
29
|
+
QPainterPath,
|
|
30
|
+
QPen,
|
|
31
|
+
QPixmap,
|
|
32
|
+
)
|
|
33
|
+
from PySide6.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
|
|
34
|
+
from PySide6.QtWidgets import (
|
|
35
|
+
QApplication,
|
|
36
|
+
QButtonGroup,
|
|
37
|
+
QFrame,
|
|
38
|
+
QGraphicsDropShadowEffect,
|
|
39
|
+
QHBoxLayout,
|
|
40
|
+
QLabel,
|
|
41
|
+
QLineEdit,
|
|
42
|
+
QPushButton,
|
|
43
|
+
QScrollArea,
|
|
44
|
+
QStackedWidget,
|
|
45
|
+
QVBoxLayout,
|
|
46
|
+
QWidget,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
50
|
+
if str(ROOT) not in sys.path:
|
|
51
|
+
sys.path.insert(0, str(ROOT))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _enable_dpi_awareness() -> None:
|
|
55
|
+
"""DPI-aware coordinates without importing the optional OCR/Pillow stack."""
|
|
56
|
+
if os.name != "nt":
|
|
57
|
+
return
|
|
58
|
+
try:
|
|
59
|
+
ctypes.windll.shcore.SetProcessDpiAwareness(2)
|
|
60
|
+
except (AttributeError, OSError):
|
|
61
|
+
try:
|
|
62
|
+
ctypes.windll.user32.SetProcessDPIAware()
|
|
63
|
+
except (AttributeError, OSError):
|
|
64
|
+
pass
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
log = logging.getLogger("aram-overlay")
|
|
68
|
+
|
|
69
|
+
TIER_COLORS = {"T0": "#D9B86C", "T1": "#5BD2C0", "T2": "#7FA6D9", "T3": "#8895A8"}
|
|
70
|
+
TIER_BG = {"T0": "#2A2315", "T1": "#112924", "T2": "#152238", "T3": "#1B2330"}
|
|
71
|
+
RARITY = {
|
|
72
|
+
"prismatic": {
|
|
73
|
+
"fill": "qlineargradient(x1:0,y1:0,x2:1,y2:0, stop:0 #5CE1E6, stop:0.5 #B388FF, stop:1 #FFD166)",
|
|
74
|
+
"color": "#1A1408",
|
|
75
|
+
"border": "#F0D79A",
|
|
76
|
+
},
|
|
77
|
+
"gold": {"fill": "#2A2315", "color": "#E8C77A", "border": "#C8AA6E"},
|
|
78
|
+
"silver": {"fill": "#202733", "color": "#D7DEE8", "border": "#AEBACB"},
|
|
79
|
+
}
|
|
80
|
+
COLORS = {
|
|
81
|
+
"bg": "#0A111D",
|
|
82
|
+
"surface": "#111C2C",
|
|
83
|
+
"surface2": "#17243A",
|
|
84
|
+
"border": "#263A55",
|
|
85
|
+
"text": "#E8F0FB",
|
|
86
|
+
"muted": "#8CA2BF",
|
|
87
|
+
"gold": "#D9B86C",
|
|
88
|
+
"gold2": "#F0D79A",
|
|
89
|
+
"teal": "#5BD2C0",
|
|
90
|
+
"red": "#FF7185",
|
|
91
|
+
"green": "#62D19A",
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
STYLE = f"""
|
|
95
|
+
* {{
|
|
96
|
+
font-family: "Microsoft YaHei UI", "Microsoft YaHei", "Segoe UI", sans-serif;
|
|
97
|
+
color: {COLORS["text"]};
|
|
98
|
+
}}
|
|
99
|
+
QWidget#Root {{ background: transparent; }}
|
|
100
|
+
QFrame#Shell {{
|
|
101
|
+
background: qlineargradient(x1:0,y1:0,x2:1,y2:1,
|
|
102
|
+
stop:0 #0D1727, stop:0.55 #0B1422, stop:1 #101B2C);
|
|
103
|
+
border: 1px solid #2A405E;
|
|
104
|
+
border-radius: 16px;
|
|
105
|
+
}}
|
|
106
|
+
QFrame#Card {{
|
|
107
|
+
background: #111C2C;
|
|
108
|
+
border: 1px solid #1E2F47;
|
|
109
|
+
border-radius: 11px;
|
|
110
|
+
}}
|
|
111
|
+
QFrame#Row {{
|
|
112
|
+
background: #101B2A;
|
|
113
|
+
border: 1px solid #1C2C43;
|
|
114
|
+
border-radius: 9px;
|
|
115
|
+
}}
|
|
116
|
+
QFrame#Row:hover {{
|
|
117
|
+
background: #142238;
|
|
118
|
+
border: 1px solid #2A4262;
|
|
119
|
+
}}
|
|
120
|
+
QLabel#Title {{ font-size: 16px; font-weight: 700; }}
|
|
121
|
+
QLabel#Sub {{ font-size: 9px; color: {COLORS["muted"]}; }}
|
|
122
|
+
QLabel#Muted {{ color: {COLORS["muted"]}; font-size: 10px; }}
|
|
123
|
+
QLabel#HeroName {{ font-size: 18px; font-weight: 700; }}
|
|
124
|
+
QLabel#HeroTitle {{ font-size: 10px; color: {COLORS["muted"]}; }}
|
|
125
|
+
QLabel#BigRate {{ font-size: 24px; font-weight: 700; color: {COLORS["gold2"]}; }}
|
|
126
|
+
QLabel#Section {{ font-size: 12px; font-weight: 700; }}
|
|
127
|
+
QLabel#Toast {{
|
|
128
|
+
background: #1B2A40; border: 1px solid #3A567A; border-radius: 7px;
|
|
129
|
+
padding: 6px 9px; font-size: 10px;
|
|
130
|
+
}}
|
|
131
|
+
QPushButton#Tab {{
|
|
132
|
+
background: transparent; border: none;
|
|
133
|
+
border-bottom: 2px solid transparent;
|
|
134
|
+
color: {COLORS["muted"]}; padding: 7px 10px 6px; font-size: 12px;
|
|
135
|
+
}}
|
|
136
|
+
QPushButton#Tab:checked {{
|
|
137
|
+
color: {COLORS["gold2"]}; border-bottom: 2px solid {COLORS["gold"]};
|
|
138
|
+
}}
|
|
139
|
+
QPushButton#Icon {{
|
|
140
|
+
background: transparent; border: 1px solid transparent;
|
|
141
|
+
border-radius: 7px; color: {COLORS["muted"]}; font-size: 14px; font-weight: 700;
|
|
142
|
+
}}
|
|
143
|
+
QPushButton#Icon:hover {{ background: #1B2A40; border: 1px solid #2A405E; }}
|
|
144
|
+
QPushButton#Gold {{
|
|
145
|
+
background: qlineargradient(x1:0,y1:0,x2:1,y2:0, stop:0 #D9B86C, stop:1 #E8CF96);
|
|
146
|
+
border: none; border-radius: 8px; color: #1B160A;
|
|
147
|
+
font-size: 11px; font-weight: 700; padding: 7px 12px;
|
|
148
|
+
}}
|
|
149
|
+
QPushButton#Ghost {{
|
|
150
|
+
background: #152236; border: 1px solid #2A405E; border-radius: 8px;
|
|
151
|
+
color: {COLORS["text"]}; font-size: 11px; padding: 6px 10px;
|
|
152
|
+
}}
|
|
153
|
+
QPushButton#Link {{
|
|
154
|
+
background: transparent; border: none; color: {COLORS["muted"]};
|
|
155
|
+
font-size: 10px; padding: 0; text-align: left;
|
|
156
|
+
}}
|
|
157
|
+
QLineEdit {{
|
|
158
|
+
background: #101B2A; border: 1px solid #2A405E; border-radius: 7px;
|
|
159
|
+
padding: 5px 8px; font-size: 11px; color: {COLORS["text"]};
|
|
160
|
+
}}
|
|
161
|
+
QCheckBox {{ font-size: 10px; color: {COLORS["muted"]}; spacing: 4px; }}
|
|
162
|
+
QScrollArea {{ background: transparent; border: none; }}
|
|
163
|
+
QScrollArea > QWidget > QWidget {{ background: transparent; }}
|
|
164
|
+
QScrollBar:vertical {{ background: transparent; width: 6px; margin: 2px; }}
|
|
165
|
+
QScrollBar::handle:vertical {{ background: #2B405D; min-height: 24px; border-radius: 3px; }}
|
|
166
|
+
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{ height: 0; }}
|
|
167
|
+
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{ background: transparent; }}
|
|
168
|
+
QToolTip {{
|
|
169
|
+
background: #152236; color: {COLORS["text"]}; border: 1px solid #345071;
|
|
170
|
+
padding: 4px 6px; font-size: 10px;
|
|
171
|
+
}}
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _pct(value: Any, digits: int = 1) -> str:
|
|
176
|
+
try:
|
|
177
|
+
return f"{float(value) * 100:.{digits}f}%"
|
|
178
|
+
except (TypeError, ValueError):
|
|
179
|
+
return "--"
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _num(value: Any) -> str:
|
|
183
|
+
try:
|
|
184
|
+
return f"{int(value):,}"
|
|
185
|
+
except (TypeError, ValueError):
|
|
186
|
+
return "--"
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _clear(layout: QVBoxLayout) -> None:
|
|
190
|
+
while layout.count():
|
|
191
|
+
item = layout.takeAt(0)
|
|
192
|
+
if item is None:
|
|
193
|
+
continue
|
|
194
|
+
widget = item.widget()
|
|
195
|
+
if widget is not None:
|
|
196
|
+
widget.deleteLater()
|
|
197
|
+
child = item.layout()
|
|
198
|
+
if child is not None:
|
|
199
|
+
_clear(child)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _label(text: str, name: str = "Muted", *, wrap: bool = False, align: Any = None) -> QLabel:
|
|
203
|
+
label = QLabel(str(text))
|
|
204
|
+
label.setObjectName(name)
|
|
205
|
+
label.setWordWrap(wrap)
|
|
206
|
+
if align is not None:
|
|
207
|
+
label.setAlignment(align)
|
|
208
|
+
return label
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _tier_badge(tier: str) -> QLabel:
|
|
212
|
+
color = TIER_COLORS.get(tier, TIER_COLORS["T2"])
|
|
213
|
+
bg = TIER_BG.get(tier, TIER_BG["T2"])
|
|
214
|
+
badge = QLabel(tier or "T2")
|
|
215
|
+
badge.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
216
|
+
badge.setFixedHeight(17)
|
|
217
|
+
badge.setStyleSheet(
|
|
218
|
+
f"background: {bg}; color: {color}; border: 1px solid {color}66; "
|
|
219
|
+
"border-radius: 4px; font-size: 9px; font-weight: 700; padding: 1px 4px;"
|
|
220
|
+
)
|
|
221
|
+
return badge
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _chip(text: str, color: str | None = None) -> QLabel:
|
|
225
|
+
chip = QLabel(str(text))
|
|
226
|
+
color = color or COLORS["muted"]
|
|
227
|
+
chip.setStyleSheet(
|
|
228
|
+
f"background: {color}14; color: {color}; border: 1px solid {color}44; "
|
|
229
|
+
"border-radius: 4px; font-size: 9px; padding: 1px 5px;"
|
|
230
|
+
)
|
|
231
|
+
return chip
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _rarity_badge(rarity: str, label: str) -> QLabel:
|
|
235
|
+
"""In-game augment rarity accents: silver, gold, prismatic."""
|
|
236
|
+
theme = RARITY.get(str(rarity), RARITY["gold"])
|
|
237
|
+
badge = QLabel(label or str(rarity or "?"))
|
|
238
|
+
badge.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
239
|
+
badge.setStyleSheet(
|
|
240
|
+
f"background: {theme['fill']}; color: {theme['color']}; "
|
|
241
|
+
f"border: 1px solid {theme['border']}; border-radius: 4px; "
|
|
242
|
+
"font-size: 9px; font-weight: 700; padding: 1px 5px;"
|
|
243
|
+
)
|
|
244
|
+
return badge
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _avatar(name: str, tier: str = "T2", size: int = 40) -> QLabel:
|
|
248
|
+
palette = ["#365E7A", "#64518F", "#8B6038", "#34736B", "#7D4E60"]
|
|
249
|
+
color = palette[sum(ord(ch) for ch in (name or "?")) % len(palette)]
|
|
250
|
+
label = QLabel((name or "?")[:1])
|
|
251
|
+
label.setFixedSize(size, size)
|
|
252
|
+
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
253
|
+
label.setStyleSheet(
|
|
254
|
+
f"background: {color}; color: white; border: 2px solid {TIER_COLORS.get(tier, '#7FA6D9')}; "
|
|
255
|
+
f"border-radius: {size // 2}px; font-size: {max(12, int(size * 0.38))}px; font-weight: 700;"
|
|
256
|
+
)
|
|
257
|
+
return label
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class DragHeader(QFrame):
|
|
261
|
+
def __init__(self, window: FloatingOverlay) -> None:
|
|
262
|
+
super().__init__()
|
|
263
|
+
self._window = window
|
|
264
|
+
self._origin: QPoint | None = None
|
|
265
|
+
self._window_origin: QPoint | None = None
|
|
266
|
+
|
|
267
|
+
def mousePressEvent(self, event):
|
|
268
|
+
if event.button() == Qt.MouseButton.LeftButton:
|
|
269
|
+
self._origin = event.globalPosition().toPoint()
|
|
270
|
+
self._window_origin = self._window.pos()
|
|
271
|
+
super().mousePressEvent(event)
|
|
272
|
+
|
|
273
|
+
def mouseMoveEvent(self, event):
|
|
274
|
+
if self._origin is not None and event.buttons() & Qt.MouseButton.LeftButton:
|
|
275
|
+
self._window.move(self._window_origin + event.globalPosition().toPoint() - self._origin)
|
|
276
|
+
super().mouseMoveEvent(event)
|
|
277
|
+
|
|
278
|
+
def mouseReleaseEvent(self, event):
|
|
279
|
+
self._origin = None
|
|
280
|
+
self._window_origin = None
|
|
281
|
+
super().mouseReleaseEvent(event)
|
|
282
|
+
|
|
283
|
+
def mouseDoubleClickEvent(self, event):
|
|
284
|
+
if event.button() == Qt.MouseButton.LeftButton:
|
|
285
|
+
self._window.collapse()
|
|
286
|
+
super().mouseDoubleClickEvent(event)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class IconWidget(QWidget):
|
|
290
|
+
clicked = Signal()
|
|
291
|
+
|
|
292
|
+
def __init__(self, overlay: FloatingOverlay) -> None:
|
|
293
|
+
super().__init__()
|
|
294
|
+
self._overlay = overlay
|
|
295
|
+
self._press: QPoint | None = None
|
|
296
|
+
self._origin: QPoint | None = None
|
|
297
|
+
self._moved = False
|
|
298
|
+
self.setFixedSize(68, 68)
|
|
299
|
+
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
300
|
+
self.setToolTip("海斗助手 · 点击展开")
|
|
301
|
+
|
|
302
|
+
def set_status(self, connected: bool, phase: str = "") -> None:
|
|
303
|
+
self._connected = connected
|
|
304
|
+
self._phase = phase
|
|
305
|
+
self.update()
|
|
306
|
+
|
|
307
|
+
def paintEvent(self, event):
|
|
308
|
+
painter = QPainter(self)
|
|
309
|
+
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
310
|
+
rect = QRectF(5, 5, 58, 58)
|
|
311
|
+
path = QPainterPath()
|
|
312
|
+
cx, cy, radius = rect.center().x(), rect.center().y(), 27
|
|
313
|
+
for index in range(6):
|
|
314
|
+
angle = math.radians(60 * index - 30)
|
|
315
|
+
point = QPoint(int(cx + radius * math.cos(angle)), int(cy + radius * math.sin(angle)))
|
|
316
|
+
if index == 0:
|
|
317
|
+
path.moveTo(point)
|
|
318
|
+
else:
|
|
319
|
+
path.lineTo(point)
|
|
320
|
+
path.closeSubpath()
|
|
321
|
+
gradient = QLinearGradient(rect.topLeft(), rect.bottomRight())
|
|
322
|
+
gradient.setColorAt(0, QColor("#E9D39A"))
|
|
323
|
+
gradient.setColorAt(1, QColor("#A87A32"))
|
|
324
|
+
painter.setPen(QPen(QColor("#3A2D16"), 1.5))
|
|
325
|
+
painter.setBrush(gradient)
|
|
326
|
+
painter.drawPath(path)
|
|
327
|
+
painter.setPen(QColor("#1A1408"))
|
|
328
|
+
font = QFont(self.font())
|
|
329
|
+
font.setPixelSize(24)
|
|
330
|
+
font.setWeight(QFont.Weight.Bold)
|
|
331
|
+
painter.setFont(font)
|
|
332
|
+
painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, "海")
|
|
333
|
+
connected = getattr(self, "_connected", False)
|
|
334
|
+
painter.setPen(QPen(QColor("#0A111D"), 2))
|
|
335
|
+
painter.setBrush(QColor(COLORS["green"] if connected else COLORS["red"]))
|
|
336
|
+
painter.drawEllipse(QRectF(50, 50, 12, 12))
|
|
337
|
+
painter.end()
|
|
338
|
+
|
|
339
|
+
def mousePressEvent(self, event):
|
|
340
|
+
if event.button() == Qt.MouseButton.LeftButton:
|
|
341
|
+
self._press = event.globalPosition().toPoint()
|
|
342
|
+
self._origin = self._overlay.pos()
|
|
343
|
+
self._moved = False
|
|
344
|
+
super().mousePressEvent(event)
|
|
345
|
+
|
|
346
|
+
def mouseMoveEvent(self, event):
|
|
347
|
+
if self._press is not None and event.buttons() & Qt.MouseButton.LeftButton:
|
|
348
|
+
delta = event.globalPosition().toPoint() - self._press
|
|
349
|
+
if abs(delta.x()) + abs(delta.y()) > 3:
|
|
350
|
+
self._moved = True
|
|
351
|
+
self._overlay.move(self._origin + delta)
|
|
352
|
+
super().mouseMoveEvent(event)
|
|
353
|
+
|
|
354
|
+
def mouseReleaseEvent(self, event):
|
|
355
|
+
if event.button() == Qt.MouseButton.LeftButton:
|
|
356
|
+
if not self._moved:
|
|
357
|
+
self.clicked.emit()
|
|
358
|
+
self._press = None
|
|
359
|
+
super().mouseReleaseEvent(event)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
class BenchRow(QFrame):
|
|
363
|
+
clicked = Signal(int)
|
|
364
|
+
|
|
365
|
+
def __init__(self, summary: dict[str, Any], my: dict[str, Any] | None, active: bool) -> None:
|
|
366
|
+
super().__init__()
|
|
367
|
+
self._cid = int(summary["champion"]["id"])
|
|
368
|
+
self.setObjectName("Row")
|
|
369
|
+
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
370
|
+
if active:
|
|
371
|
+
self.setStyleSheet("QFrame#Row { border: 1px solid #D9B86C; background: #17243A; border-radius: 9px; }")
|
|
372
|
+
layout = QHBoxLayout(self)
|
|
373
|
+
layout.setContentsMargins(8, 6, 8, 6)
|
|
374
|
+
layout.setSpacing(7)
|
|
375
|
+
c = summary["champion"]
|
|
376
|
+
layout.addWidget(_avatar(c.get("name", ""), c.get("tier", "T2"), 30))
|
|
377
|
+
body = QVBoxLayout()
|
|
378
|
+
body.setSpacing(1)
|
|
379
|
+
title = QHBoxLayout()
|
|
380
|
+
title.setSpacing(4)
|
|
381
|
+
title.addWidget(_label(c.get("name", ""), "Section"))
|
|
382
|
+
title.addWidget(_tier_badge(c.get("tier", "T2")))
|
|
383
|
+
title.addStretch(1)
|
|
384
|
+
body.addLayout(title)
|
|
385
|
+
aug = (summary.get("top_augments") or [{}])[0].get("augment", {}).get("name")
|
|
386
|
+
body.addWidget(_label(f"首选 {aug}" if aug else "暂无英雄样本", "Muted"))
|
|
387
|
+
layout.addLayout(body, 1)
|
|
388
|
+
right = QVBoxLayout()
|
|
389
|
+
right.setSpacing(1)
|
|
390
|
+
rate = QLabel(c.get("win_rate_label", "--"))
|
|
391
|
+
rate.setStyleSheet("font-size: 13px; font-weight: 700;")
|
|
392
|
+
rate.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
393
|
+
right.addWidget(rate)
|
|
394
|
+
if my:
|
|
395
|
+
try:
|
|
396
|
+
delta = float(c.get("win_rate", 0)) - float(my["champion"].get("win_rate", 0))
|
|
397
|
+
text = f"{delta * 100:+.1f} vs 我的"
|
|
398
|
+
color = COLORS["green"] if delta >= 0 else COLORS["red"]
|
|
399
|
+
except (TypeError, ValueError):
|
|
400
|
+
text = "海斗胜率"
|
|
401
|
+
color = COLORS["muted"]
|
|
402
|
+
else:
|
|
403
|
+
text = "海斗胜率"
|
|
404
|
+
color = COLORS["muted"]
|
|
405
|
+
delta_label = QLabel(text)
|
|
406
|
+
delta_label.setStyleSheet(f"font-size: 9px; color: {color};")
|
|
407
|
+
delta_label.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
408
|
+
right.addWidget(delta_label)
|
|
409
|
+
layout.addLayout(right)
|
|
410
|
+
|
|
411
|
+
def mouseReleaseEvent(self, event):
|
|
412
|
+
if event.button() == Qt.MouseButton.LeftButton:
|
|
413
|
+
self.clicked.emit(self._cid)
|
|
414
|
+
super().mouseReleaseEvent(event)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
class FloatingOverlay(QWidget):
|
|
418
|
+
def __init__(
|
|
419
|
+
self,
|
|
420
|
+
root: Path,
|
|
421
|
+
api: str,
|
|
422
|
+
*,
|
|
423
|
+
start_expanded: bool = False,
|
|
424
|
+
persist_pref: bool = False,
|
|
425
|
+
) -> None:
|
|
426
|
+
super().__init__()
|
|
427
|
+
self.root = root
|
|
428
|
+
self.api_base = api.rstrip("/")
|
|
429
|
+
self.persist_pref = persist_pref
|
|
430
|
+
self.snap: dict[str, Any] | None = None
|
|
431
|
+
self.scan: dict[str, Any] | None = None
|
|
432
|
+
self.preview: dict[str, Any] | None = None
|
|
433
|
+
self.service_error = ""
|
|
434
|
+
self.scan_error = ""
|
|
435
|
+
self.tab = "champ"
|
|
436
|
+
self.expanded = False
|
|
437
|
+
self.scanning = False
|
|
438
|
+
self._expanded_at = 0.0
|
|
439
|
+
self._signature: Any = None
|
|
440
|
+
self._search_busy = False
|
|
441
|
+
self._state_path = root / "config" / "overlay_state.json"
|
|
442
|
+
self._pid_path = root / "data" / "cache" / "overlay.pid"
|
|
443
|
+
self._lock_path = root / "data" / "cache" / "overlay.lock"
|
|
444
|
+
self._state_path.parent.mkdir(parents=True, exist_ok=True)
|
|
445
|
+
self._pid_path.parent.mkdir(parents=True, exist_ok=True)
|
|
446
|
+
self.setWindowTitle("海斗助手")
|
|
447
|
+
self.setObjectName("Root")
|
|
448
|
+
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
|
|
449
|
+
self.setWindowFlags(
|
|
450
|
+
Qt.WindowType.FramelessWindowHint
|
|
451
|
+
| Qt.WindowType.WindowStaysOnTopHint
|
|
452
|
+
| Qt.WindowType.Tool
|
|
453
|
+
)
|
|
454
|
+
self.setStyleSheet(STYLE)
|
|
455
|
+
self.setWindowIcon(_make_icon())
|
|
456
|
+
self._api = ApiClient(self.api_base, self)
|
|
457
|
+
self._api.snapshot_ready.connect(self._on_snapshot)
|
|
458
|
+
self._api.snapshot_failed.connect(self._on_snapshot_error)
|
|
459
|
+
self._api.scan_ready.connect(self._on_scan)
|
|
460
|
+
self._api.scan_failed.connect(self._on_scan_error)
|
|
461
|
+
self._api.champion_ready.connect(self._on_champion)
|
|
462
|
+
self._api.request_failed.connect(self._on_request_error)
|
|
463
|
+
|
|
464
|
+
self.stack = QStackedWidget()
|
|
465
|
+
outer = QVBoxLayout(self)
|
|
466
|
+
outer.setContentsMargins(0, 0, 0, 0)
|
|
467
|
+
self.stack.addWidget(self._build_icon_page())
|
|
468
|
+
self.stack.addWidget(self._build_panel_page())
|
|
469
|
+
outer.addWidget(self.stack)
|
|
470
|
+
self._restore_position()
|
|
471
|
+
self._apply_mode(False, start_expanded)
|
|
472
|
+
self._poll = QTimer(self)
|
|
473
|
+
self._poll.setInterval(2200)
|
|
474
|
+
self._poll.timeout.connect(self._api.get_snapshot)
|
|
475
|
+
self._poll.start()
|
|
476
|
+
# Some games (including League's borderless fullscreen window) are
|
|
477
|
+
# themselves WS_EX_TOPMOST and can end up above a Qt tool window when
|
|
478
|
+
# they regain focus. Re-assert topmost without activating our window.
|
|
479
|
+
self._top_timer = QTimer(self)
|
|
480
|
+
self._top_timer.setInterval(1200)
|
|
481
|
+
self._top_timer.timeout.connect(self._raise_topmost)
|
|
482
|
+
self._top_timer.start()
|
|
483
|
+
QTimer.singleShot(120, self._api.get_snapshot)
|
|
484
|
+
if start_expanded:
|
|
485
|
+
QTimer.singleShot(200, self.expand)
|
|
486
|
+
|
|
487
|
+
# ------------------------------------------------------------------- UI
|
|
488
|
+
def _build_icon_page(self) -> QWidget:
|
|
489
|
+
page = QWidget()
|
|
490
|
+
page.setObjectName("Root")
|
|
491
|
+
layout = QVBoxLayout(page)
|
|
492
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
493
|
+
self.icon = IconWidget(self)
|
|
494
|
+
self.icon.clicked.connect(self.expand)
|
|
495
|
+
layout.addWidget(self.icon, 0, Qt.AlignmentFlag.AlignCenter)
|
|
496
|
+
return page
|
|
497
|
+
|
|
498
|
+
def _build_panel_page(self) -> QWidget:
|
|
499
|
+
page = QWidget()
|
|
500
|
+
page.setObjectName("Root")
|
|
501
|
+
layout = QVBoxLayout(page)
|
|
502
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
503
|
+
self.shell = QFrame()
|
|
504
|
+
self.shell.setObjectName("Shell")
|
|
505
|
+
shadow = QGraphicsDropShadowEffect(self.shell)
|
|
506
|
+
shadow.setBlurRadius(28)
|
|
507
|
+
shadow.setOffset(0, 5)
|
|
508
|
+
shadow.setColor(QColor(0, 0, 0, 150))
|
|
509
|
+
self.shell.setGraphicsEffect(shadow)
|
|
510
|
+
layout.addWidget(self.shell)
|
|
511
|
+
body = QVBoxLayout(self.shell)
|
|
512
|
+
body.setContentsMargins(0, 0, 0, 0)
|
|
513
|
+
body.setSpacing(0)
|
|
514
|
+
|
|
515
|
+
header = DragHeader(self)
|
|
516
|
+
header.setFixedHeight(48)
|
|
517
|
+
head = QHBoxLayout(header)
|
|
518
|
+
head.setContentsMargins(12, 6, 8, 4)
|
|
519
|
+
head.setSpacing(7)
|
|
520
|
+
mark = QLabel("海")
|
|
521
|
+
mark.setFixedSize(26, 26)
|
|
522
|
+
mark.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
523
|
+
mark.setStyleSheet(
|
|
524
|
+
"background: qlineargradient(x1:0,y1:0,x2:1,y2:1, stop:0 #E9D39A, stop:1 #A87A32); "
|
|
525
|
+
"color: #1A1408; border-radius: 7px; font-weight: 700;"
|
|
526
|
+
)
|
|
527
|
+
head.addWidget(mark)
|
|
528
|
+
title_box = QVBoxLayout()
|
|
529
|
+
title_box.setSpacing(0)
|
|
530
|
+
title = _label("海斗助手", "Title")
|
|
531
|
+
title_box.addWidget(title)
|
|
532
|
+
self.phase_label = _label("等待客户端", "Sub")
|
|
533
|
+
title_box.addWidget(self.phase_label)
|
|
534
|
+
head.addLayout(title_box)
|
|
535
|
+
head.addStretch(1)
|
|
536
|
+
self.collapse_btn = QPushButton("收起")
|
|
537
|
+
self.collapse_btn.setObjectName("Icon")
|
|
538
|
+
self.collapse_btn.setFixedSize(38, 24)
|
|
539
|
+
self.collapse_btn.setStyleSheet("font-size: 10px; font-weight: 600;")
|
|
540
|
+
self.collapse_btn.setToolTip("缩小为屏幕图标(Esc)")
|
|
541
|
+
self.collapse_btn.clicked.connect(self.collapse)
|
|
542
|
+
head.addWidget(self.collapse_btn)
|
|
543
|
+
close = QPushButton("✕")
|
|
544
|
+
close.setObjectName("Icon")
|
|
545
|
+
close.setFixedSize(24, 24)
|
|
546
|
+
close.setToolTip("退出")
|
|
547
|
+
close.clicked.connect(self.close)
|
|
548
|
+
head.addWidget(close)
|
|
549
|
+
for child in header.findChildren(QLabel):
|
|
550
|
+
child.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
|
|
551
|
+
body.addWidget(header)
|
|
552
|
+
|
|
553
|
+
tabs = QFrame()
|
|
554
|
+
tab_layout = QHBoxLayout(tabs)
|
|
555
|
+
tab_layout.setContentsMargins(9, 0, 9, 0)
|
|
556
|
+
tab_layout.setSpacing(2)
|
|
557
|
+
self.tab_group = QButtonGroup(self)
|
|
558
|
+
self.tab_buttons: list[QPushButton] = []
|
|
559
|
+
for index, (key, text) in enumerate((("champ", "英雄"), ("augment", "推荐海克斯"), ("build", "出装"))):
|
|
560
|
+
button = QPushButton(text)
|
|
561
|
+
button.setObjectName("Tab")
|
|
562
|
+
button.setCheckable(True)
|
|
563
|
+
button.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
564
|
+
button.clicked.connect(lambda checked=False, k=key: self._select_tab(k))
|
|
565
|
+
self.tab_group.addButton(button, index)
|
|
566
|
+
self.tab_buttons.append(button)
|
|
567
|
+
tab_layout.addWidget(button)
|
|
568
|
+
tab_layout.addStretch(1)
|
|
569
|
+
body.addWidget(tabs)
|
|
570
|
+
|
|
571
|
+
self.scroll = QScrollArea()
|
|
572
|
+
self.scroll.setWidgetResizable(True)
|
|
573
|
+
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
|
574
|
+
self.content = QWidget()
|
|
575
|
+
self.content_layout = QVBoxLayout(self.content)
|
|
576
|
+
self.content_layout.setContentsMargins(11, 8, 11, 8)
|
|
577
|
+
self.content_layout.setSpacing(8)
|
|
578
|
+
self.scroll.setWidget(self.content)
|
|
579
|
+
body.addWidget(self.scroll, 1)
|
|
580
|
+
|
|
581
|
+
self.footer = QLabel("")
|
|
582
|
+
self.footer.setObjectName("Muted")
|
|
583
|
+
self.footer.setContentsMargins(12, 4, 12, 8)
|
|
584
|
+
self.footer.setWordWrap(True)
|
|
585
|
+
body.addWidget(self.footer)
|
|
586
|
+
self._select_tab("champ")
|
|
587
|
+
return page
|
|
588
|
+
|
|
589
|
+
# --------------------------------------------------------------- window
|
|
590
|
+
def _apply_mode(self, expanded: bool, start_expanded: bool = False) -> None:
|
|
591
|
+
self.expanded = expanded
|
|
592
|
+
if expanded:
|
|
593
|
+
self._expanded_at = time.monotonic()
|
|
594
|
+
self.stack.setCurrentIndex(1)
|
|
595
|
+
self.setMinimumSize(350, 480)
|
|
596
|
+
self.setMaximumSize(500, 900)
|
|
597
|
+
self.resize(390, 620)
|
|
598
|
+
self._clamp_to_screen()
|
|
599
|
+
self._raise_topmost()
|
|
600
|
+
self._api.get_snapshot()
|
|
601
|
+
self._rebuild(force=True)
|
|
602
|
+
else:
|
|
603
|
+
self.stack.setCurrentIndex(0)
|
|
604
|
+
self.setMinimumSize(68, 68)
|
|
605
|
+
self.setMaximumSize(68, 68)
|
|
606
|
+
self.resize(68, 68)
|
|
607
|
+
self._snap_right()
|
|
608
|
+
self._raise_topmost()
|
|
609
|
+
|
|
610
|
+
def _snap_right(self) -> None:
|
|
611
|
+
"""Collapsed icon always hugs the right edge, never the middle."""
|
|
612
|
+
screen = self.screen() or QApplication.primaryScreen()
|
|
613
|
+
if screen is None:
|
|
614
|
+
return
|
|
615
|
+
area = screen.availableGeometry()
|
|
616
|
+
margin = 14
|
|
617
|
+
top_offset = 96
|
|
618
|
+
x = area.right() - self.width() - margin
|
|
619
|
+
y = area.top() + top_offset
|
|
620
|
+
y = max(area.top() + margin, min(y, area.bottom() - self.height() - margin))
|
|
621
|
+
self.move(x, y)
|
|
622
|
+
|
|
623
|
+
def showEvent(self, event):
|
|
624
|
+
super().showEvent(event)
|
|
625
|
+
QTimer.singleShot(0, self._raise_topmost)
|
|
626
|
+
|
|
627
|
+
def _raise_topmost(self) -> None:
|
|
628
|
+
if os.name != "nt":
|
|
629
|
+
return
|
|
630
|
+
try:
|
|
631
|
+
hwnd = int(self.winId())
|
|
632
|
+
# HWND_TOPMOST + SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE
|
|
633
|
+
ctypes.windll.user32.SetWindowPos(
|
|
634
|
+
hwnd, -1, 0, 0, 0, 0, 0x0002 | 0x0001 | 0x0010
|
|
635
|
+
)
|
|
636
|
+
except (OSError, ValueError):
|
|
637
|
+
pass
|
|
638
|
+
|
|
639
|
+
def changeEvent(self, event):
|
|
640
|
+
# Clicking into the game (or any other app) should get the overlay out
|
|
641
|
+
# of the way: collapse to the 68 px icon automatically.
|
|
642
|
+
if (
|
|
643
|
+
event.type() == QEvent.Type.WindowDeactivate
|
|
644
|
+
and self.expanded
|
|
645
|
+
and time.monotonic() - self._expanded_at > 0.35
|
|
646
|
+
):
|
|
647
|
+
QTimer.singleShot(80, self._collapse_if_inactive)
|
|
648
|
+
super().changeEvent(event)
|
|
649
|
+
|
|
650
|
+
def _collapse_if_inactive(self) -> None:
|
|
651
|
+
if self.expanded and not self.isActiveWindow():
|
|
652
|
+
self.collapse()
|
|
653
|
+
|
|
654
|
+
def expand(self) -> None:
|
|
655
|
+
if self.expanded:
|
|
656
|
+
return
|
|
657
|
+
self._apply_mode(True)
|
|
658
|
+
self.raise_()
|
|
659
|
+
self.activateWindow()
|
|
660
|
+
|
|
661
|
+
def collapse(self) -> None:
|
|
662
|
+
if not self.expanded:
|
|
663
|
+
return
|
|
664
|
+
self._apply_mode(False)
|
|
665
|
+
self._save_position()
|
|
666
|
+
|
|
667
|
+
def toggle(self) -> None:
|
|
668
|
+
self.collapse() if self.expanded else self.expand()
|
|
669
|
+
|
|
670
|
+
def _clamp_to_screen(self) -> None:
|
|
671
|
+
screen = QApplication.primaryScreen()
|
|
672
|
+
if not screen:
|
|
673
|
+
return
|
|
674
|
+
available = screen.availableGeometry()
|
|
675
|
+
x = max(available.left(), min(self.x(), available.right() - self.width()))
|
|
676
|
+
y = max(available.top(), min(self.y(), available.bottom() - self.height()))
|
|
677
|
+
self.move(x, y)
|
|
678
|
+
|
|
679
|
+
def _restore_position(self) -> None:
|
|
680
|
+
x = y = None
|
|
681
|
+
try:
|
|
682
|
+
data = json.loads(self._state_path.read_text(encoding="utf-8"))
|
|
683
|
+
if isinstance(data, dict):
|
|
684
|
+
x, y = data.get("x"), data.get("y")
|
|
685
|
+
except (OSError, ValueError):
|
|
686
|
+
pass
|
|
687
|
+
screen = QApplication.primaryScreen()
|
|
688
|
+
available = screen.availableGeometry() if screen else None
|
|
689
|
+
if (
|
|
690
|
+
isinstance(x, int)
|
|
691
|
+
and isinstance(y, int)
|
|
692
|
+
and available is not None
|
|
693
|
+
and available.intersects(QRectF(x, y, 68, 68).toRect())
|
|
694
|
+
):
|
|
695
|
+
self.move(max(available.left(), min(x, available.right() - 68)),
|
|
696
|
+
max(available.top(), min(y, available.bottom() - 68)))
|
|
697
|
+
return
|
|
698
|
+
if available is not None:
|
|
699
|
+
self.move(available.right() - 90, available.top() + 26)
|
|
700
|
+
else:
|
|
701
|
+
self.move(80, 80)
|
|
702
|
+
|
|
703
|
+
def _save_position(self) -> None:
|
|
704
|
+
try:
|
|
705
|
+
self._state_path.write_text(
|
|
706
|
+
json.dumps({"x": self.x(), "y": self.y()}, ensure_ascii=False),
|
|
707
|
+
encoding="utf-8",
|
|
708
|
+
)
|
|
709
|
+
except OSError:
|
|
710
|
+
pass
|
|
711
|
+
|
|
712
|
+
def closeEvent(self, event):
|
|
713
|
+
self._save_position()
|
|
714
|
+
self._poll.stop()
|
|
715
|
+
if self.persist_pref:
|
|
716
|
+
try:
|
|
717
|
+
pref = self.root / "config" / "overlay_pref.json"
|
|
718
|
+
pref.parent.mkdir(parents=True, exist_ok=True)
|
|
719
|
+
pref.write_text(json.dumps({"enabled": False}, ensure_ascii=False), encoding="utf-8")
|
|
720
|
+
except OSError:
|
|
721
|
+
pass
|
|
722
|
+
try:
|
|
723
|
+
self._pid_path.unlink()
|
|
724
|
+
except OSError:
|
|
725
|
+
pass
|
|
726
|
+
event.accept()
|
|
727
|
+
app = QApplication.instance()
|
|
728
|
+
if app is not None:
|
|
729
|
+
app.quit()
|
|
730
|
+
|
|
731
|
+
def keyPressEvent(self, event):
|
|
732
|
+
if event.key() == Qt.Key.Key_Escape and self.expanded:
|
|
733
|
+
self.collapse()
|
|
734
|
+
else:
|
|
735
|
+
super().keyPressEvent(event)
|
|
736
|
+
|
|
737
|
+
# ---------------------------------------------------------------- events
|
|
738
|
+
def _on_snapshot(self, data: dict[str, Any]) -> None:
|
|
739
|
+
self.snap = data
|
|
740
|
+
self.service_error = ""
|
|
741
|
+
lcu = data.get("lcu") or {}
|
|
742
|
+
self.icon.set_status(bool(lcu.get("connected")), str(lcu.get("phase", "")))
|
|
743
|
+
phase = {
|
|
744
|
+
"ChampSelect": "选人阶段", "InProgress": "对局中", "Reconnect": "重连中",
|
|
745
|
+
"Lobby": "大厅", "None": "空闲", "Disconnected": "等待客户端",
|
|
746
|
+
}.get(str(lcu.get("phase") or "Disconnected"), str(lcu.get("phase") or ""))
|
|
747
|
+
self.phase_label.setText(
|
|
748
|
+
f"{lcu.get('summoner') or 'LCU'} · {phase}" if lcu.get("connected") else "等待英雄联盟客户端"
|
|
749
|
+
)
|
|
750
|
+
self._refresh_preview()
|
|
751
|
+
self._rebuild()
|
|
752
|
+
|
|
753
|
+
def _on_snapshot_error(self, message: str) -> None:
|
|
754
|
+
self.service_error = message
|
|
755
|
+
self.icon.set_status(False)
|
|
756
|
+
self.phase_label.setText("本地服务未连接")
|
|
757
|
+
self._rebuild(force=True)
|
|
758
|
+
|
|
759
|
+
def _on_scan(self, data: dict[str, Any]) -> None:
|
|
760
|
+
self.scanning = False
|
|
761
|
+
self._rebuild(force=True)
|
|
762
|
+
if not data.get("ok"):
|
|
763
|
+
self.scan_error = str(data.get("error") or "识别失败")
|
|
764
|
+
self.scan = None
|
|
765
|
+
else:
|
|
766
|
+
self.scan = data
|
|
767
|
+
self.scan_error = ""
|
|
768
|
+
self._rebuild(force=True)
|
|
769
|
+
|
|
770
|
+
def _on_scan_error(self, message: str) -> None:
|
|
771
|
+
self.scanning = False
|
|
772
|
+
self.scan_error = message
|
|
773
|
+
self._rebuild(force=True)
|
|
774
|
+
|
|
775
|
+
def _on_champion(self, data: dict[str, Any]) -> None:
|
|
776
|
+
self._search_busy = False
|
|
777
|
+
self.preview = data.get("summary")
|
|
778
|
+
self.tab = "champ"
|
|
779
|
+
self._select_tab("champ")
|
|
780
|
+
self._rebuild(force=True)
|
|
781
|
+
|
|
782
|
+
def _on_request_error(self, message: str) -> None:
|
|
783
|
+
self._search_busy = False
|
|
784
|
+
self.scan_error = message
|
|
785
|
+
self._rebuild(force=True)
|
|
786
|
+
|
|
787
|
+
def _refresh_preview(self) -> None:
|
|
788
|
+
if not self.preview:
|
|
789
|
+
return
|
|
790
|
+
cid = self.preview.get("champion", {}).get("id")
|
|
791
|
+
for summary in (self.snap or {}).get("bench", []):
|
|
792
|
+
if summary.get("champion", {}).get("id") == cid:
|
|
793
|
+
self.preview = summary
|
|
794
|
+
return
|
|
795
|
+
if (self.snap or {}).get("my_champion", {}).get("champion", {}).get("id") == cid:
|
|
796
|
+
self.preview = (self.snap or {}).get("my_champion")
|
|
797
|
+
return
|
|
798
|
+
|
|
799
|
+
def _active(self) -> dict[str, Any] | None:
|
|
800
|
+
return self.preview or (self.snap or {}).get("my_champion")
|
|
801
|
+
|
|
802
|
+
def _signature_now(self) -> Any:
|
|
803
|
+
snap = self.snap or {}
|
|
804
|
+
lcu = snap.get("lcu") or {}
|
|
805
|
+
my = snap.get("my_champion") or {}
|
|
806
|
+
return (
|
|
807
|
+
self.expanded,
|
|
808
|
+
self.tab,
|
|
809
|
+
self.service_error,
|
|
810
|
+
self.scan_error,
|
|
811
|
+
self.scan.get("at") if self.scan else None,
|
|
812
|
+
self.scanning,
|
|
813
|
+
lcu.get("connected"),
|
|
814
|
+
lcu.get("phase"),
|
|
815
|
+
(my.get("champion") or {}).get("id"),
|
|
816
|
+
tuple(item.get("champion", {}).get("id") for item in snap.get("bench", [])),
|
|
817
|
+
(self.preview or {}).get("champion", {}).get("id"),
|
|
818
|
+
(snap.get("meta") or {}).get("patch"),
|
|
819
|
+
)
|
|
820
|
+
|
|
821
|
+
def _select_tab(self, key: str) -> None:
|
|
822
|
+
self.tab = key
|
|
823
|
+
for button, (tab_key, _) in zip(self.tab_buttons, (("champ", ""), ("augment", ""), ("build", ""))):
|
|
824
|
+
button.setChecked(tab_key == key)
|
|
825
|
+
self._rebuild(force=True)
|
|
826
|
+
|
|
827
|
+
def _rebuild(self, force: bool = False) -> None:
|
|
828
|
+
if not self.expanded:
|
|
829
|
+
return
|
|
830
|
+
signature = self._signature_now()
|
|
831
|
+
if not force and signature == self._signature:
|
|
832
|
+
return
|
|
833
|
+
self._signature = signature
|
|
834
|
+
_clear(self.content_layout)
|
|
835
|
+
if self.service_error:
|
|
836
|
+
self._service_error_view()
|
|
837
|
+
elif self.snap is None:
|
|
838
|
+
self.content_layout.addWidget(_label("正在连接本地服务…", "Muted", wrap=True))
|
|
839
|
+
elif self.tab == "champ":
|
|
840
|
+
self._champ_view()
|
|
841
|
+
elif self.tab == "augment":
|
|
842
|
+
self._augment_view()
|
|
843
|
+
else:
|
|
844
|
+
self._build_view()
|
|
845
|
+
meta = (self.snap or {}).get("meta") or {}
|
|
846
|
+
self.footer.setText(
|
|
847
|
+
f"{meta.get('patch', '--')} · {meta.get('source', '--')} · {meta.get('license', '--')}"
|
|
848
|
+
+ (" · 桌面图标" if self.expanded else "")
|
|
849
|
+
)
|
|
850
|
+
self.content_layout.addStretch(1)
|
|
851
|
+
|
|
852
|
+
def _service_error_view(self) -> None:
|
|
853
|
+
card = QFrame()
|
|
854
|
+
card.setObjectName("Card")
|
|
855
|
+
layout = QVBoxLayout(card)
|
|
856
|
+
layout.setContentsMargins(12, 14, 12, 14)
|
|
857
|
+
layout.setSpacing(6)
|
|
858
|
+
layout.addWidget(_label("本地服务未连接", "Section", align=Qt.AlignmentFlag.AlignCenter))
|
|
859
|
+
layout.addWidget(_label(self.service_error, "Muted", wrap=True, align=Qt.AlignmentFlag.AlignCenter))
|
|
860
|
+
button = QPushButton("重试")
|
|
861
|
+
button.setObjectName("Ghost")
|
|
862
|
+
button.clicked.connect(self._api.get_snapshot)
|
|
863
|
+
layout.addWidget(button, 0, Qt.AlignmentFlag.AlignCenter)
|
|
864
|
+
self.content_layout.addWidget(card)
|
|
865
|
+
|
|
866
|
+
def _champ_view(self) -> None:
|
|
867
|
+
snap = self.snap or {}
|
|
868
|
+
my = snap.get("my_champion")
|
|
869
|
+
active = self._active()
|
|
870
|
+
if active:
|
|
871
|
+
self.content_layout.addWidget(self._hero_card(active, bool(self.preview)))
|
|
872
|
+
else:
|
|
873
|
+
empty = QFrame()
|
|
874
|
+
empty.setObjectName("Card")
|
|
875
|
+
layout = QVBoxLayout(empty)
|
|
876
|
+
layout.setContentsMargins(12, 16, 12, 16)
|
|
877
|
+
layout.addWidget(_label("等待英雄", "Section", align=Qt.AlignmentFlag.AlignCenter))
|
|
878
|
+
layout.addWidget(_label("连接客户端后自动读取你的英雄;也可以搜索英雄名或 ID。", "Muted", wrap=True, align=Qt.AlignmentFlag.AlignCenter))
|
|
879
|
+
self.content_layout.addWidget(empty)
|
|
880
|
+
|
|
881
|
+
search = QHBoxLayout()
|
|
882
|
+
self.search_input = QLineEdit()
|
|
883
|
+
self.search_input.setPlaceholderText("搜索英雄,如 亚索 / 157")
|
|
884
|
+
self.search_input.returnPressed.connect(self._search_champion)
|
|
885
|
+
search.addWidget(self.search_input, 1)
|
|
886
|
+
button = QPushButton("查询")
|
|
887
|
+
button.setObjectName("Ghost")
|
|
888
|
+
button.clicked.connect(self._search_champion)
|
|
889
|
+
search.addWidget(button)
|
|
890
|
+
self.content_layout.addLayout(search)
|
|
891
|
+
|
|
892
|
+
bench = snap.get("bench") or []
|
|
893
|
+
if bench:
|
|
894
|
+
self.content_layout.addWidget(_label(f"备战席 · {len(bench)} 位", "Section"))
|
|
895
|
+
for item in bench:
|
|
896
|
+
row = BenchRow(item, my, bool(self.preview and self.preview.get("champion", {}).get("id") == item.get("champion", {}).get("id")))
|
|
897
|
+
row.clicked.connect(self._preview_bench)
|
|
898
|
+
self.content_layout.addWidget(row)
|
|
899
|
+
else:
|
|
900
|
+
self.content_layout.addWidget(_label("暂无备战席数据;进入海克斯大乱斗选人阶段后显示。", "Muted", wrap=True))
|
|
901
|
+
self.content_layout.addWidget(_label("统计为部分对局样本,相关不等于因果。", "Muted", wrap=True))
|
|
902
|
+
|
|
903
|
+
def _hero_card(self, summary: dict[str, Any], preview: bool) -> QFrame:
|
|
904
|
+
card = QFrame()
|
|
905
|
+
card.setObjectName("Card")
|
|
906
|
+
layout = QVBoxLayout(card)
|
|
907
|
+
layout.setContentsMargins(11, 10, 11, 10)
|
|
908
|
+
layout.setSpacing(7)
|
|
909
|
+
c = summary["champion"]
|
|
910
|
+
top = QHBoxLayout()
|
|
911
|
+
top.setSpacing(9)
|
|
912
|
+
top.addWidget(_avatar(c.get("name", ""), c.get("tier", "T2"), 44))
|
|
913
|
+
info = QVBoxLayout()
|
|
914
|
+
info.setSpacing(1)
|
|
915
|
+
row = QHBoxLayout()
|
|
916
|
+
row.setSpacing(5)
|
|
917
|
+
row.addWidget(_label(c.get("name", ""), "HeroName"))
|
|
918
|
+
row.addWidget(_tier_badge(c.get("tier", "T2")))
|
|
919
|
+
row.addStretch(1)
|
|
920
|
+
info.addLayout(row)
|
|
921
|
+
info.addWidget(_label(f"{c.get('title', '')} · {summary.get('style_label', '均衡强化')}", "HeroTitle"))
|
|
922
|
+
if preview:
|
|
923
|
+
btn = QPushButton("返回我的英雄")
|
|
924
|
+
btn.setObjectName("Link")
|
|
925
|
+
btn.clicked.connect(self._reset_preview)
|
|
926
|
+
info.addWidget(btn)
|
|
927
|
+
top.addLayout(info, 1)
|
|
928
|
+
rate = QVBoxLayout()
|
|
929
|
+
rate.setSpacing(0)
|
|
930
|
+
value = QLabel(c.get("win_rate_label", "--"))
|
|
931
|
+
value.setObjectName("BigRate")
|
|
932
|
+
value.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
933
|
+
rate.addWidget(value)
|
|
934
|
+
rank = QLabel(f"第 {c.get('rank', '--')} 名")
|
|
935
|
+
rank.setObjectName("Muted")
|
|
936
|
+
rank.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
937
|
+
rate.addWidget(rank)
|
|
938
|
+
top.addLayout(rate)
|
|
939
|
+
layout.addLayout(top)
|
|
940
|
+
metrics = QHBoxLayout()
|
|
941
|
+
metrics.setSpacing(12)
|
|
942
|
+
for text, label in (
|
|
943
|
+
(c.get("pick_rate_label", "--"), "选取率"),
|
|
944
|
+
(_num(c.get("games")), "样本"),
|
|
945
|
+
(c.get("tier", "--"), "评级"),
|
|
946
|
+
):
|
|
947
|
+
box = QVBoxLayout()
|
|
948
|
+
box.setSpacing(0)
|
|
949
|
+
v = QLabel(str(text))
|
|
950
|
+
v.setStyleSheet("font-size: 12px; font-weight: 700;")
|
|
951
|
+
box.addWidget(v)
|
|
952
|
+
box.addWidget(_label(label, "Muted"))
|
|
953
|
+
metrics.addLayout(box)
|
|
954
|
+
metrics.addStretch(1)
|
|
955
|
+
layout.addLayout(metrics)
|
|
956
|
+
return card
|
|
957
|
+
|
|
958
|
+
def _augment_view(self) -> None:
|
|
959
|
+
active = self._active()
|
|
960
|
+
if not active:
|
|
961
|
+
self.content_layout.addWidget(_label("先选择英雄,再查看推荐海克斯。", "Muted", wrap=True))
|
|
962
|
+
return
|
|
963
|
+
c = active["champion"]
|
|
964
|
+
header = QFrame()
|
|
965
|
+
header.setObjectName("Card")
|
|
966
|
+
layout = QHBoxLayout(header)
|
|
967
|
+
layout.setContentsMargins(9, 7, 9, 7)
|
|
968
|
+
layout.setSpacing(7)
|
|
969
|
+
layout.addWidget(_avatar(c.get("name", ""), c.get("tier", "T2"), 30))
|
|
970
|
+
info = QVBoxLayout()
|
|
971
|
+
info.setSpacing(1)
|
|
972
|
+
info.addWidget(_label(c.get("name", ""), "Section"))
|
|
973
|
+
info.addWidget(_label(f"当前英雄 · 胜率 {c.get('win_rate_label', '--')} · {c.get('tier', '--')}", "Muted"))
|
|
974
|
+
layout.addLayout(info, 1)
|
|
975
|
+
button = QPushButton("扫描当前海克斯")
|
|
976
|
+
button.setObjectName("Gold")
|
|
977
|
+
button.setEnabled(not self.scanning)
|
|
978
|
+
button.clicked.connect(self._scan)
|
|
979
|
+
layout.addWidget(button)
|
|
980
|
+
self.content_layout.addWidget(header)
|
|
981
|
+
if self.scan_error:
|
|
982
|
+
self.content_layout.addWidget(_label(self.scan_error, "Muted", wrap=True))
|
|
983
|
+
if self.scanning:
|
|
984
|
+
self.content_layout.addWidget(_label("正在识别游戏窗口中的卡片…", "Muted", wrap=True))
|
|
985
|
+
if self.scan and self.scan.get("evaluations"):
|
|
986
|
+
self.content_layout.addWidget(_label("本局识别", "Section"))
|
|
987
|
+
for item in self.scan["evaluations"]:
|
|
988
|
+
self.content_layout.addWidget(self._augment_card(item))
|
|
989
|
+
recommendations = active.get("top_augments") or []
|
|
990
|
+
self.content_layout.addWidget(_label(f"版本推荐 · {len(recommendations)} 个", "Section"))
|
|
991
|
+
for item in recommendations:
|
|
992
|
+
self.content_layout.addWidget(self._augment_card(item))
|
|
993
|
+
if not recommendations:
|
|
994
|
+
self.content_layout.addWidget(_label("该英雄暂无足够样本。", "Muted", wrap=True))
|
|
995
|
+
self.content_layout.addWidget(_label("推荐分 = 英雄专属胜率(向全服收缩)+ 全服强度;低样本会被压低。", "Muted", wrap=True))
|
|
996
|
+
|
|
997
|
+
def _augment_card(self, item: dict[str, Any]) -> QFrame:
|
|
998
|
+
card = QFrame()
|
|
999
|
+
card.setObjectName("Card")
|
|
1000
|
+
best = bool(item.get("is_best"))
|
|
1001
|
+
if best:
|
|
1002
|
+
card.setStyleSheet("QFrame#Card { border: 1px solid #D9B86C; background: #1A1F2B; border-radius: 10px; }")
|
|
1003
|
+
layout = QVBoxLayout(card)
|
|
1004
|
+
layout.setContentsMargins(9, 7, 9, 7)
|
|
1005
|
+
layout.setSpacing(4)
|
|
1006
|
+
c = item.get("augment") or {}
|
|
1007
|
+
top = QHBoxLayout()
|
|
1008
|
+
top.setSpacing(5)
|
|
1009
|
+
rank = QLabel(str(item.get("rank", "")))
|
|
1010
|
+
rank.setFixedSize(20, 20)
|
|
1011
|
+
rank.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
1012
|
+
rank.setStyleSheet(
|
|
1013
|
+
f"background: {TIER_BG.get(item.get('tier', 'T2'), '#152238')}; "
|
|
1014
|
+
f"color: {TIER_COLORS.get(item.get('tier', 'T2'), '#7FA6D9')}; "
|
|
1015
|
+
"border-radius: 10px; font-size: 9px; font-weight: 700;"
|
|
1016
|
+
)
|
|
1017
|
+
top.addWidget(rank)
|
|
1018
|
+
top.addWidget(_label(c.get("name", ""), "Section"))
|
|
1019
|
+
top.addWidget(_rarity_badge(c.get("rarity", "gold"), c.get("rarity_label", "")))
|
|
1020
|
+
top.addWidget(_tier_badge(item.get("tier", "T2")))
|
|
1021
|
+
top.addStretch(1)
|
|
1022
|
+
score = QLabel(str(round(float(item.get("score", 0)))))
|
|
1023
|
+
score.setStyleSheet("font-size: 17px; font-weight: 700; color: #F0D79A;")
|
|
1024
|
+
top.addWidget(score)
|
|
1025
|
+
layout.addLayout(top)
|
|
1026
|
+
metrics = QHBoxLayout()
|
|
1027
|
+
metrics.setSpacing(8)
|
|
1028
|
+
metrics.addWidget(_label(f"胜率 {item.get('win_rate_label', '--')}", "Muted"))
|
|
1029
|
+
delta = item.get("delta", 0)
|
|
1030
|
+
try:
|
|
1031
|
+
delta_color = COLORS["green"] if float(delta) >= 0 else COLORS["red"]
|
|
1032
|
+
except (TypeError, ValueError):
|
|
1033
|
+
delta_color = COLORS["muted"]
|
|
1034
|
+
d = QLabel(f"差值 {item.get('delta_label', '--')}pp")
|
|
1035
|
+
d.setStyleSheet(f"font-size: 9px; color: {delta_color};")
|
|
1036
|
+
metrics.addWidget(d)
|
|
1037
|
+
metrics.addWidget(_label(f"选取 {item.get('pick_rate_label', '--')}", "Muted"))
|
|
1038
|
+
metrics.addWidget(_label(f"样本 {_num(item.get('games'))}", "Muted"))
|
|
1039
|
+
metrics.addStretch(1)
|
|
1040
|
+
layout.addLayout(metrics)
|
|
1041
|
+
tags = item.get("tags") or []
|
|
1042
|
+
if tags:
|
|
1043
|
+
tag_row = QHBoxLayout()
|
|
1044
|
+
tag_row.setSpacing(3)
|
|
1045
|
+
for tag in tags[:4]:
|
|
1046
|
+
tag_row.addWidget(_chip(str(tag), COLORS["gold2"] if "首选" in str(tag) else COLORS["muted"]))
|
|
1047
|
+
tag_row.addStretch(1)
|
|
1048
|
+
layout.addLayout(tag_row)
|
|
1049
|
+
return card
|
|
1050
|
+
|
|
1051
|
+
def _build_view(self) -> None:
|
|
1052
|
+
active = self._active()
|
|
1053
|
+
if not active:
|
|
1054
|
+
self.content_layout.addWidget(_label("先选择英雄,再查看出装。", "Muted", wrap=True))
|
|
1055
|
+
return
|
|
1056
|
+
c = active["champion"]
|
|
1057
|
+
build = active.get("build")
|
|
1058
|
+
if not build:
|
|
1059
|
+
self.content_layout.addWidget(_label(f"{c.get('name', '')} 暂无出装数据。", "Muted", wrap=True))
|
|
1060
|
+
return
|
|
1061
|
+
self.content_layout.addWidget(_label(f"{c.get('name', '')} · {c.get('tier', '')} · {c.get('win_rate_label', '')}", "Section"))
|
|
1062
|
+
self._item_section("出门装", build.get("starting_items") or [])
|
|
1063
|
+
self.content_layout.addWidget(_label("核心三件套", "Section"))
|
|
1064
|
+
for index, core in enumerate(build.get("core_builds") or [], 1):
|
|
1065
|
+
card = QFrame()
|
|
1066
|
+
card.setObjectName("Card")
|
|
1067
|
+
layout = QVBoxLayout(card)
|
|
1068
|
+
layout.setContentsMargins(8, 6, 8, 6)
|
|
1069
|
+
layout.setSpacing(3)
|
|
1070
|
+
head = QHBoxLayout()
|
|
1071
|
+
head.addWidget(_label("首选" if index == 1 else f"方案 {index}", "Section"))
|
|
1072
|
+
head.addStretch(1)
|
|
1073
|
+
head.addWidget(_label(f"{core.get('win_rate_label', '--')} 胜率", "Muted"))
|
|
1074
|
+
head.addWidget(_label(f"{_num(core.get('games'))} 局", "Muted"))
|
|
1075
|
+
layout.addLayout(head)
|
|
1076
|
+
names = " → ".join(item.get("name", "") for item in core.get("items") or [])
|
|
1077
|
+
layout.addWidget(_label(names, "HeroTitle", wrap=True))
|
|
1078
|
+
self.content_layout.addWidget(card)
|
|
1079
|
+
self._item_section("鞋子", build.get("boots") or [])
|
|
1080
|
+
if build.get("spells"):
|
|
1081
|
+
self.content_layout.addWidget(_label("召唤师技能: " + " + ".join(build.get("spells") or []), "Muted", wrap=True))
|
|
1082
|
+
destiny = build.get("destiny")
|
|
1083
|
+
self.content_layout.addWidget(_label("命运之武器", "Section"))
|
|
1084
|
+
if destiny:
|
|
1085
|
+
names = " / ".join(item.get("name", "") for item in destiny.get("items") or [])
|
|
1086
|
+
self.content_layout.addWidget(_label(f"{names} · {destiny.get('win_rate_label', '--')} · {_num(destiny.get('games'))} 局", "Muted", wrap=True))
|
|
1087
|
+
else:
|
|
1088
|
+
self.content_layout.addWidget(_label("暂无达标样本", "Muted"))
|
|
1089
|
+
skill = " → ".join(build.get("skill_priority") or []) or "暂无"
|
|
1090
|
+
self.content_layout.addWidget(_label(f"加点:{skill}(通用模板)", "Muted", wrap=True))
|
|
1091
|
+
if build.get("notes"):
|
|
1092
|
+
self.content_layout.addWidget(_label(build["notes"], "Muted", wrap=True))
|
|
1093
|
+
|
|
1094
|
+
def _item_section(self, title: str, items: list[dict[str, Any]]) -> None:
|
|
1095
|
+
self.content_layout.addWidget(_label(title, "Section"))
|
|
1096
|
+
row = QHBoxLayout()
|
|
1097
|
+
row.setSpacing(5)
|
|
1098
|
+
for item in items:
|
|
1099
|
+
chip = _chip(item.get("name", ""), COLORS["teal"])
|
|
1100
|
+
chip.setToolTip(item.get("name", ""))
|
|
1101
|
+
row.addWidget(chip)
|
|
1102
|
+
row.addStretch(1)
|
|
1103
|
+
self.content_layout.addLayout(row)
|
|
1104
|
+
|
|
1105
|
+
# --------------------------------------------------------------- actions
|
|
1106
|
+
def _scan(self) -> None:
|
|
1107
|
+
if self.scanning:
|
|
1108
|
+
return
|
|
1109
|
+
active = self._active()
|
|
1110
|
+
champion = (active or {}).get("champion", {}).get("id")
|
|
1111
|
+
if not champion:
|
|
1112
|
+
my = (self.snap or {}).get("my_champion") or {}
|
|
1113
|
+
champion = my.get("champion", {}).get("id")
|
|
1114
|
+
self.scanning = True
|
|
1115
|
+
self.scan_error = ""
|
|
1116
|
+
self._rebuild(force=True)
|
|
1117
|
+
self._api.scan(champion)
|
|
1118
|
+
|
|
1119
|
+
def _search_champion(self) -> None:
|
|
1120
|
+
if self._search_busy:
|
|
1121
|
+
return
|
|
1122
|
+
query = self.search_input.text().strip() if hasattr(self, "search_input") else ""
|
|
1123
|
+
if not query:
|
|
1124
|
+
return
|
|
1125
|
+
self._search_busy = True
|
|
1126
|
+
self.scan_error = ""
|
|
1127
|
+
self._api.get_champion(query)
|
|
1128
|
+
|
|
1129
|
+
def _preview_bench(self, champion_id: int) -> None:
|
|
1130
|
+
for item in (self.snap or {}).get("bench", []):
|
|
1131
|
+
if int(item.get("champion", {}).get("id", 0)) == int(champion_id):
|
|
1132
|
+
self.preview = item
|
|
1133
|
+
self._rebuild(force=True)
|
|
1134
|
+
return
|
|
1135
|
+
|
|
1136
|
+
def _reset_preview(self) -> None:
|
|
1137
|
+
self.preview = None
|
|
1138
|
+
self._rebuild(force=True)
|
|
1139
|
+
|
|
1140
|
+
|
|
1141
|
+
class ApiClient(QWidget):
|
|
1142
|
+
"""Network helper; QWidget gives the signals an object in the GUI thread."""
|
|
1143
|
+
|
|
1144
|
+
snapshot_ready = Signal(dict)
|
|
1145
|
+
snapshot_failed = Signal(str)
|
|
1146
|
+
scan_ready = Signal(dict)
|
|
1147
|
+
scan_failed = Signal(str)
|
|
1148
|
+
champion_ready = Signal(dict)
|
|
1149
|
+
request_failed = Signal(str)
|
|
1150
|
+
|
|
1151
|
+
def __init__(self, base: str, parent: QWidget | None = None) -> None:
|
|
1152
|
+
super().__init__(parent)
|
|
1153
|
+
self.base = base
|
|
1154
|
+
self.manager = QNetworkAccessManager(self)
|
|
1155
|
+
self._snapshot_pending = False
|
|
1156
|
+
self._scan_pending = False
|
|
1157
|
+
|
|
1158
|
+
def _request(self, path: str, method: str = "get", body: dict[str, Any] | None = None) -> QNetworkReply:
|
|
1159
|
+
request = QNetworkRequest(QUrl(self.base + path))
|
|
1160
|
+
request.setTransferTimeout(4000 if path != "/scan" else 180000)
|
|
1161
|
+
if method == "get":
|
|
1162
|
+
return self.manager.get(request)
|
|
1163
|
+
request.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "application/json")
|
|
1164
|
+
return self.manager.post(request, json.dumps(body or {}).encode("utf-8"))
|
|
1165
|
+
|
|
1166
|
+
def get_snapshot(self) -> None:
|
|
1167
|
+
if self._snapshot_pending:
|
|
1168
|
+
return
|
|
1169
|
+
self._snapshot_pending = True
|
|
1170
|
+
reply = self._request("/snapshot")
|
|
1171
|
+
reply.finished.connect(lambda: self._snapshot_done(reply))
|
|
1172
|
+
|
|
1173
|
+
def _snapshot_done(self, reply: QNetworkReply) -> None:
|
|
1174
|
+
self._snapshot_pending = False
|
|
1175
|
+
try:
|
|
1176
|
+
data = json.loads(bytes(reply.readAll()).decode("utf-8") or "{}")
|
|
1177
|
+
if reply.error() != QNetworkReply.NetworkError.NoError and not data:
|
|
1178
|
+
raise RuntimeError(reply.errorString() or "服务不可达")
|
|
1179
|
+
self.snapshot_ready.emit(data)
|
|
1180
|
+
except Exception as exc:
|
|
1181
|
+
self.snapshot_failed.emit(str(exc))
|
|
1182
|
+
finally:
|
|
1183
|
+
reply.deleteLater()
|
|
1184
|
+
|
|
1185
|
+
def scan(self, champion_id: int | None) -> None:
|
|
1186
|
+
if self._scan_pending:
|
|
1187
|
+
return
|
|
1188
|
+
self._scan_pending = True
|
|
1189
|
+
reply = self._request("/scan", "post", {"champion": champion_id})
|
|
1190
|
+
reply.finished.connect(lambda: self._scan_done(reply))
|
|
1191
|
+
|
|
1192
|
+
def _scan_done(self, reply: QNetworkReply) -> None:
|
|
1193
|
+
self._scan_pending = False
|
|
1194
|
+
try:
|
|
1195
|
+
data = json.loads(bytes(reply.readAll()).decode("utf-8") or "{}")
|
|
1196
|
+
if reply.error() != QNetworkReply.NetworkError.NoError and not data:
|
|
1197
|
+
raise RuntimeError(reply.errorString() or "扫描请求失败")
|
|
1198
|
+
self.scan_ready.emit(data)
|
|
1199
|
+
except Exception as exc:
|
|
1200
|
+
self.scan_failed.emit(str(exc))
|
|
1201
|
+
finally:
|
|
1202
|
+
reply.deleteLater()
|
|
1203
|
+
|
|
1204
|
+
def get_champion(self, query: str) -> None:
|
|
1205
|
+
reply = self._request(f"/champion?champion={quote(str(query))}&top=5")
|
|
1206
|
+
reply.finished.connect(lambda: self._champion_done(reply))
|
|
1207
|
+
|
|
1208
|
+
def _champion_done(self, reply: QNetworkReply) -> None:
|
|
1209
|
+
try:
|
|
1210
|
+
data = json.loads(bytes(reply.readAll()).decode("utf-8") or "{}")
|
|
1211
|
+
if not data.get("ok"):
|
|
1212
|
+
raise RuntimeError(data.get("error") or reply.errorString() or "未找到英雄")
|
|
1213
|
+
self.champion_ready.emit(data)
|
|
1214
|
+
except Exception as exc:
|
|
1215
|
+
self.request_failed.emit(str(exc))
|
|
1216
|
+
finally:
|
|
1217
|
+
reply.deleteLater()
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
def _make_icon() -> QIcon:
|
|
1221
|
+
pixmap = QPixmap(64, 64)
|
|
1222
|
+
pixmap.fill(Qt.GlobalColor.transparent)
|
|
1223
|
+
painter = QPainter(pixmap)
|
|
1224
|
+
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
1225
|
+
path = QPainterPath()
|
|
1226
|
+
cx, cy, radius = 32, 32, 26
|
|
1227
|
+
for index in range(6):
|
|
1228
|
+
angle = math.radians(60 * index - 30)
|
|
1229
|
+
point = QPoint(int(cx + radius * math.cos(angle)), int(cy + radius * math.sin(angle)))
|
|
1230
|
+
path.moveTo(point) if index == 0 else path.lineTo(point)
|
|
1231
|
+
path.closeSubpath()
|
|
1232
|
+
gradient = QLinearGradient(4, 4, 60, 60)
|
|
1233
|
+
gradient.setColorAt(0, QColor("#E9D39A"))
|
|
1234
|
+
gradient.setColorAt(1, QColor("#A87A32"))
|
|
1235
|
+
painter.setPen(QPen(QColor("#3A2D16"), 2))
|
|
1236
|
+
painter.setBrush(gradient)
|
|
1237
|
+
painter.drawPath(path)
|
|
1238
|
+
painter.setPen(QColor("#1A1408"))
|
|
1239
|
+
painter.setFont(QFont("Microsoft YaHei UI", 20, QFont.Weight.Bold))
|
|
1240
|
+
painter.drawText(QRectF(0, 0, 64, 64), Qt.AlignmentFlag.AlignCenter, "海")
|
|
1241
|
+
painter.end()
|
|
1242
|
+
return QIcon(pixmap)
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
def main(argv: list[str] | None = None) -> int:
|
|
1246
|
+
parser = argparse.ArgumentParser(description="ARAM: Mayhem desktop floating icon")
|
|
1247
|
+
parser.add_argument("--root", default=str(ROOT))
|
|
1248
|
+
parser.add_argument("--api", default="http://127.0.0.1:8123")
|
|
1249
|
+
parser.add_argument("--pid-file", default="")
|
|
1250
|
+
parser.add_argument("--expanded", action="store_true")
|
|
1251
|
+
parser.add_argument("--tab", choices=("champ", "augment", "build"), default="champ")
|
|
1252
|
+
parser.add_argument("--screenshot", default="")
|
|
1253
|
+
parser.add_argument("--quit-after", type=float, default=0.0)
|
|
1254
|
+
args = parser.parse_args(argv)
|
|
1255
|
+
root = Path(args.root).resolve()
|
|
1256
|
+
root.joinpath("logs").mkdir(parents=True, exist_ok=True)
|
|
1257
|
+
logging.basicConfig(
|
|
1258
|
+
level=logging.INFO,
|
|
1259
|
+
format="%(asctime)s %(levelname)-7s %(name)s: %(message)s",
|
|
1260
|
+
handlers=[logging.FileHandler(root / "logs" / "overlay.log", encoding="utf-8")],
|
|
1261
|
+
)
|
|
1262
|
+
_enable_dpi_awareness()
|
|
1263
|
+
app = QApplication.instance() or QApplication(sys.argv[:1])
|
|
1264
|
+
app.setStyle("Fusion")
|
|
1265
|
+
app.setQuitOnLastWindowClosed(False)
|
|
1266
|
+
(root / "data" / "cache").mkdir(parents=True, exist_ok=True)
|
|
1267
|
+
lock = QLockFile(str(root / "data" / "cache" / "overlay.lock"))
|
|
1268
|
+
lock.setStaleLockTime(30_000)
|
|
1269
|
+
if not lock.tryLock(100):
|
|
1270
|
+
log.info("another overlay instance is already running")
|
|
1271
|
+
return 0
|
|
1272
|
+
pid_path = Path(args.pid_file) if args.pid_file else root / "data" / "cache" / "overlay.pid"
|
|
1273
|
+
pid_path.parent.mkdir(parents=True, exist_ok=True)
|
|
1274
|
+
pid_path.write_text(str(os.getpid()), encoding="utf-8")
|
|
1275
|
+
overlay = FloatingOverlay(
|
|
1276
|
+
root,
|
|
1277
|
+
args.api,
|
|
1278
|
+
start_expanded=args.expanded,
|
|
1279
|
+
persist_pref=bool(args.pid_file),
|
|
1280
|
+
)
|
|
1281
|
+
overlay.show()
|
|
1282
|
+
if args.tab != "champ":
|
|
1283
|
+
QTimer.singleShot(300, lambda: overlay._select_tab(args.tab))
|
|
1284
|
+
if args.screenshot:
|
|
1285
|
+
def capture() -> None:
|
|
1286
|
+
path = Path(args.screenshot)
|
|
1287
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
1288
|
+
overlay.grab().save(str(path))
|
|
1289
|
+
log.info("screenshot saved to %s", path)
|
|
1290
|
+
QTimer.singleShot(1800, capture)
|
|
1291
|
+
if args.quit_after:
|
|
1292
|
+
QTimer.singleShot(int(args.quit_after * 1000), overlay.close)
|
|
1293
|
+
try:
|
|
1294
|
+
result = app.exec()
|
|
1295
|
+
finally:
|
|
1296
|
+
try:
|
|
1297
|
+
pid_path.unlink()
|
|
1298
|
+
except OSError:
|
|
1299
|
+
pass
|
|
1300
|
+
lock.unlock()
|
|
1301
|
+
return int(result)
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
if __name__ == "__main__":
|
|
1305
|
+
raise SystemExit(main())
|