From f75619c39e50c3dafa00664c33940d178d4e3b35 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Sat, 18 Feb 2023 02:55:26 -0500 Subject: [PATCH 01/11] Fix bodyshot.lua charapp appearance --- src/lua/bodyshot.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua/bodyshot.lua b/src/lua/bodyshot.lua index 1af7359..cfdf5f1 100644 --- a/src/lua/bodyshot.lua +++ b/src/lua/bodyshot.lua @@ -8,7 +8,7 @@ game:GetService("ContentProvider"):SetBaseUrl(baseUrl) game:GetService("ScriptContext").ScriptsDisabled = true local Player = game.Players:CreateLocalPlayer(0) -Player.CharacterAppearance = ("%s/users/%d/character"):format(baseUrl, assetId) +Player.CharacterAppearance = ("%s/Character?id=%d"):format(baseUrl, assetId) Player:LoadCharacter(false) game:GetService("RunService"):Run() From bb974f8f9c01f3964737c46bf203969f9d4a199f Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Sat, 18 Feb 2023 03:04:16 -0500 Subject: [PATCH 02/11] Start work on gameservers --- src/lua/gameserver.lua | 84 ++++++++---------------------------------- 1 file changed, 15 insertions(+), 69 deletions(-) diff --git a/src/lua/gameserver.lua b/src/lua/gameserver.lua index 7d6b00f..3ef8549 100644 --- a/src/lua/gameserver.lua +++ b/src/lua/gameserver.lua @@ -1,52 +1,25 @@ -local jobId, type, baseUrl, placeId, port, owner = ... +local jobId, baseUrl, placeId, port = ... ------------------- UTILITY FUNCTIONS -------------------------- - function waitForChild(parent, childName) while true do local child = parent:findFirstChild(childName) - if child then - return child - end + if child then return child end parent.ChildAdded:wait() end end -function update(LeavingPlayer) - local names = {} - for _, player in pairs(game:GetService("Players"):GetPlayers()) do - if (player ~= LeavingPlayer) then - table.insert(names, player.Name) - end - end - - local str = (#names > 0) and (#names > 1) and (names[1] .. ",") or names[1] or "" - - for i = 2, #names -1, 1 do - str = str .. names[i] .. "," - end - - str = (#names > 0) and (#names > 1) and (str .. names[#names]) or names[1] or "" - return str -end - -function keepAlive(LeavingPlayer) - game:GetService("HttpService"):PostAsync("https://dungblx.cf/API/KeepAlive", game:GetService("HttpService"):JSONEncode({ - ["ServerIP"] = jobId, - ["PlaceId"] = game.PlaceId, - ["PlayerCount"] = #game:GetService("Players"):GetPlayers(), - ["PlayerList"] = update(LeavingPlayer), - })) -end - -----------------------------------END UTILITY FUNCTIONS ------------------------- -----------------------------------"CUSTOM" SHARED CODE---------------------------------- pcall(function() settings().Network.UseInstancePacketCache = true end) pcall(function() settings().Network.UsePhysicsPacketCache = true end) +-- pcall(function() settings()["Task Scheduler"].PriorityMethod = Enum.PriorityMethod.FIFO end) pcall(function() settings()["Task Scheduler"].PriorityMethod = Enum.PriorityMethod.AccumulatedError end) +-- settings().Network.PhysicsSend = 1 -- 1==RoundRobin +-- settings().Network.PhysicsSend = Enum.PhysicsSendMethod.ErrorComputation2 settings().Network.PhysicsSend = Enum.PhysicsSendMethod.TopNErrors settings().Network.ExperimentalPhysicsEnabled = true settings().Network.WaitingForCharacterLogRate = 100 @@ -54,20 +27,16 @@ pcall(function() settings().Diagnostics:LegacyScriptMode() end) -----------------------------------START GAME SHARED SCRIPT------------------------------ -local assetId = placeId - local scriptContext = game:GetService("ScriptContext") pcall(function() scriptContext:AddStarterScript(37801172) end) scriptContext.ScriptsDisabled = true -game:SetPlaceID(assetId, false) +game:SetPlaceID(placeId, false) game:GetService("ChangeHistoryService"):SetEnabled(false) -game:SetCreatorId(owner, Enum.CreatorType.User) -game:GetService("HttpService").HttpEnabled = true local ns = game:GetService("NetworkServer") -if baseUrl~=nil then +if baseUrl ~= nil then pcall(function() game:GetService("Players"):SetAbuseReportUrl(baseUrl .. "/AbuseReport/InGameChatHandler.ashx") end) pcall(function() game:GetService("ScriptInformationProvider"):SetAssetUrl(baseUrl .. "/Asset/") end) pcall(function() game:GetService("ContentProvider"):SetBaseUrl(baseUrl .. "/") end) @@ -84,53 +53,30 @@ if baseUrl~=nil then pcall(function() loadfile(baseUrl .. "/Game/LoadPlaceInfo.ashx?PlaceId=" .. placeId)() end) end +-- pcall(function() game:GetService("NetworkServer"):SetIsPlayerAuthenticationRequired(true) end) settings().Diagnostics.LuaRamLimit = 0 +-- settings().Network:SetThroughputSensitivity(0.08, 0.01) +-- settings().Network.SendRate = 35 +-- settings().Network.PhysicsSend = 0 -- 1==RoundRobin game:GetService("Players").PlayerAdded:connect(function(player) - keepAlive() print("Player " .. player.userId .. " added") - - player.CharacterAdded:connect(function(c) - game:GetObjects("rbxasset://fonts/characterCameraScript.rbxmx")[1].Parent = c - game:GetObjects("rbxasset://fonts/characterControlScript.rbxmx")[1].Parent = c - - for i,v in pairs(c:GetChildren()) do - print(v.Name) - end - - print(c.Animate.Source) - end) end) game:GetService("Players").PlayerRemoving:connect(function(player) - keepAlive(player) print("Player " .. player.userId .. " leaving") end) -if placeId~=nil and baseUrl~=nil then +if placeId ~= nil and baseUrl ~= nil then wait() - game:Load(baseUrl .. "/thumbs/staticimage?r=" .. jobId) + game:Load(baseUrl .. "/asset/?id=" .. placeId) end ------------------------------- RENEW GAME JOB SERVICE ------------------------------- - -spawn(function() - while true do - wait(30) - if #game.Players:GetPlayers() == 0 then - pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. jobId .. "/kill") end) - else - pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. jobId .. "/renew?s=360") end) - keepAlive() - end - end -end) - -------------------------------END START GAME SHARED SCRIPT-------------------------- - ns:Start(port) scriptContext:SetTimeout(10) scriptContext.ScriptsDisabled = false +------------------------------END START GAME SHARED SCRIPT-------------------------- + game:GetService("RunService"):Run() From 715d29b286019e49db86df999c3b214b4cc002c7 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Sat, 18 Feb 2023 03:19:56 -0500 Subject: [PATCH 03/11] Gameservers --- src/lua/gameserver.lua | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/lua/gameserver.lua b/src/lua/gameserver.lua index 3ef8549..236344d 100644 --- a/src/lua/gameserver.lua +++ b/src/lua/gameserver.lua @@ -15,11 +15,8 @@ end pcall(function() settings().Network.UseInstancePacketCache = true end) pcall(function() settings().Network.UsePhysicsPacketCache = true end) --- pcall(function() settings()["Task Scheduler"].PriorityMethod = Enum.PriorityMethod.FIFO end) pcall(function() settings()["Task Scheduler"].PriorityMethod = Enum.PriorityMethod.AccumulatedError end) --- settings().Network.PhysicsSend = 1 -- 1==RoundRobin --- settings().Network.PhysicsSend = Enum.PhysicsSendMethod.ErrorComputation2 settings().Network.PhysicsSend = Enum.PhysicsSendMethod.TopNErrors settings().Network.ExperimentalPhysicsEnabled = true settings().Network.WaitingForCharacterLogRate = 100 @@ -53,11 +50,7 @@ if baseUrl ~= nil then pcall(function() loadfile(baseUrl .. "/Game/LoadPlaceInfo.ashx?PlaceId=" .. placeId)() end) end --- pcall(function() game:GetService("NetworkServer"):SetIsPlayerAuthenticationRequired(true) end) settings().Diagnostics.LuaRamLimit = 0 --- settings().Network:SetThroughputSensitivity(0.08, 0.01) --- settings().Network.SendRate = 35 --- settings().Network.PhysicsSend = 0 -- 1==RoundRobin game:GetService("Players").PlayerAdded:connect(function(player) print("Player " .. player.userId .. " added") From 7b44c0e4cfa0fb91428820f15bebfe31db1bc0c7 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Sat, 18 Feb 2023 03:51:23 -0500 Subject: [PATCH 04/11] :d --- src/lib/classes/GameJob.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index 8025e38..b9447c6 100644 --- a/src/lib/classes/GameJob.js +++ b/src/lib/classes/GameJob.js @@ -11,10 +11,6 @@ class GameJob extends Job { StartGame(id) { return new Promise(async (resolve, reject) => { - 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 this.placeId = id const started = await this.Start() @@ -34,8 +30,8 @@ class GameJob extends Job { { type: "LUA_TSTRING", value: process.env.BASE_URL }, { type: "LUA_TNUMBER", value: id }, - { type: "LUA_TNUMBER", value: server_port }, - { type: "LUA_TNUMBER", value: server_owner_id }, + { type: "LUA_TNUMBER", value: 50001 }, + { type: "LUA_TNUMBER", value: 0 }, ], }, }).catch((e) => reject(e)) From 63fd9599c96d5e49c86eec003890b5a393f7de3d Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Sat, 18 Feb 2023 03:52:46 -0500 Subject: [PATCH 05/11] :d --- src/lib/classes/GameJob.js | 1 - src/lua/gameserver.lua | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index b9447c6..ef5a24c 100644 --- a/src/lib/classes/GameJob.js +++ b/src/lib/classes/GameJob.js @@ -31,7 +31,6 @@ class GameJob extends Job { { type: "LUA_TNUMBER", value: id }, { type: "LUA_TNUMBER", value: 50001 }, - { type: "LUA_TNUMBER", value: 0 }, ], }, }).catch((e) => reject(e)) diff --git a/src/lua/gameserver.lua b/src/lua/gameserver.lua index 236344d..4048234 100644 --- a/src/lua/gameserver.lua +++ b/src/lua/gameserver.lua @@ -1,4 +1,4 @@ -local jobId, baseUrl, placeId, port = ... +local jobId, type, baseUrl, placeId, port = ... ------------------- UTILITY FUNCTIONS -------------------------- function waitForChild(parent, childName) From 39878d9edf3b54486d77712621642359004687bf Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Sun, 19 Feb 2023 03:07:51 -0500 Subject: [PATCH 06/11] Clean up exmaple.env, add authentication to endpoints --- example.env | 6 +++--- src/index.js | 30 +++++++++++++++--------------- src/lib/classes/RenderJob.js | 12 ++++++------ 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/example.env b/example.env index b757a14..1f72db2 100644 --- a/example.env +++ b/example.env @@ -1,10 +1,10 @@ +PORT= RCCSERVICE= -ARBITER_TOKEN= -ARBITER_PASSWORD= BASE_URL=https://sitetest.unexp.xyz -RENDER_FORMAT=PNG +ARBITER_TOKEN= +ARBITER_KEY= RENDER_USER_WIDTH=720 RENDER_USER_HEIGHT=720 diff --git a/src/index.js b/src/index.js index eff1167..b9d6f0b 100644 --- a/src/index.js +++ b/src/index.js @@ -6,13 +6,11 @@ const logger = require("./lib/logger.js") if (process.platform == "linux") logger.warn("Game hosting might not be fully compatible with Linux") -global.games = new Map() - -setInterval(() => { - global.games.forEach(async (value, key) => { - if (!(await value.Running())) value.Stop() - }) -}, 15000) +app.use(({ query }, response, next) => { + if (!query.key) return response.status(400).json({ error: "Missing key in query" }) + if (query.key !== process.env.ARBITER_KEY) return response.status(403).json({ error: "Incorrect key in query" }) + next() +}) app.use("/game/start", require("./routes/game/start.js")) app.use("/game/stop", require("./routes/game/stop.js")) @@ -22,17 +20,19 @@ app.use("/game/status", require("./routes/game/status.js")) app.use("/game/execute", require("./routes/game/execute.js")) app.use("/render/asset", require("./routes/render/asset.js")) -//app.use("/render/game", require("./routes/render/game.js")) -//app.use("/render/texture", require("./routes/render/texture.js")) +app.use("/render/game", require("./routes/render/game.js")) +app.use("/render/texture", require("./routes/render/texture.js")) app.use("/render/user", require("./routes/render/user.js")) app.use("/render/texture", require("./routes/render/texture.js")) app.use("*", require("./routes/index.js")) -app.listen(process.env.PORT || 64989, () => { - logger.boot(`Listening on http://127.0.0.1:${process.env.PORT || 64989}/`) -}) +process.on("uncaughtException", (err) => logger.error(err.message)) +global.games = new Map() +setInterval(() => { + global.games.forEach(async (value, key) => { + if (!(await value.Running())) value.Stop() + }) +}, 15000) -process.on("uncaughtException", (err) => { - logger.error(err.message) -}) +app.listen(process.env.PORT, () => logger.boot(`Listening on http://127.0.0.1:${process.env.PORT}/`)) diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 4da4cf9..0439098 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -1,5 +1,5 @@ const { readFile } = require("fs/promises") -const chalk = require('chalk') +const chalk = require("chalk") const Job = require("./Job.js") const logger = require("../logger.js") @@ -31,7 +31,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Headshot" }, - { type: "LUA_TSTRING", value: process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_HEIGHT }, @@ -70,7 +70,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Bodyshot" }, - { type: "LUA_TSTRING", value: three_d ? "OBJ" : process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: three_d ? "OBJ" : "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_HEIGHT }, @@ -110,7 +110,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Asset" }, - { type: "LUA_TSTRING", value: three_d ? "OBJ" : process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: three_d ? "OBJ" : "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_ASSET_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_ASSET_HEIGHT }, @@ -153,7 +153,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Place" }, - { type: "LUA_TSTRING", value: process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_PLACE_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_PLACE_HEIGHT }, @@ -191,7 +191,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Texture" }, - { type: "LUA_TSTRING", value: process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_HEIGHT }, From 0784446a61ab3205221c5cad69ebb54e068dc8ef Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Mon, 20 Feb 2023 04:12:51 -0500 Subject: [PATCH 07/11] Document, better endpoints --- README.md | 96 ++++++++++++++++++++++++++++++++++++ src/lib/classes/RenderJob.js | 10 ++++ src/routes/render/asset.js | 26 ++++------ src/routes/render/game.js | 14 ++++-- src/routes/render/texture.js | 11 +---- src/routes/render/user.js | 47 +++++++++--------- 6 files changed, 149 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 1a74536..632247f 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,99 @@ It comes preloaded with some Lua scripts made by kinery and jackd900. You **will** have to replace/modify these scripts when implementing for your own projects. Set your desired settings in `.env.example`, then rename it to `.env`. + +## Routes + +### GET /render/asset/:id + +
+200 OK + +``` +iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC... +``` + +
+ +### GET /render/asset/3d/:id + +
+200 OK + +```json +{ + "camera": { + "position": { "x": 0, "y": 0, "z": 0 }, + "direction": { "x": 0, "y": 0, "z": 0 } + }, + "AABB": { + "min": { "x": 0, "y": 0, "z": 0 }, + "max": { "x": 0, "y": 0, "z": 0 } + }, + "files": { + "scene.obj": { "content": "..." }, + "scene.mtl": { "content": "..." }, + "Handle1Tex.png": { "content": "..." } + } +} +``` + +
+ +### GET /render/texture/:id + +
+200 OK + +``` +iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC... +``` + +
+ +### GET /render/user/headshot/:id + +
+200 OK + +``` +iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC... +``` + +
+ +### GET /render/user/bodyshot/:id + +
+200 OK + +``` +iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC... +``` + +
+ +### GET /render/user/3d/:id + +
+200 OK + +```json +{ + "camera": { + "position": { "x": 0, "y": 0, "z": 0 }, + "direction": { "x": 0, "y": 0, "z": 0 } + }, + "AABB": { + "min": { "x": 0, "y": 0, "z": 0 }, + "max": { "x": 0, "y": 0, "z": 0 } + }, + "files": { + "scene.obj": { "content": "..." }, + "scene.mtl": { "content": "..." }, + "Handle1Tex.png": { "content": "..." } + } +} +``` + +
diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 0439098..78f0139 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -44,6 +44,8 @@ class RenderJob extends Job { logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Headshot RenderJob finished for ${id}`) + this.Stop() + if (!result) return false return result[0]?.OpenJobExResult?.LuaValue[0]?.value } @@ -84,6 +86,8 @@ class RenderJob extends Job { if (three_d) logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} 3D Bodyshot RenderJob finished for ${id}`) else logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Bodyshot RenderJob finished for ${id}`) + this.Stop() + if (!result) return false return result[0]?.OpenJobExResult?.LuaValue[0]?.value } @@ -125,6 +129,8 @@ class RenderJob extends Job { if (three_d) logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} 3D Asset RenderJob finished for ${id}`) else logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Asset RenderJob finished for ${id}`) + this.Stop() + if (!result) return false return result[0]?.OpenJobExResult?.LuaValue[0]?.value } @@ -166,6 +172,8 @@ class RenderJob extends Job { logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Place RenderJob finished for ${id}`) + this.Stop() + if (!result) return false return result[0]?.OpenJobExResult?.LuaValue[0]?.value } @@ -204,6 +212,8 @@ class RenderJob extends Job { logger.info(`[${this.id}] Headshot RenderJob finished for ${id}`) + this.Stop() + if (!result) return false return result[0]?.OpenJobExResult?.LuaValue[0]?.value } diff --git a/src/routes/render/asset.js b/src/routes/render/asset.js index 9fc500e..24aea09 100644 --- a/src/routes/render/asset.js +++ b/src/routes/render/asset.js @@ -6,27 +6,21 @@ const RenderJob = require("../../lib/classes/RenderJob.js") app.get("/:id", async (request, response) => { const { params, query } = request const job = new RenderJob() - let body = {} const asset = await job.RenderAsset(params.id).catch((_) => _) - if (asset?.message) { - job.Stop() - return response.status(500).json({ error: asset.message }) - } - body.asset = asset + if (asset?.message) return response.status(500).json({ error: asset.message }) - if (query.three_d) { - const three_d = await job.RenderAsset(params.id, true).catch((_) => _) - if (three_d?.message) { - job.Stop() - return response.status(500).json({ error: three_d.message }) - } - body.three_d = three_d - } + return response.end(asset) +}) - job.Stop() +app.get("/:id/3d", async (request, response) => { + const { params, query } = request + const job = new RenderJob() - return response.json(body) + const three_d = await job.RenderAsset(params.id, true).catch((_) => _) + if (three_d?.message) return response.status(500).json({ error: three_d.message }) + + return response.json(JSON.parse(three_d)) }) module.exports = app diff --git a/src/routes/render/game.js b/src/routes/render/game.js index 7616772..5962c24 100644 --- a/src/routes/render/game.js +++ b/src/routes/render/game.js @@ -3,12 +3,16 @@ const app = express.Router() 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((_) => _) +// 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) +// if (result?.message) return response.status(500).json({ error: result.message }) +// else return response.end(result) +// }) + +app.get("*", (request, response) => { + response.status(501).end("Not Implemented") }) module.exports = app diff --git a/src/routes/render/texture.js b/src/routes/render/texture.js index 5d1e7ad..6bd0fc7 100644 --- a/src/routes/render/texture.js +++ b/src/routes/render/texture.js @@ -6,18 +6,11 @@ const RenderJob = require("../../lib/classes/RenderJob.js") app.get("/:id", async (request, response) => { const { params, query } = request const job = new RenderJob() - let body = {} const texture = await job.RenderTexture(params.id).catch((_) => _) - if (texture?.message) { - job.Stop() - return response.status(500).json({ error: texture.message }) - } - body.texture = texture + if (texture?.message) return response.status(500).json({ error: texture.message }) - job.Stop() - - return response.json(body) + return response.end(texture) }) module.exports = app diff --git a/src/routes/render/user.js b/src/routes/render/user.js index 76f7376..b799dc9 100644 --- a/src/routes/render/user.js +++ b/src/routes/render/user.js @@ -3,37 +3,34 @@ const app = express.Router() const RenderJob = require("../../lib/classes/RenderJob.js") -app.get("/:id", async (request, response) => { - const { params, query } = request +app.get("/:id/bodyshot", async (request, response) => { + const { params } = request const job = new RenderJob() - let body = {} - - const headshot = await job.RenderHeadshot(params.id).catch((_) => _) - if (headshot?.message) { - job.Stop() - return response.status(500).json({ error: headshot.message }) - } - body.headshot = headshot const bodyshot = await job.RenderBodyshot(params.id).catch((_) => _) - if (bodyshot?.message) { - job.Stop() - return response.status(500).json({ error: bodyshot.message }) - } - body.bodyshot = bodyshot + if (bodyshot?.message) return response.status(500).json({ error: bodyshot.message }) - if (query.three_d) { - const three_d = await job.RenderBodyshot(params.id, true).catch((_) => _) - if (three_d?.message) { - job.Stop() - return response.status(500).json({ error: three_d.message }) - } - body.three_d = three_d - } + return response.end(bodyshot) +}) - job.Stop() +app.get("/:id/headshot", async (request, response) => { + const { params } = request + const job = new RenderJob() - return response.json(body) + const headshot = await job.RenderHeadshot(params.id).catch((_) => _) + if (headshot?.message) return response.status(500).json({ error: headshot.message }) + + return response.end(headshot) +}) + +app.get("/:id/3d", async (request, response) => { + const { params, query } = request + const job = new RenderJob() + + const three_d = await job.RenderBodyshot(params.id, true).catch((_) => _) + if (three_d?.message) return response.status(500).json({ error: three_d.message }) + + return response.json(JSON.parse(three_d)) }) module.exports = app From 173a3cc271a5b8987b94dd8ce865ce291cf329a7 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Mon, 20 Feb 2023 04:24:49 -0500 Subject: [PATCH 08/11] Gameserver should get a random UDP port --- src/lib/classes/GameJob.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index ef5a24c..1cc4528 100644 --- a/src/lib/classes/GameJob.js +++ b/src/lib/classes/GameJob.js @@ -3,6 +3,7 @@ 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() { @@ -19,6 +20,8 @@ class GameJob extends Job { 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" }), @@ -30,12 +33,12 @@ class GameJob extends Job { { type: "LUA_TSTRING", value: process.env.BASE_URL }, { type: "LUA_TNUMBER", value: id }, - { type: "LUA_TNUMBER", value: 50001 }, + { type: "LUA_TNUMBER", value: port }, ], }, }).catch((e) => reject(e)) - resolve() + resolve(port) }) } From 7da5877d58ee609a3fd1db57bdcd6a262f51d56d Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Mon, 20 Feb 2023 04:25:40 -0500 Subject: [PATCH 09/11] randport.udp() should get a random port between 60k-50k --- src/lib/randport.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/randport.js b/src/lib/randport.js index 23bd478..34dfa5f 100644 --- a/src/lib/randport.js +++ b/src/lib/randport.js @@ -14,7 +14,7 @@ exports.tcp = () => { exports.udp = () => { return new Promise((resolve) => { const server = dgram.createSocket() - server.bind(0, () => { + server.bind(Math.random() * (60_000 - 50_000) + 50_000, () => { const port = server.address().port server.close((err) => resolve(port)) }) From 6d1e6401e37e91eb3395705b13ae358dfb28b270 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Mon, 20 Feb 2023 04:52:40 -0500 Subject: [PATCH 10/11] Game thumbnails, randport.udp() should be on udp4 --- src/lib/classes/Job.js | 2 +- src/lib/classes/RenderJob.js | 6 +----- src/lib/randport.js | 2 +- src/routes/render/game.js | 14 ++++++-------- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/lib/classes/Job.js b/src/lib/classes/Job.js index 54778b2..4757f84 100644 --- a/src/lib/classes/Job.js +++ b/src/lib/classes/Job.js @@ -5,7 +5,7 @@ const RCCService = require("./RCCService.js") const logger = require("../logger.js") class Job extends RCCService { - constructor({ id = randomUUID(), expirationInSeconds = 10, category = 0, cores = 1 } = {}) { + constructor({ id = randomUUID(), expirationInSeconds = 60, category = 0, cores = 1 } = {}) { super() this.id = id this.expirationInSeconds = expirationInSeconds diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 78f0139..0d14dd3 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -136,11 +136,6 @@ class RenderJob extends Job { } async RenderPlace(id) { - 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 running = this.started if (!running) { const started = await this.Start() @@ -166,6 +161,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: process.env.BASE_URL }, { type: "LUA_TNUMBER", value: id }, + { type: "LUA_TSTRING", value: process.env.ARBITER_TOKEN }, ], }, }).catch((e) => false) diff --git a/src/lib/randport.js b/src/lib/randport.js index 34dfa5f..0da19fa 100644 --- a/src/lib/randport.js +++ b/src/lib/randport.js @@ -13,7 +13,7 @@ exports.tcp = () => { exports.udp = () => { return new Promise((resolve) => { - const server = dgram.createSocket() + const server = dgram.createSocket("udp4") server.bind(Math.random() * (60_000 - 50_000) + 50_000, () => { const port = server.address().port server.close((err) => resolve(port)) diff --git a/src/routes/render/game.js b/src/routes/render/game.js index 5962c24..a2e4c51 100644 --- a/src/routes/render/game.js +++ b/src/routes/render/game.js @@ -3,16 +3,14 @@ const app = express.Router() 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((_) => _) +app.get("/:id", async (request, response) => { + const { params, query } = request + const job = new RenderJob() -// if (result?.message) return response.status(500).json({ error: result.message }) -// else return response.end(result) -// }) + const game = await job.RenderPlace(params.id).catch((_) => _) + if (game?.message) return response.status(500).json({ error: game.message }) -app.get("*", (request, response) => { - response.status(501).end("Not Implemented") + return response.end(game) }) module.exports = app From 24e97e5e21dd487c721b12afc544b99a4bb2fd60 Mon Sep 17 00:00:00 2001 From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com> Date: Mon, 20 Feb 2023 05:15:58 -0500 Subject: [PATCH 11/11] Games should be queried by ids, not 'tokens' --- src/routes/game/execute.js | 4 ++-- src/routes/game/renew.js | 4 ++-- src/routes/game/running.js | 4 ++-- src/routes/game/start.js | 16 +++------------- src/routes/game/status.js | 4 ++-- src/routes/game/stop.js | 4 ++-- 6 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/routes/game/execute.js b/src/routes/game/execute.js index 09bcacc..6ea0070 100644 --- a/src/routes/game/execute.js +++ b/src/routes/game/execute.js @@ -6,8 +6,8 @@ const GameJob = require("../../lib/classes/GameJob.js") app.use(express.json()) -app.post("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.post("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) const { script } = request.body diff --git a/src/routes/game/renew.js b/src/routes/game/renew.js index ca3f580..68e6538 100644 --- a/src/routes/game/renew.js +++ b/src/routes/game/renew.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token/:expire", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id/:expire", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) await game.RenewLease(request.params.expire) diff --git a/src/routes/game/running.js b/src/routes/game/running.js index a33b8df..a1f97a0 100644 --- a/src/routes/game/running.js +++ b/src/routes/game/running.js @@ -3,8 +3,8 @@ 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) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.json(false) const running = await game.Running() diff --git a/src/routes/game/start.js b/src/routes/game/start.js index 2bcc564..0049888 100644 --- a/src/routes/game/start.js +++ b/src/routes/game/start.js @@ -3,26 +3,16 @@ 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(getGameById(request.params.id)?.id) + const game = global.games.get(request.params.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((_) => _) - global.games.set(job.id, job) + global.games.set(request.params.id, job) job.proc.once("exit", () => { - global.games.delete(job.id) + global.games.delete(request.params.id) }) return response.json({ success: true }) diff --git a/src/routes/game/status.js b/src/routes/game/status.js index 915bf71..7c4aeed 100644 --- a/src/routes/game/status.js +++ b/src/routes/game/status.js @@ -3,8 +3,8 @@ 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) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) const status = await game.GetStatus() diff --git a/src/routes/game/stop.js b/src/routes/game/stop.js index b82cd49..be676b5 100644 --- a/src/routes/game/stop.js +++ b/src/routes/game/stop.js @@ -3,8 +3,8 @@ 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) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) game.Stop()