Facebook
From server.lua, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 521
  1. RegisterServerEvent('eden_garage:debug')
  2. RegisterServerEvent('eden_garage:modifystate')
  3. RegisterServerEvent('eden_garage:pay')
  4.  
  5.  
  6. ESX                = nil
  7.  
  8. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  9.  
  10.  
  11. --Recupere les véhicules
  12. ESX.RegisterServerCallback('eden_garage:getVehicles', function(source, cb)
  13.         local _source = source
  14.         local xPlayer = ESX.GetPlayerFromId(_source)
  15.         local vehicules = {}
  16.  
  17.         MySQL.Async.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
  18.                 for _,v in pairs(data) do
  19.                         local vehicle = json.decode(v.vehicle)
  20.                         table.insert(vehicules, {vehicle = vehicle, state = v.state})
  21.                 end
  22.                 cb(vehicules)
  23.         end)
  24. end)
  25. -- Fin --Recupere les véhicules
  26.  
  27. --Stock les véhicules
  28. ESX.RegisterServerCallback('eden_garage:stockv',function(source,cb, vehicleProps)
  29.         local isFound = false
  30.         local _source = source
  31.         local xPlayer = ESX.GetPlayerFromId(_source)
  32.         local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
  33.         local plate = vehicleProps.plate
  34.  
  35.        
  36.                 for _,v in pairs(vehicules) do
  37.                         if(plate == v.plate)then
  38.                                 local idveh = v.id
  39.                                 local vehprop = json.encode(vehicleProps)
  40.                                 MySQL.Sync.execute("UPDATE owned_vehicles SET vehicle =@vehprop WHERE id=@id",{['@vehprop'] = vehprop, ['@id'] = v.id})
  41.                                 isFound = true
  42.                                 break
  43.                         end            
  44.                 end
  45.                 cb(isFound)
  46. end)
  47.  
  48.  
  49. --Fin stock les vehicules
  50.  
  51. --Change le state du véhicule
  52.  
  53. AddEventHandler('eden_garage:modifystate', function(vehicleProps, state)
  54.         local _source = source
  55.         local xPlayer = ESX.GetPlayerFromId(_source)
  56.         local vehicules = getPlayerVehicles(xPlayer.getIdentifier())
  57.         local plate = vehicleProps.plate
  58.         local state = state
  59.  
  60.         for _,v in pairs(vehicules) do
  61.                 if(plate == v.plate)then
  62.                         local idveh = v.id
  63.                         MySQL.Sync.execute("UPDATE owned_vehicles SET state =@state WHERE id=@id",{['@state'] = state , ['@id'] = v.id})
  64.                         break
  65.                 end            
  66.         end
  67. end)   
  68.  
  69.  
  70.  
  71. --Fin change le state du véhicule
  72.  
  73. --Fonction qui récupere les plates
  74.  
  75. -- Fin Fonction qui récupere les plates
  76.  
  77. ESX.RegisterServerCallback('eden_garage:getOutVehicles',function(source, cb)   
  78.         local _source = source
  79.         local xPlayer = ESX.GetPlayerFromId(_source)
  80.         local vehicules = {}
  81.  
  82.         MySQL.Async.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier AND state=false",{['@identifier'] = xPlayer.getIdentifier()}, function(data)
  83.                 for _,v in pairs(data) do
  84.                         local vehicle = json.decode(v.vehicle)
  85.                         table.insert(vehicules, vehicle)
  86.                 end
  87.                 cb(vehicules)
  88.         end)
  89. end)
  90.  
  91. --Foonction qui check l'argent
  92. ESX.RegisterServerCallback('eden_garage:checkMoney', function(source, cb)
  93.  
  94.         local xPlayer = ESX.GetPlayerFromId(source)
  95.  
  96.         if xPlayer.get('money') >= Config.Price then
  97.                 cb(true)
  98.         else
  99.                 cb(false)
  100.         end
  101.  
  102. end)
  103. --Fin Foonction qui check l'argent
  104.  
  105. --fonction qui retire argent
  106.  
  107. AddEventHandler('eden_garage:pay', function()
  108.  
  109.         local xPlayer = ESX.GetPlayerFromId(source)
  110.  
  111.         xPlayer.removeMoney(Config.Price)
  112.  
  113.         TriggerClientEvent('esx:showNotification', source, 'Vous avez payĂ© ' .. Config.Price)
  114.  
  115. end)
  116. --Fin fonction qui retire argent
  117.  
  118.  
  119. --Recupere les vehicules
  120. function getPlayerVehicles(identifier)
  121.        
  122.         local vehicles = {}
  123.         local data = MySQL.Sync.fetchAll("SELECT * FROM owned_vehicles WHERE owner=@identifier",{['@identifier'] = identifier})
  124.         for _,v in pairs(data) do
  125.                 local vehicle = json.decode(v.vehicle)
  126.                 table.insert(vehicles, {id = v.id, plate = vehicle.plate})
  127.         end
  128.         return vehicles
  129. end
  130. --Fin Recupere les vehicules
  131.  
  132. --Debug
  133. AddEventHandler('eden_garage:debug', function(var)
  134.         print(to_string(var))
  135. end)
  136.  
  137. function table_print (tt, indent, done)
  138.   done = done or {}
  139.   indent = indent or 0
  140.   if type(tt) == "table" then
  141.     local sb = {}
  142.     for key, value in pairs (tt) do
  143.       table.insert(sb, string.rep (" ", indent)) -- indent it
  144.       if type (value) == "table" and not done [value] then
  145.         done [value] = true
  146.         table.insert(sb, "{\n");
  147.         table.insert(sb, table_print (value, indent + 2, done))
  148.         table.insert(sb, string.rep (" ", indent)) -- indent it
  149.         table.insert(sb, "}\n");
  150.       elseif "number" == type(key) then
  151.         table.insert(sb, string.format("\"%s\"\n", tostring(value)))
  152.       else
  153.         table.insert(sb, string.format(
  154.             "%s = \"%s\"\n", tostring (key), tostring(value)))
  155.        end
  156.     end
  157.     return table.concat(sb)
  158.   else
  159.     return tt .. "\n"
  160.   end
  161. end
  162.  
  163. function to_string( tbl )
  164.     if  "nil"       == type( tbl ) then
  165.         return tostring(nil)
  166.     elseif  "table" == type( tbl ) then
  167.         return table_print(tbl)
  168.     elseif  "string" == type( tbl ) then
  169.         return tbl
  170.     else
  171.         return tostring(tbl)
  172.     end
  173. end
  174. --Fin Debug
  175.  
  176.  
  177. -- Fonction qui change les etats sorti en rentré lors d'un restart
  178. AddEventHandler('onMySQLReady', function()
  179.  
  180.         MySQL.Sync.execute("UPDATE owned_vehicles SET state=true WHERE state=false", {})
  181.  
  182. end)
  183. -- Fin Fonction qui change les etats sorti en rentré lors d'un restart