You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bingo/script.js

210 lines
6.4 KiB
JavaScript

"use strict"
2 years ago
let fields = [
2 years ago
// 'MARTINAA/JAAAAA??!!',
// '"Du bist doch auch so einer, der ..."',
// 'Brain-AFK',
// 'Rant über Kollegen',
// 'Redet in StammTV rein',
// 'Häää?',
// 'Bin mal kurz mitem Hund',
// '*schnupft*',
// 'Habt ihr des neue ... schon gesehen?',
// 'Random Nonsense-Problem',
// 'Auf Ausbildung freuen',
// 'Erzählt von Hinz und Kunz',
// 'Was heißt eigentlich ...?',
// 'Wiederholung',
// 'Story geht länger als nötig',
// 'Labert direkt los',
// 'Haljo',
// 'Klicken im Mikro',
// 'Schlafen',
// 'Akustik',
// 'Spielt LoL',
// 'Tamme Story',
// 'Ich kam heut morgen auf Arbeit...',
// 'Crypto / Fiat / Inflation ...'
'Krokodil',
'Blut zu sehen',
'„Ich kämpfe“',
'„Ich kann nicht mehr“',
'jemand trinkt unsauberes Wasser',
'jemand schneidet von einem lebenden Baum was ab',
'Knossi raucht',
'nackt',
'jemand isst eine Kokosnuss',
'stufenlos verstellbare Knoten',
'“ich frag mich wie es den anderen so geht“',
'Kleidung ist feucht/nass',
'Joris verwirft seinen Tagesplan',
'Sasha flext für die Kamera',
'„Bist du deppert?“',
'Sonnenbrand',
'7 in the wild',
'Aus Plastik Müll wird was gebaut',
'Es wird geschrien',
'es wird gesungen',
'„mir ist kalt“',
'Dinge bekommen Namen',
'„das ist auf einem anderen Level"',
'kleine Krebse',
2 years ago
2 years ago
];
let seed;
2 years ago
const urlParams = new URLSearchParams(window.location.search);
const seed_elem = document.querySelector('#seeed-value');
const centerFieldImage = document.querySelector('#center-field-image');
const bingoFields = document.querySelectorAll('#bingo td.bingo-field');
const refreshBingoButton = document.querySelector('#refresh-bingo');
const copyPermalinkButton = document.querySelector('#copy-permalink');
const winnerMessage = document.querySelector('#winner-message');
centerFieldImage.src = "img/7-vs-wild-logo.svg";
centerFieldImage.alt = "7 vs. Wild Logo";
/**
* Reads the seed from URL Parameter or generates new seed
*/
2 years ago
2 years ago
if (urlParams.has('seeed')) {
seed = urlParams.get('seeed');
} else {
seed = Date.now().toString();
}
2 years ago
const copyPermalinkToClipboard = function () {
const permalink = window.location.href;
navigator.clipboard.writeText(permalink);
}
copyPermalinkButton.addEventListener('click', copyPermalinkToClipboard);
2 years ago
const shuffleBoard = () => {
2 years ago
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]);
return fields
2 years ago
.map(value => ({value, sort: rand()}))
.sort((a, b) => a.sort - b.sort)
.map(({value}) => value)
}
2 years ago
const checkForBingo = (field) => {
let bingo = false;
let clickedRow = parseInt(field.dataset.row);
let clickedCol = parseInt(field.dataset.col);
2 years ago
//check horizontal and vertical axis
if (document.querySelectorAll(`#bingo td.drawn[data-row='${clickedRow}']`).length === 5
|| document.querySelectorAll(`#bingo td.drawn[data-col='${clickedCol}']`).length === 5) {
bingo = true
}
2 years ago
//check diagonal axis
if (clickedRow === clickedCol) {
if (
document.querySelector(`#bingo td.bingo-field.drawn[data-row="0"][data-col="0"]`) &&
document.querySelector(`#bingo td.bingo-field.drawn[data-row="1"][data-col="1"]`) &&
document.querySelector(`#bingo td.bingo-field.drawn[data-row="3"][data-col="3"]`) &&
document.querySelector(`#bingo td.bingo-field.drawn[data-row="4"][data-col="4"]`)
) {
bingo = true;
2 years ago
}
} else if (clickedRow + clickedCol === 4) {
if (
document.querySelector(`#bingo td.bingo-field.drawn[data-row="0"][data-col="4"]`) &&
document.querySelector(`#bingo td.bingo-field.drawn[data-row="1"][data-col="3"]`) &&
document.querySelector(`#bingo td.bingo-field.drawn[data-row="3"][data-col="1"]`) &&
document.querySelector(`#bingo td.bingo-field.drawn[data-row="4"][data-col="0"]`)
) {
bingo = true;
2 years ago
}
}
if (bingo) {
window.fireworks.start();
winnerMessage.classList.add('won');
}
2 years ago
};
const drawBingoFieldEventHandler = (ev) => {
let field = ev.target;
if (field) {
if (field.classList.contains('drawn')) {
field.classList.remove('drawn');
2 years ago
} else {
field.classList.add('drawn');
checkForBingo(field);
2 years ago
}
} else {
console.error('Sum ting wong (*  ̄︿ ̄)');
}
};
const drawTable = () => {
shuffleBoard().forEach((field, index) => {
bingoFields[index].textContent = field;
bingoFields[index].addEventListener('click', drawBingoFieldEventHandler);
2 years ago
});
seed_elem.innerText = seed;
window.history.pushState(null, null, '?seeed=' + seed);
2 years ago
}
// 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);
2 years ago
return [(h1 ^ h2 ^ h3 ^ h4) >>> 0, (h2 ^ h1) >>> 0, (h3 ^ h1) >>> 0, (h4 ^ h1) >>> 0];
}
function sfc32(a, b, c, d) {
2 years ago
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;
}
}
function mulberry32(a) {
2 years ago
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;
}
}
2 years ago
drawTable();
2 years ago
2 years ago
const redrawTable = function () {
winnerMessage.classList.remove('won');
bingoFields.forEach(elem => elem.classList.remove('drawn'));
2 years ago
seed = Date.now().toString();
drawTable();
2 years ago
}
refreshBingoButton.addEventListener('click', redrawTable)