diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index ffa9795..079879c 100644 --- a/src/lib/classes/GameJob.js +++ b/src/lib/classes/GameJob.js @@ -6,7 +6,7 @@ const logger = require("../logger.js") class GameJob extends Job { constructor() { - super() + super({ expirationInSeconds: 360 }) } StartGame(id) { @@ -45,4 +45,3 @@ class GameJob extends Job { } module.exports = GameJob -// a http://crapblox.cf/ -t 1 -j http://crapblox.cf//Game/JoinServer?Token=jRCfrKmwFfLBAvjNOQqNuOB210rjIvz4UfMhwIaY7teYGxXniECENL2vh8rvhSU2&PlaceId=10052 diff --git a/src/lib/classes/Job.js b/src/lib/classes/Job.js index f59340e..4a0ea4f 100644 --- a/src/lib/classes/Job.js +++ b/src/lib/classes/Job.js @@ -2,6 +2,7 @@ const soap = require("soap") const { randomUUID } = require("crypto") const RCCService = require("./RCCService.js") +const logger = require("../logger.js") class Job extends RCCService { constructor({ id = randomUUID(), expirationInSeconds = 10, category = 0, cores = 1 } = {}) { @@ -37,6 +38,7 @@ class Job extends RCCService { async RenewLease(expirationInSeconds) { if (!this.client) throw new Error("There is no client") + logger.info(`[${this.id}] Job renewed to ${expirationInSeconds} seconds`) return await this.client.RenewLeaseAsync({ jobID: this.id, expirationInSeconds }) } diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 1b13673..2b79f93 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -115,6 +115,11 @@ class RenderJob extends Job { } async RenderPlace(id, base64 = false) { + const response = await axios(`${process.env.BASE_URL}/API/Game/${id}?t=${process.env.ARBITER_TOKEN}`).catch((_) => reject(_)) + const { server_token, server_port, server_owner_id } = response.data + + this.id = server_token + const started = await this.Start() if (!started) throw new Error("RCCService failed to start") if (!this.client) await this.CreateClient() diff --git a/src/lua/gameserver.lua b/src/lua/gameserver.lua index 73515d0..c3e19bc 100644 --- a/src/lua/gameserver.lua +++ b/src/lua/gameserver.lua @@ -31,7 +31,7 @@ function update(LeavingPlayer) end function keepAlive(LeavingPlayer) - game:GetService("HttpService"):PostAsync(baseUrl .. "/API/KeepAlive", game:GetService("HttpService"):JSONEncode({ + game:GetService("HttpService"):PostAsync("https://dungblx.cf/API/KeepAlive", game:GetService("HttpService"):JSONEncode({ ["ServerIP"] = jobId, ["PlaceId"] = game.PlaceId, ["PlayerCount"] = #game:GetService("Players"):GetPlayers(), @@ -114,8 +114,9 @@ end ------------------------------ RENEW GAME JOB SERVICE ------------------------------- -coroutine.resume(coroutine.create((function() - while wait(30) do +spawn(function() + while true do + wait(10) if #game.Players:GetPlayers() == 0 then pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. jobId .. "/kill") end) else @@ -123,7 +124,7 @@ coroutine.resume(coroutine.create((function() keepAlive() end end -end))) +end) ------------------------------END START GAME SHARED SCRIPT-------------------------- diff --git a/src/lua/place.lua b/src/lua/place.lua index 22a6dac..65f266b 100644 --- a/src/lua/place.lua +++ b/src/lua/place.lua @@ -1,4 +1,4 @@ -local jobId, type, format, x, y, baseUrl, assetId, key = ... +local jobId, type, format, x, y, baseUrl, assetId = ... print(("[%s] Started RenderJob for type '%s' with assetId %d ..."):format(jobId, type, assetId)) @@ -11,7 +11,7 @@ game:GetService("ContentProvider"):SetBaseUrl(baseUrl) game:GetService("ScriptContext").ScriptsDisabled = true game:GetService("StarterGui").ShowDevelopmentGui = false -game:Load(("%s/server/%d/place?key=%s"):format(baseUrl, assetId, key)) +game:Load(baseUrl .. "/thumbs/staticimage?r=" .. jobId) game:GetService("ScriptContext").ScriptsDisabled = true game:GetService("StarterGui").ShowDevelopmentGui = false diff --git a/src/routes/game/execute.js b/src/routes/game/execute.js deleted file mode 100644 index 88822f0..0000000 --- a/src/routes/game/execute.js +++ /dev/null @@ -1,6 +0,0 @@ -const express = require("express") -const app = express.Router() - -app.all("*", (request, response) => response.status(404).json({ status: 404 })) - -module.exports = app diff --git a/src/routes/game/running.js b/src/routes/game/running.js new file mode 100644 index 0000000..b8532f3 --- /dev/null +++ b/src/routes/game/running.js @@ -0,0 +1,11 @@ +const express = require("express") +const app = express.Router() + +const GameJob = require("../../lib/classes/GameJob.js") + +app.get("/:token", async (request, response) => { + const game = global.games.get(request.params.token) + return response.end(!!game) +}) + +module.exports = app diff --git a/src/routes/render/game.js b/src/routes/render/game.js index 88822f0..7616772 100644 --- a/src/routes/render/game.js +++ b/src/routes/render/game.js @@ -1,6 +1,14 @@ const express = require("express") const app = express.Router() -app.all("*", (request, response) => response.status(404).json({ status: 404 })) +const RenderJob = require("../../lib/classes/RenderJob.js") + +app.get("/:id", async (request, response) => { + const job = new RenderJob() + const result = await job.RenderPlace(request.params.id, process.env.RENDER_BASE64).catch((_) => _) + + if (result?.message) return response.status(500).json({ error: result.message }) + else return response.end(result) +}) module.exports = app