pipefall/index.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-04-23 11:01:25 -07:00
(function () {
"use strict";
2023-04-23 22:48:07 -07:00
const fallKey = "falls";
2023-04-23 11:01:25 -07:00
const pipefall = document.getElementById("pipefall");
const now = document.getElementById("play-now");
2023-04-23 11:31:02 -07:00
const playAtRandom = document.getElementById("play-at-random");
const randomChance = document.getElementById("random-chance");
playAtRandom.checked = false;
2023-04-23 11:01:25 -07:00
2023-04-23 22:48:07 -07:00
function getFalls() {
let falls;
try {
falls = parseInt(localStorage.getItem(fallKey));
} catch {
falls = 0;
}
if (Number.isNaN(falls)) {
falls = 0;
}
return falls;
}
const updateFalls = () => {
const falls = getFalls();
for (const fall of document.querySelectorAll(".fall-count")) {
fall.innerText = falls;
}
};
2023-04-23 11:01:25 -07:00
const play = () => {
pipefall.play();
2023-04-23 22:48:07 -07:00
const falls = getFalls() + 1;
localStorage.setItem(fallKey, falls);
updateFalls();
2023-04-23 11:01:25 -07:00
};
now.addEventListener("click", play);
2023-04-23 11:18:26 -07:00
for (let pipe of document.querySelectorAll(".pipe")) {
pipe.addEventListener("click", play);
}
2023-04-23 11:31:02 -07:00
function random(max) {
return Math.floor(Math.random() * max);
}
let timeSincePlay = 0;
setInterval(() => {
if (playAtRandom.checked) {
timeSincePlay += 1;
if (timeSincePlay >= 60) {
2023-04-23 11:46:26 -07:00
const v = random(randomChance.value * 60);
2023-04-23 11:31:02 -07:00
if (v === 0) {
play();
timeSincePlay = 0;
}
}
} else {
timeSincePlay = 0;
}
}, 1000);
2023-04-23 22:48:07 -07:00
updateFalls();
2023-04-23 11:01:25 -07:00
})();