lukerogue

Untitled

Apr 21st, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. MODULE.Name = "Money"
  2. MODULE.Author = "Q2F2, Ghosty and Tenrys"
  3.  
  4. local tag = "BaseWars.Money"
  5. local PLAYER = debug.getregistry().Player
  6.  
  7. local function isPlayer(ply)
  8.  
  9. return (IsValid(ply) and ply:IsPlayer())
  10.  
  11. end
  12.  
  13. function MODULE:GetMoney(ply)
  14.  
  15. return tonumber(ply:GetNWString(tag)) or 0
  16.  
  17. end
  18. PLAYER.GetMoney = Curry(MODULE.GetMoney)
  19.  
  20. if SERVER then
  21.  
  22. function MODULE:InitMoney(ply)
  23.  
  24. BaseWars.MySQL.InitPlayer(ply, "money", tostring(BaseWars.Config.StartMoney))
  25.  
  26. end
  27. PLAYER.InitMoney = Curry(MODULE.InitMoney)
  28.  
  29. for k, v in next,player.GetAll() do
  30.  
  31. MODULE:InitMoney(v)
  32.  
  33. end
  34.  
  35. function MODULE:SaveMoney(ply, amount)
  36.  
  37. BaseWars.MySQL.SaveVar(ply, "money", amount or self:GetMoney(ply))
  38.  
  39. end
  40. PLAYER.SaveMoney = Curry(MODULE.SaveMoney)
  41.  
  42. function MODULE:LoadMoney(ply)
  43.  
  44. self:InitMoney(ply)
  45.  
  46. BaseWars.MySQL.LoadVar(ply, "money", function(ply, var, val)
  47. if not IsValid(ply) then return end
  48.  
  49. ply:SetNWString(tag, tostring(val))
  50. end)
  51.  
  52. end
  53. PLAYER.LoadMoney = Curry(MODULE.LoadMoney)
  54.  
  55. function MODULE:SetMoney(ply, amount)
  56. local amount = tonumber(amount)
  57.  
  58. if not isnumber(amount) or amount < 0 then amount = 0 end
  59. if amount ~= amount then amount = 0 end
  60.  
  61. amount = math.Round(amount)
  62. self:SaveMoney(ply, amount)
  63.  
  64. ply:SetNWString(tag, tostring(amount))
  65.  
  66. end
  67. PLAYER.SetMoney = Curry(MODULE.SetMoney)
  68.  
  69. function MODULE:GiveMoney(ply, amount)
  70.  
  71. self:SetMoney(ply, self:GetMoney(ply) + amount)
  72.  
  73. end
  74. PLAYER.GiveMoney = Curry(MODULE.GiveMoney)
  75.  
  76. function MODULE:TakeMoney(ply, amount)
  77.  
  78. self:SetMoney(ply, self:GetMoney(ply) - amount)
  79.  
  80. end
  81. PLAYER.TakeMoney = Curry(MODULE.TakeMoney)
  82.  
  83. function MODULE:TransferMoney(ply1, amount, ply2)
  84.  
  85. self:TakeMoney(ply1, amount)
  86. self:GiveMoney(ply2, amount)
  87.  
  88. end
  89. PLAYER.TransferMoney = Curry(MODULE.TransferMoney)
  90.  
  91. hook.Add("LoadData", tag .. ".Load", Curry(MODULE.LoadMoney))
  92. hook.Add("PlayerDisconnected", tag .. ".Save", Curry(MODULE.SaveMoney))
  93.  
  94. end
Add Comment
Please, Sign In to add comment