-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
459 lines (340 loc) · 13.5 KB
/
Copy pathmain.ts
File metadata and controls
459 lines (340 loc) · 13.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
import Peer from 'peerjs';
//multiplayer functionality done using peerJS: https://peerjs.com/
let clamp = (number: number, min: number, max: number) => number < min ? min : number > max ? max : number
let typer = document.getElementById("typer");
let light = document.getElementById("trafficlight");
let text;
//regular expression including all valid characters you can type in the passage
let validLetters = new RegExp(/[ -~]/m);
function getWords() {
let req = new XMLHttpRequest();
req.open("GET", "https://gist.githubusercontent.com/deekayen/4148741/raw/98d35708fa344717d8eee15d11987de6c8e26d7d/1-1000.txt", false); //credit for word pool goes to deekayen on GitHub @ https://github.com/deekayen
req.send();
return req.response.split("\n");
}
let possWords = getWords();
let wordCount = 20;
let randWord = () => possWords[Math.floor(Math.random() * possWords.length)];
console.log(possWords);
let currentPos = 0;
let incorrectStart = 0;
let incorrect = false;
let startButton = document.getElementById("start") as HTMLButtonElement;
let hostButton = document.getElementById("hostButton") as HTMLButtonElement;
let joinButton = document.getElementById("joinButton") as HTMLButtonElement;
const joinTimeout = 5000;
const yellowDuration = 3000; //delay in ms between yellow and red light
const redDuration = 3000; //delay in ms between red and green light
const minLightInterval = 2000;
const maxLightInterval = 8000;
let stop = false;
let timeouts = [];
let peer = new Peer(`st-${randWord()}-${randWord()}-${randWord()}`);
let conn: Peer.DataConnection;
let host = false;
let connected = false;
let started = false;
let opponentPos = 0;
let selfBar: HTMLDivElement;
let opponentBar: HTMLDivElement;
let curTime = Date.now();
function resetGUI() {
startButton.disabled = true;
startButton.classList.add("completely-hidden");
joinButton.disabled = false;
hostButton.disabled = false;
(document.getElementById("joinid") as HTMLFormElement).disabled = false;
document.getElementById("host").classList.remove("hidden");
}
function generateText(count, words) {
let l = words.length;
let selectedWords = [];
let i = 0;
while (i < count) {
let curWord = words[Math.floor(Math.random() * l)];
if (selectedWords.indexOf(curWord) == -1) {
if (Math.random() < 0.5) {
curWord = curWord.charAt(0).toUpperCase() + curWord.slice(1);
}
selectedWords.push(curWord);
i++;
}
}
//selects "count" number of words from the word pool, randomly capitalizes some, and appends them to the list to be returned
return selectedWords.join(" ")
}
function updateBar(bar, progress) {
bar.style.setProperty("--progress", `${progress * 100}%`);
bar.innerText = `${Math.round(progress * 100)}%`;
//updates progress bar passed with progress passed
}
function createBar(progress) {
//Progress should be a decimal from 0-1, where 1 = 100%
let bars = document.getElementById("bars");
let progContainer = document.createElement("div");
progContainer.classList.add("progresscontainer");
let progBar = document.createElement("div");
progBar.classList.add("progressbar");
updateBar(progBar, progress)
console.log(`progress bar: ${progBar.style.width}`)
progContainer.appendChild(progBar);
bars.appendChild(progContainer);
return progBar;
//creates a new progress bar with progress passed as parameter, returns it
}
function stopLight() {
light.style.setProperty("--color", "yellow");
timeouts.push(
setTimeout(() => {
light.style.setProperty("--color", "red")
stop = true;
}, yellowDuration),
setTimeout(() => {
light.style.setProperty("--color", "green")
stop = false;
}, yellowDuration + redDuration)
);
//procedure for initiating traffic light stop: turn it yellow, then red after a certain amount of time, then green
}
function trafficLoop() {
stopLight();
conn.send("t");
let delay = minLightInterval + yellowDuration + redDuration + Math.random() * (maxLightInterval - minLightInterval);
timeouts.push(
setTimeout(trafficLoop, delay)
);
console.log(`set next light with delay ${delay} ms, id ${timeouts[timeouts.length - 1]}`);
//starts a loop for stopping the traffic light at random intervals
}
function start() {
setTimeout(() => {
light.innerText = "3";
light.style.setProperty("--color", "red");
}, 0);
setTimeout(() => {
light.innerText = "2";
light.style.setProperty("--color", "yellow");
}, 1000);
setTimeout(() => {
light.innerText = "1";
}, 2000);
setTimeout(() => {
light.innerText = "GO!";
light.style.setProperty("--color", "green");
started = true;
if (host) {
setTimeout(trafficLoop, minLightInterval + Math.random() * maxLightInterval);
}
}, 3000);
setTimeout(() => {
light.innerText = "";
}, 3500);
//displays countdown and turns light green, also starts traffic light
}
function endGame(status, elim = false) {
started = false;
light.style.setProperty("--color", "red");
//ends the game and sets the light to red, preventing players from typing any further inputs
for (var i = 0; i < timeouts.length; i++) {
clearTimeout(timeouts[i]);
console.log(`clearing timeout with id ${timeouts[i]}`);
}
//stops all ongoing timeouts, preventing any further events from happening
if (status == "win") {
if (elim) {
opponentBar.style.setProperty("--barcolor", "red");
opponentBar.style.setProperty("overflow", "visible");
}
//if player wins by elimination of opponent, set opponent bar to red
selfBar.style.setProperty("--barcolor", "gold");
selfBar.style.setProperty("--progress", "100%");
selfBar.innerHTML = "You win!";
//change bar color to full gold, display win message
}
else if (status == "lose") {
if (elim) {
selfBar.style.setProperty("--barcolor", "red");
selfBar.style.setProperty("overflow", "visible");
selfBar.innerHTML = "Red light!";
}
//if player lost by elimination, set own bar to red and display elimination message
else {
selfBar.style.setProperty("overflow", "visible");
selfBar.innerHTML = "Too slow...";
}
//otherwise (losing by being slower than opponent), display relevant message
opponentBar.style.setProperty("--barcolor", "gold");
opponentBar.style.setProperty("--progress", "100%");
//regardless of losing method, change opponent bar to victorious bar
}
else {
console.log("invalid parameter passed to endGame");
}
}
hostButton.addEventListener("click", () => {
console.log(peer.id);
host = true;
text = generateText(wordCount, possWords);
typer.innerHTML = `<span class = "untyped">${text}</span>`;
//generates random words, displays them on the page to be typed
(document.getElementById("roomid") as HTMLFormElement).value = peer.id;
hostButton.disabled = true;
document.getElementById("join").classList.add("hidden")
peer.on('connection', function(connection) {
conn = connection;
if (connected == true) {
conn.close();
return;
}
connected = true;
console.log("connected");
opponentBar = createBar(0);
startButton.disabled = false;
startButton.classList.remove("completely-hidden");
conn.on('open', function() {
conn.send("x|" + text);
})
//on opponent connection, create a new progress bar and display start button, send words to be typed
conn.on('data', function(data){
switch (data[0]) {
//update opponent position with any data received
case "p":
opponentPos = parseInt(data.split("|")[1]);
break;
//if opponent wins, you lose, and vice-versa
case "w":
endGame("lose");
break;
case "l":
endGame("win", true);
break;
}
updateBar(opponentBar, opponentPos/text.length);
//updates opponent progress bar w/ new position
console.log(opponentPos);
console.log(data);
});
});
})
joinButton.addEventListener("click", () => {
console.log(peer.id);
host = false;
let roomid = (document.getElementById("joinid") as HTMLFormElement).value;
if (roomid.length == 0) {
alert("Please enter a room ID");
return;
}
conn = peer.connect(roomid);
startButton.disabled = false;
joinButton.disabled = true;
hostButton.disabled = true;
(document.getElementById("joinid") as HTMLFormElement).disabled = true;
document.getElementById("host").classList.add("hidden");
peer.on("error", function(err) {
conn.close();
alert("Connection failed");
console.log(err.type);
resetGUI();
})
conn.on('open', function() {
console.log("connected");
opponentBar = createBar(0);
//on connecting to host lobby, generate new progress bar for opponent
conn.on('data', function(data){
switch (data[0]) {
case "s": //game started via host's start button
start();
break;
case "p": //position of opponent sent
opponentPos = parseInt(data.split("|")[1]);
break;
//if opponent wins, you lose, and vice-versa
case "w": //opponent sends win message
endGame("lose");
break;
case "l": //opponent sends lose message (only happens when eliminated via red light)
endGame("win", true);
break;
case "t"://host sends signal to trigger traffic light
console.log(`traffic light triggered w/ delay ${Date.now() - curTime}`);
curTime = Date.now();
stopLight();
break;
case "x": //on receiving text to be typed from host, display it on the page
text = data.split("|")[1];
typer.innerHTML = `<span class = "untyped">${text}</span>`;
break;
}
console.log(opponentPos);
updateBar(opponentBar, opponentPos/text.length);
console.log(`not host received data: ${data}`);
})
});
})
startButton.addEventListener("click", () => {
if (!connected) {
return;
}
start();
startButton.disabled = true;
startButton.classList.add("completely-hidden");
conn.send("s");
//on start button click, start the game for self and send message to opponent to start as well
})
selfBar = createBar(0);
document.addEventListener("keydown", e => {
if (!started) {
return;
}
//you lose if you type in a key during a red light
if (stop && (e.key == "Backspace" || (e.key.length == 1 && validLetters.test(e.key)))) {
conn.send("l");
endGame("lose", true);
console.log("player eliminated");
return;
}
// incorrectStart is the proper measure for position to send to other players
if (e.key == "Backspace") { //on backspace, move the cursor back
currentPos--;
if (currentPos <= incorrectStart) {
incorrect = false;
//if cursor is behind the incorrect portion, no more incorrect text
}
}
if (e.key.length == 1 && validLetters.test(e.key)) {
//checks that key pressed is a valid character
if (e.key != text[currentPos] && !incorrect) {
//checks beginning of mistake
incorrectStart = currentPos;
incorrect = true;
}
currentPos++
//if valid key typed, move cursor regardless of correctness
}
currentPos = clamp(currentPos, 0, text.length);
if (!incorrect) {
incorrectStart = currentPos;
} //saves start of mistake if there is a mistake, does not otherwise
let correctText = text.slice(0, incorrectStart);
let incorrectText = text.slice(incorrectStart, currentPos);
// @ts-ignore
incorrectText = incorrectText.replaceAll(" ", "░​");
// @ts-ignore
correctText = correctText.replaceAll(" ", "░​");
let untypedText = text.slice(currentPos, text.length);
//gets three chunks: correct text, incorrect text, untyped text
let formattedCorrect = `<span class = "correct">${correctText}</span>`;
let formattedIncorrect = `<span class = "incorrect">${incorrectText}</span>`;
let formattedUntyped = `<span class = "untyped">${untypedText}</span>`;
typer.innerHTML = formattedCorrect + formattedIncorrect + formattedUntyped;
//formats text according to type and joins it in order
if (selfBar) {
updateBar(selfBar, incorrectStart/text.length);
}
conn.send(`p|${incorrectStart}`);
//player who types the entire length of the text first wins
if (incorrectStart == text.length) {
conn.send("w");
endGame("win");
}
})