adds dice to queue
All checks were successful
CI / deploy (push) Successful in 5m59s

This commit is contained in:
Arindy 2025-04-07 21:20:00 +02:00
parent 18bb994a0f
commit d140c785e1

View File

@ -9,13 +9,14 @@
<body>
<div id="dice-box"></div>
<script type="module">
import DiceBox from "/vendor/dice-box/dice-box-threejs.es.js";
function url() {
return window.location.protocol + '//' + window.location.hostname + (window.location.port?.length > 0 ? ':' + window.location.port : '');
}
import DiceBox from "/vendor/dice-box/dice-box-threejs.es.js";
const evtSource = new EventSource(url() + "/dice/{diceid??}/stream");
const diceBox = new DiceBox("#dice-box", {
assetPath: "/vendor/dice-box/",
light_intensity: 2,
@ -23,26 +24,67 @@
baseScale: {scale} * 10,
strength: Math.floor(Math.random() * 4),
});
diceBox.initialize();
let timeout = 0;
evtSource.addEventListener("message", function (event) {
clearInterval(timeout);
let data = JSON.parse(event.data);
diceBox.onRollComplete = () => {
if ({clearAfter} > 0) {
timeout = setTimeout(() => diceBox.clearDice(), {clearAfter} * 1000)
let rollQueue = [];
let rolling = false;
let clearTimeoutId = null;
let lastRollTime = Date.now();
function scheduleClear() {
if (clearTimeoutId) clearTimeout(clearTimeoutId);
clearTimeoutId = setTimeout(() => {
const now = Date.now();
if (now - lastRollTime >= ({clearAfter} * 1000)) {
diceBox.clearDice();
rolling = false;
processQueue();
} else {
scheduleClear(); // try again later
}
}, {clearAfter} * 1000);
}
async function processQueue() {
if (rolling || rollQueue.length === 0) return;
rolling = true;
const data = rollQueue.shift();
const rollString = data.roll
.filter(it => it.split('@')[0].split('d')[1] !== "2")
.join('&');
if (!rollString) {
rolling = false;
return;
}
diceBox.updateConfig({
lastRollTime = Date.now();
await diceBox.updateConfig({
theme_customColorset: {
texture: data.theme,
background: data.faceColor,
foreground: data.numberColor
}
}).then(() => {
diceBox.roll(data.roll.filter(it => it.split('@')[0].split('d')[1] !== "2").join('&'));
})
})
});
await diceBox.add(rollString);
scheduleClear();
rolling = false;
void processQueue();
}
evtSource.addEventListener("message", function (event) {
const data = JSON.parse(event.data);
rollQueue.push(data);
processQueue();
});
</script>
</body>
</html>