roll dice and publish to socket

This commit is contained in:
Iris Lightshard 2023-02-12 21:25:14 -07:00
parent 65c3071238
commit 2bf3c1af8b
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
5 changed files with 62 additions and 9 deletions

View file

@ -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:

View file

@ -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"`
}

36
static/dice.js Normal file
View file

@ -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(),
}});
}
}

View file

@ -25,7 +25,7 @@
<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>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 @@
</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>

View file

@ -27,7 +27,23 @@ function dial() {
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");
}
}