Advertisement
Guest User

Car Inventory System (11/06) modified - Server

a guest
Jun 10th, 2017
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. require "resources/mysql-async/lib/MySQL"
  2.  
  3. local CARS = {}
  4. local maxCapacity = 20
  5.  
  6. local all_cars = MySQL.Sync.fetchAll("SELECT vehicle_plate AS plate, items.id AS id, items.libelle AS libelle, quantity FROM user_vehicle LEFT JOIN vehicle_inventory ON `user_vehicle`.`vehicle_plate` = `vehicle_inventory`.`plate` LEFT JOIN items ON `vehicle_inventory`.`item` = `items`.`id`")
  7.  
  8. for _, v in ipairs(all_cars) do
  9. CARS[v.plate] = {}
  10. if v.id and v.libelle and v.quantity then
  11. table.insert(CARS[v.plate], v.id, {libelle = v.libelle, quantity = v.quantity})
  12. end
  13. end
  14.  
  15. RegisterServerEvent("playercar:getItems_s")
  16. AddEventHandler("playercar:getItems_s", function()
  17. TriggerEvent('es:getPlayerFromId', source, function(user)
  18. items = {}
  19. local player = user.identifier
  20.  
  21. MySQL.Async.fetchAll("SELECT * FROM user_inventory JOIN items ON `user_inventory`.`item_id` = `items`.`id` WHERE user_id = @username", {
  22. ['@username'] = player
  23. }, function (result)
  24. if (result) then
  25. for _, v in ipairs(result) do
  26. table.insert(items, tonumber(v.item_id), {libelle = v.libelle, quantity = v.quantity})
  27. end
  28. end
  29. TriggerClientEvent("playercar:hoodContent", source, items)
  30. end)
  31. end)
  32. end)
  33.  
  34. RegisterServerEvent("car:getItems")
  35. AddEventHandler("car:getItems", function(plate)
  36. local res = nil
  37. if CARS[plate] then
  38. res = CARS[plate]
  39. end
  40. TriggerClientEvent("car:hoodContent", source, res)
  41. end)
  42.  
  43. RegisterServerEvent("car:receiveItem")
  44. AddEventHandler("car:receiveItem", function(plate, item, lib, quantity)
  45. local limitslots = 0 + quantity
  46. if (limitslots <= maxCapacity) then
  47. if not CARS[plate] then
  48. CARS[plate] = {}
  49. end
  50. add({ item, quantity, plate, lib })
  51. TriggerClientEvent("player:looseItem", source, item, quantity)
  52. end
  53. end)
  54.  
  55. RegisterServerEvent("car:looseItem")
  56. AddEventHandler("car:looseItem", function(plate, item, quantity)
  57. local cItem = CARS[plate][item]
  58. if (cItem.quantity >= quantity) then
  59. delete({ item, quantity, plate })
  60. TriggerClientEvent("player:receiveItem", source, item, quantity)
  61. end
  62. end)
  63.  
  64. AddEventHandler('BuyForVeh', function(name, vehicle, price, plate, primarycolor, secondarycolor, pearlescentcolor, wheelcolor)
  65. CARS[plate] = {}
  66. end)
  67.  
  68. function add(arg)
  69. local itemId = tonumber(arg[1])
  70. local qty = arg[2]
  71. local plate = arg[3]
  72. local lib = arg[4]
  73. local query
  74. local item
  75. if CARS[plate][itemId] then
  76. item = CARS[plate][itemId]
  77. query = "UPDATE vehicle_inventory SET `quantity` = @qty WHERE `plate` = @plate AND `item` = @item"
  78. item.quantity = item.quantity + qty
  79. else
  80. CARS[plate][itemId] = {quantity = qty, libelle = lib}
  81. item = CARS[plate][itemId]
  82. print(CARS[plate][itemId].libelle)
  83. query = "INSERT INTO vehicle_inventory (`quantity`,`plate`,`item`) VALUES (@qty,@plate,@item)"
  84. end
  85. MySQL.Async.execute(query,{ ['@plate'] = plate, ['@qty'] = item.quantity, ['@item'] = itemId })
  86. end
  87.  
  88. function delete(arg)
  89. local itemId = tonumber(arg[1])
  90. local qty = arg[2]
  91. local plate = arg[3]
  92. local item = CARS[plate][itemId]
  93. item.quantity = item.quantity - qty
  94. MySQL.Async.execute("UPDATE vehicle_inventory SET `quantity` = @qty WHERE `plate` = @plate AND `item` = @item",
  95. { ['@plate'] = plate, ['@qty'] = item.quantity, ['@item'] = itemId })
  96. end
  97.  
  98. function getSlots(plate)
  99. local pods = 0
  100. for _, v in pairs(CARS[plate]) do
  101. pods = pods + v.quantity
  102. end
  103. return pods
  104. end
  105.  
  106. -- get's the player id without having to use bugged essentials
  107. function getPlayerID(source)
  108. local identifiers = GetPlayerIdentifiers(source)
  109. local player = getIdentifiant(identifiers)
  110. return player
  111. end
  112.  
  113. -- gets the actual player id unique to the player,
  114. -- independent of whether the player changes their screen name
  115. function getIdentifiant(id)
  116. for _, v in ipairs(id) do
  117. return v
  118. end
  119. end
  120.  
  121. function stringSplit(self, delimiter)
  122. local a = self:Split(delimiter)
  123. local t = {}
  124.  
  125. for i = 0, #a - 1 do
  126. table.insert(t, a[i])
  127. end
  128.  
  129. return t
  130. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement