??/
This commit is contained in:
parent
bf6636e7c8
commit
55af57ac91
|
|
@ -3,16 +3,16 @@ const { readFile } = require("fs/promises")
|
||||||
|
|
||||||
const Job = require("./Job.js")
|
const Job = require("./Job.js")
|
||||||
const logger = require("../logger.js")
|
const logger = require("../logger.js")
|
||||||
const randport = require("../randport.js")
|
|
||||||
|
|
||||||
class GameJob extends Job {
|
class GameJob extends Job {
|
||||||
constructor() {
|
constructor() {
|
||||||
super({ expirationInSeconds: 360 })
|
super({ expirationInSeconds: 360 })
|
||||||
}
|
}
|
||||||
|
|
||||||
StartGame(id) {
|
StartGame(id, port) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
this.placeId = id
|
this.placeId = id
|
||||||
|
this.port =
|
||||||
|
|
||||||
const started = await this.Start()
|
const started = await this.Start()
|
||||||
if (!started) throw new Error("RCCService failed to start")
|
if (!started) throw new Error("RCCService failed to start")
|
||||||
|
|
@ -20,8 +20,6 @@ class GameJob extends Job {
|
||||||
|
|
||||||
logger.info(`[${this.id}] GameJob started for ${id}`)
|
logger.info(`[${this.id}] GameJob started for ${id}`)
|
||||||
|
|
||||||
const port = await randport.udp()
|
|
||||||
|
|
||||||
this.OpenJobEx({
|
this.OpenJobEx({
|
||||||
name: this.id,
|
name: this.id,
|
||||||
script: await readFile(__dirname + "/../../lua/gameserver.lua", { encoding: "utf-8" }),
|
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: id },
|
||||||
{ type: "LUA_TNUMBER", value: port },
|
{ type: "LUA_TNUMBER", value: port },
|
||||||
{ type: "LUA_TSTRING", value: process.env.ARBITER_TOKEN },
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
}).catch((e) => reject(e))
|
}).catch((e) => reject(e))
|
||||||
|
|
||||||
resolve(port)
|
resolve()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,25 @@ 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(request.params.id)
|
const game = global.games.get(getGameById(request.params.id)?.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, request.query.port).catch((_) => _)
|
||||||
|
|
||||||
global.games.set(request.params.id, job)
|
global.games.set(job.id, job)
|
||||||
job.proc.once("exit", () => {
|
job.proc.once("exit", () => {
|
||||||
global.games.delete(request.params.id)
|
global.games.delete(job.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
return response.json({ success: true })
|
return response.json({ success: true })
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue