Guest User

Code

a guest
Oct 1st, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1.  
  2. SERIAL = {}
  3. SERIAL.__index = __index
  4. SERIAL.IsBackup = false
  5.  
  6. function SERIAL:Init()
  7. file.CreateDir("serialize")
  8. end
  9.  
  10. function SERIAL:Delete(ply)
  11. file.Delete("serialize/"..ply:SteamID64()..".txt")
  12. end
  13.  
  14. function SERIAL:PersistData(ply)
  15.  
  16. local init = {}
  17.  
  18. init.Weapons = {}
  19. for k,v in pairs(ply:GetWeapons()) do
  20. init.Weapons[k] = v:GetClass().." "..v:GetPrimaryAmmoType().." "..ply:GetAmmoCount( v:GetPrimaryAmmoType() )
  21. end
  22.  
  23. init.Pos = ply:GetPos()
  24. init.Ang = ply:GetAngles()
  25. init.Health = ply:Health()
  26. init.Armor = ply:Armor()
  27. init.Job = ply:Team()
  28. init.Map = game.GetMap()
  29.  
  30. file.Write("serialize/"..ply:SteamID64()..".txt",util.TableToJSON(init))
  31.  
  32. end
  33.  
  34. function SERIAL:LoadData(ply)
  35.  
  36. if(!file.Exists("serialize/"..ply:SteamID64()..".txt","DATA")) then
  37.  
  38. local init = {}
  39.  
  40. init.Weapons = {}
  41. for k,v in pairs(ply:GetWeapons()) do
  42. init.Weapons[k] = v:GetClass().." "..v:GetPrimaryAmmoType().." "..ply:GetAmmoCount( v:GetPrimaryAmmoType() )
  43. end
  44.  
  45. init.Pos = ply:GetPos()
  46. init.Ang = ply:GetAngles()
  47. init.Health = ply:Health()
  48. init.Armor = ply:Armor()
  49. init.Job = ply:Team()
  50. init.Map = game.GetMap()
  51.  
  52. file.Write("serialize/"..ply:SteamID64()..".txt",util.TableToJSON(init))
  53.  
  54. else
  55.  
  56. local tbl = util.JSONToTable(file.Read("serialize/"..ply:SteamID64()..".txt","DATA"))
  57.  
  58. if(tbl != nil) then
  59.  
  60. ply:StripWeapons()
  61.  
  62. for k,v in pairs(tbl.Weapons) do
  63. ply:Give(string.Explode(" ",v)[1])
  64. ply:GiveAmmo(string.Explode(" ",v)[3],string.Explode(" ",v)[2],true)
  65. MsgN(v," ",string.Explode(" ",v)[1]," ",string.Explode(" ",v)[2]," ",string.Explode(" ",v)[3])
  66. end
  67.  
  68. ply:SetHealth(tbl.Health)
  69. ply:SetArmor(tbl.Armor)
  70.  
  71. if(tbl.Map == game.GetMap()) then
  72. ply:SetPos(tbl.Pos)
  73. ply:SetEyeAngles(tbl.Ang)
  74. MsgN("PositionAssigned")
  75. else
  76. self:PersistData(ply)
  77. end
  78.  
  79. timer.Simple(1,function()
  80. ply:changeTeam(tbl.Job,true)
  81.  
  82. if(!self.IsBackup) then
  83. net.Start("WaitForSave")
  84. net.WriteFloat(2)
  85. net.Send(ply)
  86. end
  87.  
  88. end)
  89.  
  90. else
  91. file.Delete("serialize/"..ply:SteamID64()..".txt")
  92. self:LoadData(ply)
  93. end
  94.  
  95. end
  96. end
  97.  
  98. hook.Add("PlayerInitialSpawn","Initialize persistence",function(ply) timer.Simple(5,function() SERIAL:LoadData(ply) end) end)
  99.  
  100. hook.Add("PlayerDeath","RemoveData",function(ply) SERIAL:Delete(ply) end)
  101.  
  102. hook.Add("PlayerDisconnect","PersistByLeave",function(ply) SERIAL:PersistData(ply) end)
  103.  
  104. if SERVER then
  105.  
  106. hook.Add("Think","CheckForBackUps",function()
  107. for k,v in pairs(player.GetAll()) do
  108. if(v.NB == nil) then
  109. v.NB = CurTime() + 500
  110. self.IsBackup = true
  111. SERIAL:PersistData(v)
  112. net.Start("WaitForSave")
  113. net.WriteFloat(4)
  114. net.Send(v)
  115. self.IsBackup = false
  116. end
  117.  
  118. if(v.NB <= CurTime()) then
  119. self.IsBackup = true
  120. SERIAL:PersistData(v)
  121. v.NB = CurTime() + 500
  122. net.Start("WaitForSave")
  123. net.WriteFloat(1)
  124. net.Send(v)
  125. self.IsBackup = false
  126. end
  127. end
  128. end)
  129.  
  130. end
  131.  
  132. hook.Add("PlayerSay","SaveByCommand",function(ply,text)
  133.  
  134. if(text == "!save" or text == "/save") then
  135.  
  136. if((ply.NBB or 0) < CurTime()) then
  137. ply.NBB = CurTime() + 10
  138. SERIAL:PersistData(ply)
  139. net.Start("WaitForSave")
  140. net.WriteFloat(1)
  141. net.Send(ply)
  142. else
  143. net.Start("WaitForSave")
  144. net.WriteFloat(0)
  145. net.Send(ply)
  146. end
  147.  
  148. if(ply.NBB == nil) then
  149. ply.NBB = CurTime() + 10
  150. SERIAL:PersistData(ply)
  151. end
  152.  
  153. return ""
  154.  
  155. end
  156.  
  157. if(text == "!erase" or text == "/erase") then
  158.  
  159. SERIAL:Delete(ply)
  160. net.Start("WaitForSave")
  161. net.WriteFloat(3)
  162. net.Send(ply)
  163.  
  164. return ""
  165.  
  166. end
  167.  
  168. end)
  169.  
  170. SERIAL:Init()
  171.  
  172. if SERVER then
  173. util.AddNetworkString("WaitForSave")
  174. end
  175.  
  176. net.Receive("WaitForSave",function()
  177. local b = net.ReadFloat()
  178. if(b == 0) then
  179. chat.AddText(Color(214,51,14),"[SAVE] ",Color(235,235,235),"You can only save each 10 seconds.")
  180. elseif(b == 1) then
  181. chat.AddText(Color(51,214,14),"[SAVE] ",Color(235,235,235),"Your data has been saved!")
  182. elseif(b == 2) then
  183. chat.AddText(Color(51,14,214),"[SAVE] ",Color(235,235,235),"Your data has been loaded!")
  184. elseif(b == 3) then
  185. chat.AddText(Color(214,51,14),"[SAVE] ",Color(235,235,235),"Your data has been deleted.")
  186. else
  187. MsgC(Color(51,214,14),"[BackUp created]",Color(235,235,235)," You can continue playing.")
  188. end
  189. end)
Advertisement
Add Comment
Please, Sign In to add comment