Compare commits

..

No commits in common. "adds-dice-instead-of-roll" and "main" have entirely different histories.

View File

@ -9,14 +9,13 @@
<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 : '');
} }
const evtSource = new EventSource(url() + "/dice/{diceid??}/stream"); 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", { const diceBox = new DiceBox("#dice-box", {
assetPath: "/vendor/dice-box/", assetPath: "/vendor/dice-box/",
light_intensity: 2, light_intensity: 2,
@ -24,67 +23,26 @@
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;
let rollQueue = []; evtSource.addEventListener("message", function (event) {
let rolling = false; clearInterval(timeout);
let clearTimeoutId = null; let data = JSON.parse(event.data);
let lastRollTime = Date.now(); diceBox.onRollComplete = () => {
if ({clearAfter} > 0) {
function scheduleClear() { timeout = setTimeout(() => diceBox.clearDice(), {clearAfter} * 1000)
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>