From 55af57ac9172dbfcfd5fc86fba408f05b1a1b396 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Tue, 21 Feb 2023 17:35:54 -0500 Subject: [PATCH] ??/ --- src/lib/classes/GameJob.js | 11 ++++------- src/routes/game/start.js | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index ba22159..2727d94 100644 --- a/src/lib/classes/GameJob.js +++ b/src/lib/classes/GameJob.js @@ -3,25 +3,23 @@ const { readFile } = require("fs/promises") const Job = require("./Job.js") const logger = require("../logger.js") -const randport = require("../randport.js") class GameJob extends Job { constructor() { super({ expirationInSeconds: 360 }) } - StartGame(id) { + StartGame(id, port) { return new Promise(async (resolve, reject) => { this.placeId = id - + this.port = + const started = await this.Start() if (!started) throw new Error("RCCService failed to start") if (!this.client) await this.CreateClient() logger.info(`[${this.id}] GameJob started for ${id}`) - const port = await randport.udp() - this.OpenJobEx({ name: this.id, script: await readFile(__dirname + "/../../lua/gameserver.lua", { encoding: "utf-8" }), @@ -34,12 +32,11 @@ class GameJob extends Job { { type: "LUA_TNUMBER", value: id }, { type: "LUA_TNUMBER", value: port }, - { type: "LUA_TSTRING", value: process.env.ARBITER_TOKEN }, ], }, }).catch((e) => reject(e)) - resolve(port) + resolve() }) } diff --git a/src/routes/game/start.js b/src/routes/game/start.js index 0049888..24deea3 100644 --- a/src/routes/game/start.js +++ b/src/routes/game/start.js @@ -3,16 +3,25 @@ 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(request.params.id) + const game = global.games.get(getGameById(request.params.id)?.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((_) => _) + const result = await job.StartGame(request.params.id, request.query.port).catch((_) => _) - global.games.set(request.params.id, job) + global.games.set(job.id, job) job.proc.once("exit", () => { - global.games.delete(request.params.id) + global.games.delete(job.id) }) return response.json({ success: true })