felt/static/dice.js

35 lines
801 B
JavaScript
Raw Permalink Normal View History

2023-02-13 04:25:14 +00:00
function rollDice() {
2023-07-19 07:43:06 +00:00
const name = $("name_entry");
const numDice = $("num_dice");
const faces = $("dice_faces");
const note = $("dice_note");
2023-02-13 04:25:14 +00:00
2023-07-19 07:43:06 +00:00
if (!conn || !tableKey.name) {
2023-02-13 04:25:14 +00:00
setErr("Looks like you haven't joined a table yet.");
return;
}
if (!name || name.value.length < 1) {
setErr("Who are you? What's your name? Super brother?");
return;
}
if (numDice && faces && note) {
const n = Number(numDice.value);
const d = Number(faces.value);
r = new Uint8Array(n);
crypto.getRandomValues(r);
const rolls = [];
for (const i of r) {
rolls.push(i%d + 1)
2023-02-13 04:25:14 +00:00
}
publish({diceRoll: {
faces: d,
roll: rolls,
player: name.value,
note: note.value,
timestamp: new Date(),
}});
note.value = "";
2023-02-13 04:25:14 +00:00
}
}