From 24e97e5e21dd487c721b12afc544b99a4bb2fd60 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Mon, 20 Feb 2023 05:15:58 -0500 Subject: [PATCH] Games should be queried by ids, not 'tokens' --- src/routes/game/execute.js | 4 ++-- src/routes/game/renew.js | 4 ++-- src/routes/game/running.js | 4 ++-- src/routes/game/start.js | 16 +++------------- src/routes/game/status.js | 4 ++-- src/routes/game/stop.js | 4 ++-- 6 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/routes/game/execute.js b/src/routes/game/execute.js index 09bcacc..6ea0070 100644 --- a/src/routes/game/execute.js +++ b/src/routes/game/execute.js @@ -6,8 +6,8 @@ const GameJob = require("../../lib/classes/GameJob.js") app.use(express.json()) -app.post("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.post("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) const { script } = request.body diff --git a/src/routes/game/renew.js b/src/routes/game/renew.js index ca3f580..68e6538 100644 --- a/src/routes/game/renew.js +++ b/src/routes/game/renew.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token/:expire", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id/:expire", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) await game.RenewLease(request.params.expire) diff --git a/src/routes/game/running.js b/src/routes/game/running.js index a33b8df..a1f97a0 100644 --- a/src/routes/game/running.js +++ b/src/routes/game/running.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.json(false) const running = await game.Running() diff --git a/src/routes/game/start.js b/src/routes/game/start.js index 2bcc564..0049888 100644 --- a/src/routes/game/start.js +++ b/src/routes/game/start.js @@ -3,26 +3,16 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -function getGameById(id) { - let game - - global.games.forEach((value, key) => { - if (value.placeId == id) game = value - }) - - return game -} - app.get("/:id", async (request, response) => { - const game = global.games.get(getGameById(request.params.id)?.id) + const game = global.games.get(request.params.id) if (game) return response.status(400).json({ error: "Game is running" }) const job = new GameJob() const result = await job.StartGame(request.params.id).catch((_) => _) - global.games.set(job.id, job) + global.games.set(request.params.id, job) job.proc.once("exit", () => { - global.games.delete(job.id) + global.games.delete(request.params.id) }) return response.json({ success: true }) diff --git a/src/routes/game/status.js b/src/routes/game/status.js index 915bf71..7c4aeed 100644 --- a/src/routes/game/status.js +++ b/src/routes/game/status.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) const status = await game.GetStatus() diff --git a/src/routes/game/stop.js b/src/routes/game/stop.js index b82cd49..be676b5 100644 --- a/src/routes/game/stop.js +++ b/src/routes/game/stop.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) game.Stop()