-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_interface_bridge.html
More file actions
487 lines (425 loc) · 15.4 KB
/
Copy pathquiz_interface_bridge.html
File metadata and controls
487 lines (425 loc) · 15.4 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pannello di Controllo Quiz</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #1e1e1e;
color: #ffffff;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
height: 100vh;
box-sizing: border-box;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
background: #2c3e50;
padding: 15px 30px;
border-radius: 10px;
margin-bottom: 15px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
.header h1 {
margin: 0;
font-size: 24px;
text-transform: uppercase;
letter-spacing: 2px;
}
.help-text {
font-size: 13px;
color: #bdc3c7;
margin-left: 10px;
font-weight: normal;
text-transform: none;
}
/* Pannello Opzioni */
.options-panel {
background-color: #2a2a2a;
border: 1px solid #3d3d3d;
border-radius: 10px;
padding: 12px 25px;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 20px;
box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}
.options-title {
font-weight: bold;
text-transform: uppercase;
font-size: 14px;
color: #3498db;
letter-spacing: 1px;
}
.toggle-option {
display: flex;
align-items: center;
cursor: pointer;
font-size: 15px;
user-select: none;
}
.toggle-option input {
margin-right: 10px;
width: 18px;
height: 18px;
cursor: pointer;
accent-color: #3498db;
}
button {
padding: 12px 25px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.2s;
}
button:hover {
background-color: #2980b9;
}
.status-container {
font-size: 18px;
}
.status {
font-weight: bold;
color: #e74c3c;
text-transform: uppercase;
}
.status.connected {
color: #2ecc71;
}
/* Griglia principale per i 4 concorrenti */
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 20px;
flex-grow: 1;
}
/* Stile base del riquadro */
.box {
border-radius: 15px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.3);
position: relative;
overflow: hidden;
transition: transform 0.3s ease;
}
/* Badge Posizione di Arrivo */
.position-badge {
position: absolute;
top: 15px;
left: 20px;
background: rgba(0, 0, 0, 0.4);
border: 2px solid rgba(255, 255, 255, 0.2);
color: #ffffff;
font-size: 1.8rem;
font-weight: 900;
padding: 5px 15px;
border-radius: 10px;
min-width: 45px;
text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
display: none; /* Nascosto se non c'è risposta */
}
/* Colori dei pulsanti */
.box-1 { background-color: #c0392b; color: white; } /* Rosso */
.box-2 { background-color: #f1c40f; color: #2c3e50; } /* Giallo */
.box-3 { background-color: #27ae60; color: white; } /* Verde */
.box-4 { background-color: #2980b9; color: white; } /* Blu */
.box h2 {
margin: 0 0 10px 0;
font-size: 2.2rem;
text-transform: uppercase;
letter-spacing: 1px;
opacity: 0.95;
}
/* Stile cronometro olimpico */
.time-display {
font-family: 'Courier New', Courier, monospace;
font-size: 4.8rem;
font-weight: 900;
background: rgba(0, 0, 0, 0.25);
padding: 10px 30px;
border-radius: 12px;
margin: 10px 0;
min-width: 380px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.4);
}
.status-msg {
font-size: 2rem;
font-weight: bold;
min-height: 40px;
text-transform: uppercase;
}
/* Stile per il Vincitore */
.winner {
border: 8px solid #FFD700;
animation: pulse-glow 1.5s infinite alternate;
transform: scale(1.02);
z-index: 10;
box-shadow: 0 0 30px #FFD700;
}
.winner .position-badge {
background: #FFD700;
color: #111;
border-color: #FFD700;
}
.winner .time-display {
background: #FFD700;
color: #111;
text-shadow: none;
}
@keyframes pulse-glow {
from { box-shadow: 0 0 20px #FFD700; }
to { box-shadow: 0 0 50px #FFD700, 0 0 20px inset #FFD700; }
}
/* Stile per gli squalificati */
.penalized {
opacity: 0.7;
background-color: #333 !important;
color: #aaa !important;
}
.penalized .time-display {
text-decoration: line-through;
color: #e74c3c;
background: rgba(0, 0, 0, 0.5);
}
.penalized .status-msg {
color: #e74c3c;
font-size: 2.5rem;
}
</style>
</head>
<body>
<div class="header">
<h1>
QuizBuzzer
<span class="help-text">(SPAZIO per cambiare modalità)</span>
</h1>
<div class="status-container">
Stato: <span id="status" class="status">Disconnesso</span>
</div>
<button id="connectBtn">Connetti Dispositivo</button>
</div>
<!-- Pannello Opzioni -->
<div class="options-panel">
<span class="options-title">⚙️ Opzioni Visualizzazione:</span>
<label class="toggle-option">
<input type="checkbox" id="deltaToggle">
Mostra distacco dal vincitore (+ms)
</label>
</div>
<div class="grid-container">
<!-- Concorrente 1 (Rosso) -->
<div id="p1" class="box box-1">
<div class="position-badge">--</div>
<h2>Rosso</h2>
<div class="time-display">--,--- ms</div>
<div class="status-msg"></div>
</div>
<!-- Concorrente 2 (Giallo) -->
<div id="p2" class="box box-2">
<div class="position-badge">--</div>
<h2>Giallo</h2>
<div class="time-display">--,--- ms</div>
<div class="status-msg"></div>
</div>
<!-- Concorrente 3 (Verde) -->
<div id="p3" class="box box-3">
<div class="position-badge">--</div>
<h2>Verde</h2>
<div class="time-display">--,--- ms</div>
<div class="status-msg"></div>
</div>
<!-- Concorrente 4 (Blu) -->
<div id="p4" class="box box-4">
<div class="position-badge">--</div>
<h2>Blu</h2>
<div class="time-display">--,--- ms</div>
<div class="status-msg"></div>
</div>
</div>
<script>
let port;
let reader;
let serialBuffer = "";
let lastSummaryData = null; // Mantiene in memoria l'ultimo report per l'aggiornamento dinamico delle opzioni
const connectBtn = document.getElementById('connectBtn');
const statusText = document.getElementById('status');
const deltaToggle = document.getElementById('deltaToggle');
if (!("serial" in navigator)) {
alert("API Web Serial non supportata in questo browser. Usa Chrome o Edge.");
connectBtn.disabled = true;
}
connectBtn.addEventListener('click', async () => {
try {
port = await navigator.serial.requestPort();
await port.open({ baudRate: 115200 });
statusText.innerText = 'Connesso';
statusText.classList.add('connected');
connectBtn.style.display = 'none';
const textDecoder = new TextDecoderStream();
port.readable.pipeTo(textDecoder.writable);
reader = textDecoder.readable.getReader();
readLoop();
} catch (error) {
console.error('Errore di connessione:', error);
statusText.innerText = 'Errore di connessione';
}
});
async function readLoop() {
while (true) {
const { value, done } = await reader.read();
if (done) {
reader.releaseLock();
break;
}
serialBuffer += value;
let lines = serialBuffer.split('\n');
serialBuffer = lines.pop();
for (let line of lines) {
line = line.trim();
if (!line) continue;
if (line.startsWith('#')) {
handleEvent(line.substring(1));
} else {
console.log("LOG DISPOSITIVO:", line);
}
}
}
}
// --- GESTIONE DELLE OPZIONI DINAMICHE ---
deltaToggle.addEventListener('change', () => {
if (lastSummaryData) {
renderSummary(lastSummaryData);
}
});
// --- GESTIONE DELLA BARRA SPAZIATRICE ---
document.addEventListener('keydown', async (event) => {
if (event.code === 'Space') {
event.preventDefault(); // Evita lo scroll della pagina
if (port && port.writable) {
try {
const writer = port.writable.getWriter();
const data = new TextEncoder().encode('{"action":"click"}\n');
await writer.write(data);
writer.releaseLock();
console.log("Comando click inviato al bridge!");
} catch (err) {
console.error("Errore nell'invio del comando seriale:", err);
}
} else {
console.warn("Porta seriale non connessa. Impossibile inviare il comando.");
}
}
});
function handleEvent(jsonData) {
try {
const payload = JSON.parse(jsonData);
if (payload.type === 'ready') {
console.log("Bridge inizializzato e pronto!");
}
else if (payload.type === 'mode') {
if (payload.value === 'arm') {
resetUI();
}
}
else if (payload.type === 'summary') {
lastSummaryData = payload; // Salva il riepilogo
renderSummary(payload);
}
} catch (e) {
console.error("Errore nel parsing del JSON:", e, jsonData);
}
}
function resetUI() {
lastSummaryData = null;
for (let i = 1; i <= 4; i++) {
const box = document.getElementById(`p${i}`);
const posEl = box.querySelector('.position-badge');
const timeEl = box.querySelector('.time-display');
const statusEl = box.querySelector('.status-msg');
box.classList.remove('winner', 'penalized');
posEl.style.display = 'none';
posEl.innerHTML = '--';
timeEl.innerHTML = '--,--- ms';
statusEl.innerHTML = '';
}
}
function renderSummary(data) {
const showDelta = deltaToggle.checked;
// 1. Calcola l'ordine di arrivo per chi ha risposto (tempo > 0)
const responders = data.results
.filter(res => res.time > 0)
.sort((a, b) => a.time - b.time);
// Mappa per associare ad ogni ID la sua posizione (1°, 2°, 3°, 4°)
const positions = {};
responders.forEach((res, index) => {
positions[res.id] = index + 1;
});
// 2. Trova il tempo del vincitore
const winnerResult = data.results.find(res => res.id === data.winnerId);
const winnerTime = (winnerResult && winnerResult.time > 0) ? winnerResult.time : 0;
// 3. Renderizza ogni box
data.results.forEach(res => {
const box = document.getElementById(`p${res.id}`);
const posEl = box.querySelector('.position-badge');
const timeEl = box.querySelector('.time-display');
const statusEl = box.querySelector('.status-msg');
// Resetta classi temporanee
box.classList.remove('winner', 'penalized');
const hasResponded = res.time > 0;
if (hasResponded) {
// Posizione di arrivo
posEl.style.display = 'block';
posEl.innerHTML = `${positions[res.id]}°`;
// Formattazione tempo (normale o Delta)
if (showDelta && res.id !== data.winnerId && winnerTime > 0) {
const delta = res.time - winnerTime;
timeEl.innerHTML = `+${formatTime(delta)}`;
} else {
timeEl.innerHTML = formatTime(res.time);
}
} else {
// Non ha risposto
posEl.style.display = 'none';
timeEl.innerHTML = '--,--- ms';
}
// Gestione Penalità / Squalifica
if (res.penalty) {
box.classList.add('penalized');
statusEl.innerHTML = '🚫 SQUALIFICATO';
} else {
statusEl.innerHTML = '';
}
// Evidenzia il vincitore (solo se ha risposto ed è valido)
if (res.id === data.winnerId && !res.penalty && hasResponded) {
box.classList.add('winner');
statusEl.innerHTML = '👑 VINCITORE';
}
});
}
// Helper per la formattazione dei millisecondi in formato italiano (es. 3.246,5 ms)
function formatTime(ms) {
const formatted = new Intl.NumberFormat("it-IT", {
minimumFractionDigits: 0,
maximumFractionDigits: 3
}).format(ms);
return `${formatted} ms`;
}
</script>
</body>
</html>