-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
362 lines (285 loc) · 8.63 KB
/
Copy pathscript.js
File metadata and controls
362 lines (285 loc) · 8.63 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// script for game.html
const rules = localStorage.getItem("GM_rules");
let settings = null;
if (rules) {
const parsed = JSON.parse(rules);
const isExpired = Date.now() - parsed.createdAt > 8 * 1000;
if (!isExpired) {
alert(
"Do not reload the website. Otherwise you will be redirected back to the main page.",
);
settings = parsed;
} else {
localStorage.removeItem("GM_rules");
}
}
if (!settings) {
window.location.href = "index.html";
}
const { mode, timer, gameType } = settings;
const inputField = document.querySelector("#countryGuessField");
const flagImg = document.querySelector("#flagImg");
const countryQuestion = document.querySelector("#countryQuestion");
let modeFull;
let countryData;
let question = 1;
let hintsLeft = 3;
let gameTimer = new easytimer.Timer();
// =====================
// INIT MODE SETTINGS
// =====================
function init() {
const el = document.querySelector("#timer");
el.textContent = `0:${timer}`;
switch (mode) {
case "guess-country-by-flag":
modeFull = "Guess the Country by Flag";
inputField.placeholder = "Guess the Country";
countryQuestion.style.display = "none";
break;
case "guess-capital-by-flag":
modeFull = "Guess the Capital by Flag";
inputField.placeholder = "Guess the Capital";
countryQuestion.style.display = "none";
break;
case "guess-continent-by-country":
modeFull = "Guess the Continent by Country";
inputField.placeholder = "Guess the Continent";
break;
case "guess-iso-by-flag":
modeFull = "Guess the ISO Code by Flag";
inputField.placeholder = "Guess the ISO Code";
countryQuestion.style.display = "none";
break;
case "guess-country-by-iso":
modeFull = "Guess the Country by ISO Code";
inputField.placeholder = "Guess the Country";
flagImg.style.display = "none";
break;
default:
modeFull = "Unknown Mode";
}
}
init();
console.log(`Settings:
Mode: ${modeFull}
Timer: ${timer}
Game Type: ${gameType}`);
document.querySelector("title").innerHTML = `Globe Mind | ${gameType} Game`;
document.querySelector("#modeHeader").textContent = `${modeFull} - ${gameType}`;
function setupHintDisplay() {
const answer = getAnswerRaw();
const hintEl = document.querySelector("#countryNameHints");
let display = "";
for (let char of answer) {
if (char === " ") display += " ";
else display += "-";
}
hintEl.textContent = display;
}
// =====================
// DATA HELPERS
// =====================
function questionData(q) {
return countryData[q - 1];
}
function normalizeAnswer(text) {
return text.toLowerCase().replaceAll("-", " ").replaceAll("'", "");
}
function getCorrectAnswer() {
const data = questionData(question);
switch (mode) {
case "guess-country-by-flag":
return normalizeAnswer(data.country);
case "guess-capital-by-flag":
return normalizeAnswer(data.capital);
case "guess-continent-by-country":
return normalizeAnswer(data.continent);
case "guess-iso-by-flag":
return normalizeAnswer(data.iso);
case "guess-country-by-iso":
return normalizeAnswer(data.country);
}
}
function getAnswerRaw() {
const data = questionData(question);
switch (mode) {
case "guess-country-by-flag":
case "guess-country-by-iso":
return data.country;
case "guess-capital-by-flag":
return data.capital;
case "guess-continent-by-country":
return data.continent;
case "guess-iso-by-flag":
return data.iso;
}
}
// =====================
// LOAD QUESTION (CORE)
// =====================
function loadQuestion() {
const data = questionData(question);
if (!data) {
console.log("Game Over!");
return;
}
gameTimer.stop();
gameTimer.start({
countdown: true,
startValues: { seconds: timer },
});
inputField.value = "|";
if (mode.includes("flag")) {
flagImg.src = data.image;
}
if (mode === "guess-continent-by-country") {
countryQuestion.textContent = data.country;
flagImg.src = data.image;
document.querySelector("#hintBtnPara").style.display = "none";
document.querySelector("#hintsBlockSpan").style.display = "none";
}
if (mode === "guess-country-by-iso") {
countryQuestion.textContent = data.iso;
}
setupHintDisplay();
}
// =====================
// FETCH DATA
// =====================
fetch("https://countrylookup-api.netlify.app/api/random/195")
.then((res) => res.json())
.then((data) => {
countryData = data;
loadQuestion();
})
.catch((err) => console.log(err));
// =====================
// KEYBOARD LOGIC
// =====================
function pressKey(key) {
if (key === " ") key = "space";
const btn = document.querySelector(
`.keyboard button[data-key="${key.toLowerCase()}"]`,
);
if (btn) {
btn.classList.add("pressed");
setTimeout(() => btn.classList.remove("pressed"), 50);
}
let value = inputField.value.replace("|", "");
if (key === "backspace") value = value.slice(0, -1);
else if (key === "enter") {
guessFunc();
return;
} else if (key === "space") value += " ";
else value += key;
inputField.value = value + "|";
}
document.querySelectorAll(".keyboard button").forEach((btn) => {
btn.addEventListener("click", () => {
pressKey(btn.dataset.key);
btn.blur();
});
});
document.addEventListener("keydown", (e) => {
const key = e.key;
if ([" ", "Enter", "Backspace"].includes(key)) {
e.preventDefault();
}
if (key === "Enter") {
pressKey("enter");
} else if (key === "Backspace") {
pressKey("backspace");
} else if (key === " ") {
pressKey("space");
} else if (/^[a-zA-Z]$/.test(key)) {
pressKey(key.toLowerCase());
}
});
// =====================
// GUESS LOGIC
// =====================
function guessFunc() {
let guess = normalizeAnswer(inputField.value.replace("|", ""));
const correct = getCorrectAnswer();
const el = document.querySelector("#timer");
if (guess === correct.toLowerCase()) {
document.querySelector("#checkSpan").textContent = "Correct!";
document.querySelector("#checkSpan").style.color = "green";
setTimeout(() => {
document.querySelector("#checkSpan").textContent = "";
document.querySelector("#checkSpan").style.color = "white";
}, 1000);
question++;
el.textContent = `0:${timer}`;
if (gameType === "Casual" && question === 11) {
alert("🎉 You guessed 10 correctly! You win!");
window.location.href = "index.html";
return;
}
if (question % 3 === 1 && question !== 1) {
hintsLeft += 3;
document.querySelector("#hintsLeftSpan").textContent = hintsLeft;
}
document.querySelector("#questionNum").textContent = question;
loadQuestion();
} else {
console.log("Wrong!");
document.querySelector("#checkSpan").textContent = "Wrong, try again...";
document.querySelector("#checkSpan").style.color = "red";
setTimeout(() => {
document.querySelector("#checkSpan").textContent = "";
document.querySelector("#checkSpan").style.color = "white";
}, 1000);
}
inputField.value = "|";
}
// =====================
// HINT BUTTON
// =====================
document.querySelector("#hintBtn").addEventListener("click", () => {
const answer = getAnswerRaw();
const hintEl = document.querySelector("#countryNameHints");
let current = hintEl.textContent.split("");
let hiddenIndexes = [];
for (let i = 0; i < answer.length; i++) {
if (current[i] === "-" && answer[i] !== " ") {
hiddenIndexes.push(i);
}
}
if (hiddenIndexes.length === 0) return;
if (hintsLeft <= 0) return;
hintsLeft--;
document.querySelector("#hintsLeftSpan").textContent = hintsLeft;
const randomIndex =
hiddenIndexes[Math.floor(Math.random() * hiddenIndexes.length)];
current[randomIndex] = answer[randomIndex];
hintEl.textContent = current.join("");
});
// =====================
// OTHER
// =====================
gameTimer.addEventListener("targetAchieved", () => {
const el = document.querySelector("#timer");
el.textContent = `0:00`;
if (gameType === "Casual") {
alert(
`Time's up! You lost.\nThe answer was ${getCorrectAnswer().toUpperCase()}`,
);
} else {
alert(
`Time's up!\n\nYou guessed ${question - 1} countries correctly! 🎉\n\nThe answer was ${getCorrectAnswer().toUpperCase()}`,
);
}
window.location.href = "index.html";
});
gameTimer.addEventListener("secondsUpdated", () => {
const time = gameTimer.getTimeValues();
const el = document.querySelector("#timer");
el.textContent = `0:${time.seconds.toString().padStart(2, "0")}`;
if (time.seconds <= 3) {
el.style.color = "red";
} else {
el.style.color = "";
}
});