-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
341 lines (289 loc) · 9.27 KB
/
client.js
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
let room = 0;
let stagesList = "darso1AJTI";
const stageNoms = ['Battlefield', 'Castle Siege', 'Dream Land', 'Final Destination', 'Fountain of Dreams', 'Frigate Orpheon', 'Halberd', 'Hollow Bastion', 'Kalos Pokemon League', 'Lylat Cruise', 'Mementos', 'Midgar', 'Northern Cave', 'Pokemon Stadium', 'Pokemon Stadium 2', 'Rainbow Cruise', 'Skyloft', 'Small Battlefield', 'Smashville', 'Town and City', 'Unova Pokemon League', 'WarioWare, Inc.', 'Wily Castle', 'Wuhu Island', "Yggdrasil's Altar", "Yoshi's Island (Brawl)", "Yoshi's Story"];
var url = new URL(location);
const room_param = url.searchParams.get("room");
const title_param = url.searchParams.get("title");
const stages_param = url.searchParams.get("stages");
const stream_param = url.searchParams.get("stream");
if (Number(stream_param) == 1)
document.body.classList.add("stream");
if (room_param)
room = Number(room_param);
if (stages_param)
stagesList = stages_param;
if (title_param) {
const title = decodeURIComponent(title_param);
document.getElementsByTagName('title')[0].innerHTML = title;
document.getElementById('title').innerHTML = title;
}
let ws;
function wsConnect () {
if (window.location.protocol == "https:")
ws = new WebSocket("wss://" + location.host);
else
ws = new WebSocket("ws://" + location.host);
ws.addEventListener("open", () =>{
console.log("We are connected");
const req = {
"room": 0,
"code": 0,
"value": room
};
ws.send(JSON.stringify(req));
});
// reconnect if client lost connection
ws.addEventListener("close", () =>{
console.log("websocket connection lost");
// try reconnect after 1s
setTimeout(wsConnect, 1000)
});
ws.addEventListener('message', function (event) {
console.log(event.data);
const data = JSON.parse(event.data);
switch (data.code) {
case 0:
room = data.value;
break;
case 1: {
const stage = document.getElementById("stage-" + data.value);
stage.classList.add("checked");
updateAndValidate(stage);
break;
}
case 2: {
const stage = document.getElementById("stage-" + data.value);
stage.classList.remove("checked");
updateAndValidate(stage);
break;
}
case 3:
// renvoyer toutes les infos pour chaque stage (ex: nouveau client join la room)
console.log("renvoyer infos");
renvoyerInfos();
break;
case 4:
stagesList = data.value;
changerStages();
break;
case 5: {
const title = decodeURIComponent(data.value);
document.getElementsByTagName('title')[0].innerHTML = title;
document.getElementById('title').innerHTML = title;
break;
}
}
});
}
wsConnect();
function renvoyerInfos() {
// renvoyer la liste de stages
const reqStages = {
"room": room,
"code": 4,
"value": stagesList
};
ws.send(JSON.stringify(reqStages));
// on doit sauvegarder l’état actuel de chaque stage (checked ou unchecked)
// car les objets vont etre supprimes lors de la mise a jour
const stages = document.querySelectorAll('.stage');
stages.forEach(el => {
const id = Number(el.id.split('-')[1]);
let req = {
"room": room,
"code": 2,
"value": id
};
if(el.classList.contains("checked"))
req["code"] = 1;
ws.send(JSON.stringify(req));
});
// renvoyer le titre
const reqTitre = {
"room": room,
"code": 5,
"value": encodeURIComponent(document.getElementById('title').innerHTML)
};
ws.send(JSON.stringify(reqTitre));
}
function isCounterpickStage (letter) {
return ('A' <= letter && letter <= 'Z');
}
function letterValue (letter) {
if ('A' <= letter && letter <= 'Z')
return letter.charCodeAt() - 65;
else
return letter.charCodeAt() - 97;
}
// on a pas besoin de verifier les erreurs sur la chaine de caractere car le serveur
// a deja verifie qu'elle est conforme au format attendu
function changerStages () {
document.getElementById("starters").innerHTML = "";
document.getElementById("counterpicks").innerHTML = "";
let i = 0;
while (i < stagesList.length) {
if ('0' <= stagesList[i] && stagesList[i] <= '9') {
ajouterStage(isCounterpickStage(stagesList[i+1]), Number(stagesList[i])*26 + letterValue(stagesList[i+1]), i);
i += 2;
}
else {
ajouterStage(isCounterpickStage(stagesList[i]), letterValue(stagesList[i]), i);
i++;
}
}
if (document.getElementById("counterpicks").childNodes.length === 0) {
document.getElementsByTagName('h2')[0].style.display = "none";
document.getElementsByTagName('h2')[1].style.display = "none";
}
else {
document.getElementsByTagName('h2')[0].style.display = "";
document.getElementsByTagName('h2')[1].style.display = "";
}
}
function ajouterStage(isCounter, stageId, i) {
const nom = stageNoms[stageId];
let newStage = document.createElement("li");
newStage.setAttribute("class", "stage");
newStage.setAttribute("id", "stage-" + i);
newStage.innerHTML = '<img src="images/' + nom + '.jpg" alt="' + nom + ' stage"> <p>' + nom + '</p>';
if (isCounter)
document.getElementById("counterpicks").appendChild(newStage);
else
document.getElementById("starters").appendChild(newStage);
// add click event
newStage.addEventListener('click', () => {
let req = {
"room": room,
"code": 1,
"value": i
};
if(newStage.classList.contains("checked"))
req["code"] = 2;
ws.send(JSON.stringify(req));
console.log("toggle check");
});
// double click pour valider directement le stage sélectionné
newStage.addEventListener('dblclick', () => {
const stages = document.querySelectorAll('.stage');
stages.forEach(el => {
if (el == newStage) {
const req = {
"room": room,
"code": 2,
"value": el.id.split('-')[1]
};
ws.send(JSON.stringify(req));
}
else {
const req = {
"room": room,
"code": 1,
"value": el.id.split('-')[1]
};
ws.send(JSON.stringify(req));
}
});
});
}
// boutons en pied de page
document.getElementById("linkBtn").onclick = function() {
copyTextToClipboard(location.protocol + "//" + location.host + "?title=" + encodeURIComponent(document.getElementById('title').innerHTML) + "&stages=" + stagesList + "&room=" + room);
}
document.getElementById("resetBtn").onclick = resetStages;
document.getElementById("editBtn").onclick = function() {
if (document.getElementsByTagName("aside")[0].style.display == "none") {
document.getElementsByTagName("aside")[0].style.display = "";
window.scroll({
top: document.body.scrollHeight,
behavior: "smooth"
});
}
else
document.getElementsByTagName("aside")[0].style.display = "none";
}
async function copyTextToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
alert('Text copied to clipboard');
} catch(err) {
alert('Error in copying text: ', err);
}
}
function resetStages () {
const stages = document.querySelectorAll('.stage');
stages.forEach(el => {
const req = {
"room": room,
"code": 2,
"value": el.id.split('-')[1]
};
ws.send(JSON.stringify(req));
});
}
// obtenir le code avec 2 lettres pour un stage
function idCode (id, isCounter) {
let txt = "";
if (parseInt(id / 26, 10) > 0)
txt += parseInt(id / 26, 10);
if (isCounter)
txt += String.fromCharCode(id % 26 + 65)
else
txt += String.fromCharCode(id % 26 + 97)
return txt;
}
function genOptionsList (parentEl, isCounter) {
let newSelect = document.createElement("select");
newSelect.setAttribute("class", "editStage");
newSelect.innerHTML = '<option value="null">None</option>';
for (let i = 0; i < stageNoms.length; i++)
newSelect.innerHTML += '<option value="' + idCode(i, isCounter) + '">' + stageNoms[i] + '</option>';
parentEl.appendChild(newSelect);
}
const editStartersEl = document.getElementById("editStarters");
const editCounterpicksEl = document.getElementById("editCounterpicks");
for (let i = 0; i < 10; i++) {
genOptionsList(editStartersEl, false);
genOptionsList(editCounterpicksEl, true);
}
function newStageList () {
let txt ="";
const editStages = document.querySelectorAll('.editStage');
editStages.forEach(el => {
if (el.value != "null")
txt += el.value
});
return txt;
}
// bouton pour editer la page
document.getElementById("applyBtn").onclick = function() {
const reqStages = {
"room": room,
"code": 4,
"value": newStageList()
};
ws.send(JSON.stringify(reqStages));
}
// ajouter la class update pendant 3s et valider si un
// seul stage restant
function updateAndValidate (stage) {
stage.classList.add("update");
setTimeout(function () {
stage.classList.remove("update");
}, 3000);
const stagesValidated = document.querySelectorAll('.stageValidated');
stagesValidated.forEach(el => {
el.classList.remove("stageValidated");
});
let stagesUnchecked = [];
const stages = document.querySelectorAll('.stage');
stages.forEach(el => {
if (!el.classList.contains("checked"))
stagesUnchecked.push(el);
});
if (stagesUnchecked.length == 1) {
stagesUnchecked[0].classList.add("stageValidated");
stages.forEach(el => {
el.classList.remove("update");
});
}
}