This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
476 lines (432 loc) · 16.5 KB
/
index.html
File metadata and controls
476 lines (432 loc) · 16.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
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>choices</title>
<style>
@keyframes pulse {
0% {transform: scale(0.95);}
70% {transform: scale(1);}
100% {transform: scale(0.95);}
}
@keyframes barberpole {
100% {background-position: 100% 100%;}
}
* {
background-color:rgb(20, 20, 52);
font-family: fantasy;
}
body {
background-color: transparent;
overflow: hidden;
}
h1 {
text-align: center;
color: white;
}
.player-grid-container {
scale: 80%;
margin-top: -1vh;
margin-bottom: -2vh;
display: grid;
grid-template-columns: repeat(2, 1fr);
background-color: transparent;
column-gap: 20vw;
row-gap: 2vh;
align-items: center;
}
.player-grid-item {
border: 1.5px solid rgba(0, 0, 0, 0.8);
padding: 2vw;
font-size: large;
text-align: center;
border-radius: 10px;
background: repeating-linear-gradient(
45deg,
#ccffff,
#ccffff 10px,
#acffff 10px,
#acffff 20px
);
}
img {
border-radius: 10px;
}
.choice-grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
background-color: transparent;
column-gap: 0vw;
row-gap: 4vh;
align-items: center;
}
.choice-grid-item {
border: 1.5px solid rgba(0, 0, 0, 0.8);
border-radius: 5vw;
padding: 0px;
font-size: 20px;
margin: auto;
font-style: italic;
text-wrap: balance;
align-items: center;
display:inline-block;
text-align: center;
background: repeating-linear-gradient(
135deg,
#fdccff,
#fdccff 10px,
#f7b9ff 10px,
#f7b9ff 20px
);
}
.choice-caption {
background-color: transparent;
margin-left: -100%;
margin-right: -100%;
text-align: center;
}
.active-choice {
background-color: #ce901ee0;
box-shadow: 0px 0px 5vh 1vh #ce901ee0;
background-size: 200% 200%;
animation: barberpole 10s linear infinite;
}
.open-slot {
color: rgb(120, 120, 120)
}
.taken-slot {
color: rgb(0, 0, 0)
}
.question-box {
text-align: center;
margin-top: 2rem;
margin-bottom: 2rem;
font-size: xx-large;
color: white
}
img {
object-fit: cover;
width: 8vw;
height: 8vw;
background-color: inherit;
}
figure {
height: 15vw;
width: 10vw;
background-color: transparent;
align-items: center;
color: black;
text-wrap: nowrap;
}
.ready-button {
width: 60vw;
height: 13vh;
margin: auto;
margin-top: 5vh;
padding-top: 8vh;
text-align: center;
vertical-align: middle;
border: 1.5px solid rgb(0, 0, 0);
border-radius: 20px;
background: repeating-linear-gradient(
45deg,
#4e6051,
#4e6051 10px,
#39413a 10px,
#39413a 20px
);
}
.ready-button-active {
background: repeating-linear-gradient(
45deg,
#1dd83c,
#1dd83c 10px,
#4aff56 10px,
#4aff56 20px
);
background-size: 200% 200%;
animation: barberpole 8s linear infinite reverse;
background-color: #1ece44e0;
box-shadow: 0px 0px 5vh 1vh #1ece44e0;
}
.ready-button-text {
background-color: transparent;
font-size: xx-large;
}
.view-inactive {
display: none;
}
.waiting-text {
color: whitesmoke;
text-align: center;
font-size: xx-large;
font-style: italic;
margin-top: 30vh;
}
.results-grid-container {
scale: 80%;
margin-top: -1vh;
margin-bottom: -2vh;
display: grid;
grid-template-columns: repeat(2, 1fr);
background-color: transparent;
column-gap: 0vw;
row-gap: 2vh;
align-items: center;
}
.results-grid-nametag {
border: 1.5px solid rgba(0, 0, 0, 0.8);
padding: 2vw;
font-size: large;
text-align: center;
border-radius: 10px;
background: repeating-linear-gradient(
45deg,
#ccffff,
#ccffff 10px,
#acffff 10px,
#acffff 20px
);
}
.restart-button {
margin: auto;
margin-top: 10vh;
align-self: center;
border-radius: 20px;
border: 1.5px solid rgb(0, 0, 0);
height: 10vh;
width: 40vw;
background: repeating-linear-gradient(
45deg,
#bd72c4,
#bd72c4 10px,
#a460aa 10px,
#a460aa 20px
);
}
.restart-button-text {
background-color: transparent;
text-align: center;
padding-top: 3.8vh;
font-size: larger;
}
</style>
</head>
<body>
<h1>choices</h1>
<div class="player-grid-container">
<!--TODO_LATER: could generate on each connection to expand?-->
<div class="player-grid-item open-slot" id="player1name">(open slot)</div>
<div class="player-grid-item open-slot" id="player2name">(open slot)</div>
<div class="player-grid-item open-slot" id="player3name">(open slot)</div>
<div class="player-grid-item open-slot" id="player4name">(open slot)</div>
</div>
<div class="question-box"></div>
<div class="selection-view" id="selection-view">
<div class="choice-grid-container" id="choice-grid"></div>
<div class="ready-button" id="ready">
<div class="ready-button-text">
Ready
</div>
</div>
</div>
<div class="waiting-view view-inactive" id="waiting-view">
<div class="my-result-holder" id="my-result-holder"></div>
<div class="waiting-text">Waiting for other players...</div>
</div>
<div class="results-view view-inactive" id="results-view">
<div class="results-grid-container" id="results-grid"></div>
<div class="restart-button" id="restart-button">
<div class="restart-button-text">
Next Question
</div>
</div>
</div>
</body>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
var usersLast;
var me;
const CHOICE = "ice_cream";
const CHOICE_PROMPT = "Which ice cream flavour is your favourite?";
const CHOICE_LIST = ["vanilla", "chocolate", "strawberry", "mint chip", "banana", "black cherry"];
function restartGame() {
document.getElementsByClassName("active-choice")[0].classList.remove("active-choice");
document.getElementById("results-view").classList.add("view-inactive");
document.getElementById("selection-view").classList.remove("view-inactive");
document.getElementById("ready").classList.remove("ready-button-active");
document.getElementById("results-grid").innerHTML = "";
}
var restartButton = document.getElementById("restart-button");
restartButton.addEventListener('click', restartGame, false);
function toUnderscore(string) {
return string.replace(" ", "_");
}
function toSpace(string) {
return string.replace("_", " ");
}
// "choice" = the question
function generateChoice(choice, name) {
return "<div class=\"choice-grid-item\" id=\"choice-" + toUnderscore(name) + "\"><figure><img src=\"/choices/" + choice + "/" + toUnderscore(name) + ".png\"><figcaption class=\"choice-caption\">" + name + "</figcaption></figure></div>";
}
function populateChoiceGrid(choice, choicePrompt, itemList) {
let questionBox = document.getElementsByClassName("question-box")[0];
questionBox.innerHTML = choicePrompt;
let choiceGrid = document.getElementById("choice-grid");
while (choiceGrid.firstChild) {
choiceGrid.removeChild(choiceGrid.firstChild); //remove existing children
}
for (var i = 0; i<itemList.length; i++) {
let currentChoiceStr = generateChoice(choice, itemList[i]);
let currentChoiceNode = document.createRange().createContextualFragment(currentChoiceStr) //incantation for str -> Element
choiceGrid.appendChild(currentChoiceNode);
}
}
populateChoiceGrid(CHOICE, CHOICE_PROMPT, CHOICE_LIST);
function getChoicesList() {
return document.getElementsByClassName("choice-grid-item");
}
choicesList = getChoicesList();
function submitChoice() {
var choice = document.getElementsByClassName("active-choice")[0];
console.log(choice);
readyUp();
}
function prepReadyButton() {
var readyButton = document.getElementById("ready")
readyButton.classList.add("ready-button-active")
readyButton.addEventListener("click", submitChoice, false);
}
function disableReadyButton() {
var readyButton = document.getElementById("ready")
readyButton.classList.remove("ready-button-active")
readyButton.removeEventListener("click", submitChoice, false);
}
function clearAndMarkChoice(event) {
var choicesList = getChoicesList()
if (event.currentTarget.classList.contains("active-choice")) {
event.currentTarget.classList.remove("active-choice");
disableReadyButton();
}
else {
for (var i=0; i<choicesList.length; i++) {
choicesList[i].classList.remove("active-choice");
}
event.currentTarget.classList.add("active-choice");
prepReadyButton();
}
}
//returns a single element of class choice-grid-item
function getSelectedChoice() {
return document.getElementsByClassName("active-choice")[0];
}
function packageSubmission() {
const selection = getSelectedChoice();
var readyData = new Object();
readyData.sender = socket.id;
readyData.senderName = document.getElementsByClassName("player-me")[0].innerHTML.slice(0, -6) //sigh..
console.log("package delivery!");
console.log(selection.childNodes[0]);
readyData.name = selection.id.slice(7);
readyData.choice = CHOICE;
return readyData;
}
function readyUp() {
var readyData = packageSubmission();
socket.emit('ready up', (readyData));
console.log("readied!");
document.getElementById("selection-view").classList.add("view-inactive");
document.getElementById("waiting-view").classList.remove("view-inactive");
}
//TODO: other users ready indicator?
for (var i = 0; i < choicesList.length; i++) {
console.log("Adding click listener on element " + i);
choicesList[i].addEventListener('click', clearAndMarkChoice, false);
}
function rename() {
var newName = window.prompt("Enter a username:", me.innerHTML.slice(0, -6));
console.log(newName);
var pkg = new Object();
pkg.id = socket.id;
pkg.name = newName;
socket.emit("name change", (pkg));
}
socket.on('player update', (users) => {
console.log("New player joined! Count: " + users.length);
usersLast = users;
var p1 = document.getElementById("player1name")
var p2 = document.getElementById("player2name")
var p3 = document.getElementById("player3name")
var p4 = document.getElementById("player4name")
var players = [p1, p2, p3, p4]
if (me == null) {
meIndex = users.findIndex((element) => element.id == socket.id);
me = players[meIndex];
me.classList.add("player-me");
me.addEventListener("click", rename, false)
} //flawed when players below you leave
console.log(players)
for (var u = 0; u < 4; u++) {
if (u < users.length) { //user exists, slot taken
console.log(1);
if (users[u].id == socket.id) {
console.log(2);
players[u].innerHTML = (users[u].name + " (you)");
}
else {players[u].innerHTML = users[u].name;
console.log(3);}
console.log(4);
players[u].classList.add("taken-slot");
console.log(5);
players[u].classList.remove("open-slot");
}
else { //user dne, slot open
players[u].innerHTML = "(open slot)";
players[u].classList.add("open-slot");
players[u].classList.remove("taken-slot");
}
};
});
function getSenderSubmission(ID, submissions) {
return submissions.find((s) => s.sender == ID);
}
function getSenderSubmissionEle(choice, name) {
var string = generateChoice(choice, name);
var ele = document.createRange().createContextualFragment(string);
console.log(ele);
return ele.cloneNode(deep=true)
}
function getSenderNametag(senderID) {
if (senderID == socket.id) {
var ele = document.getElementsByClassName("player-me")[0];
}
else {
var playerNum = usersLast.findIndex((user) => user.id == senderID)+1;
var ele = document.getElementById("player"+playerNum+"name");
}
return ele.cloneNode(deep=true);
}
socket.on('results', (submissions) => {
document.getElementById("waiting-view").classList.add("view-inactive");
document.getElementById("results-view").classList.remove("view-inactive");
var resultsGrid = document.getElementById("results-grid");
console.log(submissions);
for (var i = 0; i<usersLast.length; i++) {
var currentUserSubmission = getSenderSubmission(usersLast[i].id, submissions);
var submittorEle = getSenderNametag(currentUserSubmission.sender);
var submissionEle = getSenderSubmissionEle(currentUserSubmission.choice, toSpace(currentUserSubmission.name));
console.log(submittorEle);
console.log(submissionEle);
submittorEle.classList.add("results-grid-nametag");
submittorEle.classList.remove("player-grid-item");
//submissionEle.classList.add("results-grid-item");
resultsGrid.appendChild(submittorEle);
resultsGrid.appendChild(submissionEle);
resultsGrid.childNodes[resultsGrid.childNodes.length-1].classList.add("results-grid-item");
resultsGrid.childNodes[resultsGrid.childNodes.length-1].classList.remove("player-grid-item");
//can't do this with the partial document fragment until added to dom
}
});
</script>
</html>