bingo/script.js

220 lines
7.4 KiB
JavaScript
Raw Normal View History

"use strict"
2022-07-05 20:17:38 +02:00
let fields = [
2023-12-29 01:09:09 +01:00
"Datenelch",
"6 Stunden Schlaf",
"Tschunk getrunken",
"Spaß gehabt",
"Sticker getauscht",
"DECT genutzt",
"Hardware gehackt",
"Kabel vergessen",
"Halle gesucht",
"$dinge gelötet",
"an SoS teilgenommen",
"Neue Leute kennengelernt",
"Wasser getrunken",
"Waffel gegessen",
"Corona Test gemacht",
"2 Mahlzeiten gegessen",
"Fairydust bewundert",
"Talk angeschaut",
"CCC Merch getragen",
2023-12-29 01:10:19 +01:00
"Engelschicht gemacht",
2023-12-29 01:09:09 +01:00
"In der Lounge getanzt",
"Etwas Neues ausprobiert",
"Maske getragen",
"geduscht"
2022-07-05 20:17:38 +02:00
];
let seed;
2022-12-02 00:40:51 +01:00
const urlParams = new URLSearchParams(window.location.search);
const seed_elem = document.querySelector('#seeed-value');
2022-12-04 17:32:31 +01:00
const allFields = document.querySelectorAll('#bingo td');
const bingoFields = document.querySelectorAll('#bingo td.bingo-field');
2022-12-04 17:32:31 +01:00
const centerFieldImage = document.querySelector('#center-field-image');
const refreshBingoButton = document.querySelector('#refresh-bingo');
const copyPermalinkButton = document.querySelector('#copy-permalink');
2022-12-03 15:08:13 +01:00
const winnerMessage = document.querySelector('#winner-message');
2023-12-29 01:09:09 +01:00
centerFieldImage.src = "img/logo.svg"
centerFieldImage.alt = "37C3 Logo";
2022-12-04 17:32:31 +01:00
centerFieldImage.parentElement.addEventListener('contextmenu', ev => {
ev.preventDefault();
centerFieldImage.parentElement.classList.remove('bingo');
});
/**
* Reads the seed from URL Parameter or generates new seed
*/
2022-11-30 18:52:25 +01:00
2022-12-02 00:40:51 +01:00
if (urlParams.has('seeed')) {
seed = urlParams.get('seeed');
} else {
seed = Date.now().toString();
}
2022-11-30 19:30:06 +01:00
2022-12-02 00:40:51 +01:00
const copyPermalinkToClipboard = function () {
const permalink = window.location.href;
navigator.clipboard.writeText(permalink);
}
copyPermalinkButton.addEventListener('click', copyPermalinkToClipboard);
2022-07-05 20:17:38 +02:00
const shuffleBoard = () => {
2022-12-02 00:40:51 +01:00
let prng_hash_seed = cyrb128(seed);
let rand = sfc32(prng_hash_seed[0], prng_hash_seed[1], prng_hash_seed[2], prng_hash_seed[3]);
2022-11-30 19:30:06 +01:00
return fields
2022-12-02 00:40:51 +01:00
.map(value => ({value, sort: rand()}))
.sort((a, b) => a.sort - b.sort)
.map(({value}) => value)
2022-12-04 14:43:21 +01:00
.slice(0, 24)
}
2022-07-05 20:42:38 +02:00
const checkForBingo = (field) => {
let bingo = false;
let clickedRow = parseInt(field.dataset.row);
let clickedCol = parseInt(field.dataset.col);
2022-12-02 00:40:51 +01:00
//check horizontal and vertical axis
2022-12-04 17:32:31 +01:00
if (document.querySelectorAll(`#bingo td.drawn[data-row='${clickedRow}']`).length === 5) {
document.querySelectorAll(`#bingo td.drawn[data-row='${clickedRow}']`).forEach(elem => elem.classList.add('bingo'));
bingo = true;
} else {
}
if (document.querySelectorAll(`#bingo td.drawn[data-col='${clickedCol}']`).length === 5) {
document.querySelectorAll(`#bingo td.drawn[data-col='${clickedCol}']`).forEach(elem => elem.classList.add('bingo'));
bingo = true;
} else {
}
2022-12-02 00:40:51 +01:00
//check diagonal axis
if (clickedRow === clickedCol) {
if (
2022-12-04 17:32:31 +01:00
document.querySelector(`#bingo td.drawn[data-row="0"][data-col="0"]`) &&
document.querySelector(`#bingo td.drawn[data-row="1"][data-col="1"]`) &&
document.querySelector(`#bingo td.drawn[data-row="3"][data-col="3"]`) &&
document.querySelector(`#bingo td.drawn[data-row="4"][data-col="4"]`)
) {
2022-12-04 17:32:31 +01:00
document.querySelector(`#bingo td.drawn[data-row="0"][data-col="0"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="1"][data-col="1"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="2"][data-col="2"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="3"][data-col="3"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="4"][data-col="4"]`).classList.add('bingo');
bingo = true;
2022-12-02 00:40:51 +01:00
}
} else if (clickedRow + clickedCol === 4) {
if (
2022-12-04 17:32:31 +01:00
document.querySelector(`#bingo td.drawn[data-row="0"][data-col="4"]`) &&
document.querySelector(`#bingo td.drawn[data-row="1"][data-col="3"]`) &&
document.querySelector(`#bingo td.drawn[data-row="3"][data-col="1"]`) &&
document.querySelector(`#bingo td.drawn[data-row="4"][data-col="0"]`)
) {
2022-12-04 17:32:31 +01:00
document.querySelector(`#bingo td.drawn[data-row="0"][data-col="4"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="1"][data-col="3"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="2"][data-col="2"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="3"][data-col="1"]`).classList.add('bingo');
document.querySelector(`#bingo td.drawn[data-row="4"][data-col="0"]`).classList.add('bingo');
bingo = true;
2022-11-30 18:52:25 +01:00
}
}
2022-12-04 17:32:31 +01:00
if (bingo) {
2022-12-04 17:32:31 +01:00
winnerMessage.classList.remove('won');
window.fireworks.start();
2022-12-03 15:08:13 +01:00
winnerMessage.classList.add('won');
}
2022-12-02 00:40:51 +01:00
};
const drawBingoFieldEventHandler = (ev) => {
2022-12-04 17:32:31 +01:00
let field = ev.target;
if (field && !field.classList.contains('drawn')) {
field.classList.add('drawn');
checkForBingo(field);
} else {
console.error('Sum ting wong (*  ̄︿ ̄)');
}
};
const undrawBingoFieldEventHandler = (ev) => {
ev.preventDefault();
let field = ev.target;
if (field) {
2022-12-04 17:32:31 +01:00
field.classList.remove('drawn');
field.classList.remove('bingo');
} else {
console.error('Sum ting wong (*  ̄︿ ̄)');
}
};
const drawTable = () => {
shuffleBoard().forEach((field, index) => {
bingoFields[index].textContent = field;
bingoFields[index].addEventListener('click', drawBingoFieldEventHandler);
2022-12-04 17:32:31 +01:00
bingoFields[index].addEventListener('contextmenu', undrawBingoFieldEventHandler);
2022-12-02 00:40:51 +01:00
});
seed_elem.innerText = seed;
window.history.pushState(null, null, '?seeed=' + seed);
2022-07-05 20:17:38 +02:00
}
2022-11-30 19:30:06 +01:00
// hash function, thx stackoverflow :3
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
2022-12-02 00:40:51 +01:00
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
2022-11-30 19:30:06 +01:00
}
function sfc32(a, b, c, d) {
2022-12-02 00:40:51 +01:00
return function () {
a >>>= 0;
b >>>= 0;
c >>>= 0;
d >>>= 0;
var t = (a + b) | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = (c << 21 | c >>> 11);
d = d + 1 | 0;
t = t + d | 0;
c = c + t | 0;
return (t >>> 0) / 4294967296;
2022-11-30 19:30:06 +01:00
}
}
function mulberry32(a) {
2022-12-02 00:40:51 +01:00
return function () {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
2022-11-30 19:30:06 +01:00
}
}
2022-12-02 00:40:51 +01:00
drawTable();
2022-07-05 20:17:38 +02:00
2022-12-02 00:40:51 +01:00
const redrawTable = function () {
2022-12-03 15:08:13 +01:00
winnerMessage.classList.remove('won');
2022-12-04 17:32:31 +01:00
allFields.forEach(elem => {
elem.classList.remove('bingo');
});
bingoFields.forEach(elem => {
elem.classList.remove('drawn');
});
2022-12-02 00:40:51 +01:00
seed = Date.now().toString();
drawTable();
2022-12-02 00:40:51 +01:00
}
refreshBingoButton.addEventListener('click', redrawTable)