-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayTimer.js
More file actions
95 lines (83 loc) · 2.52 KB
/
PlayTimer.js
File metadata and controls
95 lines (83 loc) · 2.52 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
import DementiaEvent from './DementiaEvent.js';
export default class PlayTimer {
_gameboard_buttons = document.getElementById('gameboard-buttons');
_message = [
'Time is of the essence.',
'Did you see the countdown clock below the gameboard?',
'I guess you really like this level and just want to keep playing it over and over again?',
'Here\'s a hint. Try memorizing some of the matches before you start playing.',
'Are you wearing a blindfold?'
];
_currentTime = 0;
_worker = undefined;
_running = false;
constructor(soundPlayer) {
this._soundPlayer = soundPlayer;
}
_createWorkAndListener() {
this._worker = new Worker('./playTimerWorker.js');
this._worker.onmessage = (e) => {
this._handleDataFromWorker(Number(e.data));
};
}
start(secs) {
if (typeof secs === 'number') {
this._currentTime = secs * 1000;
}
this._createWorkAndListener();
this._worker.postMessage(this._currentTime);
this._running = true;
}
_end(status) {
return new Promise(resolve => {
DementiaEvent.dispatch('timer-end', {
message: this._message[Math.floor(Math.random() * this._message.length)],
status
});
this._soundPlayer.play('fail');
resolve();
});
}
pause() {
if (this._running) {
this._worker.terminate();
this._running = false;
}
}
resume() {
this.start();
}
stop() {
if (this._running) {
this._worker.terminate();
this._gameboard_buttons.innerHTML = '';
this._currentTime = 0;
this._running = false;
}
}
_handleDataFromWorker(time) {
this._currentTime = time;
if (time === 0) {
this.stop();
DementiaEvent.queue([
this._end.bind(this)
]);
return;
}
if (time >= 3000) {
if (time % 1000 === 0) {
this._gameboard_buttons.innerHTML = time / 1000;
this._soundPlayer.play('playTimer');
}
}
else {
if (time % 1000 === 0) {
this._gameboard_buttons.innerHTML = `${time/1000}.0`;
this._soundPlayer.play('playTimer');
}
else {
this._gameboard_buttons.innerHTML = time / 1000;
}
}
}
}