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> <body>
<div id="dice-box"></div> <div id="dice-box"></div>
<script type="module"> <script type="module">
import DiceBox from "/vendor/dice-box/dice-box-threejs.es.js";
function url() { function url() {
return window.location.protocol + '//' + window.location.hostname + (window.location.port?.length > 0 ? ':' + window.location.port : ''); 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 evtSource = new EventSource(url() + "/dice/{diceid??}/stream");
const diceBox = new DiceBox("#dice-box", { const diceBox = new DiceBox("#dice-box", {
assetPath: "/vendor/dice-box/", assetPath: "/vendor/dice-box/",
light_intensity: 2, light_intensity: 2,
@ -23,26 +24,67 @@
baseScale: {scale} * 10, baseScale: {scale} * 10,
strength: Math.floor(Math.random() * 4), strength: Math.floor(Math.random() * 4),
}); });
diceBox.initialize(); diceBox.initialize();
let timeout = 0;
evtSource.addEventListener("message", function (event) { let rollQueue = [];
clearInterval(timeout); let rolling = false;
let data = JSON.parse(event.data); let clearTimeoutId = null;
diceBox.onRollComplete = () => { let lastRollTime = Date.now();
if ({clearAfter} > 0) {
timeout = setTimeout(() => diceBox.clearDice(), {clearAfter} * 1000) 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: { theme_customColorset: {
texture: data.theme, texture: data.theme,
background: data.faceColor, background: data.faceColor,
foreground: data.numberColor 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> </script>
</body> </body>
</html> </html>