From 4ab93e1afe66bcef96fc9102eefb7691086032cf Mon Sep 17 00:00:00 2001 From: Linventif Date: Thu, 21 Nov 2024 17:08:15 +0000 Subject: [PATCH] Feat: add ajustTime for reconnect after map change --- lua/gmod_integration/shared/sh_api_format.lua | 3 +- .../shared/sh_player_meta.lua | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lua/gmod_integration/shared/sh_api_format.lua b/lua/gmod_integration/shared/sh_api_format.lua index 1b0237c..d48076b 100644 --- a/lua/gmod_integration/shared/sh_api_format.lua +++ b/lua/gmod_integration/shared/sh_api_format.lua @@ -10,7 +10,8 @@ function gmInte.getPlayerFormat(ply) ["kills"] = ply:Frags(), ["deaths"] = ply:Deaths(), ["customValues"] = ply:gmIntGetCustomValues(), - ["connectTime"] = math.Round(RealTime() - ply:gmIntGetConnectTime()), + ["connectTime"] = math.Round(math.Round(RealTime() - ply:gmIntGetConnectTime())), + ["ajustTime"] = math.Round(ply:getAjustTime()), ["ping"] = ply:Ping(), ["fps"] = ply:gmIntGetFPS(), ["position"] = gmInte.getVectorFormat(ply:GetPos()), diff --git a/lua/gmod_integration/shared/sh_player_meta.lua b/lua/gmod_integration/shared/sh_player_meta.lua index 977c34f..eed33bc 100644 --- a/lua/gmod_integration/shared/sh_player_meta.lua +++ b/lua/gmod_integration/shared/sh_player_meta.lua @@ -69,7 +69,60 @@ end function ply:gmIntGetCustomValues() return getCustomValues(self) end + function ply:gmIntGetFPS() return self.gmIntFPS || 0 end +// Backup players before map change +hook.Add("ShutDown", "gmInte:Server:ShutDown:SavePlayer", function() + // save in data/gm_integration/player_before_map_change.json + local dataToSave = { + ["version"] = "1.0", + ["serverID"] = gmInte.config.id, + ["playersList"] = {}, + ["sysTime"] = os.time() + } + + if SERVER then + for _, ply in ipairs(player.GetAll()) do + dataToSave.playersList[ply:SteamID()] = gmInte.getPlayerFormat(ply) + end + + if !file.Exists("gm_integration", "DATA") then file.CreateDir("gm_integration") end + file.Write("gm_integration/player_before_map_change.json", util.TableToJSON(dataToSave, true)) + else + dataToSave.playersList[LocalPlayer():SteamID()] = gmInte.getPlayerFormat(LocalPlayer()) + local oldData = {} + if file.Exists("gmod_integration/player_before_map_change.json", "DATA") then oldData = util.JSONToTable(file.Read("gmod_integration/player_before_map_change.json", "DATA")) end + oldData[gmInte.config.id] = dataToSave + file.Write("gmod_integration/player_before_map_change.json", util.TableToJSON(oldData, true)) + end +end) + +gmInte.restoreFileCache = gmInte.restoreFileCache || {} +function ply:getAjustTime() + if SERVER then + if table.IsEmpty(gmInte.restoreFileCache) then + if file.Exists("gm_integration/player_before_map_change.json", "DATA") then + gmInte.restoreFileCache = util.JSONToTable(file.Read("gm_integration/player_before_map_change.json", "DATA")) + else + return 0 + end + end + else + if table.IsEmpty(gmInte.restoreFileCache) then + if file.Exists("gmod_integration/player_before_map_change.json", "DATA") then + gmInte.restoreFileCache = util.JSONToTable(file.Read("gmod_integration/player_before_map_change.json", "DATA")) + else + return 0 + end + + gmInte.restoreFileCache = gmInte.restoreFileCache[gmInte.config.id] + end + end + + if (gmInte.restoreFileCache.sysTime + 60 * 5) < (os.time() - self:gmIntGetConnectTime()) then return 0 end + if !gmInte.restoreFileCache.playersList || !gmInte.restoreFileCache.playersList[self:SteamID()] then return 0 end + return gmInte.restoreFileCache.playersList[self:SteamID()].connectTime || 0 +end \ No newline at end of file