This commit is contained in:
I-Have-An-Issue 2023-02-21 17:35:54 -05:00
parent bf6636e7c8
commit 55af57ac91
No known key found for this signature in database
GPG Key ID: E55435DEA0825091
2 changed files with 18 additions and 12 deletions

View File

@ -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()
})
}

View File

@ -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 })