Sulfito_PointSaving/lua/autorun/server/pointsaving.lua

98 lines
2.7 KiB
Lua
Raw Normal View History

2018-07-15 10:07:37 +00:00
if engine.ActiveGamemode() ~= "zombiesurvival" then return end
2018-07-15 10:07:37 +00:00
local settings = {
StartingSavedPoints = 100,
2018-07-16 14:58:18 +00:00
MinimumPoints = 0,
2018-07-20 13:49:38 +00:00
PointsaveInterval = 30, -- Save points every x seconds
IgnoredMaps = { -- Use $ at the end if you want to define exact map names with versions. (Otherwise it will match with all maps beginning with the given name)
"zs_obj_gauntlet",
"zs_gauntlet_reborn"
}
}
2018-07-16 15:02:46 +00:00
function PointSaving_shouldIgnoreMap(mapName)
for k, v in ipairs(settings.IgnoredMaps) do
2018-07-15 14:34:14 +00:00
if string.match(string.lower(mapName), "^"..v) then return true end
2018-07-15 10:07:37 +00:00
end
end
2018-07-15 10:07:37 +00:00
local function savePoints(ply)
2018-07-16 15:02:46 +00:00
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
2018-07-24 16:15:35 +00:00
if not ply.PointSaving_StartPoints then return end
2018-07-20 13:49:38 +00:00
if ply:Team() == TEAM_HUMAN then
ply:SetSavedPoints(ply:GetPoints())
2018-07-15 10:07:37 +00:00
end
end
hook.Add("PlayerDeath", "pointsave_death", savePoints)
hook.Add("PlayerDisconnected", "pointsave_disconnect", savePoints)
2018-07-15 14:34:14 +00:00
hook.Add("PostEndRound", "pointsave_postendround", function(winner)
2018-07-16 15:02:46 +00:00
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
2018-07-15 14:34:14 +00:00
for k, ply in pairs(player.GetAll()) do
savePoints(ply)
end
end)
2018-07-20 13:49:38 +00:00
timer.Create("pointsave_pointsavetimer", settings.PointsaveInterval, 0, function()
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
for k, ply in pairs(player.GetAll()) do
savePoints(ply)
end
end)
local function loadPoints(ply)
2018-07-16 15:02:46 +00:00
if PointSaving_shouldIgnoreMap(game.GetMap()) then return end
timer.Simple(0, function()
if IsValid(ply) and ply:Team() == TEAM_HUMAN then
2018-07-24 16:15:35 +00:00
local savedPoints = ply:GetSavedPoints()
ply:SetPoints(savedPoints)
ply.PointSaving_StartPoints = savedPoints
2018-07-15 10:07:37 +00:00
end
end)
end
hook.Add("PlayerSpawn", "pointsave_spawn", loadPoints)
2018-07-15 10:07:37 +00:00
2018-07-15 14:34:14 +00:00
-- #### Player meta #### TODO: Put in an extra file
2018-07-15 10:07:37 +00:00
local meta = FindMetaTable("Player") -- Get the meta table of player
2018-07-15 10:07:37 +00:00
-- Shared methods
2018-07-15 10:07:37 +00:00
function meta:GetSavedPoints()
if SERVER then
local points = self:GetPData("zs_savedpoints", settings.StartingSavedPoints)
self:SetNWInt("zs_savedpoints", points)
--print("### PS Loaded:", points, "for", tostring(self))
return points
2018-07-15 10:07:37 +00:00
end
return self:GetNWInt("zs_savedpoints", settings.StartingSavedPoints)
2018-07-15 10:07:37 +00:00
end
if not SERVER then return end
-- Server side methods
function meta:SetSavedPoints(amount)
2018-07-16 14:58:18 +00:00
if amount < settings.MinimumPoints then amount = settings.MinimumPoints end
amount = math.Round(amount)
--print("### PS Saved: ", amount, "for", tostring(self))
self:SetNWInt("zs_savedpoints", amount)
self:SetPData("zs_savedpoints", amount)
2018-07-15 10:07:37 +00:00
end
function meta:AddSavedPoints(amount)
local points = self:GetSavedPoints()
self:SetSavedPoints(points + amount)
2018-07-15 10:07:37 +00:00
end
function meta:TakeSavedPoints(amount)
self:AddSavedPoints(-amount)
2018-07-15 10:07:37 +00:00
end