Repository navigation
Expand file tree
/
Copy pathgeneration.js
More file actions
69 lines (53 loc) · 2.06 KB
/
Copy pathgeneration.js
File metadata and controls
69 lines (53 loc) · 2.06 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
function start() {
let searchParams = new URLSearchParams(window.location.search);
let lang = searchParams.get("lang");
fetch(`bingo-${lang}.txt`)
.then(res => res.text())
.then(text => populateTables(text));
}
function populateTables(rawText) {
let texts = parseText(rawText);
shuffleArray(texts);
let searchParams = new URLSearchParams(window.location.search);
let width = parseInt(searchParams.get("width"));
let height = parseInt(searchParams.get("height"));
let table = document.getElementById("main");
for (let rowIndex = 0; rowIndex < height; rowIndex++) {
let row = table.insertRow();
for (let cellIndex = 0; cellIndex < width; cellIndex++) {
let cell = row.insertCell();
cell.onclick = () => {
if (cell.style["background-color"] !== "") {
cell.style["background-color"] = "";
}
else {
cell.style["background-color"] = "#ffe0e0";
}
};
const textIndex = rowIndex * width + cellIndex;
if (textIndex >= texts.length) {
// Maybe provide a more helpful error message?
continue;
}
cell.innerHTML += `<div class="long">${texts[textIndex].short}</div>`;
cell.innerHTML += `<div class="short">${texts[textIndex].long}</div>`;
if (texts[textIndex].code != undefined) {
eval(texts[textIndex].code);
}
}
}
}
function parseText(text) {
return text.split("\n")
.map(line => {
[short, long, code] = line.split("--").map(part => part.trim());
return { short, long, code };
});
}
// taken from https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}