all repos — felt @ 2bf3c1af8bc49811ebaf17fcb775b269a9ab9445

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

roll dice and publish to socket
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmPpu6oACgkQO3+8IhRO
Y5i0+g//UXI9tRjFxaRumESV5N0Psbj1dW3pOowBVpNVfeI1g9xXVyIeaGlxqR4a
HQtRFEgbjndp2NCAeq9Ec8uKoZHtLpk806a5MvfOIOJYkhD02FuxFoxU0nzJ4/01
/zUTq8PnMju9FINlO//21yKhiBqLlJDfF8hEg07pBPpk1L6HoFzzw5wlpMRXEozc
ROhaVlzYwD2UcRnb/PrsoPJZfmAQRgO7vjgGPaGOzMRTbaPHV+qjEJ/M9gcXA8mJ
ucHJi/rUiHWg57N1GtaRqC+hrEsCu87vLTVq4h6NRB4Q85mXh8s+f5dvOzgi87+g
GHsL2V4BCEkqov+NyKdmPcLMOpiegJo24vaYeS0PmD0Yiq0jKg4DP4qXhNJO1v7x
3wQ7O9o5SQlv5MFuCO+6I7+0HC2lL7OVWBX6rF9iAhAw0L03/FqpaqU+xMkDjPKX
gB5wI1Z08EkYCCA4dVksIqLFGMW7kugpXcC6qNK9IklZpMQHBtlsUAKWnCvLN7zD
yZL54KKAaUMEKzMwhFM/WG5qW2z4TqIdoWeeBBtYuFEnbY5801Cviv+JX3CbhsaV
+CeXIX2MJfng2DmB3obVmnfyMVnnillU/dF4TweNfZYK9hgnt6OjVaGQVgnHQOOT
OPb8mjZiSBQ54/r/6RpRdVoEHc8hcqbz5PrlObfGUDqKTmnSz5Y=
=FNPF
-----END PGP SIGNATURE-----
commit

2bf3c1af8bc49811ebaf17fcb775b269a9ab9445

parent

65c30712382e1da8757cdaabafa725b42770ed59

5 files changed, 62 insertions(+), 9 deletions(-)

jump to
M gametable/server.gogametable/server.go

@@ -166,7 +166,7 @@

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:
M models/models.gomodels/models.go

@@ -36,9 +36,9 @@ AuxMessage string `json:"auxMessage"`

} 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"` }
A 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(), + }}); + } +}
M static/index.htmlstatic/index.html

@@ -25,7 +25,7 @@ <label>pass<input type="password" id="input_admin_pass"></label>

<button type="submit" id="admin_login" onclick="doLogin()">login</button> </form> <div id="dice_log"></div> - <select name="num_dice"> + <select id="num_dice"> <option>1</option> <option>2</option> <option>3</option>

@@ -56,7 +56,7 @@ <option>10</option>

<option>12</option> <option>20</option> </select> - <input id="dice_note"><button id="dice_submit">Roll</button> + <input id="dice_note"><button id="dice_submit" onclick="rollDice()">Roll</button> <div id="map"></div> <div id="adminWrapper" style="display:none;"> <div id="adminCtrl">

@@ -73,5 +73,6 @@ </div>

</body> <script src="./util.js" type="text/javascript"></script> <script src="./socket.js" type="text/javascript"></script> + <script src="./dice.js" type="text/javascript"></script> <script src="./admin.js" type="text/javascript"></script> </html>
M static/socket.jsstatic/socket.js

@@ -27,7 +27,23 @@ // TODO: add message to let user know they are at the table

console.info("socket connected"); }); conn.addEventListener("message", e => { - console.dir(e); + console.log(e.data); + if (table == null) { + table = JSON.parse(e.data); + } else { + // UPDATE THE TABLE! + } }); + } +} + +async function publish(msg) { + msg.key = tableKey; + const res = await fetch('/publish', { + method: 'POST', + body: JSON.stringify(msg) + }); + if (!res.ok) { + setErr("Failed to publish message"); } }