-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
334 lines (287 loc) · 8.54 KB
/
script.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
'use strict';
///////////////////////////////////////////////////////////////////
// Database
import {
scienceObj,
historyObj,
geographyObj,
philosophyObj,
sportsObj,
randomObj,
} from './database.js';
let science = JSON.parse(JSON.stringify(scienceObj));
let history = JSON.parse(JSON.stringify(historyObj));
let geography = JSON.parse(JSON.stringify(geographyObj));
let philosophy = JSON.parse(JSON.stringify(philosophyObj));
let sports = JSON.parse(JSON.stringify(sportsObj));
let random = JSON.parse(JSON.stringify(randomObj));
///////////////////////////////////////////////////////////////////
// Getting elements & declaring variables
const categories = document.querySelectorAll('.card');
const startButton = document.getElementById('begin-btn');
const settingsButton = document.getElementById('settings-btn');
const okayButton = document.getElementById('okay-btn');
const applyButton = document.getElementById('apply-btn');
const newGameButton = document.getElementById('new-game-btn');
const difficultyButtons = document.querySelectorAll('input[name="diff"]');
const buttons = document.querySelectorAll('.answer-btn');
const settingsMenu = document.getElementById('settings');
const gameMenu = document.getElementById('game-menu');
const welcomeMenu = document.getElementById('welcome');
const questionHTML = document.getElementById('question');
const detailsHTML = document.getElementById('game-details');
const correctHTML = document.getElementById('correct');
const incorrectHTML = document.getElementById('incorrect');
const endGameMenu = document.getElementById('endgame');
const finalCorrect = document.getElementById('correct-answers');
const finalIncorrect = document.getElementById('incorrect-answers');
const totalScore = document.getElementById('score');
const happyEmoji = document.getElementById('happy-emoji');
const sadEmoji = document.getElementById('sad-emoji');
const display = document.getElementById('timer');
const timerBox = document.querySelector('.timer-box');
const releaseNotes = document.getElementById('release-notes');
let answer;
let category;
let question;
let options;
let mode;
let index = 0;
let correctAnswers = 0;
let incorrectAnswers = 0;
let questionsArr;
let length;
let countdown;
let duration = 15;
let isFinished = false;
///////////////////////////////////////////////////////////////////
// Force app to reset initial position on reload
window.onbeforeunload = function () {
window.scrollTo(0, 0);
};
///////////////////////////////////////////////////////////////////
// Categories selection
categories.forEach(panel => {
panel.addEventListener('click', () => {
removeActiveClasses();
panel.classList.add('active');
});
});
///////////////////////////////////////////////////////////////////
// Buttons
startButton.addEventListener('click', () => {
playAudio('click');
scrollTo(gameMenu);
categories.forEach(panel => {
if (panel.classList.contains('active')) category = panel.id;
});
category = parseCategory(category);
questionsArr = generateQuestion(category);
setDifficulty();
nextQuestion(category);
length = questionsArr.length;
});
buttons.forEach(button =>
button.addEventListener('click', function () {
selectAnswer(button);
})
);
difficultyButtons.forEach(button => {
button.addEventListener('click', () => {
playAudio('click');
});
});
newGameButton.addEventListener('click', () => {
playAudio('click');
newGame();
});
settingsButton.addEventListener('click', () => {
playAudio('click');
settingsMenu.classList.remove('hidden');
});
applyButton.addEventListener('click', () => {
playAudio('click');
settingsMenu.classList.add('hidden');
});
okayButton.addEventListener('click', () => {
playAudio('click');
releaseNotes.classList.add('hidden');
});
///////////////////////////////////////////////////////////////////
// Functions
function scrollTo(section) {
section.scrollIntoView({ behavior: 'smooth' });
}
function removeActiveClasses() {
categories.forEach(panel => {
panel.classList.remove('active');
});
}
function parseCategory(category) {
if (category === 'random') {
category = random[Math.floor(Math.random() * random.length)];
mode = 'Random';
}
switch (category) {
case 'science':
category = science;
mode = 'Science';
break;
case 'history':
category = history;
mode = 'History';
break;
case 'sports':
category = sports;
mode = 'Sports';
break;
case 'geography':
category = geography;
mode = 'Geography';
break;
case 'philosophy':
category = philosophy;
mode = 'Philosophy';
break;
default:
break;
}
return category;
}
function generateQuestion(category) {
return Object.keys(category).sort((a, b) => 0.5 - Math.random());
}
function setDifficulty() {
difficultyButtons.forEach(button => {
if (button.checked) {
switch (button.id) {
case 'easy':
duration = 30;
break;
case 'normal':
duration = 15;
break;
case 'hard':
duration = 5;
break;
}
}
});
}
function selectAnswer(button) {
if (button.textContent === answer) {
playAudio('correct-sound');
correctAnswers++;
} else {
playAudio('wrong-sound');
incorrectAnswers++;
}
if (index < length - 1) {
index++;
} else {
endGame();
}
clearInterval(countdown);
nextQuestion(category);
}
function nextQuestion(category) {
question = questionsArr[index];
answer = category[question][0];
options = category[question].sort((a, b) => 0.5 - Math.random());
console.log(answer);
questionHTML.textContent = question;
buttons.forEach((button, index) => (button.textContent = options[index]));
detailsHTML.textContent = `${mode} - Question ${index + 1} of ${Object.keys(category).length}`;
correctHTML.textContent = correctAnswers;
incorrectHTML.textContent = incorrectAnswers;
startTimer(duration);
}
function endGame() {
endGameMenu.classList.remove('hidden');
const score = Math.round((correctAnswers / length) * 100);
finalCorrect.textContent = `${correctAnswers} correct`;
finalIncorrect.textContent = `${incorrectAnswers} incorrect`;
totalScore.textContent = `${score}%`;
if (score > 50) {
totalScore.style.color = '#00ff15';
happyEmoji.classList.remove('hidden');
sadEmoji.classList.add('hidden');
playAudio('victory');
} else {
totalScore.style.color = 'red';
happyEmoji.classList.add('hidden');
sadEmoji.classList.remove('hidden');
playAudio('defeat');
}
isFinished = true;
}
function newGame() {
endGameMenu.classList.add('hidden');
isFinished = false;
index = 0;
correctAnswers = 0;
incorrectAnswers = 0;
science = JSON.parse(JSON.stringify(scienceObj));
history = JSON.parse(JSON.stringify(historyObj));
geography = JSON.parse(JSON.stringify(geographyObj));
philosophy = JSON.parse(JSON.stringify(philosophyObj));
sports = JSON.parse(JSON.stringify(sportsObj));
random = JSON.parse(JSON.stringify(randomObj));
clearInterval(countdown);
scrollTo(welcomeMenu);
}
function playAudio(audio) {
stopAudio();
const sound = document.getElementById(audio);
sound.play();
}
function stopAudio() {
const sounds = ['wrong-sound', 'correct-sound', 'clock-tick-sound'];
sounds.forEach(sound => {
const s = document.getElementById(sound);
s.pause();
s.currentTime = 0;
});
}
function startTimer(duration) {
let timer = duration;
let minutes = 0;
let seconds = 0;
countdown = setInterval(() => {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
// minutes = minutes < 10 ? '0' + minutes : minutes;
// seconds = seconds < 10 ? '0' + seconds : seconds;
// display.textContent = minutes + ':' + seconds;
display.textContent = seconds;
if (seconds >= 10) {
display.style.color = '#00ff15';
timerBox.style.borderColor = '#00ff15';
} else if (seconds > 6) {
display.style.color = '#ffa600';
timerBox.style.borderColor = '#ffa600';
} else if (seconds < 6) {
display.style.color = 'red';
timerBox.style.borderColor = 'red';
playAudio('clock-tick-sound');
}
if (isFinished) {
clearInterval(countdown);
stopAudio();
}
if (--timer < 0) {
clearInterval(countdown);
playAudio('wrong-sound');
incorrectAnswers++;
console.log(index);
if (index < length - 1) {
index++;
clearInterval(countdown);
nextQuestion(category);
} else {
clearInterval(countdown);
endGame();
}
}
}, 1000);
}