-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.js
More file actions
155 lines (131 loc) · 3.76 KB
/
display.js
File metadata and controls
155 lines (131 loc) · 3.76 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
let steps = [];
let currentStep = 0;
let timer = null;
let timeLeft = 0;
let totalTime = 0;
let elapsed = 0;
let paused = false;
let inStopPhase = false;
let isCustom = false;
const display = document.getElementById("timeDisplay");
const progressBar = document.getElementById("progressBar");
function formatTime(s) {
const m = Math.floor(s / 60);
const sec = s % 60;
return m.toString().padStart(2, '0') + ":" + sec.toString().padStart(2, '0');
}
function updateProgress() {
const percent = 100 - (elapsed / totalTime) * 100;
progressBar.style.width = percent + "%";
if (timeLeft <= 10) {
display.style.color = display.style.color === "red" ? "white" : "red";
progressBar.style.backgroundColor = "red";
} else if (timeLeft <= 30) {
progressBar.style.backgroundColor = "orange";
display.style.color = "white";
} else {
progressBar.style.backgroundColor = "green";
display.style.color = "white";
}
}
function shouldHaveStopPhase(current, next) {
if ((current === 120 || current === 180) && next === 60) return true;
if (current === 60 && next === 30) return true;
return false;
}
function showIntermediateStop(callback, forceStop = false) {
inStopPhase = true;
const stopSeconds = 5;
const previousDuration = steps[currentStep - 1];
const nextDuration = steps[currentStep];
let message = "GO";
let color = "limegreen";
if (forceStop || shouldHaveStopPhase(previousDuration, nextDuration)) {
message = "STOP";
color = "red";
}
display.textContent = message;
display.style.color = color;
progressBar.style.width = "0%";
progressBar.style.backgroundColor = color;
const stopStart = Date.now();
const stopTimer = setInterval(() => {
if (!paused) {
const stopElapsed = Math.floor((Date.now() - stopStart) / 1000);
if (stopElapsed >= stopSeconds) {
clearInterval(stopTimer);
display.style.color = "white";
inStopPhase = false;
callback();
}
}
}, 200);
}
function startNextStep() {
if (currentStep >= steps.length) {
display.textContent = "FIN DU COMBAT";
progressBar.style.width = "0%";
display.style.color = "white";
if (isCustom) {
showIntermediateStop(() => {}, true);
}
return;
}
timeLeft = steps[currentStep];
totalTime = timeLeft;
elapsed = 0;
display.textContent = formatTime(timeLeft);
updateProgress();
// Envoie la mise à jour du temps toutes les secondes
sendTimeUpdate();
timer = setInterval(() => {
if (!paused) {
timeLeft--;
elapsed++;
display.textContent = formatTime(timeLeft);
updateProgress();
sendTimeUpdate();
if (timeLeft <= 0) {
clearInterval(timer);
currentStep++;
if (currentStep < steps.length) {
showIntermediateStop(startNextStep);
} else {
display.textContent = "FIN";
progressBar.style.width = "0%";
display.style.color = "white";
if (isCustom) {
showIntermediateStop(() => {}, true);
}
}
}
}
}, 1000);
}
function sendTimeUpdate() {
if (window.opener) {
window.opener.postMessage({ action: "timeUpdate", timeLeft: timeLeft }, "*");
}
}
window.addEventListener("message", (event) => {
if (!event.data) return;
if (event.data.action === "start") {
steps = event.data.durations;
isCustom = event.data.isCustom || false;
currentStep = 0;
paused = false;
inStopPhase = false;
clearInterval(timer);
startNextStep();
} else if (event.data.action === "pause") {
paused = true;
} else if (event.data.action === "resume") {
paused = false;
} else if (event.data.action === "reset") {
clearInterval(timer);
currentStep = 0;
paused = false;
inStopPhase = false;
startNextStep();
}
});