summaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2024-01-31 01:24:59 +0100
committerTor Andersson <tor@ccxvii.net>2024-02-07 20:55:56 +0100
commit1e489124cb895e4c7f9c5d974ae846b760d1c6d8 (patch)
tree299ea5a24a8a184c11e1c79649cea210f6bac007 /server.js
parent8da3c4a8fedd7a26d4a323822146eb3a91d39cd1 (diff)
downloadserver-1e489124cb895e4c7f9c5d974ae846b760d1c6d8.tar.gz
Store matching replay_id in game_snap.
Diffstat (limited to 'server.js')
-rw-r--r--server.js18
1 files changed, 9 insertions, 9 deletions
diff --git a/server.js b/server.js
index dba7d56..98444f1 100644
--- a/server.js
+++ b/server.js
@@ -1231,9 +1231,9 @@ const SQL_UPDATE_PLAYERS_ADD_TIME = SQL(`
players.game_id = ? and players.role = ?
`)
-const SQL_INSERT_REPLAY = SQL("insert into game_replay (game_id,replay_id,role,action,arguments) values (?, (select coalesce(max(replay_id), 0) + 1 from game_replay where game_id=?) ,?,?,?)")
+const SQL_INSERT_REPLAY = SQL("insert into game_replay (game_id,replay_id,role,action,arguments) values (?, (select coalesce(max(replay_id), 0) + 1 from game_replay where game_id=?) ,?,?,?) returning replay_id").pluck()
-const SQL_INSERT_SNAP = SQL("insert into game_snap (game_id,snap_id,state) values (?, (select coalesce(max(snap_id), 0) + 1 from game_snap where game_id=?), ?) returning snap_id").pluck()
+const SQL_INSERT_SNAP = SQL("insert into game_snap (game_id,snap_id,replay_id,state) values (?, (select coalesce(max(snap_id), 0) + 1 from game_snap where game_id=?), ?, ?) returning snap_id").pluck()
const SQL_SELECT_SNAP = SQL("select state from game_snap where game_id = ? and snap_id = ?").pluck()
const SQL_SELECT_SNAP_COUNT = SQL("select max(snap_id) from game_snap where game_id=?").pluck()
@@ -2019,8 +2019,8 @@ function start_game(game) {
state = RULES[game.title_id].setup(seed, game.scenario, options)
SQL_START_GAME.run(state.active, game.game_id)
- put_replay(game.game_id, null, ".setup", [ seed, game.scenario, options ])
- put_snap(game.game_id, state)
+ let replay_id = put_replay(game.game_id, null, ".setup", [ seed, game.scenario, options ])
+ put_snap(game.game_id, replay_id, state)
SQL_INSERT_GAME_STATE.run(game.game_id, JSON.stringify(state))
SQL_COMMIT.run()
@@ -2537,11 +2537,11 @@ function snap_from_state(state) {
function put_replay(game_id, role, action, args) {
if (args !== undefined && args !== null && typeof args !== "number")
args = JSON.stringify(args)
- SQL_INSERT_REPLAY.run(game_id, game_id, role, action, args)
+ return SQL_INSERT_REPLAY.run(game_id, game_id, role, action, args)
}
-function put_snap(game_id, state) {
- let snap_id = SQL_INSERT_SNAP.get(game_id, game_id, snap_from_state(state))
+function put_snap(game_id, replay_id, state) {
+ let snap_id = SQL_INSERT_SNAP.get(game_id, game_id, replay_id, snap_from_state(state))
if (game_clients[game_id])
for (let other of game_clients[game_id])
send_message(other, "snapsize", snap_id)
@@ -2569,10 +2569,10 @@ function put_game_state(game_id, state, old_active, current_role) {
function put_new_state(game_id, state, old_active, role, action, args) {
SQL_BEGIN.run()
try {
- put_replay(game_id, role, action, args)
+ let replay_id = put_replay(game_id, role, action, args)
if (state.active !== old_active)
- put_snap(game_id, state)
+ put_snap(game_id, replay_id, state)
put_game_state(game_id, state, old_active, role)