Advertisement
MYTHARP-Merlin

sv_init.lua

Jul 23rd, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.43 KB | None | 0 0
  1. --[[
  2.     pointshop/sv_init.lua
  3.     first file included serverside.
  4. ]]--
  5.  
  6. include "sh_init.lua"
  7. include "sv_player_extension.lua"
  8. include "sv_manifest.lua"
  9.  
  10. -- net hooks
  11.  
  12. net.Receive('PS_BuyItem', function(length, ply)
  13.     ply:PS_BuyItem(net.ReadString())
  14. end)
  15.  
  16. net.Receive('PS_SellItem', function(length, ply)
  17.     ply:PS_SellItem(net.ReadString())
  18. end)
  19.  
  20. net.Receive('PS_EquipItem', function(length, ply)
  21.     ply:PS_EquipItem(net.ReadString())
  22. end)
  23.  
  24. net.Receive('PS_HolsterItem', function(length, ply)
  25.     ply:PS_HolsterItem(net.ReadString())
  26. end)
  27.  
  28. net.Receive('PS_ModifyItem', function(length, ply)
  29.     ply:PS_ModifyItem(net.ReadString(), net.ReadTable())
  30. end)
  31.  
  32. -- player to player
  33.  
  34. net.Receive('PS_SendPoints', function(length, ply)
  35.     local other = net.ReadEntity()
  36.     local points = math.Clamp(net.ReadInt(32), 0, 1000000)
  37.    
  38.     if not PS.Config.CanPlayersGivePoints then return end
  39.     if not points or points == 0 then return end
  40.     if not other or not IsValid(other) or not other:IsPlayer() then return end
  41.     if not ply or not IsValid(ply) or not ply:IsPlayer() then return end
  42.     if not ply:PS_HasPoints(points) then
  43.         ply:PS_Notify("You can't afford to give away ", points, " of your ", PS.Config.PointsName, ".")
  44.         return
  45.     end
  46.  
  47.     ply.PS_LastGavePoints = ply.PS_LastGavePoints or 0
  48.     if ply.PS_LastGavePoints + 5 > CurTime() then
  49.         ply:PS_Notify("Slow down! You can't give away points that fast.")
  50.         return
  51.     end
  52.  
  53.     ply:PS_TakePoints(points)
  54.     ply:PS_Notify("You gave ", other:Nick(), " ", points, " of your ", PS.Config.PointsName, ".")
  55.        
  56.     other:PS_GivePoints(points)
  57.     other:PS_Notify(ply:Nick(), " gave you ", points, " of their ", PS.Config.PointsName, ".")
  58.  
  59.     ply.PS_LastGavePoints = CurTime()
  60. end)
  61.  
  62. -- admin points
  63.  
  64. net.Receive('PS_GivePoints', function(length, ply)
  65.     local other = net.ReadEntity()
  66.     local points = net.ReadInt(32)
  67.    
  68.     if not PS.Config.AdminCanAccessAdminTab and not PS.Config.SuperAdminCanAccessAdminTab then return end
  69.    
  70.     local admin_allowed = PS.Config.AdminCanAccessAdminTab and ply:IsAdmin()
  71.     local super_admin_allowed = PS.Config.SuperAdminCanAccessAdminTab and ply:IsSuperAdmin()
  72.    
  73.     if (admin_allowed or super_admin_allowed) and other and points and IsValid(other) and other:IsPlayer() then
  74.         other:PS_GivePoints(points)
  75.         other:PS_Notify(ply:Nick(), ' gave you ', points, ' ', PS.Config.PointsName, '.')
  76.     end
  77. end)
  78.  
  79. net.Receive('PS_TakePoints', function(length, ply)
  80.     local other = net.ReadEntity()
  81.     local points = net.ReadInt(32)
  82.    
  83.     if not PS.Config.AdminCanAccessAdminTab and not PS.Config.SuperAdminCanAccessAdminTab then return end
  84.    
  85.     local admin_allowed = PS.Config.AdminCanAccessAdminTab and ply:IsAdmin()
  86.     local super_admin_allowed = PS.Config.SuperAdminCanAccessAdminTab and ply:IsSuperAdmin()
  87.    
  88.     if (admin_allowed or super_admin_allowed) and other and points and IsValid(other) and other:IsPlayer() then
  89.         other:PS_TakePoints(points)
  90.         other:PS_Notify(ply:Nick(), ' took ', points, ' ', PS.Config.PointsName, ' from you.')
  91.     end
  92. end)
  93.  
  94. net.Receive('PS_SetPoints', function(length, ply)
  95.     local other = net.ReadEntity()
  96.     local points = net.ReadInt(32)
  97.    
  98.     if not PS.Config.AdminCanAccessAdminTab and not PS.Config.SuperAdminCanAccessAdminTab then return end
  99.    
  100.     local admin_allowed = PS.Config.AdminCanAccessAdminTab and ply:IsAdmin()
  101.     local super_admin_allowed = PS.Config.SuperAdminCanAccessAdminTab and ply:IsSuperAdmin()
  102.    
  103.     if (admin_allowed or super_admin_allowed) and other and points and IsValid(other) and other:IsPlayer() then
  104.         other:PS_SetPoints(points)
  105.         other:PS_Notify(ply:Nick(), ' set your ', PS.Config.PointsName, ' to ', points, '.')
  106.     end
  107. end)
  108.  
  109. -- admin items
  110.  
  111. net.Receive('PS_GiveItem', function(length, ply)
  112.     local other = net.ReadEntity()
  113.     local item_id = net.ReadString()
  114.    
  115.     if not PS.Config.AdminCanAccessAdminTab and not PS.Config.SuperAdminCanAccessAdminTab then return end
  116.    
  117.     local admin_allowed = PS.Config.AdminCanAccessAdminTab and ply:IsAdmin()
  118.     local super_admin_allowed = PS.Config.SuperAdminCanAccessAdminTab and ply:IsSuperAdmin()
  119.    
  120.     if (admin_allowed or super_admin_allowed) and other and item_id and PS.Items[item_id] and IsValid(other) and other:IsPlayer() and not other:PS_HasItem(item_id) then
  121.         other:PS_GiveItem(item_id)
  122.     end
  123. end)
  124.  
  125. net.Receive('PS_TakeItem', function(length, ply)
  126.     local other = net.ReadEntity()
  127.     local item_id = net.ReadString()
  128.    
  129.     if not PS.Config.AdminCanAccessAdminTab and not PS.Config.SuperAdminCanAccessAdminTab then return end
  130.    
  131.     local admin_allowed = PS.Config.AdminCanAccessAdminTab and ply:IsAdmin()
  132.     local super_admin_allowed = PS.Config.SuperAdminCanAccessAdminTab and ply:IsSuperAdmin()
  133.    
  134.     if (admin_allowed or super_admin_allowed) and other and item_id and PS.Items[item_id] and IsValid(other) and other:IsPlayer() and other:PS_HasItem(item_id) then
  135.         -- holster it first without notificaiton
  136.         other.PS_Items[item_id].Equipped = false
  137.    
  138.         local ITEM = PS.Items[item_id]
  139.         ITEM:OnHolster(other)
  140.         other:PS_TakeItem(item_id)
  141.     end
  142. end)
  143.  
  144. -- hooks
  145.  
  146. -- Ability to use any button to open pointshop.
  147. hook.Add("PlayerButtonDown", "PS_ToggleKey", function(ply, btn)
  148.     if PS.Config.ShopKey and PS.Config.ShopKey ~= "" then
  149.         local psButton = _G["KEY_" .. string.upper(PS.Config.ShopKey)]
  150.         if psButton and psButton == btn then
  151.             ply:PS_ToggleMenu()
  152.         end
  153.     end
  154. end)
  155.  
  156. hook.Add('PlayerSpawn', 'PS_PlayerSpawn', function(ply) ply:PS_PlayerSpawn() end)
  157. hook.Add('PlayerDeath', 'PS_PlayerDeath', function(ply) ply:PS_PlayerDeath() end)
  158. hook.Add('PlayerInitialSpawn', 'PS_PlayerInitialSpawn', function(ply) ply:PS_PlayerInitialSpawn() end)
  159. hook.Add('PlayerDisconnected', 'PS_PlayerDisconnected', function(ply) ply:PS_PlayerDisconnected() end)
  160.  
  161. hook.Add('PlayerSay', 'PS_PlayerSay', function(ply, text)
  162.     if string.len(PS.Config.ShopChatCommand) > 0 then
  163.         if string.sub(text, 0, string.len(PS.Config.ShopChatCommand)) == PS.Config.ShopChatCommand then
  164.             ply:PS_ToggleMenu()
  165.             return ''
  166.         end
  167.     end
  168. end)
  169.  
  170. -- ugly networked strings
  171.  
  172. util.AddNetworkString('PS_Items')
  173. util.AddNetworkString('PS_Points')
  174. util.AddNetworkString('PS_BuyItem')
  175. util.AddNetworkString('PS_SellItem')
  176. util.AddNetworkString('PS_EquipItem')
  177. util.AddNetworkString('PS_HolsterItem')
  178. util.AddNetworkString('PS_ModifyItem')
  179. util.AddNetworkString('PS_SendPoints')
  180. util.AddNetworkString('PS_GivePoints')
  181. util.AddNetworkString('PS_TakePoints')
  182. util.AddNetworkString('PS_SetPoints')
  183. util.AddNetworkString('PS_GiveItem')
  184. util.AddNetworkString('PS_TakeItem')
  185. util.AddNetworkString('PS_AddClientsideModel')
  186. util.AddNetworkString('PS_RemoveClientsideModel')
  187. util.AddNetworkString('PS_SendClientsideModels')
  188. util.AddNetworkString('PS_SendNotification')
  189. util.AddNetworkString('PS_ToggleMenu')
  190.  
  191. -- console commands
  192.  
  193. concommand.Add(PS.Config.ShopCommand, function(ply, cmd, args)
  194.     ply:PS_ToggleMenu()
  195. end)
  196.  
  197. concommand.Add('ps_clear_points', function(ply, cmd, args)
  198.     if IsValid(ply) then return end -- only allowed from server console
  199.    
  200.     for _, ply in pairs(player.GetAll()) do
  201.         ply:PS_SetPoints(0)
  202.     end
  203.    
  204.     sql.Query("DELETE FROM playerpdata WHERE infoid LIKE '%PS_Points%'")
  205. end)
  206.  
  207. concommand.Add('ps_clear_items', function(ply, cmd, args)
  208.     if IsValid(ply) then return end -- only allowed from server console
  209.    
  210.     for _, ply in pairs(player.GetAll()) do
  211.         ply.PS_Items = {}
  212.         ply:PS_SendItems()
  213.     end
  214.    
  215.     sql.Query("DELETE FROM playerpdata WHERE infoid LIKE '%PS_Items%'")
  216. end)
  217.  
  218. -- version checker
  219.  
  220. PS.CurrentBuild = 0
  221. PS.LatestBuild = 0
  222. PS.BuildOutdated = false
  223.  
  224. local function CompareVersions()
  225.     if PS.CurrentBuild < PS.LatestBuild then
  226.         MsgN('PointShop is out of date!')
  227.         MsgN('Local version: ' .. PS.CurrentBuild .. ', Latest version: ' .. PS.LatestBuild)
  228.  
  229.         PS.BuildOutdated = true
  230.     else
  231.         MsgN('PointShop is on the latest version.')
  232.     end
  233. end
  234.  
  235. function PS:CheckVersion()
  236.     if file.Exists('data/pointshop_build.txt', 'GAME') then
  237.         PS.CurrentBuild = tonumber(file.Read('data/pointshop_build.txt', 'GAME')) or 0
  238.     end
  239.  
  240.     local url = self.Config.Branch .. 'data/pointshop_build.txt'
  241.     http.Fetch( url,
  242.         function( content ) -- onSuccess
  243.             PS.LatestBuild = tonumber( content ) or 0
  244.             CompareVersions()
  245.         end,
  246.         function(failCode) -- onFailure
  247.             MsgN('PointShop couldn\'t check version.')
  248.             MsgN(url, ' returned ', failCode)
  249.         end
  250.     )
  251. end
  252.  
  253. -- data providers
  254.  
  255. function PS:LoadDataProvider()
  256.     local path = "pointshop/providers/" .. self.Config.DataProvider .. ".lua"
  257.     if not file.Exists(path, "LUA") then
  258.         error("Pointshop data provider not found. " .. path)
  259.     end
  260.  
  261.     PROVIDER = {}
  262.     PROVIDER.__index = {}
  263.     PROVIDER.ID = self.Config.DataProvider
  264.        
  265.     include(path)
  266.        
  267.     self.DataProvider = PROVIDER
  268.     PROVIDER = nil
  269. end
  270.  
  271. function PS:GetPlayerData(ply, callback)
  272.     self.DataProvider:GetData(ply, function(points, items)
  273.         callback(PS:ValidatePoints(tonumber(points)), PS:ValidateItems(items))
  274.     end)
  275. end
  276.  
  277. function PS:SetPlayerData(ply, points, items)
  278.     self.DataProvider:SetData(ply, points, items)
  279. end
  280.  
  281. function PS:SetPlayerPoints(ply, points)
  282.     self.DataProvider:SetPoints(ply, points)
  283. end
  284.  
  285. function PS:GivePlayerPoints(ply, points)
  286.     self.DataProvider:GivePoints(ply, points, items)
  287. end
  288.  
  289. function PS:TakePlayerPoints(ply, points)
  290.     self.DataProvider:TakePoints(ply, points)
  291. end
  292.  
  293. function PS:SavePlayerItem(ply, item_id, data)
  294.     self.DataProvider:SaveItem(ply, item_id, data)
  295. end
  296.  
  297. function PS:GivePlayerItem(ply, item_id, data)
  298.     self.DataProvider:GiveItem(ply, item_id, data)
  299. end
  300.  
  301. function PS:TakePlayerItem(ply, item_id)
  302.     self.DataProvider:TakeItem(ply, item_id)
  303. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement