-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
314 lines (257 loc) · 9.39 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
// DOM nodes
const startButton = document.querySelector('#startButton');
const resetButton = document.querySelector('#resetButton');
const board = document.querySelector('#board');
const evalDiv = document.querySelector('#evaluation');
const headerDiv = document.querySelector('#header');
const directionsDiv = document.querySelector('#directions');
const scoreListDiv = document.querySelector('#scoreList');
const averageScoreDiv = document.querySelector('#averageScore');
// app state variables
const colors = ['red', 'green', 'blue'];
const possibleTickRates = [1000, 1500, 2000, 2500];
let score = 0;
let gameStarted = false;
let hasClickedGreen = false;
let hasNotClickedGreen = false;
let changeBoardColorIntervalID = null;
let scoreList = [];
let genreatedColors = [];
let startDate = null;
let endDate = null;
let count = 0;
let randomTickRate = getRandomTickRate();
// D3 initialization
// set the dimensions and margins of the graph
// var margin = {top: 10, right: 30, bottom: 30, left: 50},
// width = 460 - margin.left - margin.right,
// height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
// var svg = d3.select("#my_dataviz")
// .append("svg")
// .attr("width", width + margin.left + margin.right)
// .attr("height", height + margin.top + margin.bottom)
// .append("g")
// .attr("transform",
// "translate(" + margin.left + "," + margin.top + ")");
function getRandomColor() {
return colors[Math.floor(Math.random() * colors.length)];
}
function getRandomTickRate() {
return possibleTickRates[Math.floor(Math.random() * possibleTickRates.length)];
}
function changeBoardColor() {
startDate = 0;
endDate = 0;
changeBoardColorIntervalID = setInterval(() => {
score = 0;
if (hasClickedGreen || hasNotClickedGreen) {
clearInterval(changeBoardColorIntervalID);
}
if (gameStarted) {
let randomColor = getRandomColor();
genreatedColors.push(randomColor);
if (genreatedColors.length === 1 && isBoardGreen()) {
randomColor = getRandomColor();
}
while (randomColor === board.style.backgroundColor) {
randomColor = getRandomColor();
}
count++;
if (count > 4) {
randomColor = 'green';
}
genreatedColors.push(randomColor);
board.style.backgroundColor = randomColor;
startDate = +new Date();
if (isBoardGreen()) {
ding().play();
}
}
}, randomTickRate);
}
function isInteractionTooEarly() {
if (!genreatedColors.length ) {
return true;
}
return false;
}
function isBoardGreen() {
return board.style.backgroundColor === 'green';
}
function setUpGame(event) {
event.stopImmediatePropagation();
gameStarted = true;
changeBoardColor();
toggleResetButton(false);
toggleStartButton(false);
toggleHeader(false);
toggleDirections(false);
}
function clickedGameBoard(event) {
if (isInteractionTooEarly()) return true;
event.stopImmediatePropagation();
score = (+new Date() - startDate);
clearInterval(changeBoardColorIntervalID);
if (gameStarted) {
gameStarted = false;
const boardColor = board.style.backgroundColor;
if (boardColor === 'green') {
hasClickedGreen = true;
evalDiv.textContent = `Congrats! You clicked on the green color. Your reaction time is ${Math.round(score)} ms!`;
scoreList.push([{date: moment().format('YYYY-MM-DD')}, {value: score}]);
// append score to score list if list is less than 5 items
if (scoreList.length < 5) {
toggleResetButton(true);
const scoreListItem = document.createElement('li');
scoreListItem.textContent = `${scoreList.length}: ${Math.round(score)} ms`;
scoreListDiv.appendChild(scoreListItem);
}
console.log(scoreList)
// if score list is 5 items, calculate average score and display it
if (scoreList.length === 5) {
toggleResetButton(true, 'Reset');
const averageScore = scoreList.reduce((acc, curr) => acc + curr[1].value, 0) / scoreList.length;
const scoreListItem = document.createElement('li');
scoreListItem.textContent = `${scoreList.length}: ${Math.round(score)} ms`;
scoreListDiv.appendChild(scoreListItem);
averageScoreDiv.textContent = `Your average reaction time over the past five tries is ${Math.round(averageScore)} ms!`;
}
// fail safe to reset game if score list is greater than 5 items
if (scoreList.length > 5) {
resetGame(event);
}
} else {
// if not green color, reset game, show reset button with Try again text, and display evaluation text with error message
toggleResetButton(true);
hasNotClickedGreen = true;
evalDiv.textContent = `You did not click green. Please try again. However, your reaction time from the last color to the one you clicked was ${Math.round(score)} ms!`;
evalDiv.style.color = 'white';
}
// setupD3LineChart(scoreList);
}
}
function resetGame(event) {
event.stopImmediatePropagation();
genreatedColors = [];
// if score list has 5 entries, reset score list, remove all li elements from score list div, and remove average score text from evaluation div
if (scoreList.length === 5) {
scoreList = [];
while (scoreListDiv.firstChild) {
scoreListDiv.removeChild(scoreListDiv.firstChild);
}
averageScoreDiv.textContent = '';
}
if (scoreList.length < 5) {
// change text on reset button to try again
resetButton.textContent = 'Try Again';
}
// if start button is hidden, show it
if (startButton.style.display === 'none') {
startButton.style.display = 'block';
toggleResetButton(false);
}
// reset game state
score = 0;
count = 0;
endDate = 0;
startDate = 0;
gameStarted = false;
hasClickedGreen = false;
hasNotClickedGreen = false;
// reset board, buttons, and headers to default text state.
evalDiv.textContent = '';
headerDiv.style.display = 'block';
directionsDiv.style.display = 'block';
board.style.backgroundColor = 'lightblue';
}
function toggleStartButton(shouldShow) {
if (shouldShow) {
startButton.style.display = 'block';
} else {
startButton.style.display = 'none';
}
}
function toggleHeader(shouldShow) {
if (shouldShow) {
headerDiv.style.display = 'block';
} else {
headerDiv.style.display = 'none';
}
}
function toggleDirections(shouldShow) {
if (shouldShow) {
directionsDiv.style.display = 'block';
} else {
directionsDiv.style.display = 'none';
}
}
function toggleResetButton(shouldShow, text = 'Try again') {
if (shouldShow) {
resetButton.style.display = 'block';
} else {
resetButton.style.display = 'none';
}
if (text) resetButton.textContent = text;
}
function ding() {
const audioPlayer = document.createElement('audio');
audioPlayer.setAttribute('src', 'ding.mp3');
audioPlayer.setAttribute('autoplay', 'autoplay');
return audioPlayer;
}
startButton.addEventListener('mousedown', setUpGame);
startButton.addEventListener('touchstart', setUpGame);
resetButton.addEventListener('mousedown', resetGame);
resetButton.addEventListener('touchstart', resetGame);
board.addEventListener('mousedown', clickedGameBoard);
board.addEventListener('touchstart', clickedGameBoard);
// function setupD3LineChart(data = []) {
// // Keep only the 90 first rows
// // data = data.filter(function(d,i){ return i<90})
// // Add X axis --> it is a date format
// var x = d3.scaleTime()
// .domain(d3.extent(data, function(d) { return d.date; }))
// .range([ 0, width ]);
// svg.append("g")
// .attr("transform", "translate(0," + (height+5) + ")")
// .call(d3.axisBottom(x).ticks(5).tickSizeOuter(0));
// // Add Y axis
// var y = d3.scaleLinear()
// .domain( d3.extent(data, function(d) { return +d.value; }) )
// .range([ height, 0 ]);
// svg.append("g")
// .attr("transform", "translate(-5,0)")
// .call(d3.axisLeft(y).tickSizeOuter(0));
// // Add the area
// svg.append("path")
// .datum(data)
// .attr("fill", "#69b3a2")
// .attr("fill-opacity", .3)
// .attr("stroke", "none")
// .attr("d", d3.area()
// .x(function(d) { return x(d.date) })
// .y0( height )
// .y1(function(d) { return y(d.value) })
// )
// // Add the line
// svg.append("path")
// .datum(data)
// .attr("fill", "none")
// .attr("stroke", "#69b3a2")
// .attr("stroke-width", 4)
// .attr("d", d3.line()
// .x(function(d) { return x(d.date) })
// .y(function(d) { return y(d.value) })
// )
// // Add the line
// svg.selectAll("myCircles")
// .data(data)
// .enter()
// .append("circle")
// .attr("fill", "red")
// .attr("stroke", "none")
// .attr("cx", function(d) { return x(d.date) })
// .attr("cy", function(d) { return y(d.value) })
// .attr("r", 3)
// }