diff options
-rw-r--r-- | public/common/play.js | 27 | ||||
-rw-r--r-- | server.js | 27 |
2 files changed, 20 insertions, 34 deletions
diff --git a/public/common/play.js b/public/common/play.js index ce57606..054343c 100644 --- a/public/common/play.js +++ b/public/common/play.js @@ -785,7 +785,13 @@ async function init_replay() { let rules = await require("rules.js") console.log("LOADING REPLAY") - let response = await fetch((params.mode === "debug" ? "/replay-debug/" : "/replay/") + params.game_id) + let response = await fetch("/api/replay/" + params.game_id) + if (!response.ok) { + let text = await response.text() + document.getElementById("prompt").textContent = "ERROR " + response.status + ": " + text + return + } + let body = await response.json() replay = body.replay @@ -797,22 +803,23 @@ async function init_replay() { let s = {} function eval_action(item, p) { - switch (item.action) { + let [ item_role, item_action, item_arguments ] = item + switch (item_action) { case "restore": - s = JSON.parse(item.arguments) + s = JSON.parse(item_arguments) break case "setup": - s = rules.setup(item.arguments[0], item.arguments[1], item.arguments[2]) + s = rules.setup(item_arguments[0], item_arguments[1], item_arguments[2]) break case "resign": if (params.mode === "debug") - s.log.push([p, item.role.substring(0,2), item.action, null]) - s = rules.resign(s, item.role) + s.log.push([p, item_role.substring(0,2), item_action, null]) + s = rules.resign(s, item_role) break default: if (params.mode === "debug") - s.log.push([p, item.role.substring(0,2), item.action, item.arguments]) - s = rules.action(s, item.role, item.action, item.arguments) + s.log.push([p, item_role.substring(0,2), item_action, item_arguments]) + s = rules.action(s, item_role, item_action, item_arguments) break } } @@ -824,7 +831,7 @@ async function init_replay() { let ss for (p = 0; p < replay.length; ++p) { - replay[p].arguments = JSON.parse(replay[p].arguments) + replay[p][2] = JSON.parse(replay[p][2]) if (rules.is_checkpoint) { replay[p].is_checkpoint = p > 1 && rules.is_checkpoint(ss, s) @@ -834,7 +841,7 @@ async function init_replay() { try { eval_action(replay[p], p) } catch (err) { - console.log("ERROR IN REPLAY %d %s %s/%s/%s", p, s.state, replay[p].role, replay[p].action, replay[p].arguments) + console.log("ERROR IN REPLAY %d %s %s/%s/%s", p, s.state, replay[p][0], replay[p][1], replay[p][2]) console.log(err) if (params.mode === "debug") replay.length = p @@ -1070,7 +1070,7 @@ 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_SELECT_REPLAY = SQL("SELECT role,action,arguments FROM game_replay WHERE game_id=?") +const SQL_SELECT_REPLAY = SQL("SELECT role,action,arguments FROM game_replay WHERE game_id=?").raw() const SQL_SELECT_GAME = SQL("SELECT * FROM games WHERE game_id=?") const SQL_SELECT_GAME_VIEW = SQL("SELECT * FROM game_view WHERE game_id=?") @@ -1670,34 +1670,13 @@ app.get('/:title_id/replay\::game_id', function (req, res) { return res.sendFile(__dirname + '/public/' + title_id + '/play.html') }) -app.get('/:title_id/debug\::game_id', function (req, res) { - if (!req.user || req.user.user_id !== 1) - return res.status(401).send("Not authorized to debug.") - let title_id = req.params.title_id +app.get('/api/replay/:game_id', function (req, res) { let game_id = req.params.game_id let game = SQL_SELECT_GAME.get(game_id) if (!game) return res.status(404).send("Invalid game ID.") - if (game.title_id !== title_id) - return res.status(404).send("Invalid game ID.") - return res.sendFile(__dirname + '/public/' + title_id + '/play.html') -}) - -app.get('/replay/:game_id', function (req, res) { - let game_id = req.params.game_id - let game = SQL_SELECT_GAME.get(game_id) - if (game.status < 2) - return res.status(404).send("Invalid game ID.") - let players = SQL_SELECT_PLAYERS_JOIN.all(game_id) - let state = SQL_SELECT_GAME_STATE.get(game_id) - let replay = SQL_SELECT_REPLAY.all(game_id) - return res.json({players, state, replay}) -}) - -app.get('/replay-debug/:game_id', function (req, res) { - if (!req.user || req.user.user_id !== 1) + if (game.status < 2 && (!req.user || req.user.user_id !== 1)) return res.status(401).send("Not authorized to debug.") - let game_id = req.params.game_id let players = SQL_SELECT_PLAYERS_JOIN.all(game_id) let state = SQL_SELECT_GAME_STATE.get(game_id) let replay = SQL_SELECT_REPLAY.all(game_id) |