-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.html
More file actions
491 lines (418 loc) · 16.2 KB
/
Copy pathplay.html
File metadata and controls
491 lines (418 loc) · 16.2 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="static/css/styles.css">
<title>ScriptGuesser</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-4.0.0.js" integrity="sha256-9fsHeVnKBvqh3FB2HYu7g2xseAZ5MlN6Kz/qnkASV8U=" crossorigin="anonymous"></script>
<script src="static/js/script_utils.js"></script>
<div class="container-fluid bg-dark min-vh-100 d-flex flex-column text-white">
<nav class="navbar bg-primary navbar-dark fixed-top">
<div class="container-fluid flex-nowrap">
<ul class="navbar-nav flex-row gap-3">
<li class="nav-item">
<a class="nav-link active" href="index.html">Main Menu</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Play</a>
</li>
<li class="nav-item">
<a class="nav-link" href="learn.html">Learn</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">About</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="about/qa.html">Q&A</a></li>
<li><a class="dropdown-item" href="about/documentation.html">Documentation</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<!-- Script comparison popup -->
<div class="modal fade" id="script-comparison-modal">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Script Comparison</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div id="script-comparison-modal-body" class="modal-body">
Loading scripts...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="row">
<div class="container-fluid p-5 my-4 bg-light text-black text-center text-break" style="height: 180px; display: flex; overflow: hidden;">
<h1 id="script-lbl" class="text-center">Placeholder</h1>
</div>
</div>
<div class="row d-flex align-items-center justify-content-center">
<div class="col-md-8 text-center">
<div id="status-alert" class="alert" hidden>
Placeholder
</div>
</div>
</div>
<div class="row d-flex align-items-center justify-content-center">
<div class="col-md-6">
<div id="guess-bar" class="input-group mb-3 input-group-lg">
<input id="guess-text" type="text" class="form-control text-center" placeholder="Your guess">
<button id="guess-btn" class="btn btn-primary text-white" onclick="sendUserGuess(guessText.value)">Guess!</button>
</div>
<div id="control-bar" class="mb-3 input-group-lg align-items-center justify-content-center text-center" hidden>
<a id="learn-more-button" class="btn btn-secondary text-white" type="submit" href="learn.html" target="_blank">Learn More</a>
<button id="next-round-button" class="btn btn-primary text-white" type="submit" onclick="nextRound()">Next Round</button>
<br>
<button id="compare-scripts-button" class="btn btn-primary text-white my-2" data-bs-toggle="modal" data-bs-target="#script-comparison-modal" type="submit" onclick="compareScripts([game.rounds[game.currentRound].script, game.rounds[game.currentRound].userDetectedScript], $('#script-comparison-modal-body')[0])">Compare with my guess</button>
</div>
</div>
</div>
<div class="row d-flex align-items-center justify-content-center">
<div class="col-md-5"></div>
<div class="col-md-7 text-end">
<button id="config-button" class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#config-container">Config</button>
<div id="config-container" class="collapse">
<div class="container-fluid bg-light text-black">
<div class="row">
<div class="col">
<div class="border rounded p-2" style="height: 330px; overflow-y: auto;">
<div class="container-fluid text-start" style="height: 220px">
<label for="config-sentence-length-range" class="form-label">Sentence Length</label>
<div class="mb-2 gap-2 align-items-center d-flex">
<input id="config-sentence-length-range" type="range" class="form-range flex-grow-1" min="1" max="50" step="1" value="10" oninput="configUpdateSentenceLength(this.value)" list="config-sentence-length-range-options">
<datalist id="config-sentence-length-range-options">
<option value="10"></option>
</datalist>
<input id="config-sentence-length-input" type="number" class="form-control w-auto" min="1" max="50" step="1" value="10" style="max-width: 100px;" oninput="configUpdateSentenceLength(this.value)">
</div>
</div>
<div class="border rounded p-2" style="height: 100px;">
<div class="row">
<div class="col-md-6 text-start">
<select selected="TITLE" id="config-preset-list" class="form-select py-2" style="overflow-y: scroll; max-height: 300px;" data-live-search="true" data-live-search-style="startsWith" onchange="enableScriptsByPreset(this.value); this.value = 'TITLE'">
<option value="TITLE" selected disabled>Add by preset</option>
</select>
<select selected="TITLE" id="config-country-list" class="form-select py-2" style="overflow-y: scroll; max-height: 300px;" data-live-search="true" data-live-search-style="startsWith" onchange="enableScriptsByCountry(this.value); this.value = 'TITLE'">
<option value="TITLE" disabled>Add by country</option>
</select>
</div>
<div class="col-md-6 d-flex align-items-center">
<div class="input-group mb-2 border flex-nowrap align-items-center">
<button class="btn btn-primary text-white" onclick="enableAllScripts()">All</button>
<button class="btn btn-primary text-white" onclick="disableAllScript()">None</button>
<button class="btn btn-primary text-white" onclick="invertEnabledScripts()">Invert</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col">
<div class="border rounded p-2" style="height: 330px;">
<input id="config-script-list-search" type="text" class="form-control text-center my-1" placeholder="Search...">
<div id="config-script-list" class="list-group" style="height: 270px; overflow-y: auto;">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="script-information" hidden>
<div class="row d-flex justify-content-center m-2 p-2 border">
<div class="col-md-8" id="script-information-main">
<p id="script-information-description"></p>
<b id="script-information-tip"></b>
</div>
<div class="col-md-4" id="script-information-map">
</div>
</div>
</div>
</div>
<script>
// Consts
const statusAlert = $("#status-alert")[0];
const guessText = $("#guess-text")[0];
const scriptLabel = $("#script-lbl")[0];
const uiConfigCountryList = $("#config-script-list")[0];
// Prepare config
var config = {
"enabledScripts": [],
"sentenceLength": 10,
"MIN_SENTENCE_LENGTH": 1,
"MAX_SENTENCE_LENGTH": 50
}
for (var i in allScripts) {
config.enabledScripts.push(i);
var btn = document.createElement("button");
btn.className = "list-group-item list-group-item-action";
if (config.enabledScripts.includes(i)) {
btn.classList.add("active");
}
btn.value = i;
btn.type = "button";
btn.innerHTML = allScripts[i].label;
btn.onclick = function() {
toggleScriptEnabled(this.value);
}
uiConfigCountryList.appendChild(btn);
}
for (var i of orderedScriptsByCountryIsos) {
var option = document.createElement("option");
option.value = i;
option.innerHTML = countriesIso2[i];
$("#config-country-list")[0].appendChild(option);
}
for (var i in allPresets) {
var option = document.createElement("option");
option.value = i;
option.innerHTML = allPresets[i].label;
$("#config-preset-list")[0].appendChild(option);
}
$("#config-country-list")[0].value = "TITLE";
var game = {
"rounds": [],
"currentRound": -1
}
function showScriptInfo(scriptId) {
var script = allScripts[scriptId];
if (!script) {
console.error("Unable to load script information for script id '" + scriptId + "'.");
return;
}
$("#script-information-description")[0].innerText = script.description;
$("#script-information-tip")[0].innerText = script.tip;
mapHighlightCountries(scriptInfoMap.querySelector("svg"), script.countries);
$("#script-information")[0].hidden = false;
mapZoomToCountries(scriptInfoMap.querySelector("svg"), script.countries);
}
function hideScriptInfo() {
$("#script-information-description")[0].innerText = "";
$("#script-information-tip")[0].innerText = "";
$("#script-information")[0].hidden = true;
}
function fitText(element, maxFont = 80, minFont = 10) {
var fontSize = maxFont;
element.style.fontSize = fontSize + "px";
// Dirty hack
while (element.scrollHeight > 100 && fontSize > minFont) {
fontSize--;
element.style.fontSize = fontSize + "px";
}
}
function configUpdateSentenceLength(newLength) {
var length = newLength;
if (length > config.MAX_SENTENCE_LENGTH) {
length = config.MAX_SENTENCE_LENGTH;
} else if (length < config.MIN_SENTENCE_LENGTH) {
length = config.MIN_SENTENCE_LENGTH;
}
config.sentenceLength = length;
$("#config-sentence-length-range")[0].value = length;
$("#config-sentence-length-input")[0].value = length;
}
function enableAllScripts() {
for (var i in allScripts) {
enableScript(i);
}
}
function disableAllScript() {
for (var i in allScripts) {
disableScript(i);
}
}
function invertEnabledScripts() {
for (var i in allScripts) {
toggleScriptEnabled(i);
}
}
function enableScriptsByCountry(country) {
for (var i in scriptsByCountry[country]) {
enableScript(scriptsByCountry[country][i]);
}
}
function enableScriptsByPreset(presetId) {
for (var i of allPresets[presetId].scripts) {
enableScript(i);
}
}
function enableScript(script) {
if (!config.enabledScripts.includes(script)) {
config.enabledScripts.push(script);
}
$("#config-script-list :input[value='" + script + "']")[0].classList.add("active");
}
function disableScript(script) {
if (config.enabledScripts.includes(script)) {
config.enabledScripts.remove(script);
}
$("#config-script-list :input[value='" + script + "']")[0].classList.remove("active");
}
function toggleScriptEnabled(script) {
if (config.enabledScripts.includes(script)) {
disableScript(script);
} else {
enableScript(script);
}
}
function getRandomEnabledScript() {
return config.enabledScripts[Math.floor(Math.random() * config.enabledScripts.length)];
}
function generateRandomPseudoSentence(amount) {
var result = "";
for (var i = 0; i <= amount - 1; i++) {
result += wordlist[Math.floor(Math.random() * wordlist.length)] + " ";
}
return result;
}
function setScriptLabel(text, obfuscateText = false, obfuscationOpacity = 100) {
scriptLabel.textContent = text;
fitText(scriptLabel);
}
function initRound(round) {
setConfigButtonLocked(true);
clearStatus();
clearGuessText();
hideScriptInfo();
setScriptLabel(game.rounds[round].sentence);
$("#compare-scripts-button")[0].hidden = true;
setControlBarVisibility(false);
setGuessBarVisibility(true);
guessText.focus();
}
function nextRound() {
if (config.enabledScripts.length <= 0) {
setStatus("danger", "Error!", "No scripts selected.");
}
newRound = {
"script": "",
"sentence": "",
"isCorrect": false,
"userInput": "",
"userDetectedScript": ""
}
newRound.script = getRandomEnabledScript();
var pseudoSentence = generateRandomPseudoSentence(config.sentenceLength);
newRound.sentence = translateSentence(allScripts[newRound.script].code, pseudoSentence);
var splitPseudoSentence = pseudoSentence.split(" ");
for (var i in splitPseudoSentence) {
newRound.sentence = newRound.sentence.replace(splitPseudoSentence[i], "");
}
game.rounds.push(newRound);
game.currentRound++;
initRound(game.currentRound);
}
function endRound() {
setGuessBarVisibility(false);
setControlBarVisibility(true);
setConfigButtonLocked(false);
showScriptInfo(game.rounds[game.currentRound].script);
$("#learn-more-button")[0].href = "learn.html#" + game.rounds[game.currentRound].script;
$("#next-round-button")[0].focus();
}
function setConfigButtonLocked(isLocked = true) {
if (isLocked) {
$("#config-container")[0].classList.remove("show");
$("#config-button")[0].classList.add("disabled");
} else {
$("#config-button")[0].classList.remove("disabled");
}
}
function setStatus(type, title, text) {
clearStatus();
statusAlert.classList.add("alert");
statusAlert.classList.add("alert-" + type);
statusAlert.innerHTML = `<strong>${title}</strong> ${text}`;
statusAlert.hidden = false;
}
function clearStatus() {
statusAlert.classList = [];
statusAlert.innerHTML = "";
statusAlert.hidden = true;
}
function clearGuessText() {
guessText.value = "";
}
function setGuessBarVisibility(isVisible) {
$("#guess-bar")[0].hidden = !isVisible;
}
function setControlBarVisibility(isVisible) {
$("#control-bar")[0].hidden = !isVisible;
}
function detectScriptByKeyword(keyword) {
for (var currentScript in allScripts) {
if (allScripts[currentScript].keywords.includes(keyword.toLowerCase().trim())) {
return currentScript;
}
}
return "";
}
function sendUserGuess(userGuess) {
game.rounds[game.currentRound].userInput = userGuess;
var correctScript = game.rounds[game.currentRound].script;
if (allScripts[correctScript].keywords.includes(userGuess.toLowerCase().trim())) {
setStatus("success", "Correct!", `The script was <strong>${allScripts[correctScript].label}</strong>.`);
game.rounds[game.currentRound].isCorrect = true;
} else {
setStatus("danger", "False!", `The script was <strong>${allScripts[correctScript].label}</strong>.`);
game.rounds[game.currentRound].userDetectedScript = detectScriptByKeyword(game.rounds[game.currentRound].userInput);
if (game.rounds[game.currentRound].userDetectedScript) {
$("#compare-scripts-button")[0].hidden = false;
}
}
endRound();
}
function updateUiConfigCountryList(searchString) {
for (var cur in Array.from(uiConfigCountryList.children)) {
if (uiConfigCountryList.children[cur].textContent.toLowerCase().includes(searchString.toLowerCase().trim())) {
uiConfigCountryList.children[cur].hidden = false;
} else {
uiConfigCountryList.children[cur].hidden = true;
}
}
}
</script>
<script>
guessText.addEventListener("keypress", function(e) {
if (e.key == "Enter") {
e.preventDefault();
sendUserGuess(guessText.value)
}
});
$("#config-script-list-search")[0].addEventListener("input", function(e) {
e.preventDefault();
updateUiConfigCountryList($("#config-script-list-search")[0].value);
});
Object.defineProperty(Array.prototype, "remove", {
value: function (value) {
const index = this.indexOf(value);
if (index === -1) {
throw new Error("Value not found in array");
}
this.splice(index, 1);
return this;
},
writable: true,
configurable: true,
enumerable: false
});
// Init script info map
var scriptInfoMap = initNewMap(function() {});
scriptInfoMap.id = "script-information-map-div";
$("#script-information-map")[0].appendChild(scriptInfoMap);
// Start round
nextRound();
</script>
</body>
</html>