-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuzz.html
433 lines (367 loc) · 18.1 KB
/
buzz.html
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
<!DOCTYPE html>
<html lang="en">
<!--
This is all just super quick & dirty junk html and js to get this mvp/prototype working.
Please don't judge it too harshly, it only needs to work, and quickly :)
-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="/buzzerico.ico">
<title>Buzzer</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
crossorigin="anonymous"></script>
<audio id="sfx-buzzer" src="media/bewoop.mp3" preload="auto"></audio>
<div class="container">
<div class="row justify-content-md-center">
<div class="col-fluid">
<div class="container border border-3 shadow-sm p-3 mb-5 bg-body rounded">
<h1>Buzzer Beta! <button class="btn btn-primary" type="button" data-bs-toggle="collapse"
data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Toggle Options
</button></h1>
<div class=".collapse show" id="collapseExample">
<div class="input-group mb-3">
<span class="input-group-text">Room ID</span>
<input id="roomcode" class="form-control" type="text" placeholder="XXXX">
<button class="btn btn-outline-secondary" id="join-room" type="button">Join Room</button>
<button class="btn btn-outline-secondary" id="create-room" type="button">Create
Room</button>
<button class="btn btn-outline-secondary" id="leave-room" type="button" disabled>Leave
Room</button>
</div>
<div class="alert alert-danger collapse" role="alert" id="alert-room-not-found">
Room not found! Did you put the right room-ID in?
</div>
<div class="input-group mb-3">
<input id="name" type="text" class="form-control" placeholder="Display Name"
aria-label="Username" aria-describedby="basic-addon1" value="New Player">
<button class="btn btn-outline-primary" id="send-name" type="button">Set Name</button>
</div>
<div class="row-cols-2">
<div class="col">
<div class="input-group mb-3">
<label for="buzzer-volume" class="form-label" value="10">Buzzer Volume 🔊</label>
<input type="range" class="form-range" id="buzzer-volume">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-auto lobby-active">
<div class="container border border-3 shadow-sm p-3 mb-5 bg-body rounded">
<h3>Controls</h3>
<button class="btn btn-outline-primary" id="hit-buzzer" type="button" disabled>BUZZER!</button>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="admin_buzzer_lock" disabled>
<label class="form-check-label" for="flexSwitchCheckDefault">Buzzer Lock (⭐ only)</label>
</div>
<button class="btn btn-outline-secondary admin" id="reset-buzzers" type="button" disabled>Reset
Buzzers</button>
<div class="form-check admin-button admin">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="admin_first_to_buzz"
checked disabled>
<label class="form-check-label" for="flexRadioDefault2">
First To Buzz
</label>
</div>
<div class="form-check admin-button admin">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="admin_buzzer_race"
disabled>
<label class="form-check-label" for="flexRadioDefault2">
Buzzer Race
</label>
</div>
</div>
</div>
<div class="col-md lobby-active">
<div class="container border border-3 shadow-sm p-3 mb-5 bg-body rounded">
<h3>Players</h3>
<ul class="list-group" id="player-list-display">
</ul>
</div>
</div>
</div>
</div>
<!--<textarea id="chat" style="display:block; width:600px; height:400px; box-sizing: border-box" cols="30"
rows="10"></textarea>-->
<script>
const roomcode = document.querySelector("#roomcode");
const name_field = document.querySelector("#name");
const join_btn = document.querySelector("#join-room");
const create_btn = document.querySelector("#create-room");
const leave_btn = document.querySelector("#leave-room");
//const textarea = document.querySelector("#chat");
const input = document.querySelector("#input");
const send_name = document.querySelector("#send-name");
const admin_buzzer_lock = document.querySelector("#admin_buzzer_lock");
const admin_first_to_buzz = document.querySelector("#admin_first_to_buzz");
const admin_buzzer_race = document.querySelector("#admin_buzzer_race");
const admin_buzzer_reset = document.querySelector("#reset-buzzers");
const hit_buzzer = document.querySelector("#hit-buzzer");
const buzzer_volume = document.querySelector("#buzzer-volume");
const buzzer_sound = document.querySelector("#sfx-buzzer");
const alert_room_not_found = document.querySelector("#alert-room-not-found");
const room_not_found_collapse = new bootstrap.Collapse(alert_room_not_found, {
toggle: false
});
const player_list = document.querySelector("#player-list-display");
const active_when_unjoined = [join_btn, create_btn, roomcode];
const active_when_joined = [leave_btn, send_name];
const admin_buttons = [admin_buzzer_lock, admin_first_to_buzz, admin_buzzer_race, admin_buzzer_reset];
const admin_containers = document.querySelectorAll(".admin");
const round_active_containers = document.querySelectorAll(".lobby-active");
let decorate_socket = function (websocket) {
websocket["update_name"] = function (new_name) {
let set_name = {
op: "set_name",
value: new_name
};
websocket.send(JSON.stringify(set_name));
}
websocket["buzzer_hit"] = function () {
let buzzer_event = {
op: "buzzer_hit",
value: ""
}
websocket.send(JSON.stringify(buzzer_event));
}
websocket["reset_buzzers"] = function () {
let reset_event = {
op: "reset_buzzers",
value: ""
}
websocket.send(JSON.stringify(reset_event));
}
websocket["set_lock"] = function (is_locked) {
let lock_event = {
op: "set_buzzer_lock",
value: `${is_locked}`
}
websocket.send(JSON.stringify(lock_event));
}
websocket["set_mode"] = function (new_mode) {
let mode_event = {
op: "set_mode",
value: `${new_mode}`
}
websocket.send(JSON.stringify(mode_event));
}
return websocket
}
const escapeHtml = (unsafe) => {
return unsafe.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
}
let client_state = {
// 0 - disconnected, 1 - connecting, 2 - connected
connection_state: 0,
my_id: -1,
current_websocket: null,
pending_messages: [],
prev_buzzer_count: 0, // when this increases we need to fire the buzzer
};
let update_room_state = function (state) {
// set basic variables
roomcode.value = state.room_id;
// can we buzz?
let has_buzzed = state.activated_buzzers.indexOf(client_state.my_id) > -1;
if (state.room_mode == "FirstToBuzz") {
hit_buzzer.disabled = state.room_locked || has_buzzed || state.activated_buzzers.length > 0;
admin_first_to_buzz.checked = true;
} else if (state.room_mode = "BuzzerRace") {
hit_buzzer.disabled = (state.room_locked || has_buzzed)
admin_buzzer_race.checked = true;
}
// admin buzzer stuff
admin_buzzer_lock.checked = state.room_locked;
// if the captain status has changed then we wanna re-evaluate the disabled-ness of
// some buttons
let captain = state.room_captain;
let is_captain = captain == client_state.my_id;
admin_buttons.map(x => x.disabled = !is_captain);
admin_containers.forEach(x => {
if (is_captain) {
x.style.display = "block";
} else {
x.style.display = "none";
}
})
// any time the total activated buzzers increases we wanna play the sound
if (state.activated_buzzers.length > client_state.prev_buzzer_count) {
// fire buzzer noise
buzzer_sound.currentTime = 0.0;
buzzer_sound.play();
}
client_state.prev_buzzer_count = state.activated_buzzers.length;
// show players
let new_player_html = Object.keys(state.participants)
.map(id_str => {
let name = state.participants[id_str].name;
let id = parseInt(id_str);
if (id == client_state.my_id) {
name += " (you)"
}
if (id == captain) {
name = "⭐" + escapeHtml(name);
}
let badge = "";
let buzzer_index = state.activated_buzzers.indexOf(id);
if (buzzer_index > -1) {
console.log(`room state: ${state.room_mode}`);
// if the mode is first to buzz we can just show an alarm bell
if (state.room_mode == "FirstToBuzz") {
badge = `<span class="badge bg-primary rounded-pill">🔔</span>`;
} else if (state.room_mode == "BuzzerRace") {
badge = `<span class="badge bg-primary rounded-pill">🔔 #${buzzer_index + 1}</span>`;
}
}
return `<li class="list-group-item">${name} ${badge}</li>`;
}).join('');
player_list.innerHTML = new_player_html;
}
let room_behavior = async function () {
active_when_unjoined.map(x => x.disabled = true);
active_when_joined.map(x => x.disabled = false);
admin_buttons.map(x => x.disabled = true);
while (client_state.connection_state == 1) {
await new Promise(resolve => setTimeout(resolve, 50));
// wait to connect
}
if (client_state.connection_state == 2) {
// we're joining! otherwise something went wrong
client_state.current_websocket.update_name(name_field.value);
round_active_containers.forEach(x => {
x.style.display = "block";
});
room_not_found_collapse.hide();
}
while (client_state.connection_state == 2) {
// 50ms tick
await new Promise(resolve => setTimeout(resolve, 5));
while (client_state.pending_messages.length > 0) {
let msg = client_state.pending_messages.shift();
if (msg["your_id"]) {
client_state.my_id = msg.your_id;
console.log("received authoritative id: " + client_state.my_id);
} else if (msg["room_id"]) {
update_room_state(msg);
}
}
}
console.log("cleaning up");
active_when_unjoined.map(x => x.disabled = false);
active_when_joined.map(x => x.disabled = true);
admin_buttons.map(x => x.disabled = true);
round_active_containers.forEach(x => {
x.style.display = "none";
});
roomcode.value = "";
};
// clicking leave wont trigger cleanup directly, that will happen via
// the websocket onclose callback.
let on_leave = function (e) {
client_state.current_websocket.close();
}
leave_btn.addEventListener("click", on_leave);
let connect_behavior = function (room_code) {
let ws_protocol = location.hostname == "localhost" ? "ws://" : "wss://";
const websocket = decorate_socket(new WebSocket(ws_protocol + location.host + "/buzzer?room=" + room_code));
client_state.current_websocket = websocket;
client_state.connection_state = 1;
websocket.onopen = function () {
console.log("connection opened");
client_state.connection_state = 2;
}
websocket.onclose = function () {
console.log("connection closed");
client_state.connection_state = 0;
client_state.current_websocket = null;
}
websocket.onmessage = function (e) {
client_state.pending_messages.push(JSON.parse(e.data));
//textarea.value += e.data + "\r\n"; // todo: temporary
}
room_behavior();
}
create_btn.addEventListener("click", function (e) {
connect_behavior("new");
});
join_btn.addEventListener("click", function (e) {
// make sure room is real and can be joined
let request = new XMLHttpRequest();
if (location.hostname == 'localhost') {
request.open("GET", `http://${location.host}/room_exists?room_id=${roomcode.value}`);
} else {
request.open("GET", `https://${location.host}/room_exists?room_id=${roomcode.value}`);
}
request.send();
request.onload = () => {
if (request.status == 200) {
// room exists! do the join
connect_behavior(roomcode.value);
room_not_found_collapse.hide();
} else if (request.status = 404) {
room_not_found_collapse.show();
}
}
});
let stored_name = localStorage.getItem("client_name");
if (stored_name != null) {
name_field.value = stored_name;
}
send_name.addEventListener("click", function (e) {
localStorage.setItem("client_name", name_field.value);
if (client_state.connection_state == 2) {
client_state.current_websocket.update_name(name_field.value);
}
})
hit_buzzer.addEventListener("click", function (e) {
if (client_state.connection_state == 2) {
client_state.current_websocket.buzzer_hit();
}
})
admin_buzzer_reset.addEventListener("click", function (e) {
if (client_state.connection_state == 2) {
client_state.current_websocket.reset_buzzers();
}
})
admin_buzzer_lock.addEventListener("click", function (e) {
if (client_state.connection_state == 2) {
client_state.current_websocket.set_lock(admin_buzzer_lock.checked);
}
})
admin_first_to_buzz.addEventListener("click", function (e) {
client_state.current_websocket.set_mode("first_to_buzz");
})
admin_buzzer_race.addEventListener("click", function (e) {
client_state.current_websocket.set_mode("buzz_race");
})
Number.prototype.clamp = function (min, max) {
return Math.min(Math.max(this, min), max);
};
let stored_volume_str = parseInt(localStorage.getItem("buzzer_volume"));
if (stored_volume_str != null) {
let stored_volume = parseInt(stored_volume_str);
buzzer_volume.value = stored_volume.clamp(0, 100);
buzzer_sound.volume = buzzer_volume.value / 100.0;
}
buzzer_sound.volume = buzzer_volume.value / 100.0;
buzzer_volume.addEventListener("click", function (e) {
localStorage.setItem("buzzer_volume", buzzer_volume.value);
console.log(`volume: ${buzzer_volume.value}`);
buzzer_sound.currentTime = 0.0;
buzzer_sound.volume = buzzer_volume.value / 100.0;
buzzer_sound.play();
});
round_active_containers.forEach(x => {
x.style.display = "none";
});
</script>
</body>
</html>