-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebuilt.py
More file actions
381 lines (290 loc) · 13.5 KB
/
Copy pathrebuilt.py
File metadata and controls
381 lines (290 loc) · 13.5 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
REBUILT (2026) Match Progression Visualizer
- Field diagram + phase progression (AUTO, TELEOP segments, END GAME)
- Highlights active HUB(s) per manual rules.
Install:
pip install pygame
Run:
python rebuilt_match_viz.py
"""
from __future__ import annotations
import random
import time
from dataclasses import dataclass
from typing import List, Optional, Tuple
import pygame
# -----------------------------
# Match model
# -----------------------------
@dataclass(frozen=True)
class Phase:
key: str
label: str
duration_s: float
# Optional "official timer display" for this phase (mm:ss start, mm:ss end)
timer_start: Optional[Tuple[int, int]] = None
timer_end: Optional[Tuple[int, int]] = None
# Manual table: AUTO 20s; TELEOP = 2:20 split into:
# TRANSITION 10s, SHIFT1-4 25s each, END GAME 30s.
# Manual also states a 3-second delay between AUTO and TELEOP for scoring purposes.
PHASES: List[Phase] = [
Phase("AUTO", "AUTO", 20.0, (0, 20), (0, 0)),
Phase("DELAY", "AUTO→TELEOP scoring delay", 3.0, None, None), # interstitial
Phase("TRANSITION", "TELEOP: TRANSITION SHIFT", 10.0, (2, 20), (2, 10)),
Phase("SHIFT1", "TELEOP: SHIFT 1", 25.0, (2, 10), (1, 45)),
Phase("SHIFT2", "TELEOP: SHIFT 2", 25.0, (1, 45), (1, 20)),
Phase("SHIFT3", "TELEOP: SHIFT 3", 25.0, (1, 20), (0, 55)),
Phase("SHIFT4", "TELEOP: SHIFT 4", 25.0, (0, 55), (0, 30)),
Phase("ENDGAME", "END GAME", 30.0, (0, 30), (0, 0)),
]
def mmss_to_seconds(mm: int, ss: int) -> int:
return mm * 60 + ss
def seconds_to_mmss(total_s: int) -> Tuple[int, int]:
mm = total_s // 60
ss = total_s % 60
return mm, ss
class MatchState:
"""
Maintains phase progression and HUB activity rules.
HUB rules per manual:
- Both HUBs active during AUTO, TRANSITION SHIFT, END GAME.
- During SHIFT 1-4, only one alliance HUB is active (other inactive).
- Which HUB is inactive in SHIFT 1 depends on AUTO winner:
If RED scored more in AUTO (or selected), RED hub is INACTIVE in SHIFT 1.
If BLUE scored more (or selected), BLUE hub is INACTIVE in SHIFT 1.
Then activity alternates each subsequent shift; END GAME returns both active.
"""
def __init__(self) -> None:
self.reset()
def reset(self) -> None:
self.phase_idx: int = 0
self.phase_elapsed: float = 0.0
self.running: bool = False
self.speed: float = 1.0
# AUTO winner selection:
# "RED", "BLUE", or "TIE" (randomly select at TELEOP start)
self.auto_winner_mode: str = "TIE"
self._auto_winner_selected: Optional[str] = None # resolved "RED"/"BLUE"
@property
def phase(self) -> Phase:
return PHASES[self.phase_idx]
def set_auto_winner_mode(self, mode: str) -> None:
assert mode in ("RED", "BLUE", "TIE")
self.auto_winner_mode = mode
self._auto_winner_selected = None # re-resolve at TELEOP start
def _resolve_auto_winner_if_needed(self) -> None:
if self._auto_winner_selected is not None:
return
if self.auto_winner_mode in ("RED", "BLUE"):
self._auto_winner_selected = self.auto_winner_mode
else:
self._auto_winner_selected = random.choice(["RED", "BLUE"])
def skip_to_next_phase(self) -> None:
self.phase_idx = min(self.phase_idx + 1, len(PHASES) - 1)
self.phase_elapsed = 0.0
# Resolve auto winner when TELEOP begins (start of TRANSITION)
if self.phase.key == "TRANSITION":
self._resolve_auto_winner_if_needed()
def skip_to_previous_phase(self) -> None:
self.phase_idx = max(0, self.phase_idx - 1)
self.phase_elapsed = 0.0
# If we rewind to before TELEOP starts, forget the resolved AUTO winner so it can
# be re-resolved when TRANSITION begins again.
if self.phase.key in ("AUTO", "DELAY"):
self._auto_winner_selected = None
# Resolve auto winner when TELEOP begins (start of TRANSITION)
if self.phase.key == "TRANSITION":
self._resolve_auto_winner_if_needed()
def update(self, dt: float) -> None:
if not self.running:
return
dt *= self.speed
self.phase_elapsed += dt
while self.phase_elapsed >= self.phase.duration_s and self.phase_idx < len(PHASES) - 1:
self.phase_elapsed -= self.phase.duration_s
self.phase_idx += 1
if self.phase.key == "TRANSITION":
self._resolve_auto_winner_if_needed()
def match_over(self) -> bool:
# Match is considered over once ENDGAME has fully elapsed
return self.phase.key == "ENDGAME" and self.phase_elapsed >= self.phase.duration_s
def is_hub_active(self, alliance: str) -> bool:
alliance = alliance.upper()
assert alliance in ("RED", "BLUE")
# After ENDGAME timer reaches 0, match is over: both inactive
if self.match_over():
return False
key = self.phase.key
if key in ("AUTO", "TRANSITION", "ENDGAME", "DELAY"):
return True
if key.startswith("SHIFT"):
self._resolve_auto_winner_if_needed()
winner = self._auto_winner_selected or "RED"
shift_num = int(key.replace("SHIFT", ""))
winner_inactive = (shift_num % 2 == 1)
if alliance == winner:
return not winner_inactive
else:
return winner_inactive
return True
def display_timer_text(self) -> str:
"""
Uses the manual-defined timer values for TELEOP timeframes when available.
For DELAY, shows a short countdown label.
"""
p = self.phase
if p.timer_start and p.timer_end:
start_s = mmss_to_seconds(*p.timer_start)
end_s = mmss_to_seconds(*p.timer_end)
# Count down within this phase
remaining = max(0, int(round(p.duration_s - self.phase_elapsed)))
# Map remaining to timer space
# The timer should go from start_s down to end_s across duration.
# At elapsed=0 -> show start; at elapsed=duration -> show end.
shown = end_s + max(0, min(start_s - end_s, int(round((remaining / p.duration_s) * (start_s - end_s)))))
mm, ss = seconds_to_mmss(shown)
return f"{mm}:{ss:02d}"
else:
remaining = max(0, int(round(p.duration_s - self.phase_elapsed)))
return f"{remaining}s"
# -----------------------------
# Rendering
# -----------------------------
WINDOW_W, WINDOW_H = 1100, 700
FPS = 60
FIELD_MARGIN = 60
FIELD_W, FIELD_H = 900, 500
FIELD_X, FIELD_Y = (WINDOW_W - FIELD_W) // 2, 120
# HUBs (simple rectangles)
HUB_W, HUB_H = 130, 90
RED_HUB_RECT = pygame.Rect(FIELD_X + 80, FIELD_Y + (FIELD_H - HUB_H) // 2, HUB_W, HUB_H)
BLUE_HUB_RECT = pygame.Rect(FIELD_X + FIELD_W - 80 - HUB_W, FIELD_Y + (FIELD_H - HUB_H) // 2, HUB_W, HUB_H)
# Minimal "tower" markers behind hubs
TOWER_W, TOWER_H = 30, 160
RED_TOWER_RECT = pygame.Rect(RED_HUB_RECT.left - 40, FIELD_Y + (FIELD_H - TOWER_H) // 2, TOWER_W, TOWER_H)
BLUE_TOWER_RECT = pygame.Rect(BLUE_HUB_RECT.right + 10, FIELD_Y + (FIELD_H - TOWER_H) // 2, TOWER_W, TOWER_H)
def draw_text(surface: pygame.Surface, text: str, font: pygame.font.Font, x: int, y: int, color=(20, 20, 20)) -> None:
img = font.render(text, True, color)
surface.blit(img, (x, y))
def main() -> None:
pygame.init()
pygame.display.set_caption("REBUILT Match Progression Visualizer")
screen = pygame.display.set_mode((WINDOW_W, WINDOW_H))
clock = pygame.time.Clock()
font_big = pygame.font.SysFont("Arial", 28, bold=True)
font_med = pygame.font.SysFont("Arial", 20)
font_small = pygame.font.SysFont("Arial", 16)
state = MatchState()
show_help = True
last_time = time.perf_counter()
running = True
while running:
dt = clock.tick(FPS) / 1000.0
_ = dt # pygame dt already available; we'll use perf_counter to be robust
now = time.perf_counter()
dt = now - last_time
last_time = now
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
running = False
elif event.key == pygame.K_SPACE:
state.running = not state.running
elif event.key == pygame.K_r:
state.reset()
elif event.key == pygame.K_s:
state.skip_to_next_phase()
elif event.key == pygame.K_d:
state.skip_to_previous_phase()
elif event.key == pygame.K_h:
show_help = not show_help
elif event.key in (pygame.K_EQUALS, pygame.K_PLUS, pygame.K_KP_PLUS):
state.speed = min(8.0, state.speed + 0.25)
elif event.key in (pygame.K_MINUS, pygame.K_KP_MINUS):
state.speed = max(0.125, state.speed - 0.25)
elif event.key == pygame.K_1:
state.set_auto_winner_mode("RED")
elif event.key == pygame.K_2:
state.set_auto_winner_mode("BLUE")
elif event.key == pygame.K_0:
state.set_auto_winner_mode("TIE")
state.update(dt)
# Colors
bg = (245, 246, 248)
field_bg = (235, 235, 235)
line = (80, 80, 80)
red = (210, 60, 60)
blue = (60, 110, 210)
inactive_gray = (170, 170, 170)
active_glow = (255, 215, 0)
screen.fill(bg)
# Header
draw_text(screen, "REBUILT Match Progression", font_big, 40, 25)
draw_text(screen, f"Phase: {state.phase.label}", font_med, 40, 65)
status = "ENDED" if state.match_over() else ("RUNNING" if state.running else "PAUSED")
draw_text(
screen,
f"Timer: {state.display_timer_text()} | Speed: {state.speed:.2f}x | {status}",
font_med, 40, 90
)
# Field
field_rect = pygame.Rect(FIELD_X, FIELD_Y, FIELD_W, FIELD_H)
pygame.draw.rect(screen, field_bg, field_rect, border_radius=10)
pygame.draw.rect(screen, line, field_rect, width=2, border_radius=10)
# Center line (visual)
pygame.draw.line(screen, line, (FIELD_X + FIELD_W // 2, FIELD_Y + 15), (FIELD_X + FIELD_W // 2, FIELD_Y + FIELD_H - 15), 2)
# Towers
pygame.draw.rect(screen, red, RED_TOWER_RECT, border_radius=6)
pygame.draw.rect(screen, blue, BLUE_TOWER_RECT, border_radius=6)
# HUB activity
red_active = state.is_hub_active("RED")
blue_active = state.is_hub_active("BLUE")
# HUB rectangles
pygame.draw.rect(screen, red if red_active else inactive_gray, RED_HUB_RECT, border_radius=10)
pygame.draw.rect(screen, blue if blue_active else inactive_gray, BLUE_HUB_RECT, border_radius=10)
# Glow outline when active
if red_active:
pygame.draw.rect(screen, active_glow, RED_HUB_RECT.inflate(10, 10), width=4, border_radius=12)
if blue_active:
pygame.draw.rect(screen, active_glow, BLUE_HUB_RECT.inflate(10, 10), width=4, border_radius=12)
# Labels
draw_text(screen, "RED HUB", font_med, RED_HUB_RECT.left + 18, RED_HUB_RECT.top + 30, (255, 255, 255))
draw_text(screen, "BLUE HUB", font_med, BLUE_HUB_RECT.left + 18, BLUE_HUB_RECT.top + 30, (255, 255, 255))
# AUTO winner mode display
resolved = state._auto_winner_selected if state._auto_winner_selected else "(unresolved)"
draw_text(screen,
f"AUTO winner setting: {state.auto_winner_mode} | Resolved (for SHIFT logic): {resolved}",
font_small, 40, FIELD_Y + FIELD_H + 18)
# Help (draw inside field so it stays on-screen)
if show_help:
help_lines = [
"Controls:",
" Space = Pause/Unpause",
" R = Reset to start of AUTO",
" S = Skip to next phase",
" D = Previous phase",
" 1 = Force AUTO winner RED (affects SHIFT hub activity)",
" 2 = Force AUTO winner BLUE (affects SHIFT hub activity)",
" 0 = AUTO tie → random selection (FMS-like)",
" + / - = Speed up / slow down",
" H = Toggle this help",
" Q = Quit",
]
panel_pad = 12
line_h = 18
panel_w = 360
panel_h = panel_pad * 2 + line_h * len(help_lines)
panel_x = FIELD_X + 20
panel_y = FIELD_Y + 20
panel_rect = pygame.Rect(panel_x, panel_y, panel_w, panel_h)
pygame.draw.rect(screen, (250, 250, 250), panel_rect, border_radius=10)
pygame.draw.rect(screen, line, panel_rect, width=1, border_radius=10)
for i, line_txt in enumerate(help_lines):
draw_text(screen, line_txt, font_small, panel_x + panel_pad, panel_y + panel_pad + i * line_h)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()