-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
239 lines (202 loc) · 7.54 KB
/
script.js
File metadata and controls
239 lines (202 loc) · 7.54 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
const rawSegments = [
{ name: "Auto", start: "0:20", end: "0:00" },
{ name: "Delay", start: "0:05", end: "0:00" },
{ name: "Transition Shift", start: "2:20", end: "2:10" },
{ name: "Shift 1", start: "2:10", end: "1:45" },
{ name: "Shift 2", start: "1:45", end: "1:20" },
{ name: "Shift 3", start: "1:20", end: "0:55" },
{ name: "Shift 4", start: "0:55", end: "0:30" },
{ name: "End Game", start: "0:30", end: "0:00" }
];
const parseTime = str => {
const [m, s] = str.split(":").map(Number);
return (m * 60 + s) * 1000;
};
// ----- Preprocess segments with durations + cumulative start -----
let cumulative = 0;
const segments = rawSegments.map(seg => {
const startMs = parseTime(seg.start);
const endMs = parseTime(seg.end);
const duration = startMs - endMs;
const segObj = {
name: seg.name,
startMs,
endMs,
duration,
cumulativeStart: cumulative
};
cumulative += duration;
return segObj;
});
// ----- Timer state -----
let state = {
running: false,
startTimestamp: null,
accumulatedTime: 0
};
// restore previous state (optional)
const saved = localStorage.getItem("seq-timer");
if (saved) state = JSON.parse(saved);
function saveState() {
localStorage.setItem("seq-timer", JSON.stringify(state));
}
function totalElapsed() {
if (!state.running) return state.accumulatedTime;
return state.accumulatedTime + (Date.now() - state.startTimestamp);
}
// ----- Find current segment -----
function getCurrentSegment(elapsed) {
for (let i = 0; i < segments.length; i++) {
const seg = segments[i];
if (elapsed < seg.cumulativeStart + seg.duration) {
const elapsedInSegment = elapsed - seg.cumulativeStart;
const displayMs = seg.startMs - elapsedInSegment;
return {
name: seg.name,
displayMs
};
}
}
return null; // finished
}
// ----- Format milliseconds -----
function formatTimeMs(ms) {
const m = Math.floor(ms / 60000);
const s = Math.floor((ms % 60000) / 1000);
return `${m}:${String(s).padStart(2,"0")}`;
}
// ----- Render loop -----
const display = document.getElementById("time");
const phase = document.getElementById("phase");
const durationDisplay = document.getElementById("duration");
let prevRemainingSegSec = null;
function render() {
const elapsed = totalElapsed();
const current = getCurrentSegment(elapsed);
if (!current) {
matchBuzzer();
display.textContent = "0:00";
phase.textContent = "Match Over";
durationDisplay.textContent = "0";
reset(); // added by request to auto reset
return;
}
display.textContent = formatTimeMs(current.displayMs);
const seg = segments.find(s => s.name === current.name);
const elapsedInSegment = elapsed - seg.cumulativeStart;
const remainingSegSec = Math.floor((seg.duration - elapsedInSegment) / 1000);
durationDisplay.textContent = remainingSegSec;
if (remainingSegSec !== prevRemainingSegSec) {
if (remainingSegSec == totalFlashDurationSec) { // heads-up alert
if (elapsed > 20000-totalFlashDurationSec*1000) {
matchSonar();
if (supportVibrate) {
vibrate();
}
flash(brightFlashPerSecond, totalFlashDurationSec);
}
}
if (remainingSegSec == 0) {
switch (phase.textContent) {
// force inject the upcoming phase even though technically not there yet (deviation of 1 second, the 0th second)
// this is for a visual seamless transition, as the colors already change
case "Auto":
matchBuzzer();
phase.textContent = "Delay";
break;
case "Delay":
matchThreeBells();
phase.textContent = "Transition Shift";
break;
case "End Game":
break;
case "Transition Shift":
matchShift();
if (!AutoWinner) {
// if we are red and blue won auto, then we do not change colors
// if we are blue and red won auto, then we do not change colors
phase.textContent = "Shift 1";
break;
} else if (AutoWinner) {
// if we are red and won auto, we change color
// if we are blue and won auto, we change color
switchHub();
phase.textContent = "Shift 1";
break;
}
case "Shift 1":
matchShift();
switchHub();
phase.textContent = "Shift 2";
break;
case "Shift 2":
matchShift();
switchHub();
phase.textContent = "Shift 3";
break;
case "Shift 3":
matchShift();
switchHub();
phase.textContent = "Shift 4";
break;
case "Shift 4":
matchEndGame();
if (!AutoWinner) {
// if we are red and blue won auto, we change color
// if we are blue and red won auto, we change color
switchHub();
phase.textContent = "End Game";
break;
} else if (AutoWinner) {
// if we are red and won auto, then we do not change colors
// if we are blue and won auto, then we do not change colors
phase.textContent = "End Game";
break;
}
default:
// extensive testing says we never hit this... good luck with debug if we ever get here
alert("Something broke:\nEither you borked something or this is an unknown edge case");
break;
}
}
prevRemainingSegSec = remainingSegSec; // store new value
}
requestAnimationFrame(render);
}
render();
// ----- Controls -----
function start() {
if (state.running) return;
if (totalElapsed() == 0) matchCavalryCharge();
state.running = true;
state.startTimestamp = Date.now();
saveState();
}
function pause() {
if (!state.running) return;
state.accumulatedTime = totalElapsed();
state.running = false;
state.startTimestamp = null;
saveState();
}
function reset() {
state = {
running: false,
startTimestamp: null,
accumulatedTime: 0
};
saveState();
// Update display immediately to first segment
const first = segments[0];
display.textContent = formatTimeMs(first.startMs);
phase.textContent = first.name;
// Segment duration countdown
durationDisplay.textContent = Math.ceil(first.duration / 1000);
requestAnimationFrame(render);
// reset UI
AutoWinner = false;
WinAutoNo.style.display = "inline-block", WinAutoYes.style.display = "none";
killFlash();
chooseAlliance(); // edge case: resyncs colors if user reset during a match period that already ran switchHub(); also flips between alliances for JIC compulsion
killAudio();
}