-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.js
More file actions
165 lines (127 loc) · 4.74 KB
/
Clock.js
File metadata and controls
165 lines (127 loc) · 4.74 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
// ENUMS
// -------------
const GameState = Object.freeze({
READY: 0,
TICKING: 1,
PAUSED: 2,
GAMEOVER: 3
});
// -------------
const TurnState = Object.freeze({
WHITE: 0,
BLACK: 1
});
// -------------
// constants
const TEN_MILLI_SEC = 10;
// Class
// -------------
class Clock {
constructor() {
this.state = GameState.READY; // initial state
this.playerTurn = TurnState.WHITE; // p1 starts the game
this.white_clock = {
time: 300000, // starting time in milli seconds
move: 0, // number of moves played
increment: 0 // increment seconds in milli seconds
};
this.black_clock = {
time: 300000, // starting time in milli seconds
move: 0, // number of moves played
increment: 0 // increment seconds in milli seconds
};
this.tickingClock = null; // to keep track of ticking clock on turns
this.tickingFunction = null; // to keep global setInterval function to this obj
}
start(viewUpdateCallBack, viewSelector) {
// GameState.READY is a state where the game started from reset
// or the game resumed from paused
if (this.state === GameState.READY) {
// if tickingClock is null, the game started from reset
// Thus white becomes tickingClock
if (this.tickingClock === null) {
this.tickingClock = this.white_clock;
}
// Ready game should start ticking to carry on ticking process
this.state = GameState.TICKING;
}
// Start the clock
if (this.state === GameState.TICKING) {
// Use JS global [setInterval] func to tick every ten milli seconds ### lookup [setInterval] to understand how it works
this.tickingFunction = setInterval(() => {
// consume current player's clock
this.tickingClock.time -= TEN_MILLI_SEC;
// check gameover status
if (this.tickingClock.time < TEN_MILLI_SEC) {
this.tickingClock.time = 0;
this.state = GameState.GAMEOVER;
clearInterval(this.tickingFunction);
}
viewUpdateCallBack(viewSelector, this.tickingClock.time);
}, TEN_MILLI_SEC); // Ten instead of One milli sec used for performance and accracy wise
}
}
switchTurn(viewUpdateCallBack, viewSelector) {
/*** switchTurn only happens while game is ongoing */
if (this.state === GameState.TICKING) {
// stop previous [setInterval] process
clearInterval(this.tickingFunction);
// Update moves and calculate increment
this.tickingClock.move++;
// ### implement increment here -------------------------------------
if (this.playerTurn === TurnState.WHITE) {
this.playerTurn = TurnState.BLACK;
this.tickingClock = this.black_clock;
console.log('BLACK to play');
}
else if (this.playerTurn === TurnState.BLACK) {
this.playerTurn = TurnState.WHITE;
this.tickingClock = this.white_clock;
console.log('WHITE to play');
}
else {
console.log('Error occured in determining player state');
}
// start a new [setInterval] process
this.start(viewUpdateCallBack, viewSelector);
}
}
reset(t_1=DEFAULT_TIMES.c_1, t_2=DEFAULT_TIMES.c_2) {
clearInterval(this.tickingFunction);
this.tickingClock = null
this.tickingFunction = null
this.state = GameState.READY;
this.playerTurn = TurnState.WHITE;
this.white_clock = {
time: t_1,
move: 0,
// ### need to include incre
};
this.black_clock = {
time: t_2,
move: 0,
// ### need to include incre
};
}
set_timers(t_1, t_2) {
this.white_clock.time = t_1;
this.black_clock.time = t_2;
}
pause() {
/*** Game can be paused only while game is ongoing */
if (this.state === GameState.TICKING) {
this.state = GameState.PAUSED;
// stop previous [setInterval] process
clearInterval(this.tickingFunction);
}
}
resume(viewUpdateCallBack, viewSelector) {
/*** Game can be resumed only while game is paused */
if (this.state === GameState.PAUSED) {
this.state = GameState.READY;
// start a new [setInterval] process
this.start(viewUpdateCallBack, viewSelector);
}
}
}
// -------------