-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
319 lines (301 loc) · 8.69 KB
/
app.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
const videoDiv = document.querySelector("#intro-vid");
const startButton = document.querySelector("#start-btn");
const main = document.querySelector("main");
const game = document.querySelector("#game");
const question = document.querySelector("#question-text");
const option = document.querySelectorAll(".option");
const timer = document.querySelector("#timer-text");
const username = document.querySelector("#username");
const namee = document.querySelector("#name");
const prizeWon = document.querySelector("#prize-won");
const prizeHighlighter = document.querySelector("#question-info");
const winningScreen = document.querySelector(".winning-screen");
let seconds = 3,
money = 0,
gameStarted = 0,
flag = true;
const prizeMoney = [
"$500",
"$1,000",
"$1,500",
"$3,000",
"$5,000",
"$10,000",
"$25,000",
"$50,000",
"$1,00,000",
"$5,00,000",
];
const suspense = new Audio("./sounds/suspense.mp3");
const lock = new Audio("./sounds/lock.mp3");
const right = new Audio("./sounds/correct.mp3");
const wrong = new Audio("./sounds/wrong.mp3");
const optionColors = {
4: ["transparent goldenrod transparent #3e0575", "#3e0575"], //violet
0: ["transparent goldenrod transparent #f9ba06", "#f9ba06"], //yellow
1: ["transparent goldenrod transparent green", "green", "white"], //green
2: ["transparent goldenrod transparent red", "red", "white"], //red
};
const messages = {
winning: "You are now the CreworkPati. You won $500,000.",
losing: `Better luck next time.<br><img style="width: 250px; border-radius: 10px;" src="./images/lol.png" alt="">`,
};
function startGame(a) {
flag = true;
for (let m = 1; m < 11; m++) {
prizeHighlighter.children[m].style.color = "white";
}
let timer = 0;
if (a == 0) {
threeTwoOneStart();
timer = 4000;
} else {
timer = 1000;
}
//start game on button press
winningScreen.style.display = "none";
namee.innerHTML = "Hi, " + username.value + "👋🏻";
if (username.value == "") {
namee.innerHTML = "Hi, Guest 👋🏻";
}
gameStarted++;
setTimeout(() => {
//hide the video after playing it
main.style.display = "none";
videoDiv.style.display = "flex";
videoDiv.play();
}, timer);
setTimeout(() => {
videoDiv.style.display = "none";
game.style.display = "flex";
game.style.transition = "ease 200ms";
stopTTO(); //stop the 321 setinterval timer
l();
startQuestionTimer();
}, 30000);
}
function changeColorOfOptions(t, elements) {
elements[0].style.borderColor = optionColors[t][0];
elements[1].style.backgroundColor = optionColors[t][1];
elements[2].style.borderColor = optionColors[t][0];
}
let io = 0; //flagship question traversal counter - do not touch
game.addEventListener("click", (e) => {
let hasClass = e.target.classList.contains("option");
let parent = e.target.parentElement;
let childs = parent.children;
let userAnswer = childs[1].innerHTML;
if (hasClass) {
suspense.pause();
suspense.currentTime = 0;
game.style.pointerEvents = "none";
let splCaseFlag = false;
changeColorOfOptions(0, childs); //0 for yellow
lock.play();
if (userAnswer.indexOf(" ") > -1) {
//removing spaces from options - api has a bug
let kol = userAnswer.slice(0, -6);
userAnswer = kol;
let jol = correctAns[io - 1].slice(0, -1);
splCaseFlag = kol == jol;
}
if (userAnswer == correctAns[io - 1] || splCaseFlag) {
splCaseFlag = false;
console.log("Correct answer");
setTimeout(() => {
//setting green
lock.pause();
lock.currentTime = 0;
right.play();
changeColorOfOptions(1, childs); //1 for green
}, 3000);
setTimeout(() => {
money = money + 1000;
prizeWon.style.display = "flex";
prizeWon.innerHTML = `🎉 You won ${prizeMoney[(io - 1) % 10]} 🎉`;
}, 4500);
setTimeout(() => {
prizeWon.style.display = "none";
}, 8000);
setTimeout(() => {
//setting back to purple
changeColorOfOptions(4, childs); //4 for purple
}, 10000);
} else {
console.log("Wrong answer");
document.querySelector(".winning-text").innerHTML = messages.losing;
flag = false;
setTimeout(() => {
//setting red
lock.pause();
lock.currentTime = 0;
wrong.play();
changeColorOfOptions(2, childs); //2 for red
}, 3000);
setTimeout(() => {
prizeWon.style.display = "flex";
if (io % 10 == 1) {
prizeWon.innerHTML = `You won $0. Better luck next time.`;
} else {
prizeWon.innerHTML = `You won ${
prizeMoney[(io - 2) % 10]
}. Better luck next time.`;
}
}, 4500);
setTimeout(() => {
prizeWon.style.display = "none";
}, 8000);
setTimeout(() => {
//setting back to purple
changeColorOfOptions(4, childs); //4 for purple
}, 10000);
}
stopQuestionTimer();
if (io % 10 < 10 && flag) {
if (io % 10 == 9) {
flag = false;
document.querySelector(".winning-text").innerHTML = messages.winning;
}
setTimeout(() => {
l();
startQuestionTimer();
}, 10000);
} else {
//game reaches last question or answered wrong
setTimeout(() => {
game.style.display = "none";
winningScreen.style.display = "flex";
io = gameStarted * 10;
callingQnAFromAPI(10);
}, 10000);
}
}
});
function threeTwoOneStart() {
//timer for launch kbc button
tto = setInterval(() => {
startButton.textContent = `Starting in ${seconds}`;
seconds--;
}, 1000);
}
function stopTTO() {
//stop timer for launch kbc button
clearInterval(tto);
}
function startQuestionTimer() {
let ss = 60;
qt = setInterval(() => {
//countdowning the timer
timer.textContent = ss;
if (ss == 0) {
//timer runs out of time
stopQuestionTimer();
setTimeout(() => {
game.style.display = "none";
suspense.pause();
suspense.currentTime = 0;
prizeWon.style.display = "flex";
prizeWon.innerHTML = prizeMoney[(io - 1) % 10];
money = 0;
}, 1500);
setTimeout(() => {
prizeWon.style.display = "none";
winningScreen.style.display = "flex";
document.querySelector(".winning-text").innerHTML = messages.losing;
io = gameStarted * 10;
fetch10QnABruteForce(10);
username.value = "";
}, 5000);
}
ss--;
}, 1000);
}
function stopQuestionTimer() {
clearInterval(qt);
}
function shuffle(arr) {
//shuffle the options order
var j, x, index;
for (index = arr.length - 1; index > 0; index--) {
j = Math.floor(Math.random() * (index + 1));
x = arr[index];
arr[index] = arr[j];
arr[j] = x;
}
return arr;
}
//fix the difficulty of questions order in api calls here
let difficulty = [
"easy",
"medium",
"medium",
"medium",
"medium",
"medium",
"hard",
"hard",
"hard",
"hard",
];
let jsonArr = [],
optionsArr = [],
correctAns = [],
count = 0,
pushed = 0;
function callingQnAFromAPI(x) {
for (let i = 0; i < x; i++) {
let url = `https://the-trivia-api.com/api/questions?limit=1&difficulty=${difficulty[i]}`;
axios.get(url).then((res) => {
count++;
if (res.data[0].question.length < 100) {
//checking for question length
if (
res.data[0].correctAnswer.length < 60 &&
res.data[0].incorrectAnswers[0].length < 60 &&
res.data[0].incorrectAnswers[1].length < 60 &&
res.data[0].incorrectAnswers[2].length < 60
) {
//checking for options length
pushed++;
jsonArr.push(res.data[0].question);
let arr = [
res.data[0].correctAnswer,
res.data[0].incorrectAnswers[0],
res.data[0].incorrectAnswers[1],
res.data[0].incorrectAnswers[2],
];
optionsArr.push(shuffle(arr));
correctAns.push(res.data[0].correctAnswer);
}
}
});
}
console.log(`api called ${count} times`);
return x - pushed;
}
fetch10QnABruteForce(10);
function fetch10QnABruteForce(q) {
let xx = 0;
for (let i = 0; i < 2; i++) {
xx = callingQnAFromAPI(q);
q = xx;
}
}
function l() {
game.style.pointerEvents = "auto";
q(io);
io++;
}
function q(ioo) {
prizeHighlighter.children[10 - (ioo % 10)].style.color = "goldenrod";
if (ioo % 10 > 0) {
prizeHighlighter.children[10 - (ioo % 10) + 1].style.color = "white";
}
question.textContent = jsonArr[ioo];
option[0].textContent = optionsArr[ioo][0];
option[1].textContent = optionsArr[ioo][1];
option[2].textContent = optionsArr[ioo][2];
option[3].textContent = optionsArr[ioo][3];
suspense.play();
suspense.loop = true;
}