Games should be queried by ids, not 'tokens'

This commit is contained in:
I-Have-An-Issue 2023-02-20 05:15:58 -05:00
parent 6d1e6401e3
commit 24e97e5e21
No known key found for this signature in database
GPG Key ID: E55435DEA0825091
6 changed files with 13 additions and 23 deletions

View File

@ -6,8 +6,8 @@ const GameJob = require("../../lib/classes/GameJob.js")
app.use(express.json()) app.use(express.json())
app.post("/:token", async (request, response) => { app.post("/:id", async (request, response) => {
const game = global.games.get(request.params.token) const game = global.games.get(request.params.id)
if (!game) return response.status(404).json({ error: "Game is not running" }) if (!game) return response.status(404).json({ error: "Game is not running" })
const { script } = request.body const { script } = request.body

View File

@ -3,8 +3,8 @@ const app = express.Router()
const GameJob = require("../../lib/classes/GameJob.js") const GameJob = require("../../lib/classes/GameJob.js")
app.get("/:token/:expire", async (request, response) => { app.get("/:id/:expire", async (request, response) => {
const game = global.games.get(request.params.token) const game = global.games.get(request.params.id)
if (!game) return response.status(404).json({ error: "Game is not running" }) if (!game) return response.status(404).json({ error: "Game is not running" })
await game.RenewLease(request.params.expire) await game.RenewLease(request.params.expire)

View File

@ -3,8 +3,8 @@ const app = express.Router()
const GameJob = require("../../lib/classes/GameJob.js") const GameJob = require("../../lib/classes/GameJob.js")
app.get("/:token", async (request, response) => { app.get("/:id", async (request, response) => {
const game = global.games.get(request.params.token) const game = global.games.get(request.params.id)
if (!game) return response.json(false) if (!game) return response.json(false)
const running = await game.Running() const running = await game.Running()

View File

@ -3,26 +3,16 @@ const app = express.Router()
const GameJob = require("../../lib/classes/GameJob.js") 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) => { 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" }) if (game) return response.status(400).json({ error: "Game is running" })
const job = new GameJob() const job = new GameJob()
const result = await job.StartGame(request.params.id).catch((_) => _) 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", () => { job.proc.once("exit", () => {
global.games.delete(job.id) global.games.delete(request.params.id)
}) })
return response.json({ success: true }) return response.json({ success: true })

View File

@ -3,8 +3,8 @@ const app = express.Router()
const GameJob = require("../../lib/classes/GameJob.js") const GameJob = require("../../lib/classes/GameJob.js")
app.get("/:token", async (request, response) => { app.get("/:id", async (request, response) => {
const game = global.games.get(request.params.token) const game = global.games.get(request.params.id)
if (!game) return response.status(404).json({ error: "Game is not running" }) if (!game) return response.status(404).json({ error: "Game is not running" })
const status = await game.GetStatus() const status = await game.GetStatus()

View File

@ -3,8 +3,8 @@ const app = express.Router()
const GameJob = require("../../lib/classes/GameJob.js") const GameJob = require("../../lib/classes/GameJob.js")
app.get("/:token", async (request, response) => { app.get("/:id", async (request, response) => {
const game = global.games.get(request.params.token) const game = global.games.get(request.params.id)
if (!game) return response.status(404).json({ error: "Game is not running" }) if (!game) return response.status(404).json({ error: "Game is not running" })
game.Stop() game.Stop()