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) {
mapUrl, _ := dbAdapter.GetMapImageUrl(tableKey)
auxMessage, _ := dbAdapter.GetAuxMessage(tableKey)
tokens, _ := dbAdapter.GetTokens(tableKey, true)
tokens, _ := dbAdapter.GetTokens(tableKey, false)
diceRolls, _ := dbAdapter.GetDiceRolls(tableKey)
AddContextValue(req, "tableData", models.Table{

View file

@ -272,14 +272,16 @@ func (self *GameTableServer) writeToDB(tableMsg *models.TableMessage) error {
}
}
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 {
id, err := self.dbAdapter.CreateToken(key, t)
if err == nil {
*tableMsg.Token.Id = id
} else {
return err
}
id, err := self.dbAdapter.CreateToken(key, *t)
t.Id = &id
return err
} else {
if t.X == nil && t.Y == nil && !t.Active {
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) {
tables := self.db.Collection("tables")
id := primitive.NewObjectID().Hex()
token.Id = &id
if tables != nil {
var result models.Table
err := tables.FindOneAndUpdate(
@ -332,8 +334,7 @@ func (self *DbEngine) CreateToken(table models.TableKey, token models.Token) (st
},
).Decode(&result)
if err == nil {
newId := result.Tokens[len(result.Tokens)-1].Id
return *newId, nil
return id, err
} else {
return "", err
}

View file

@ -18,7 +18,7 @@ const tokenAspect = document.getElementById("tokenKeepAspect");
const aspectLockLabel = document.getElementById("aspectLockLabel");
const tokenZone = document.getElementById("tokenZone");
async function getTable(name, pass) {
async function rebindUi(name, pass) {
try {
const headers = new Headers();
headers.set('Authorization', 'Bearer ' + adminToken.access_token);
@ -72,7 +72,7 @@ async function getTable(name, pass) {
}
tokenListHTML += "</ul>";
fillSpriteDropdown(tokens);
redrawTokenMasterList();
} else {
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) {
let options = "<option value=''>select</option>";
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
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() {
@ -267,7 +270,7 @@ async function uploadMapImg() {
});
if (res.ok) {
// refresh so we can see the new entry in the list
getTable(tableKey.name, tableKey.passcode);
rebindUi(tableKey.name, tableKey.passcode);
} else {
throw new Error("Something went wrong uploading the map BG...");
}
@ -291,7 +294,7 @@ async function uploadTokenImg() {
});
if (res.ok) {
// refresh so we can see the new entry in the list
getTable(tableKey.name, tableKey.passcode);
rebindUi(tableKey.name, tableKey.passcode);
} else {
throw new Error("Something went wrong uploading the token sprite...");
}
@ -317,7 +320,7 @@ async function deleteImg(url) {
if (res.ok) {
// refresh UI
getTable(tableKey.name, tableKey.passcode);
rebindUi(tableKey.name, tableKey.passcode);
} else {
throw new Error ("Something went wrong deleting the image...");
}
@ -363,7 +366,7 @@ async function getTables() {
const tableList = await res.json();
let tableListHTML = "<ul>\n";
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>"
adminZone.innerHTML = tableListHTML;

View file

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

View file

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

View file

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