-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_combo_commands.py
More file actions
230 lines (199 loc) · 8.52 KB
/
_combo_commands.py
File metadata and controls
230 lines (199 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
UI command handlers: apply/save/delete combo, new combo, clear history.
Internal module: call only from ComboTrackerEngine while holding the engine lock.
"""
from __future__ import annotations
from typing import Any
from persistence import fresh_combo_stats
def apply_enders_from_text(engine, raw: str) -> tuple[bool, str | None]:
raw = (raw or "").strip()
if not raw:
engine.combo_enders = {}
setattr(engine, "combo_enders_soft", set())
engine.save_combos()
return True, None
parsed: dict[str, int] = {}
soft_keys: set[str] = set()
for token in engine.split_inputs(raw):
t = token.strip()
if not t:
continue
if ":" in t:
k, v = t.split(":", 1)
raw_key = k.strip().lower()
# ~key:2s = soft ender (does not drop combo when pressed during hold)
if raw_key.startswith("~"):
raw_key = raw_key[1:].strip().lower()
if raw_key:
soft_keys.add(raw_key)
key = raw_key
val = (v or "").strip().lower()
if not key:
continue
# Require explicit "s" suffix for seconds: key:2s, key:0.2s
if val.endswith("s"):
val = val[:-1].strip()
else:
return False, f"Ender cooldown must be in seconds with 's' suffix, e.g. {key}:2s or {key}:0.2s"
try:
sec = float(val)
except ValueError:
return False, f"Invalid timing for '{key}'. Use seconds with 's', e.g. {key}:2s"
parsed[key] = max(0, int(sec * 1000))
else:
key = t.strip().lower()
if key.startswith("~"):
key = key[1:].strip().lower()
if key:
soft_keys.add(key)
if key:
parsed[key] = 0
engine.combo_enders = parsed
engine.combo_enders_soft = soft_keys
engine.save_combos()
return True, None
def save_or_update_combo(
engine,
*,
name: str,
inputs: str,
enders: str,
expected_time: str | None = None,
user_difficulty: str | None = None,
step_display_mode: str | None = None,
key_images: Any | None = None,
demo_video: str | None = None,
target_game: str | None = None,
ww_team_id: str | None = None,
) -> tuple[bool, str | None]:
name = (name or "").strip()
keys_str = (inputs or "").strip()
if not name or not keys_str:
return False, "Please fill in Name and Inputs."
ok, err = apply_enders_from_text(engine, enders)
if not ok:
return False, err
expected_ms = None
expected_raw = (expected_time or "").strip()
if expected_raw:
expected_ms = engine._parse_expected_time_ms(expected_raw)
if expected_ms is None:
return False, "Invalid Expected time. Examples: 1.05s or 1050ms"
user_diff_val = None
ud_raw = (user_difficulty or "").strip()
if ud_raw:
try:
user_diff_val = float(ud_raw)
except Exception:
return False, "Invalid Your difficulty. Use a number from 0 to 10."
if not (0.0 <= user_diff_val <= 10.0):
return False, "Invalid Your difficulty. Use a number from 0 to 10."
input_list = [k.strip().lower() for k in engine.split_inputs(keys_str) if k.strip()]
if not input_list:
return False, "Please provide at least one input."
old_name = engine.active_combo_name if engine.active_combo_name in engine.combos else None
if old_name and name != old_name:
if old_name in engine.combo_stats and name not in engine.combo_stats:
engine.combo_stats[name] = engine.combo_stats.pop(old_name)
engine.combos[name] = input_list
if old_name != name and old_name in engine.combos:
del engine.combos[old_name]
if old_name in engine.combo_expected_ms:
del engine.combo_expected_ms[old_name]
if old_name in engine.combo_user_difficulty:
del engine.combo_user_difficulty[old_name]
if old_name in engine.combo_step_display_mode and name not in engine.combo_step_display_mode:
engine.combo_step_display_mode[name] = engine.combo_step_display_mode.pop(old_name)
if old_name in engine.combo_key_images and name not in engine.combo_key_images:
engine.combo_key_images[name] = engine.combo_key_images.pop(old_name)
if old_name in getattr(engine, "combo_demo_video", {}) and name not in getattr(engine, "combo_demo_video", {}):
engine.combo_demo_video[name] = engine.combo_demo_video.pop(old_name)
engine.ww.rename_combo(old_name, name)
else:
# Same name or new combo: if steps changed, clear all history for this combo
old_list = engine.combos.get(name)
if old_list is not None and old_list != input_list:
engine.combo_stats[name] = fresh_combo_stats()
engine.combos[name] = input_list
if expected_ms is not None:
engine.combo_expected_ms[name] = int(expected_ms)
else:
engine.combo_expected_ms.pop(name, None)
if user_diff_val is not None:
engine.combo_user_difficulty[name] = float(user_diff_val)
else:
engine.combo_user_difficulty.pop(name, None)
mode_raw = (step_display_mode or "").strip().lower()
if mode_raw in ("icons", "images"):
engine.combo_step_display_mode[name] = mode_raw
else:
engine.combo_step_display_mode.pop(name, None)
cleaned_imgs: dict[str, str] = {}
if isinstance(key_images, dict):
for k, v in key_images.items():
key = str(k).strip().lower()
url = str(v).strip()
if not key or not url:
continue
cleaned_imgs[key] = url
if cleaned_imgs:
engine.combo_key_images[name] = cleaned_imgs
else:
if isinstance(key_images, dict):
engine.combo_key_images.pop(name, None)
demo_url = (demo_video or "").strip()
if demo_url:
engine.combo_demo_video[name] = demo_url
else:
engine.combo_demo_video.pop(name, None)
g_raw = str(target_game or "").strip().lower()
engine.ww.set_target_game(name, g_raw)
engine.ww.apply_combo_team_assignment(name, target_game=engine.ww.get_target_game(name), ww_team_id=ww_team_id)
engine._ensure_combo_stats(name)
engine.set_active_combo(name, emit=False)
engine.save_combos()
engine._send({"type": "init", **engine.init_payload()})
return True, None
def delete_combo(engine, name: str) -> tuple[bool, str | None]:
name = (name or "").strip()
if not name or name not in engine.combos:
return False, "Select a combo to delete."
del engine.combos[name]
if name in engine.combo_stats:
del engine.combo_stats[name]
if name in engine.combo_expected_ms:
del engine.combo_expected_ms[name]
if name in engine.combo_user_difficulty:
del engine.combo_user_difficulty[name]
if name in engine.combo_step_display_mode:
del engine.combo_step_display_mode[name]
if name in engine.combo_key_images:
del engine.combo_key_images[name]
if name in getattr(engine, "combo_demo_video", {}):
del engine.combo_demo_video[name]
engine.ww.delete_combo(name)
if engine.active_combo_name == name:
engine.active_combo_name = None
engine.active_combo_tokens = []
engine.runtime_steps = []
engine.reset_tracking()
engine.save_combos()
engine._send({"type": "init", **engine.init_payload()})
return True, None
def new_combo(engine) -> None:
engine.active_combo_name = None
engine.active_combo_tokens = []
engine.runtime_steps = []
engine.reset_tracking()
engine._send({"type": "init", **engine.init_payload()})
def clear_history_and_stats(engine) -> None:
engine.reset_tracking()
if engine.active_combo_name:
engine.combo_stats[engine.active_combo_name] = fresh_combo_stats()
engine.save_combos()
engine._send({"type": "clear_results"})
engine._send({"type": "stat_update", "stats": engine.stats_text()})
engine._send({"type": "fail_update", "fail_by_step": engine.failures_by_step()})
engine._send({"type": "timeline_update", "steps": engine.timeline_steps()})
st = engine.get_status()
engine._send({"type": "status", "text": st.text, "color": st.color})