-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
314 lines (260 loc) · 8.15 KB
/
main.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
let keys;
let americanKeys;
let current = {
active: null,
next: null,
mode: "text",
};
let difficulty = "normal";
let flagSet = "world";
let timeline = { active: false, transition: false };
let score = { current: 0, best: 0, deductions: 2 };
let history = [];
document.addEventListener("keypress", (e) => {
const inputElement = document.getElementById("guess");
if (e.key === "Enter") {
if (current.active.aliases !== undefined) {
for (let i = 0; i < current.active.aliases.length; i++) {
if (guessAccuracy(inputElement.value, current.active.aliases[i]) > 0.8) {
transition(true);
return;
}
}
}
if (guessAccuracy(inputElement.value, current.active.country) > 0.8) {
transition(true);
} else {
showIncorrect();
}
}
return;
});
const transition = async (correct) => {
timeline.active = true;
if (correct) {
showCorrect();
} else {
showIncorrect();
}
await reScale(1);
let allFlags = document.getElementsByClassName("flag");
allFlags[0].style.opacity = 0;
allFlags[1].style.opacity = 1;
allFlags[1].style.left = 0;
setBackgroundGradient(allFlags[1].src);
multiTransitionDestroy();
await sleep(200);
document.getElementById("image-container").removeChild(allFlags[0]);
if (current.mode === "multi") multiTransitionCreate();
newFlag();
};
async function showCorrect() {
document.getElementById("check").classList.add("scale");
await sleep(500);
document.getElementById("check").classList.remove("scale");
document.getElementById("guess").value = "";
}
async function showIncorrect() {
document.getElementById("cross").classList.add("scale");
await sleep(500);
document.getElementById("cross").classList.remove("scale");
document.getElementById("guess").value = "";
}
async function multiTransitionCreate() {
let container = document.getElementsByClassName("multi-choice-container")[0];
if (container.childElementCount > 0) {
await sleep(200);
}
let elements = await createMultiOption();
for (let i = elements.length - 1; i >= 0; i--) {
await sleep(30);
container.appendChild(elements[i]);
createOption(elements[i]);
}
}
async function createOption(element) {
await sleep(200);
element.style.opacity = 1;
}
function hintTransitionDestroy() {
let hint = document.getElementsByClassName("hint-letter");
if (hint.length) hint[0].remove();
}
function descriptionTransitionDestroy() {
let desc = document.getElementsByClassName("description-container");
desc[0].innerText = "";
}
async function multiTransitionDestroy() {
hintTransitionDestroy();
descriptionTransitionDestroy();
let multi = document.getElementsByClassName("hint-option");
if (multi.length > 0) {
for (let j = multi.length - 1; j >= 0; j--) {
await sleep(70);
destroyOption(multi[j]);
}
}
}
async function destroyOption(element) {
element.style.opacity = 0;
await sleep(300);
element.remove();
}
async function reScale(target) {
const res = reSizeImage(target);
if (!res) {
await sleep(500);
reSizeImage(target);
}
return;
}
function reSizeImage(target) {
let totalWidth = window.innerWidth;
let imageContainer = document.getElementById("image-container");
let image = document.getElementsByClassName("flag")[target];
let imageWidth = image.offsetWidth;
let imageHeight = image.offsetHeight;
let imageAspect = imageWidth / imageHeight;
if (imageAspect > 4) {
return false;
}
let difference = elementIntersection(
document.getElementsByClassName("flag")[target],
document.getElementsByClassName("text-input")[0]
);
let calculation = imageContainer.offsetWidth - difference * imageAspect;
if (calculation > totalWidth - 50) {
calculation = totalWidth - 50;
}
if (imageAspect < 4) {
imageContainer.style.width = `${calculation}px`;
}
return true;
}
async function setBackgroundGradient(source) {
document.getElementById("colored-panel").style.backgroundColor = "rgba(44, 44, 44, 1)";
await sleep(500);
let col = await get_average_rgb(source);
document.getElementsByTagName(
"body"
)[0].style.background = `linear-gradient(rgb(${col[0]}, ${col[1]}, ${col[2]} ), var(--main-bg), var(--main-bg))`;
document.getElementById("colored-panel").style.backgroundColor = "rgba(44, 44, 44, 0)";
if (contrastFunction(col[0], col[1], col[2])) {
document.querySelector("#settings-icon").style.fill = "black";
} else {
document.querySelector("#settings-icon").style.fill = "white";
}
}
document.getElementById("skip").onclick = async (e) => {
if (timeline.active) return;
skip();
};
const skip = async () => {
if (timeline.active) return;
timeline.active = true;
revealCountry();
transition(false);
};
// Levenshtein distance
function guessAccuracy(s1, s2) {
if (s1.length === 0 || s2.length === 0) return;
if (s2.length > s1.length) {
[s1, s2] = [s2, s1];
}
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
let costs = [];
for (let i = 0; i <= s1.length; i++) {
let lastValue = i;
for (let j = 0; j <= s2.length; j++) {
if (i == 0) costs[j] = j;
else {
if (j > 0) {
let newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) costs[s2.length] = lastValue;
}
console.log((s1.length - costs[s2.length]) / s1.length);
return (s1.length - costs[s2.length]) / s1.length;
}
const randomCountry = (skipHistory) => {
let res;
let index = Math.round(Math.random() * (keys.length - 1));
res = keys[index];
if (difficulty === "easy" && countries[res].difficulty > 5) {
res = randomCountry();
console.log("Too Hard: skipped");
} else if (difficulty === "hard" && countries[res].difficulty < 6) {
res = randomCountry();
console.log("Too Easy: skipped");
}
if (skipHistory) return res;
if (history.indexOf(res) !== -1) {
res = randomCountry();
console.log("In history: skipped");
}
history.push(res);
if (history.length > 25) {
history.shift();
}
return res;
};
const randomState = () => {
let key = americanKeys[Math.round(Math.random() * (americanKeys.length - 1))];
let formattedKey = key.replace("us-", "");
if (history.indexOf(formattedKey) !== -1) {
formattedKey = randomState();
console.log("In history: skipped");
}
history.push(formattedKey);
if (history.length > 25) {
history.shift();
}
return formattedKey;
};
async function get_average_rgb(src) {
return new Promise((resolve) => {
let context = document.createElement("canvas").getContext("2d");
context.imageSmoothingEnabled = true;
let img = new Image();
img.src = src;
img.crossOrigin = "";
img.onload = () => {
context.drawImage(img, 0, 0, 2, 2);
resolve(context.getImageData(0, 0, 1, 1).data.slice(0, 3));
};
});
}
const contrastFunction = (r, g, b) => {
let x = r * 299 + g * 587 + b * 114;
x = x / 1000;
return x >= 128;
};
const sleep = (t) => new Promise((s) => setTimeout(s, t));
const debounce = (func) => {
let time = 100;
let timer;
return (event) => {
if (timer) clearTimeout(timer);
timer = setTimeout(func, time, event);
};
};
window.addEventListener(
"resize",
debounce(() => {
reSizeImage(0);
})
);
(() => {
keys = Object.keys(countries);
americanKeys = Object.keys(countries.us.states);
newFlag();
setBackgroundGradient(document.getElementsByClassName("flag")[0].src);
reScale(0);
})();