save dice rolls to db; mongo int arrays are weird

This commit is contained in:
Iris Lightshard 2023-02-17 23:35:29 -07:00
parent 2bf3c1af8b
commit 8804908923
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
4 changed files with 19 additions and 9 deletions

View file

@ -205,6 +205,13 @@ func (self *GameTableServer) getCurrentState(tableKey models.TableKey) []byte {
}
func (self *GameTableServer) writeToDB(tableMsg models.TableMessage) error {
key := *tableMsg.Key
if tableMsg.DiceRoll != nil {
err := self.dbAdapter.InsertDiceRoll(key, *tableMsg.DiceRoll)
if err != nil {
fmt.Println(err.Error())
}
}
return nil
}

View file

@ -37,7 +37,7 @@ type Table struct {
type TableMessage struct {
Key *TableKey `json:"key"`
Roll *DiceRoll `json:"roll"`
DiceRoll *DiceRoll `json:"diceRoll"`
Token *Token `json:"token"`
MapImg *string `json:"mapImg"`
AuxMsg *string `json:"auxMsg"`

View file

@ -15,16 +15,14 @@ function rollDice() {
}
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)
rolls.push(i%d + 1)
}
console.log(rolls);
publish({diceRoll: {
faces: d,
roll: rolls,

View file

@ -27,12 +27,17 @@ function dial() {
console.info("socket connected");
});
conn.addEventListener("message", e => {
console.log(e.data);
const data = JSON.parse(e.data);
if (table == null) {
// first fetch comes from mongo, so the rolls array in each diceRoll is a byte array and needs to be decoded
data.diceRolls.forEach(r=>{
r.roll = Uint8Array.from(atob(r.roll), c => c.charCodeAt(0))
})
table = JSON.parse(e.data);
} else {
// UPDATE THE TABLE!
}
console.log(data);
});
}
}