??/
This commit is contained in:
parent
bf6636e7c8
commit
55af57ac91
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 })
|
||||
|
|
|
|||
Loading…
Reference in New Issue