From 2bf3c1af8bc49811ebaf17fcb775b269a9ab9445 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sun, 12 Feb 2023 21:25:14 -0700 Subject: [PATCH] roll dice and publish to socket --- gametable/server.go | 2 +- models/models.go | 10 +++++----- static/dice.js | 36 ++++++++++++++++++++++++++++++++++++ static/index.html | 5 +++-- static/socket.js | 18 +++++++++++++++++- 5 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 static/dice.js diff --git a/gametable/server.go b/gametable/server.go index ed4a728..0cc7596 100644 --- a/gametable/server.go +++ b/gametable/server.go @@ -166,7 +166,7 @@ func (self *GameTableServer) publish(msg []byte) { self.publishLimiter.Wait(context.Background()) for s, k := range self.subscribers { - if k == tableMsg.Key { + if k == *tableMsg.Key { select { case s.msgs <- msg: default: diff --git a/models/models.go b/models/models.go index 942cd06..1145354 100644 --- a/models/models.go +++ b/models/models.go @@ -36,9 +36,9 @@ type Table struct { } type TableMessage struct { - Key TableKey `json:"key"` - Roll DiceRoll `json:"roll"` - Token Token `json:"token"` - MapImg string `json:"mapImg"` - AuxMsg string `json:"auxMsg"` + Key *TableKey `json:"key"` + Roll *DiceRoll `json:"roll"` + Token *Token `json:"token"` + MapImg *string `json:"mapImg"` + AuxMsg *string `json:"auxMsg"` } diff --git a/static/dice.js b/static/dice.js new file mode 100644 index 0000000..2e30f84 --- /dev/null +++ b/static/dice.js @@ -0,0 +1,36 @@ +function rollDice() { + const name = document.getElementById("name_entry"); + const numDice = document.getElementById("num_dice"); + const faces = document.getElementById("dice_faces"); + const note = document.getElementById("dice_note"); + + if (conn == null || table == null) { + 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) { + console.log("here?") + 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(r%d + 1) + } + console.log(rolls); + publish({diceRoll: { + faces: d, + roll: rolls, + player: name.value, + note: note.value, + timestamp: new Date(), + }}); + } +} \ No newline at end of file diff --git a/static/index.html b/static/index.html index 6c50547..4206766 100644 --- a/static/index.html +++ b/static/index.html @@ -25,7 +25,7 @@
- @@ -56,7 +56,7 @@ - +