diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-05-21 11:53:02 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-05-23 01:48:09 +0200 |
commit | bd6644214c757a08c1c533048352a902fdefe4ae (patch) | |
tree | 6f511e147ab648dee286afd2d79c79b9ba18267b /server.js | |
parent | 649fcc09f6addf06dda10862a0f8ff5da19d536e (diff) | |
download | server-bd6644214c757a08c1c533048352a902fdefe4ae.tar.gz |
Improve replay id handling.
Use a without rowid table and create the replay_id dynamically.
This saves a lot of database space and performance as it removes
the need for a separate index.
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -1096,7 +1096,8 @@ const SQL_UPDATE_GAME_STATE = SQL("INSERT OR REPLACE INTO game_state (game_id,st const SQL_UPDATE_GAME_RESULT = SQL("UPDATE games SET status=?, result=? WHERE game_id=?") const SQL_UPDATE_GAME_PRIVATE = SQL("UPDATE games SET is_private=1 WHERE game_id=?") -const SQL_INSERT_REPLAY = SQL("INSERT INTO game_replay (game_id,role,action,arguments) VALUES (?,?,?,?)") +const SQL_INSERT_REPLAY = SQL("insert into game_replay (game_id,replay_id,role,action,arguments) values (?, (select count(1) + 1 from game_replay where game_id=?), ?,?,?)") +const SQL_DELETE_REPLAY = SQL("delete from game_replay where game_id=?") const SQL_SELECT_REPLAY = SQL(` select json_object( @@ -2059,7 +2060,7 @@ function put_game_state(game_id, state, old_active) { function put_replay(game_id, role, action, args) { if (args !== undefined && args !== null) args = JSON.stringify(args) - SQL_INSERT_REPLAY.run(game_id, role, action, args) + SQL_INSERT_REPLAY.run(game_id, game_id, role, action, args) } function on_action(socket, action, arg) { @@ -2210,8 +2211,10 @@ function on_restore(socket, state_text) { SQL_UPDATE_GAME_RESULT.run(1, null, socket.game_id) SQL_UPDATE_GAME_STATE.run(socket.game_id, state_text, state.active) put_replay(socket.game_id, null, 'debug-restore', state_text) - for (let other of game_clients[socket.game_id]) + for (let other of game_clients[socket.game_id]) { + other.seen = 0 send_state(other, state) + } } catch (err) { console.log(err) return send_message(socket, 'error', err.toString()) |