From 32a2748d176d05f94e8866fa181f83ef98d6fcfd 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:39:42 -0500 Subject: [PATCH] Better game management --- src/lib/classes/GameJob.js | 11 +++++++---- src/lib/classes/RenderJob.js | 9 ++++++++- src/lua/gameserver.lua | 33 ++++++++++++++++++++++++--------- src/lua/place.lua | 5 +++-- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index 1cc4528..a3d402a 100644 --- a/src/lib/classes/GameJob.js +++ b/src/lib/classes/GameJob.js @@ -12,6 +12,10 @@ 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 } = response.data + + this.serverToken = server_token this.placeId = id const started = await this.Start() @@ -20,8 +24,6 @@ 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" }), @@ -33,12 +35,13 @@ class GameJob extends Job { { type: "LUA_TSTRING", value: process.env.BASE_URL }, { type: "LUA_TNUMBER", value: id }, - { type: "LUA_TNUMBER", value: port }, + { type: "LUA_TNUMBER", value: server_port }, + { type: "LUA_TSTRING", value: this.serverToken }, ], }, }).catch((e) => reject(e)) - resolve(port) + resolve() }) } diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 0d14dd3..867cf7c 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -1,5 +1,6 @@ const { readFile } = require("fs/promises") const chalk = require("chalk") +const axios = require("axios") const Job = require("./Job.js") const logger = require("../logger.js") @@ -136,6 +137,12 @@ 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 } = response.data + + this.serverToken = server_token + console.log(`${process.env.BASE_URL}/API/Game/${id}?t=${process.env.ARBITER_TOKEN}`, server_token) + const running = this.started if (!running) { const started = await this.Start() @@ -161,7 +168,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 }, + { type: "LUA_TSTRING", value: this.serverToken }, ], }, }).catch((e) => false) diff --git a/src/lua/gameserver.lua b/src/lua/gameserver.lua index e866833..3156182 100644 --- a/src/lua/gameserver.lua +++ b/src/lua/gameserver.lua @@ -1,6 +1,7 @@ -local jobId, type, baseUrl, placeId, port = ... +local jobId, type, baseUrl, placeId, port, token = ... ------------------- UTILITY FUNCTIONS -------------------------- + function waitForChild(parent, childName) while true do local child = parent:findFirstChild(childName) @@ -63,16 +64,19 @@ 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(placeId, false) +game:SetPlaceID(assetId, false) game:GetService("ChangeHistoryService"):SetEnabled(false) +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) @@ -92,16 +96,29 @@ end settings().Diagnostics.LuaRamLimit = 0 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 .. "/asset/?id=" .. placeId) + game:Load(baseUrl .. "/thumbs/staticimage?r=" .. token) end ------------------------------ RENEW GAME JOB SERVICE ------------------------------- @@ -109,9 +126,9 @@ end spawn(function() while wait(30) do if #game.Players:GetPlayers() == 0 then - pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. jobId .. "/kill") end) + pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. token .. "/kill") end) else - pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. jobId .. "/renew?s=360") end) + pcall(function() game:HttpGet(baseUrl .. "/arbiter/" .. token .. "/renew?s=360") end) keepAlive() end end @@ -124,6 +141,4 @@ ns:Start(port) scriptContext:SetTimeout(10) scriptContext.ScriptsDisabled = false -------------------------------END START GAME SHARED SCRIPT-------------------------- - game:GetService("RunService"):Run() diff --git a/src/lua/place.lua b/src/lua/place.lua index 5dee862..a4e48ae 100644 --- a/src/lua/place.lua +++ b/src/lua/place.lua @@ -1,4 +1,5 @@ -local jobId, type, format, x, y, baseUrl, assetId = ... +local jobId, type, format, x, y, baseUrl, assetId, token = ... +print(token) print(("[%s] Started RenderJob for type '%s' with assetId %d ..."):format(jobId, type, assetId)) @@ -11,7 +12,7 @@ game:GetService("ContentProvider"):SetBaseUrl(baseUrl) game:GetService("ScriptContext").ScriptsDisabled = true game:GetService("StarterGui").ShowDevelopmentGui = false -game:Load(("%s/asset/?id=%d"):format(baseUrl, assetId)) +game:Load(("%s/thumbs/staticimage?r=%s"):format(baseUrl, token)) game:GetService("ScriptContext").ScriptsDisabled = true game:GetService("StarterGui").ShowDevelopmentGui = false