From 9c0939ed935d03ec79b2dbdd4730e21f8abe1d59 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 16 Feb 2024 00:55:06 +0100 Subject: Add export of full game json via HTTP. --- server.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index 5572868..d7d5caf 100644 --- a/server.js +++ b/server.js @@ -1247,22 +1247,20 @@ const SQL_DELETE_GAME_SNAP = SQL("delete from game_snap where game_id=? and snap const SQL_DELETE_GAME_REPLAY = SQL("delete from game_replay where game_id=? and replay_id > ?") const SQL_SELECT_REPLAY = SQL(` - select json_object( - 'title', title_id, - 'scenario', scenario, - 'options', json(options), + select + json_object( 'players', (select json_group_array( json_object('role', role, 'name', name) ) from players join users using(user_id) - where game_id = :game_id + where game_id = outer.game_id ), 'state', (select json(state) from game_state - where game_id = :game_id + where game_id = outer.game_id ), 'replay', (select json_group_array( @@ -1273,13 +1271,15 @@ const SQL_SELECT_REPLAY = SQL(` end ) from game_replay - where game_id = :game_id + where game_id = outer.game_id ) - ) - from games - where game_id = :game_id + ) as export + from games as outer + where game_id = ? `).pluck() +const SQL_SELECT_EXPORT = SQL("select export from game_export_view where game_id=?").pluck() + const SQL_SELECT_GAME = SQL("SELECT * FROM games WHERE game_id=?") const SQL_SELECT_GAME_VIEW = SQL("SELECT * FROM game_view WHERE game_id=?") const SQL_SELECT_GAME_TITLE = SQL("SELECT title_id FROM games WHERE game_id=?").pluck() @@ -2078,7 +2078,17 @@ app.get("/api/replay/:game_id", function (req, res) { return res.status(404).send("Invalid game ID.") if (game.status < STATUS_FINISHED && (!req.user || req.user.user_id !== 1)) return res.status(401).send("Not authorized to debug.") - return res.send(SQL_SELECT_REPLAY.get({ game_id })) + return res.type("application/json").send(SQL_SELECT_REPLAY.get(game_id)) +}) + +app.get("/api/export/:game_id", function (req, res) { + let game_id = req.params.game_id | 0 + let game = SQL_SELECT_GAME.get(game_id) + if (!game) + return res.status(404).send("Invalid game ID.") + if (game.status < STATUS_FINISHED && (!req.user || req.user.user_id !== 1)) + return res.status(401).send("Not authorized to debug.") + return res.type("application/json").send(SQL_SELECT_EXPORT.get(game_id)) }) app.get("/admin/rewind/:game_id/:snap_id", must_be_administrator, function (req, res) { -- cgit v1.2.3