mirror of
https://github.com/gmod-integration/lua.git
synced 2025-06-16 01:03:57 +00:00
refact: extract module code into their own file
This commit is contained in:
parent
6eebe5f165
commit
f7dbb0250d
82
lua/gmod_integration/core/utils/sh_player_adjusted_time.lua
Normal file
82
lua/gmod_integration/core/utils/sh_player_adjusted_time.lua
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
local ply = FindMetaTable("Player")
|
||||||
|
function ply:gmIntGetTimeLastTeamChange()
|
||||||
|
return self.gmIntTimeLastTeamChange || RealTime()
|
||||||
|
end
|
||||||
|
|
||||||
|
function ply:gmInteResetTimeLastTeamChange()
|
||||||
|
self.gmIntTimeLastTeamChange = RealTime()
|
||||||
|
end
|
||||||
|
|
||||||
|
gmInte.restoreFileCache = gmInte.restoreFileCache || {}
|
||||||
|
function ply:getAdjustedTime()
|
||||||
|
if gmInte.restoreFileCache == nil || gmInte.restoreFileCache.sysTime == nil || gmInte.restoreFileCache.playersList == nil then return 0 end
|
||||||
|
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 || !gmInte.restoreFileCache.playersList then return 0 end
|
||||||
|
if (gmInte.restoreFileCache.sysTime + 60 * 5) < (os.time() - self:gmIntGetConnectTime()) then return 0 end
|
||||||
|
if !gmInte.restoreFileCache.playersList[self:SteamID()] then return 0 end
|
||||||
|
return gmInte.restoreFileCache.playersList[self:SteamID()].connectTime || 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if SERVER then
|
||||||
|
gameevent.Listen("player_connect")
|
||||||
|
hook.Add("player_connect", "gmInte:Player:Connect:RemoveRestore", function(data)
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if gmInte.restoreFileCache.playersList && gmInte.restoreFileCache.playersList[data.networkid] then
|
||||||
|
gmInte.restoreFileCache.playersList[data.networkid] = nil
|
||||||
|
file.Write("gm_integration/player_before_map_change.json", util.TableToJSON(gmInte.restoreFileCache, true))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function saveTimeToLocal()
|
||||||
|
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
|
||||||
|
|
||||||
|
hook.Add("ShutDown", "gmInte:Server:ShutDown:SavePlayer", saveTimeToLocal)
|
||||||
|
hook.Add("GMI:SaveBeforeCrash", "gmInte:Server:BeforeCrash:SavePlayers", saveTimeToLocal)
|
57
lua/gmod_integration/core/utils/sh_player_custom_values.lua
Normal file
57
lua/gmod_integration/core/utils/sh_player_custom_values.lua
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
local ply = FindMetaTable("Player")
|
||||||
|
function ply:gmIntSetCustomValue(key, value)
|
||||||
|
self.gmIntCustomValues = self.gmIntCustomValues || {}
|
||||||
|
self.gmIntCustomValues[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
function ply:gmIntGetCustomValue(key)
|
||||||
|
return self.gmIntCustomValues && self.gmIntCustomValues[key]
|
||||||
|
end
|
||||||
|
|
||||||
|
function ply:gmIntRemoveCustomValue(key)
|
||||||
|
if self.gmIntCustomValues then self.gmIntCustomValues[key] = nil end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getCustomCompatability(ply)
|
||||||
|
local values = {}
|
||||||
|
// DarkRP
|
||||||
|
if DarkRP then
|
||||||
|
values.money = ply:getDarkRPVar("money")
|
||||||
|
values.job = ply:getDarkRPVar("job")
|
||||||
|
end
|
||||||
|
|
||||||
|
// GUI Level System
|
||||||
|
if GUILevelSystem then
|
||||||
|
values.level = ply:GetLevel()
|
||||||
|
values.xp = ply:GetXP()
|
||||||
|
end
|
||||||
|
|
||||||
|
// Pointshop 2
|
||||||
|
if Pointshop2 && ply.PS2_Wallet then
|
||||||
|
values.ps2Points = ply.PS2_Wallet.points
|
||||||
|
values.ps2PremiumPoints = ply.PS2_Wallet.premiumPoints
|
||||||
|
end
|
||||||
|
|
||||||
|
if CH_ATM && SERVER then values.bank = CH_ATM.GetMoneyBankAccount(ply) end
|
||||||
|
return values
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getCustomValues(ply)
|
||||||
|
local values = {}
|
||||||
|
// Get compatability values
|
||||||
|
for key, value in pairs(getCustomCompatability(ply)) do
|
||||||
|
values[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
// Get custom values or overwrite compatability values
|
||||||
|
if ply.gmIntCustomValues then
|
||||||
|
for key, value in pairs(ply.gmIntCustomValues) do
|
||||||
|
values[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return values
|
||||||
|
end
|
||||||
|
|
||||||
|
function ply:gmIntGetCustomValues()
|
||||||
|
return getCustomValues(self)
|
||||||
|
end
|
|
@ -3,83 +3,14 @@ function ply:gmIntGetConnectTime()
|
||||||
return self.gmIntTimeConnect || 0
|
return self.gmIntTimeConnect || 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function ply:gmIntIsVerified()
|
|
||||||
return self.gmIntVerified || false
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmIntGetTimeLastTeamChange()
|
|
||||||
return self.gmIntTimeLastTeamChange || RealTime()
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmInteResetTimeLastTeamChange()
|
|
||||||
self.gmIntTimeLastTeamChange = RealTime()
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmInteGetBranch()
|
function ply:gmInteGetBranch()
|
||||||
return CLIENT && BRANCH || self.branch || "unknown"
|
return CLIENT && BRANCH || self.branch || "unknown"
|
||||||
end
|
end
|
||||||
|
|
||||||
function ply:gmIntSetCustomValue(key, value)
|
|
||||||
self.gmIntCustomValues = self.gmIntCustomValues || {}
|
|
||||||
self.gmIntCustomValues[key] = value
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmIntIsAdmin()
|
function ply:gmIntIsAdmin()
|
||||||
return gmInte.config.adminRank[self:GetUserGroup()] || false
|
return gmInte.config.adminRank[self:GetUserGroup()] || false
|
||||||
end
|
end
|
||||||
|
|
||||||
function ply:gmIntGetCustomValue(key)
|
|
||||||
return self.gmIntCustomValues && self.gmIntCustomValues[key]
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmIntRemoveCustomValue(key)
|
|
||||||
if self.gmIntCustomValues then self.gmIntCustomValues[key] = nil end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getCustomCompatability(ply)
|
|
||||||
local values = {}
|
|
||||||
// DarkRP
|
|
||||||
if DarkRP then
|
|
||||||
values.money = ply:getDarkRPVar("money")
|
|
||||||
values.job = ply:getDarkRPVar("job")
|
|
||||||
end
|
|
||||||
|
|
||||||
// GUI Level System
|
|
||||||
if GUILevelSystem then
|
|
||||||
values.level = ply:GetLevel()
|
|
||||||
values.xp = ply:GetXP()
|
|
||||||
end
|
|
||||||
|
|
||||||
// Pointshop 2
|
|
||||||
if Pointshop2 && ply.PS2_Wallet then
|
|
||||||
values.ps2Points = ply.PS2_Wallet.points
|
|
||||||
values.ps2PremiumPoints = ply.PS2_Wallet.premiumPoints
|
|
||||||
end
|
|
||||||
|
|
||||||
if CH_ATM && SERVER then values.bank = CH_ATM.GetMoneyBankAccount(ply) end
|
|
||||||
return values
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getCustomValues(ply)
|
|
||||||
local values = {}
|
|
||||||
// Get compatability values
|
|
||||||
for key, value in pairs(getCustomCompatability(ply)) do
|
|
||||||
values[key] = value
|
|
||||||
end
|
|
||||||
|
|
||||||
// Get custom values or overwrite compatability values
|
|
||||||
if ply.gmIntCustomValues then
|
|
||||||
for key, value in pairs(ply.gmIntCustomValues) do
|
|
||||||
values[key] = value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return values
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmIntGetCustomValues()
|
|
||||||
return getCustomValues(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
function ply:gmIntGetFPS()
|
function ply:gmIntGetFPS()
|
||||||
return self.gmIntFPS || 0
|
return self.gmIntFPS || 0
|
||||||
end
|
end
|
||||||
|
@ -89,77 +20,3 @@ function ply:gmInteSetFPS(fps)
|
||||||
fps = math.Clamp(fps, 0, 1000)
|
fps = math.Clamp(fps, 0, 1000)
|
||||||
self.gmIntFPS = fps
|
self.gmIntFPS = fps
|
||||||
end
|
end
|
||||||
|
|
||||||
gmInte.restoreFileCache = gmInte.restoreFileCache || {}
|
|
||||||
function ply:getAdjustedTime()
|
|
||||||
if gmInte.restoreFileCache == nil || gmInte.restoreFileCache.sysTime == nil || gmInte.restoreFileCache.playersList == nil then return 0 end
|
|
||||||
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 || !gmInte.restoreFileCache.playersList then return 0 end
|
|
||||||
if (gmInte.restoreFileCache.sysTime + 60 * 5) < (os.time() - self:gmIntGetConnectTime()) then return 0 end
|
|
||||||
if !gmInte.restoreFileCache.playersList[self:SteamID()] then return 0 end
|
|
||||||
return gmInte.restoreFileCache.playersList[self:SteamID()].connectTime || 0
|
|
||||||
end
|
|
||||||
|
|
||||||
if SERVER then
|
|
||||||
gameevent.Listen("player_connect")
|
|
||||||
hook.Add("player_connect", "gmInte:Player:Connect:RemoveRestore", function(data)
|
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if gmInte.restoreFileCache.playersList && gmInte.restoreFileCache.playersList[data.networkid] then
|
|
||||||
gmInte.restoreFileCache.playersList[data.networkid] = nil
|
|
||||||
file.Write("gm_integration/player_before_map_change.json", util.TableToJSON(gmInte.restoreFileCache, true))
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function saveTimeToLocal()
|
|
||||||
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
|
|
||||||
|
|
||||||
hook.Add("ShutDown", "gmInte:Server:ShutDown:SavePlayer", saveTimeToLocal)
|
|
||||||
hook.Add("GMI:SaveBeforeCrash", "gmInte:Server:BeforeCrash:SavePlayers", saveTimeToLocal)
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
local ply = FindMetaTable("Player")
|
||||||
|
function ply:gmIntIsVerified()
|
||||||
|
return self.gmIntVerified || false
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user