-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
623 lines (525 loc) · 20.3 KB
/
index.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Current format
let currentFormat
let currentIncrement
let displayFormat1 = document.getElementById("current-format1");
let displayFormat2 = document.getElementById("current-format2");
// Player 1 clock
let currentP1Hours
let currentP1Minutes
let currentP1Seconds
let p1moveCount
let clock1
//Player 2 clock
let currentP2Hours
let currentP2Minutes
let currentP2Seconds
let p2moveCount
let clock2
// Nav icon settings
let reset = document.getElementById("reset-icon");
let startPause = document.getElementById("start-icon");
let editTime = document.getElementById("edit-icon");
let volume = document.getElementById("volume-icon");
//Edit time formats settings buttons
const bullet1 = document.getElementById("bullet1");
const bullet2 = document.getElementById("bullet2");
const bullet3 = document.getElementById("bullet3");
const blitz1 = document.getElementById("blitz1");
const blitz2 = document.getElementById("blitz2");
const blitz3 = document.getElementById("blitz3");
const rapid1 = document.getElementById("rapid1");
const rapid2 = document.getElementById("rapid2");
const rapid3 = document.getElementById("rapid3");
// Swap variables
let currentPlayer = "player1";
let isPaused = false; // Paused game is only when a game is running but the clocks are paused.
let isGameRunning = false;
let timer1;
let timer2;
let firstCall = true; // will use this to wait 1sec in order to start decrementing the clock seconds
//Audio files
let isMuted = false;
const endMoveAudio = new Audio("/sounds/clock-sound.mp3");
const timeoutAudio = new Audio("/sounds/timeout.mp3");
//Set current Format variables
setCurrentFormatStats();
// When display is clicked or pressed, start the opponent's clock
clock1.addEventListener("click", startClock2);
clock2.addEventListener("click", startClock1);
//Event listeners nav settings
reset.addEventListener("click", resetClock);
startPause.addEventListener("click", toggleStartPause);
editTime.addEventListener("click", editTimeFormat);
volume.addEventListener("click", muteUnmute);
function setCurrentFormatStats(hour, min, sec, increment, showOnDisplay){
// Current format
currentFormat = {
hours: hour ? hour : 0,
minutes: min ? min : 0,
seconds: sec ? sec : 0,
increment: increment ? true : false,
};
//Current game increment
currentIncrement = currentFormat.increment ? increment : 0;
//Display format on clock screens
displayFormat1.textContent = showOnDisplay ? showOnDisplay : "";
displayFormat2.textContent = showOnDisplay ? showOnDisplay : "";
// Player 1 clock
currentP1Hours = currentFormat.hours;
currentP1Minutes = currentFormat.minutes;
currentP1Seconds = currentFormat.seconds;
p1moveCount = 0;
clock1 = document.getElementById("player1");
//Player 2 clock
currentP2Hours = currentFormat.hours;
currentP2Minutes = currentFormat.minutes;
currentP2Seconds = currentFormat.seconds;
p2moveCount = 0;
clock2 = document.getElementById("player2");
//Set game Status
isPaused = false;
isGameRunning = false;
currentPlayer = "player1";
p1moveCount = 0;
p2moveCount = 0;
document.getElementById("p1-moveCount").textContent = p1moveCount;
document.getElementById("p2-moveCount").textContent = p2moveCount;
clock2.classList.remove("red");
clock1.classList.remove("red");
clock2.classList.remove("green");
clock1.classList.remove("green");
//Disable clock area buttons
clock1.disabled = false;
clock2.disabled = false;
updateClock_1_Display();
updateClock_2_Display();
};
function updateClock_1_Display(){
//Hide hours display if hour < 1
if ( String(currentP1Hours).padStart(2, '0') < 1 ) {
document.getElementById("hours1").classList.add("hidden");
document.getElementById("hours1-separator").classList.add("hidden");
}
if ( String(currentP1Hours).padStart(2, '0') >= 1 ) {
document.getElementById("hours1").classList.remove("hidden");
document.getElementById("hours1-separator").classList.remove("hidden");
}
//Display Values
document.getElementById("hours1").textContent = String(currentP1Hours).padStart(2, '0');
document.getElementById("minutes1").textContent = String(currentP1Minutes).padStart(2, '0');
document.getElementById("seconds1").textContent = String(currentP1Seconds).padStart(2, '0');
};
function updateClock_2_Display(){
//Hide hours display if hour < 1
if ( String(currentP2Hours).padStart(2, '0') < 1 ) {
document.getElementById("hours2").classList.add("hidden");
document.getElementById("hours2-separator").classList.add("hidden");
}
if ( String(currentP2Hours).padStart(2, '0') >= 1 ) {
document.getElementById("hours2").classList.remove("hidden");
document.getElementById("hours2-separator").classList.remove("hidden");
}
//Display Values
document.getElementById("hours2").textContent = String(currentP2Hours).padStart(2, '0');
document.getElementById("minutes2").textContent = String(currentP2Minutes).padStart(2, '0');
document.getElementById("seconds2").textContent = String(currentP2Seconds).padStart(2, '0');
};
// Player 1 Clock
function startClock1(){
//Stop the opponent's clock
clearInterval(timer2);
//Handle if both clock are set up to 00:00
if ( currentP1Seconds == 0 && currentP1Minutes == 0 && currentP1Hours == 0){
editTimeFormat();
return;
}
// If game resumes from pause. prevent adding increment and place start icon. Also prevents swapping icons during game between turns
if (isPaused || !isGameRunning) {
document.getElementById("minutes2").textContent = String(parseInt(currentP2Minutes)).padStart(2, '0');
document.getElementById("seconds2").textContent = String(parseInt(currentP2Seconds)).padStart(2, '0');
toggleStartPauseIcons("/images/pause.png");
};
// add increment to opponent's clock if current format has increment
if ( currentFormat.increment && !isPaused ) {
currentP2Seconds = parseInt(document.getElementById("seconds2").textContent) + parseInt(currentIncrement)
//Handle if seconds or minutes > 60
if (currentP2Seconds >= 60) {
currentP2Seconds = currentP2Seconds - 60;
currentP2Minutes ++ ;
}
if ( currentP2Minutes >= 60 ) {
currentP2Minutes = currentP2Minutes - 60;
currentP2Hours ++ ;
}
updateClock_2_Display();
};
//Increment move count
if (isGameRunning && !isPaused ) { // Only increments after the first move (game is running) prevents incrementing if game was paused
p2moveCount++;
}
//Update Move count display
document.getElementById("p2-moveCount").textContent = p2moveCount;
//handle swap variables
currentPlayer = "player1";
isPaused = false;
isGameRunning = true;
firstCall = true;
//Change Background colors
handleActiveClockColor();
//Disable/enable buttons
clock2.disabled = true;
clock1.disabled = false;
//Play Sound
if ( !isMuted ) endMoveAudio.play();
//Start countdown
countdown();
timer1 = setInterval(countdown, 1000);
}
//Player 2 Clock
function startClock2(){
//Stop the opponent's clock
clearInterval(timer1);
//prevent start game if both clock are set up to 00:00
if ( currentP2Seconds == 0 && currentP2Minutes == 0 && currentP2Hours == 0){
editTimeFormat();
return;
}
// If game resumes from pause. prevent adding increment and place start icon. Also prevents swapping icons during game between turns
if (isPaused || !isGameRunning) { //Prevents adding increment after press "resume clock"
document.getElementById("minutes1").textContent = String(parseInt(currentP1Minutes)).padStart(2, '0');
document.getElementById("seconds1").textContent = String(parseInt(currentP1Seconds)).padStart(2, '0');
toggleStartPauseIcons("/images/pause.png");
}
// add increment to opponent's clock if current format has increment
if ( currentFormat.increment && !isPaused ) {
currentP1Seconds = parseInt(document.getElementById("seconds1").textContent) + parseInt(currentIncrement)
//Handle if seconds or minutes is > 60
if (currentP1Seconds >= 60) {
currentP1Seconds = currentP1Seconds - 60;
currentP1Minutes ++ ;
}
if ( currentP1Minutes >= 60 ) {
currentP1Minutes = currentP1Minutes - 60;
currentP1Hours ++ ;
};
updateClock_1_Display();
};
//Increment move count
if (isGameRunning && !isPaused) { // Only increments after the first move (game is running) prevents incrementing is game was paused
p1moveCount++;
}
//Update Move count display
document.getElementById("p1-moveCount").textContent = p1moveCount;
//handle swap variables
currentPlayer = "player2";
isPaused = false;
isGameRunning = true;
firstCall = true;
//Change Background colors
handleActiveClockColor();
//Disable buttons
clock2.disabled = false;
clock1.disabled = true;
//Play Sound
if ( !isMuted ) endMoveAudio.play();
//Start countdown
countdown();
timer2 = setInterval(countdown, 1000);
};
// Countdown function
function countdown(){
// Player 1 Clock
if (currentPlayer === "player1") {
// check if this is the first call so the first second does not get decremented immediately
if (firstCall){
firstCall = false;
return
};
currentP1Hours = document.getElementById("hours1").textContent;
currentP1Minutes = document.getElementById("minutes1").textContent;
currentP1Seconds = document.getElementById("seconds1").textContent;
//Decrease Seconds
currentP1Seconds -- ;
//Decrease minutes
if ( currentP1Seconds < 0 && currentP1Minutes > 0 ) {
currentP1Seconds = 59;
currentP1Minutes --;
}
//Decrease hour
if ( currentP1Seconds < 0 && currentP1Minutes == 0 && currentP1Hours > 0) {
currentP1Seconds = 59;
currentP1Minutes = 59;
currentP1Hours -- ;
}
//If time run out
if ( currentP1Seconds == 0 && currentP1Minutes == 0 && currentP1Hours == 0) {
currentP1Hours = String(currentP1Hours).padStart(2, '0');
currentP1Minutes = String(currentP1Minutes).padStart(2, '0');
currentP1Seconds = String(currentP1Seconds).padStart(2, '0');
endGame();
toggleStartPauseIcons("/images/start.png");
}
updateClock_1_Display();
};
//Player 2 Clock
if (currentPlayer === "player2"){
// check if this is the first call so the first second does not get decremented immediately
if (firstCall){
firstCall = false;
return
};
//Decrease Seconds
currentP2Seconds -- ;
//Decrease minutes
if ( currentP2Seconds < 0 && currentP2Minutes > 0 ) {
currentP2Seconds = 59;
currentP2Minutes --;
}
//Decrease hour
if ( currentP2Seconds < 0 && currentP2Minutes == 0 && currentP2Hours > 0) {
currentP2Seconds = 59;
currentP2Minutes = 59;
currentP2Hours -- ;
}
//If time run out
if ( currentP2Seconds == 0 && currentP2Minutes == 0 && currentP2Hours == 0 ) {
currentP2Hours = String(currentP2Hours).padStart(2, '0');
currentP2Minutes = String(currentP2Minutes).padStart(2, '0');
currentP2Seconds = String(currentP2Seconds).padStart(2, '0');
endGame();
startPause.setAttribute("src", "/images/start.png");
}
updateClock_2_Display();
}
}
function endGame(){
if ( currentPlayer === "player1" ) {
clock1.classList.add("red");
} else if ( currentPlayer === "player2" ) {
clock2.classList.add("red");
}
clearInterval(timer1);
clearInterval(timer2);
clock1.disabled = true;
clock2.disabled = true;
isGameRunning = false;
currentPlayer = "none";
//Play Sound
if ( !isMuted ) timeoutAudio.play();
};
//Start/pause button - Start the current Player's clock or pause current player's clock
function toggleStartPause(){
//prevent start game if both clock are set up to 00:00
if ( currentP2Seconds == 0 && currentP2Minutes == 0 && currentP2Hours == 0 && currentP1Seconds == 0 && currentP1Minutes == 0 && currentP1Hours == 0){
editTimeFormat();
return;
}
if ((isPaused && isGameRunning) || (!isGameRunning && !isPaused)){
//Start current Player's clock and swap icon
if (currentPlayer === "player1"){
startClock1();
} else if ( currentPlayer === "player2" ) {
startClock2();
//If no current player the game has ended
} else {
startPause.disabled = true;
};
isPaused = false;
//Swap icon
toggleStartPauseIcons("/images/pause.png");
} else if ( isGameRunning && !isPaused ) {
//Pause current player clock and swap icon
if (currentPlayer === "player1") {
clearInterval(timer1);
}
if (currentPlayer === "player2") {
clearInterval(timer2);
}
//Disable clock area buttons
clock1.disabled = true;
clock2.disabled = true;
//Handle swap variables
isPaused = true;
//Swap icon
toggleStartPauseIcons("/images/start.png");
}
};
function toggleStartPauseIcons(srcPath){
document.getElementById("start-icon").setAttribute("src", srcPath);
};
function resetClock() {
//Stop clocks -- NOT PAUSE
clearInterval(timer1);
clearInterval(timer2);
//Disable clock area buttons
clock1.disabled = true;
clock2.disabled = true;
//Toggle start pause icon
toggleStartPauseIcons("/images/start.png");
// Show confirmation pop up
let resetPopup = document.getElementById("reset-alert");
resetPopup.classList.remove("hidden");
//Add eventlistener to each button:
//No - go back
document.getElementById("reset-no").addEventListener("click", () => {
resetPopup.classList.add("hidden");
resumeCurrentGame();
});
//Yes
document.getElementById("reset-yes").addEventListener("click", () => {
resetPopup.classList.add("hidden");
clock2.classList.remove("red");
clock1.classList.remove("red");
clock2.classList.remove("green");
clock1.classList.remove("green");
//Get values to reset game with same format
let formatHours = currentFormat.hours;
let formatMinutes = currentFormat.minutes;
let formatSeconds = currentFormat.seconds;
let formatIncrement = currentIncrement;
let showOnDisplay = displayFormat1.textContent;
//reset game values
setCurrentFormatStats(formatHours, formatMinutes, formatSeconds, formatIncrement, showOnDisplay);
})
}
function handleActiveClockColor(){
if (currentPlayer === "player1") {
clock1.classList.add("green");
clock2.classList.remove("green");
} else if (currentPlayer === "player2") {
clock1.classList.remove("green");
clock2.classList.add("green");
}
};
function muteUnmute(){
isMuted = isMuted === false ? true : false ;
//Swap icon
if ( isMuted ) {
document.getElementById("volume-icon").setAttribute("src", "/images/soundOff.png")
} else document.getElementById("volume-icon").setAttribute("src", "/images/soundOn.png")
}
function editTimeFormat(){
//Stop clocks -- NOT PAUSE
clearInterval(timer1);
clearInterval(timer2);
//Disable clock area buttons
clock1.disabled = true;
clock2.disabled = true;
//Set icon to "start".
toggleStartPauseIcons("/images/start.png");
//Show options panel
document.getElementById("edit-format-panel").classList.remove("hidden");
//go back and resume game
const close = document.getElementById("close");
close.addEventListener("click", () => {
document.getElementById("edit-format-panel").classList.add("hidden");
resumeCurrentGame();
});
//Manual input form
let show
const customTimeForm = document.getElementById("custom-time-form");
customTimeForm.addEventListener("submit", (event) => {
const minInput = document.querySelector('input[name="minutes"]');
//Check if valid inputs were made
if (!minInput.checkValidity()) {
alert("Need to insert at least a '0' in the minutes field.");
minInput.focus();
EventTarget.preventDefault();
} else {
event.preventDefault();
let hoursInput = 0;
let minutesInput = customTimeForm.minutes.value;
if (minutesInput >= 60 ) {
minutesInput = minutesInput - 60;
hoursInput ++ ;
}
let secondsInput = customTimeForm.seconds.value;
let incrementInput = customTimeForm.inc.value;
//Format inputs to show on display
show = formatInputs(hoursInput, minutesInput, secondsInput, incrementInput);
//Set game stats
setCurrentFormatStats(hoursInput, minutesInput, secondsInput, incrementInput, show);
//Close panel
document.getElementById("edit-format-panel").classList.add("hidden");
}
})
//Eventlistener for all the option buttons
const editCard = document.getElementById("time-options");
//Select all buttons except the submit form button
const timeOptions = editCard.querySelectorAll("button:not(#custom-time-form button)");
timeOptions.forEach(option => {
option.addEventListener("click", () => {
switch (option.id) {
case "bullet1":
//set game stats
show = "1 min";
setCurrentFormatStats(0, 1, 0, 0, show);
break;
case "bullet2":
show = "1 | 1";
setCurrentFormatStats(0, 1, 0, 1, show);
break;
case "bullet3":
show = "2 | 1";
setCurrentFormatStats(0, 2, 0, 1, show);
break;
case "blitz1":
show = "3 min";
setCurrentFormatStats(0, 3, 0, 0, show);
break;
case "blitz2":
show = "3 | 2";
setCurrentFormatStats(0, 3, 0, 2, show);
break;
case "blitz3":
show = "5 min";
setCurrentFormatStats(0, 5, 0, 0, show);
break;
case "rapid1":
show = "10 min";
setCurrentFormatStats(0, 10, 0, 0, show);
break;
case "rapid2":
show = "15 | 10";
setCurrentFormatStats(0, 15, 0, 10, show);
break;
case "rapid3":
show = "30 min";
setCurrentFormatStats(0, 30, 0, 0, show);
break;
default:
setCurrentFormatStats(0, 0, 0, 0);
break;
}
//Close panel
document.getElementById("edit-format-panel").classList.add("hidden");
})
});
}
function resumeCurrentGame(){
if (isGameRunning && currentPlayer != "none"){
isPaused = true;
if(currentPlayer === "player1") clock2.disabled = false;
if(currentPlayer === "player2") clock1.disabled = false;
}
if ( !isGameRunning && currentPlayer != "none") {
isPaused = false;
clock2.disabled = false;
clock1.disabled = false;
}
}
function formatInputs(h, min, sec, increm){
if (increm > 0){
if (h > 0){
show = h + "h | " + increm;
} else if (min > 0){
show = min + " | " + increm;
} else show = sec + "sec | " + increm;
//In case of no increment
} else {
if (h > 0) show = h + "h";
else if (min > 0) show = min + "min";
else show = sec + "sec";
}
return show;
}