Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SERIAL = {}
- SERIAL.__index = __index
- SERIAL.IsBackup = false
- function SERIAL:Init()
- file.CreateDir("serialize")
- end
- function SERIAL:Delete(ply)
- file.Delete("serialize/"..ply:SteamID64()..".txt")
- end
- function SERIAL:PersistData(ply)
- local init = {}
- init.Weapons = {}
- for k,v in pairs(ply:GetWeapons()) do
- init.Weapons[k] = v:GetClass().." "..v:GetPrimaryAmmoType().." "..ply:GetAmmoCount( v:GetPrimaryAmmoType() )
- end
- init.Pos = ply:GetPos()
- init.Ang = ply:GetAngles()
- init.Health = ply:Health()
- init.Armor = ply:Armor()
- init.Job = ply:Team()
- init.Map = game.GetMap()
- file.Write("serialize/"..ply:SteamID64()..".txt",util.TableToJSON(init))
- end
- function SERIAL:LoadData(ply)
- if(!file.Exists("serialize/"..ply:SteamID64()..".txt","DATA")) then
- local init = {}
- init.Weapons = {}
- for k,v in pairs(ply:GetWeapons()) do
- init.Weapons[k] = v:GetClass().." "..v:GetPrimaryAmmoType().." "..ply:GetAmmoCount( v:GetPrimaryAmmoType() )
- end
- init.Pos = ply:GetPos()
- init.Ang = ply:GetAngles()
- init.Health = ply:Health()
- init.Armor = ply:Armor()
- init.Job = ply:Team()
- init.Map = game.GetMap()
- file.Write("serialize/"..ply:SteamID64()..".txt",util.TableToJSON(init))
- else
- local tbl = util.JSONToTable(file.Read("serialize/"..ply:SteamID64()..".txt","DATA"))
- if(tbl != nil) then
- ply:StripWeapons()
- for k,v in pairs(tbl.Weapons) do
- ply:Give(string.Explode(" ",v)[1])
- ply:GiveAmmo(string.Explode(" ",v)[3],string.Explode(" ",v)[2],true)
- MsgN(v," ",string.Explode(" ",v)[1]," ",string.Explode(" ",v)[2]," ",string.Explode(" ",v)[3])
- end
- ply:SetHealth(tbl.Health)
- ply:SetArmor(tbl.Armor)
- if(tbl.Map == game.GetMap()) then
- ply:SetPos(tbl.Pos)
- ply:SetEyeAngles(tbl.Ang)
- MsgN("PositionAssigned")
- else
- self:PersistData(ply)
- end
- timer.Simple(1,function()
- ply:changeTeam(tbl.Job,true)
- if(!self.IsBackup) then
- net.Start("WaitForSave")
- net.WriteFloat(2)
- net.Send(ply)
- end
- end)
- else
- file.Delete("serialize/"..ply:SteamID64()..".txt")
- self:LoadData(ply)
- end
- end
- end
- hook.Add("PlayerInitialSpawn","Initialize persistence",function(ply) timer.Simple(5,function() SERIAL:LoadData(ply) end) end)
- hook.Add("PlayerDeath","RemoveData",function(ply) SERIAL:Delete(ply) end)
- hook.Add("PlayerDisconnect","PersistByLeave",function(ply) SERIAL:PersistData(ply) end)
- if SERVER then
- hook.Add("Think","CheckForBackUps",function()
- for k,v in pairs(player.GetAll()) do
- if(v.NB == nil) then
- v.NB = CurTime() + 500
- self.IsBackup = true
- SERIAL:PersistData(v)
- net.Start("WaitForSave")
- net.WriteFloat(4)
- net.Send(v)
- self.IsBackup = false
- end
- if(v.NB <= CurTime()) then
- self.IsBackup = true
- SERIAL:PersistData(v)
- v.NB = CurTime() + 500
- net.Start("WaitForSave")
- net.WriteFloat(1)
- net.Send(v)
- self.IsBackup = false
- end
- end
- end)
- end
- hook.Add("PlayerSay","SaveByCommand",function(ply,text)
- if(text == "!save" or text == "/save") then
- if((ply.NBB or 0) < CurTime()) then
- ply.NBB = CurTime() + 10
- SERIAL:PersistData(ply)
- net.Start("WaitForSave")
- net.WriteFloat(1)
- net.Send(ply)
- else
- net.Start("WaitForSave")
- net.WriteFloat(0)
- net.Send(ply)
- end
- if(ply.NBB == nil) then
- ply.NBB = CurTime() + 10
- SERIAL:PersistData(ply)
- end
- return ""
- end
- if(text == "!erase" or text == "/erase") then
- SERIAL:Delete(ply)
- net.Start("WaitForSave")
- net.WriteFloat(3)
- net.Send(ply)
- return ""
- end
- end)
- SERIAL:Init()
- if SERVER then
- util.AddNetworkString("WaitForSave")
- end
- net.Receive("WaitForSave",function()
- local b = net.ReadFloat()
- if(b == 0) then
- chat.AddText(Color(214,51,14),"[SAVE] ",Color(235,235,235),"You can only save each 10 seconds.")
- elseif(b == 1) then
- chat.AddText(Color(51,214,14),"[SAVE] ",Color(235,235,235),"Your data has been saved!")
- elseif(b == 2) then
- chat.AddText(Color(51,14,214),"[SAVE] ",Color(235,235,235),"Your data has been loaded!")
- elseif(b == 3) then
- chat.AddText(Color(214,51,14),"[SAVE] ",Color(235,235,235),"Your data has been deleted.")
- else
- MsgC(Color(51,214,14),"[BackUp created]",Color(235,235,235)," You can continue playing.")
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment