-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
486 lines (443 loc) · 12.1 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>Score keeper</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"/>
</head>
<body>
<div id="app">
<nav class="toolbar">
<section>
<button class="control"
@click="onUndoScore()">
<i class="fa fa-undo"></i>
</button>
<button class="control"
@click="onReset()">
<i class="fa fa-refresh"></i>
</button>
<label class="control">
<input type="checkbox" v-model="isSpeechOn"/>
<i class="fa"
:class="(isSpeechOn) ? 'fa-volume-up' : 'fa-volume-off'">
</i>
</label>
</section>
<section class="match-score">
{{ matchScore[0] }} : {{ matchScore[1] }}
</section>
<section>
<button class="control" @click="onSwitchBreak()">
<i class="fa fa-exchange"></i>
</button>
<button class="control" @click="onGoFullscreen()">
<i class="fa fa-desktop"></i>
</button>
<label class="control">
<input type="checkbox" v-model="isTimerOn"/>
<i class="fa fa-hourglass"></i>
</label>
<button class="control"
@click="onFinishMatch()">
<i class="fa fa-flag-checkered"></i>
</button>
</section>
</nav>
<div class="score">
<div class="score--player-1">
<button class="name-container" @click="onChangeName(0)">
{{ name[0] }}
</button>
<button class="score-container" @click="onIncreaseScore(0)">
<span class="number">{{ score[0] }}</span>
<span class="break-indicator" v-if="(playerBreaking===0)">
<i class="fa fa-circle"></i>
</span>
</button>
</div>
<div v-if="(isTimerOn)" class="timer-controls">
<button class="control"
@click="onTimerStart()">
<i class="fa fa-play"></i>
</button>
<div class="time-left"
:class="{ critical: (timerCurrent < 6) }">
{{ timerCurrent }}
</div>
<button class="control" @click="onTimerStop()">
<i class="fa fa-stop"></i>
</button>
<button class="control" @click="onTimerSetTime()">
{{ timerStartFrom }}
</button>
</div>
<div class="score--player-2">
<button class="name-container" @click="onChangeName(1)">
{{ name[1] }}
</button>
<button class="score-container" @click="onIncreaseScore(1)">
<span class="number">{{ score[1] }}</span>
<span class="break-indicator" v-if="(playerBreaking===1)">
<i class="fa fa-circle"></i>
</span>
</button>
</div>
</div>
</div>
<script>
var phrases = {
pointWon: [
'Good job %var!',
'Really nice %var!',
'Wow %var, nicely done!',
'Alright %var, keep up the good work!',
'Yes, that\'s my boy %var!',
'Amazing %var, keep going!',
'You did it %var, keep going!',
'Yea, one more point for my man %var!',
'Nice nice nice %var, very very nice',
'Score, one point for %var!',
'Damn, that was a nice point %var!',
'Wow %var, you\'re really good at this!',
],
matchWon: [
'Yes, congratulations %var!',
'And he does it, I can\'t believe it, amazing job %var!',
'I didn\'t think you could do it %var, but you did it, very nice!',
'Holy moly %var, that was a hell of a game!',
'I knew you could do it %var, very well done!',
'Wow %var, I hope you\'re signing autographs outside after this amazing performance!',
'Holy moly %var, have my babies, this was amazing!'
]
};
new Vue({
el: '#app',
data() {
return {
isSpeechOn: false, // if speechSynthesis is on
isTimerOn: false, // if we want timer to be used
isTimerCounting: false, // if timer is counting right now
matchScore: [0,0], // current match score for both players
name: ['Player 1', 'Player 2'], // names of players
playerBreaking: 0, // which player is currently on break
score: [0,0], // current points in this match
scoreHistory: [], // array of previous points in a match for undoing
timerStartFrom: 15, // when using timer, this is the starting value
timerCurrent: 15, // current state of the timer countdown
timeoutRef: null, // reference for the setTimeout() when ticking timer
}
},
methods: {
feedback(message) {
if(this.isSpeechOn) {
this.speak(message);
} else {
alert(message);
}
},
getRandomPhrase(phrases, variable=null) {
let _phrase = phrases[Math.floor(Math.random()*phrases.length)];
if(variable) {
_phrase = _phrase.replace('%var', variable);
}
return _phrase;
},
onChangeName(player) {
let promptTxt = `Enter new name for ${this.name[player]}`;
let name = prompt(promptTxt, this.name[player]);
if(name !== null) {
if(name.length > 0) {
Vue.set(this.name, [player], name);
if(this.isSpeechOn) {
this.speak(`Name changed to ${name}`);
}
} else {
this.feedback('That name is too short');
}
}
},
onGoFullscreen() {
try {
document.documentElement.requestFullscreen();
} catch {
alert('Unable to go fullscreen :(');
}
},
onIncreaseScore(player) {
this.scoreHistory.push(JSON.parse(JSON.stringify(this.score)));
Vue.set(this.score, player, this.score[player]+1);
if(this.isSpeechOn) {
let message = this.getRandomPhrase(phrases.pointWon, this.name[player]);
this.speak(message);
}
if(this.isTimerOn) {
this.timerRestart();
}
this.onSwitchBreak();
},
onFinishMatch() {
if(confirm('Finish this match?')) {
// count match won for player with more points
if(this.score[0] > this.score[1]) {
Vue.set(this.matchScore, 0, this.matchScore[0]+1);
if(this.isSpeechOn) {
let text = this.getRandomPhrase(phrases.matchWon, this.name[0]);
this.speak(text);
}
} else if(this.score[1] > this.score[0]) {
Vue.set(this.matchScore, 1, this.matchScore[1]+1);
if(this.isSpeechOn) {
let text = this.getRandomPhrase(phrases.matchWon, this.name[1]);
this.speak(text);
}
} else {
// draw, nobody gets a point.
this.feedback('The match is a draw, nobody gets a point');
}
// reset score for new match
this.score = [0,0]
this.scoreHistory = [];
this.onTimerStop();
}
},
onReset() {
let promptTxt = 'This will reset the point score, and the match score. Are you sure you want to continue?';
if(confirm(promptTxt)) {
this.scoreHistory = [];
this.score[0] = this.score[1]
= this.matchScore[0] = this.matchScore[1]
= 0;
}
},
onSwitchBreak() {
this.playerBreaking = (this.playerBreaking + 1) % 2;
},
onTimerSetTime() {
let amount = prompt('Enter time to use for timer', this.timerStartFrom);
amount = parseInt(amount);
if(amount > 0) {
this.timerStartFrom = amount;
this.timerCurrent = amount;
}
},
onTimerStart() {
if(this.timerCurrent === 0) {
this.timerCurrent = this.timerStartFrom;
}
this.isTimerCounting = true;
this.timeoutRef = setTimeout(this.timerTick, 1000);
},
onTimerStop() {
this.isTimerCounting = false;
this.timerCurrent = this.timerStartFrom;
clearTimeout(this.timeoutRef);
},
onUndoScore() {
let score = this.scoreHistory.pop();
if(score) {
this.score = score;
if(this.isSpeechOn) {
this.speak('Undoing last score update');
}
} else {
this.feedback('Nothing to undo');
}
},
playTickSound() {
let context = new AudioContext();
let o = context.createOscillator();
let g = context.createGain();
o.connect(g);
o.frequency.value = 440;
g.connect(context.destination);
o.start(0);
setTimeout(() => o.stop(), 100);
},
playEndSound() {
let context = new AudioContext();
let o = context.createOscillator();
let g = context.createGain();
o.connect(g);
o.frequency.value = 880;
g.connect(context.destination);
o.start(0);
setTimeout(() => o.stop(), 1000);
},
timerRestart() {
this.onTimerStop();
this.onTimerStart();
},
timerTick() {
if(this.timerCurrent > 0) {
this.timerCurrent--;
if(this.isTimerCounting) {
this.timeoutRef = setTimeout(this.timerTick, 1000);
if(this.timerCurrent < 6 && this.timerCurrent > 0) {
this.playTickSound();
} else if(this.timerCurrent === 0) {
this.playEndSound();
}
}
}
},
speak(text) {
let utterance = new SpeechSynthesisUtterance(text);
window.speechSynthesis.cancel();
window.speechSynthesis.speak(utterance);
}
}
})
</script>
<style>
body {
padding: 0;
margin: 0;
color: black;
background: white;
font-family: sans-serif;
}
* {
box-sizing: border-box;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#app {
display: flex;
flex-direction: column;
height: 100vh;
}
.toolbar {
display: flex;
justify-content: space-between;
flex-basis: 3rem;
flex-grow: 0;
flex-shrink: 0;
height: 3rem;
font-size: 1.5rem;
border-bottom: 1px solid grey;
}
.toolbar > section {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
/*flex-basis: 40%;*/
flex-grow: 2;
flex-shrink: 0;
}
.toolbar > section:last-child {
text-align: right;
justify-content: flex-end;
}
.toolbar .control {
position: relative;
padding: .75rem;
/*min-width: 3rem;*/
font-size: 1.2rem;
background: transparent;
border: none;
}
.toolbar .control input[type=checkbox] {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
.toolbar .match-score {
display: block;
line-height: 3rem;
/*flex-basis: 20%;*/
/*flex-grow: 1;*/
text-align: center;
}
.score {
display: flex;
flex-basis: 2;
flex-grow: 3;
flex-shrink: 0;
}
@media screen and (orientation:portrait) {
.score {
flex-direction: column;
}
}
.score--player-1,
.score--player-2 {
display: flex;
flex-direction: column;
flex-basis: 5rem;
flex-shrink: 0;
flex-grow: 2;
padding: 1rem;
height: 100%;
}
.name-container {
margin-bottom: 1rem;
flex-basis: 3rem;
flex-grow: 0;
flex-shrink: 0;
text-align: center;
font-size: 2rem;
line-height: 3rem;
border: 1px solid grey;
border-radius: .5rem;
}
.score-container {
position: relative;
display: flex;
width: 100%;
height: 100%;
border: 1px solid grey;
border-radius: .5rem;
}
.score-container .number {
margin: auto;
font-size: 6rem;
}
.score-container .break-indicator {
position: absolute;
top: 1em;
left: 1em;
font-size: 2em;
}
.timer-controls {
display: flex;
justify-content: space-around;
flex-direction: column;
flex-basis: 3rem;
flex-grow: 0;
flex-shrink: 0;
}
@media screen and (orientation:portrait) {
.timer-controls {
flex-direction: row;
}
}
.time-left {
text-align: center;
font-size: 2rem;
}
.time-left.critical {
color: red;
font-weight: bold;
}
.timer-controls .control {
padding: 1rem;
border: 1px solid black;
color: black;
background: grey;
border-radius: .5rem;
}
</style>
</body>
</html>