can write tokens to the db and get them back

This commit is contained in:
Iris Lightshard 2023-07-08 01:42:31 -06:00
parent 01fccb55f0
commit 4ba6c9315f
7 changed files with 81 additions and 66 deletions

View file

@ -48,7 +48,7 @@ func apiGetTableData(next http.Handler, udb auth.UserStore, dbAdapter mongodb.Db
if dbAdapter.CheckTable(tableKey) { if dbAdapter.CheckTable(tableKey) {
mapUrl, _ := dbAdapter.GetMapImageUrl(tableKey) mapUrl, _ := dbAdapter.GetMapImageUrl(tableKey)
auxMessage, _ := dbAdapter.GetAuxMessage(tableKey) auxMessage, _ := dbAdapter.GetAuxMessage(tableKey)
tokens, _ := dbAdapter.GetTokens(tableKey, true) tokens, _ := dbAdapter.GetTokens(tableKey, false)
diceRolls, _ := dbAdapter.GetDiceRolls(tableKey) diceRolls, _ := dbAdapter.GetDiceRolls(tableKey)
AddContextValue(req, "tableData", models.Table{ AddContextValue(req, "tableData", models.Table{

View file

@ -272,14 +272,16 @@ func (self *GameTableServer) writeToDB(tableMsg *models.TableMessage) error {
} }
} }
if tableMsg.Token != nil { if tableMsg.Token != nil {
t := *tableMsg.Token t := tableMsg.Token
strId := ""
if t.Id != nil {
strId = *t.Id
}
fmt.Println(strId +"::" + t.Name + "::" + t.Sprite)
if t.Id == nil { if t.Id == nil {
id, err := self.dbAdapter.CreateToken(key, t) id, err := self.dbAdapter.CreateToken(key, *t)
if err == nil { t.Id = &id
*tableMsg.Token.Id = id return err
} else {
return err
}
} else { } else {
if t.X == nil && t.Y == nil && !t.Active { if t.X == nil && t.Y == nil && !t.Active {
err := self.dbAdapter.DestroyToken(key, *t.Id) err := self.dbAdapter.DestroyToken(key, *t.Id)

View file

@ -319,6 +319,8 @@ func (self *DbEngine) CheckToken(table models.TableKey, tokenId string) (bool, b
func (self *DbEngine) CreateToken(table models.TableKey, token models.Token) (string, error) { func (self *DbEngine) CreateToken(table models.TableKey, token models.Token) (string, error) {
tables := self.db.Collection("tables") tables := self.db.Collection("tables")
id := primitive.NewObjectID().Hex()
token.Id = &id
if tables != nil { if tables != nil {
var result models.Table var result models.Table
err := tables.FindOneAndUpdate( err := tables.FindOneAndUpdate(
@ -332,8 +334,7 @@ func (self *DbEngine) CreateToken(table models.TableKey, token models.Token) (st
}, },
).Decode(&result) ).Decode(&result)
if err == nil { if err == nil {
newId := result.Tokens[len(result.Tokens)-1].Id return id, err
return *newId, nil
} else { } else {
return "", err return "", err
} }

View file

@ -18,7 +18,7 @@ const tokenAspect = document.getElementById("tokenKeepAspect");
const aspectLockLabel = document.getElementById("aspectLockLabel"); const aspectLockLabel = document.getElementById("aspectLockLabel");
const tokenZone = document.getElementById("tokenZone"); const tokenZone = document.getElementById("tokenZone");
async function getTable(name, pass) { async function rebindUi(name, pass) {
try { try {
const headers = new Headers(); const headers = new Headers();
headers.set('Authorization', 'Bearer ' + adminToken.access_token); headers.set('Authorization', 'Bearer ' + adminToken.access_token);
@ -72,7 +72,7 @@ async function getTable(name, pass) {
} }
tokenListHTML += "</ul>"; tokenListHTML += "</ul>";
fillSpriteDropdown(tokens); fillSpriteDropdown(tokens);
redrawTokenMasterList();
} else { } else {
tokenListHTML += "<label>Sprites couldn't be retrieved</label>" tokenListHTML += "<label>Sprites couldn't be retrieved</label>"
} }
@ -88,15 +88,6 @@ async function getTable(name, pass) {
} }
} }
function redrawTokenMasterList() {
if (tokenZone) {
const headers = new Headers();
headers.set('Authorization', 'Bearer ' + adminToken.access_token);
const res = await fetch(`/`
}
}
function fillSpriteDropdown(tokens) { function fillSpriteDropdown(tokens) {
let options = "<option value=''>select</option>"; let options = "<option value=''>select</option>";
for (const t of tokens) { for (const t of tokens) {
@ -225,9 +216,21 @@ function createToken() {
*/ */
// really though we have to send it on the websocket and wait for it to come back // really though we have to send it on the websocket and wait for it to come back
const [x, y] = getCascadingPos();
sendToken({
w: w,
h: h,
ox:oX,
oy:oY,
x: x,
y: y,
sprite: img,
name: name,
active: false}
);
return;
} }
setErr("All token fields are required");
} }
function publishAuxMsg() { function publishAuxMsg() {
@ -267,7 +270,7 @@ async function uploadMapImg() {
}); });
if (res.ok) { if (res.ok) {
// refresh so we can see the new entry in the list // refresh so we can see the new entry in the list
getTable(tableKey.name, tableKey.passcode); rebindUi(tableKey.name, tableKey.passcode);
} else { } else {
throw new Error("Something went wrong uploading the map BG..."); throw new Error("Something went wrong uploading the map BG...");
} }
@ -291,7 +294,7 @@ async function uploadTokenImg() {
}); });
if (res.ok) { if (res.ok) {
// refresh so we can see the new entry in the list // refresh so we can see the new entry in the list
getTable(tableKey.name, tableKey.passcode); rebindUi(tableKey.name, tableKey.passcode);
} else { } else {
throw new Error("Something went wrong uploading the token sprite..."); throw new Error("Something went wrong uploading the token sprite...");
} }
@ -317,7 +320,7 @@ async function deleteImg(url) {
if (res.ok) { if (res.ok) {
// refresh UI // refresh UI
getTable(tableKey.name, tableKey.passcode); rebindUi(tableKey.name, tableKey.passcode);
} else { } else {
throw new Error ("Something went wrong deleting the image..."); throw new Error ("Something went wrong deleting the image...");
} }
@ -363,7 +366,7 @@ async function getTables() {
const tableList = await res.json(); const tableList = await res.json();
let tableListHTML = "<ul>\n"; let tableListHTML = "<ul>\n";
for (const t of tableList) { for (const t of tableList) {
tableListHTML += `<li><a href="#" onclick="getTable('${t.name}','${t.passcode}');return false;">${t.name}</a></li>\n` tableListHTML += `<li><a href="#" onclick="rebindUi('${t.name}','${t.passcode}');return false;">${t.name}</a></li>\n`
} }
tableListHTML += "</ul>" tableListHTML += "</ul>"
adminZone.innerHTML = tableListHTML; adminZone.innerHTML = tableListHTML;

View file

@ -9,7 +9,9 @@
</head> </head>
<body> <body>
<div id="map"></div> <div id="map"></div>
<div id="errWrapper" style='display:none'><button id="closeErr" onclick="closeErr()">x</button><div id="errDiv"></div></div> <div id="errWrapper" style='display:none'><button id="closeErr" onclick="closeErr()">x</button><div id="errDiv"></div></div>
<nav> <nav>
<section id="user_section"> <section id="user_section">
<details class="ui_win"><summary>identity</summary> <details class="ui_win"><summary>identity</summary>
@ -27,44 +29,49 @@
<div id="tabletop" style="display:none;"> <div id="tabletop" style="display:none;">
<details id="dice_win" class="ui_win"><summary>dice</summary> <details id="dice_win" class="ui_win"><summary>dice</summary>
<select id="num_dice"> <select id="num_dice">
<option>1</option> <option>1</option>
<option>2</option> <option>2</option>
<option>3</option> <option>3</option>
<option>4</option> <option>4</option>
<option>5</option> <option>5</option>
<option>6</option> <option>6</option>
<option>7</option> <option>7</option>
<option>8</option> <option>8</option>
<option>9</option> <option>9</option>
<option>10</option> <option>10</option>
<option>11</option> <option>11</option>
<option>12</option> <option>12</option>
<option>13</option> <option>13</option>
<option>14</option> <option>14</option>
<option>15</option> <option>15</option>
<option>16</option> <option>16</option>
<option>17</option> <option>17</option>
<option>18</option> <option>18</option>
<option>19</option> <option>19</option>
<option>20</option> <option>20</option>
</select> </select>
<label for="dice_faces">d</label> <label for="dice_faces">d</label>
<select id="dice_faces"> <select id="dice_faces">
<option>4</option> <option>4</option>
<option selected>6</option> <option selected>6</option>
<option>8</option> <option>8</option>
<option>10</option> <option>10</option>
<option>12</option> <option>12</option>
<option>20</option> <option>20</option>
</select> </select>
<input id="dice_note"><button id="dice_submit" onclick="rollDice()">Roll</button> <input id="dice_note"><button id="dice_submit" onclick="rollDice()">Roll</button>
<div id="dice_log"></div> <div id="dice_log"></div>
</details><br/> </details><br/>
<details class="ui_win"><summary>status</summary><pre id="aux"></pre></details><br/>
</div>
</section>
<details class="ui_win"><summary>status</summary><pre id="aux"></pre></details><br/>
<details class="ui_win"><summary>token select</summary>
<div id="token_select"></div>
</details><br/>
</div>
</section>
<section id="admin_section"> <section id="admin_section">
<details class="ui_win admin_win"><summary>admin</summary> <details class="ui_win admin_win"><summary>admin</summary>
@ -74,6 +81,7 @@
</form> </form>
</details> </details>
<br/> <br/>
<div id="adminWrapper" style="display:none;"> <div id="adminWrapper" style="display:none;">
<details id="admin_table_win" class="ui_win admin_win"><summary>table</summary> <details id="admin_table_win" class="ui_win admin_win"><summary>table</summary>
<button onclick="setTableCreateFormVisible(true)">New Table</button> <button onclick="setTableCreateFormVisible(true)">New Table</button>

View file

@ -36,10 +36,10 @@ function resizeMarkers() {
} }
function getCascadingPos() { function getCascadingPos() {
const topLeft = worldBounds[0]; const topLeft = [0,0];
const n = tokens.length; const n = tokens.length;
topLeft[1] += (n+1)*5; topLeft[1] += (n)*Math.random()*10 - 5;
topLeft[0] -= (n+1)*5; topLeft[0] -= (n)*Math.random()*10 - 5;
return topLeft; return topLeft;
} }

View file

@ -112,6 +112,7 @@ pre {
#auxMsgZone { #auxMsgZone {
width: 100%; width: 100%;
padding:0.2em; padding:0.2em;
height: 8em;
} }
#adminWrapper { #adminWrapper {