Sulfito_PointSaving/lua/autorun/server/pointsaving.lua

86 lines
2.2 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,
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
if ply:Team() == TEAM_HUMAN then
2018-07-15 14:34:14 +00:00
--print("### PS Saved: "..ply:GetPoints())
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)
-- TODO: Save points every minute or so
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-15 14:34:14 +00:00
--print("### Loaded: "..ply:GetSavedPoints())
ply:SetPoints(ply:GetSavedPoints())
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)
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
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