all repos — felt @ main

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

static/dice.js (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function rollDice() {
  const name = $("name_entry");
  const numDice = $("num_dice");
  const faces = $("dice_faces");
  const note = $("dice_note");
  
  if (!conn || !tableKey.name) {
    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)
    }
    publish({diceRoll: {
      faces: d,
      roll: rolls,
      player: name.value,
      note: note.value,
      timestamp: new Date(),
    }});
    note.value = "";
  }
}