Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. RegisterServerEvent('eden_garage:debug')
  2. RegisterServerEvent('eden_garage:modifystate')
  3. RegisterServerEvent('eden_garage:pay')
  4. RegisterServerEvent('eden_garage:payhealth')
  5. RegisterServerEvent('eden_garage:logging')
  6.  
  7. ESX = nil
  8.  
  9. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  10.  
  11. -- Vehicle fetch
  12.  
  13. ESX.RegisterServerCallback('eden_garage:getVehicles', function(source, cb)
  14. local _source = source
  15. local xPlayer = ESX.GetPlayerFromId(_source)
  16. local vehicules = {}
  17.  
  18. MySQL.Async.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
  19. for _,v in pairs(data) do
  20. local vehicle = json.decode(v.vehicle)
  21. table.insert(vehicules, {vehicle = vehicle, state = v.state, plate = v.plate})
  22. end
  23. cb(vehicules)
  24. end)
  25. end)
  26.  
  27. -- End vehicle fetch
  28. -- Store & update vehicle properties
  29.  
  30. ESX.RegisterServerCallback('eden_garage:stockv',function(source,cb, vehicleProps)
  31. local isFound = false
  32. local _source = source
  33. local xPlayer = ESX.GetPlayerFromId(_source)
  34. local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
  35. local plate = vehicleProps.plate
  36. print(plate)
  37.  
  38. for _,v in pairs(vehicules) do
  39. if(plate == plate)then
  40. local vehprop = json.encode(vehicleProps)
  41. MySQL.Sync.execute("UPDATE owned_vehicles SET vehicle=@vehprop WHERE plate=@plate",{['@vehprop'] = vehprop, ['@plate'] = plate})
  42. isFound = true
  43. break
  44. end
  45. end
  46. cb(isFound)
  47. end)
  48.  
  49. -- End vehicle store
  50. -- Change state of vehicle
  51.  
  52. AddEventHandler('eden_garage:modifystate', function(plate, state)
  53. local _source = source
  54. local xPlayer = ESX.GetPlayerFromId(_source)
  55. local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
  56. local state = state
  57. print('UPDATING STATE')
  58. print(plate)
  59. for _,v in pairs(vehicules) do
  60. MySQL.Sync.execute("UPDATE owned_vehicles SET state =@state WHERE plate=@plate",{['@state'] = state , ['@plate'] = plate})
  61. break
  62. end
  63. end)
  64.  
  65. -- End state update
  66. -- Get user properties
  67.  
  68. ESX.RegisterServerCallback('eden_garage:getOwnedProperties',function(source, cb)
  69. local _source = source
  70. local xPlayer = ESX.GetPlayerFromId(_source)
  71. local properties = {}
  72.  
  73. MySQL.Async.fetchAll("SELECT * FROM owned_properties WHERE owner=@identifier",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
  74. for _,v in pairs(data) do
  75. table.insert(properties, v.name)
  76. end
  77. cb(properties)
  78. end)
  79. end)
  80.  
  81. -- End user properties
  82. -- Function to recover plates deprecated and removed.
  83. -- Get list of vehicles already out
  84.  
  85. ESX.RegisterServerCallback('eden_garage:getOutVehicles',function(source, cb)
  86. local _source = source
  87. local xPlayer = ESX.GetPlayerFromId(_source)
  88. local vehicules = {}
  89.  
  90. MySQL.Async.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier AND state=false",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
  91. for _,v in pairs(data) do
  92. local vehicle = json.decode(v.vehicle)
  93. table.insert(vehicules, vehicle)
  94. end
  95. cb(vehicules)
  96. end)
  97. end)
  98.  
  99. -- End out list
  100. -- Check player has funds
  101.  
  102. ESX.RegisterServerCallback('eden_garage:checkMoney', function(source, cb)
  103.  
  104. local xPlayer = ESX.GetPlayerFromId(source)
  105.  
  106. if xPlayer.get('money') >= Config.Price then
  107. cb(true)
  108. else
  109. cb(false)
  110. end
  111. end)
  112.  
  113. -- End funds check
  114. -- Withdraw money
  115.  
  116. AddEventHandler('eden_garage:pay', function()
  117.  
  118. local xPlayer = ESX.GetPlayerFromId(source)
  119.  
  120. xPlayer.removeMoney(Config.Price)
  121.  
  122. TriggerClientEvent('esx:showNotification', source, _U('you_paid')..' ' .. Config.Price)
  123.  
  124. end)
  125.  
  126. -- End money withdraw
  127. -- Find player vehicles
  128.  
  129. function getPlayerVehicles(identifier)
  130.  
  131. local vehicles = {}
  132. local data = MySQL.Sync.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = identifier})
  133. for _,v in pairs(data) do
  134. local vehicle = json.decode(v.vehicle)
  135. table.insert(vehicles, {id = v.id, plate = v.plate})
  136. end
  137. return vehicles
  138. end
  139.  
  140. -- End fetch vehicles
  141. -- Debug [not sure how to use this tbh] COMMENTED OUT???????? FOR REPAIR VEHICLE BUG
  142.  
  143. -- AddEventHandler('eden_garage:debug', function(var)
  144. -- print(to_string(var))
  145. -- end)
  146.  
  147. -- function table_print (tt, indent, done)
  148. -- done = done or {}
  149. -- indent = indent or 0
  150. -- if type(tt) == "table" then
  151. -- local sb = {}
  152. -- for key, value in pairs (tt) do
  153. -- table.insert(sb, string.rep (" ", indent)) -- indent it
  154. -- if type (value) == "table" and not done [value] then
  155. -- done [value] = true
  156. -- table.insert(sb, "{\n");
  157. -- table.insert(sb, table_print (value, indent + 2, done))
  158. -- table.insert(sb, string.rep (" ", indent)) -- indent it
  159. -- table.insert(sb, "}\n");
  160. -- elseif "number" == type(key) then
  161. -- table.insert(sb, string.format("\"%s\"\n", tostring(value)))
  162. -- else
  163. -- table.insert(sb, string.format(
  164. -- "%s = \"%s\"\n", tostring (key), tostring(value)))
  165. -- end
  166. -- end
  167. -- return table.concat(sb)
  168. -- else
  169. -- return tt .. "\n"
  170. -- end
  171. -- end
  172.  
  173. function to_string( tbl )
  174. if "nil" == type( tbl ) then
  175. return tostring(nil)
  176. elseif "table" == type( tbl ) then
  177. return table_print(tbl)
  178. elseif "string" == type( tbl ) then
  179. return tbl
  180. else
  181. return tostring(tbl)
  182. end
  183. end
  184.  
  185. -- End debug
  186. -- Return all vehicles to garage (state update) on server restart
  187.  
  188. -- AddEventHandler('onMySQLReady', function()
  189.  
  190. -- MySQL.Sync.execute("UPDATE owned_vehicles SET state=true WHERE state=false", {})
  191.  
  192. -- end)
  193.  
  194. -- End vehicle return
  195. -- Pay vehicle repair cost
  196.  
  197. AddEventHandler('eden_garage:payhealth', function(price)
  198. local xPlayer = ESX.GetPlayerFromId(source)
  199.  
  200. xPlayer.removeMoney(price)
  201.  
  202. TriggerClientEvent('esx:showNotification', source, _U('you_paid')..' ' .. price)
  203.  
  204. end)
  205.  
  206. -- End repair cost
  207. -- Log to the console
  208.  
  209. AddEventHandler('eden_garage:logging', function(logging)
  210. RconPrint(logging)
  211. end)
  212.  
  213. -- End console log
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement