summaryrefslogtreecommitdiff
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
parent8da3c4a8fedd7a26d4a323822146eb3a91d39cd1 (diff)
downloadserver-1e489124cb895e4c7f9c5d974ae846b760d1c6d8.tar.gz
Store matching replay_id in game_snap.
-rw-r--r--schema.sql1
-rw-r--r--server.js18
-rwxr-xr-xtools/patchgame.js7
3 files changed, 13 insertions, 13 deletions
diff --git a/schema.sql b/schema.sql
index 650b379..36c2efc 100644
--- a/schema.sql
+++ b/schema.sql
@@ -435,6 +435,7 @@ create table if not exists game_replay (
create table if not exists game_snap (
game_id integer,
snap_id integer,
+ replay_id integer,
state text,
primary key (game_id, snap_id)
);
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)
diff --git a/tools/patchgame.js b/tools/patchgame.js
index b149460..3a21f67 100755
--- a/tools/patchgame.js
+++ b/tools/patchgame.js
@@ -11,7 +11,7 @@ let delete_replay = db.prepare("delete from game_replay where game_id=?")
let insert_replay = db.prepare("insert into game_replay (game_id,replay_id,role,action,arguments) values (?,?,?,?,?)")
let delete_snap = db.prepare("delete from game_snap where game_id=?")
-let insert_snap = db.prepare("insert into game_snap(game_id,snap_id,state) values (?,?,?)")
+let insert_snap = db.prepare("insert into game_snap(game_id,snap_id,replay_id,state) values (?,?,?,?)")
let update_state = db.prepare("update game_state set state=? where game_id=?")
let update_active = db.prepare("update games set active=? where game_id=?")
@@ -162,10 +162,9 @@ function patch_game(game_id, {validate_actions=true, save_snaps=true, delete_und
if (need_to_rewrite) {
delete_replay.run(game_id)
- let replay_id = 0
for (item of replay)
if (!item.remove)
- insert_replay.run(game_id, ++replay_id, item.role, item.action, item.arguments)
+ insert_replay.run(game_id, item.replay_id, item.role, item.action, item.arguments)
}
if (save_snaps) {
@@ -173,7 +172,7 @@ function patch_game(game_id, {validate_actions=true, save_snaps=true, delete_und
let snap_id = 0
for (item of replay)
if (item.save)
- insert_snap.run(game_id, ++snap_id, item.state)
+ insert_snap.run(game_id, ++snap_id, item.replay_id, item.state)
}
update_active.run(state.active, game_id)