daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ESX              = nil
  2.  
  3. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  4.  
  5. AddEventHandler('esx:playerLoaded', function(source)
  6.  
  7.   local xPlayer = ESX.GetPlayerFromId(source)
  8.    
  9. end)
  10.  
  11.  
  12. ESX.RegisterServerCallback('netr_garages:getOwnedVehicles', function (source, cb)
  13.   local xPlayer = ESX.GetPlayerFromId(source)
  14.  
  15.   MySQL.Async.fetchAll(
  16.     'SELECT * FROM owned_vehicles WHERE owner = @owner',
  17.     { ['@owner'] = xPlayer.identifier },
  18.     function (result)
  19.       local vehicles = {}
  20.  
  21.       for i=1, #result, 1 do
  22.         local vehicleData = json.decode(result[i].vehicle)
  23.         table.insert(vehicles, vehicleData)
  24.       end
  25.  
  26.       cb(vehicles)
  27.     end
  28.   )
  29. end)
  30.  
  31. ESX.RegisterServerCallback('netr_garages:checkIfVehicleIsOwned', function (source, cb, plate)
  32.  
  33.   local xPlayer = ESX.GetPlayerFromId(source)
  34.   local found = nil
  35.   local vehicleData = nil
  36.  
  37.   MySQL.Async.fetchAll(
  38.     'SELECT * FROM owned_vehicles WHERE owner = @owner',
  39.     { ['@owner'] = xPlayer.identifier },
  40.     function (result)
  41.       local vehicles = {}
  42.  
  43.       for i=1, #result, 1 do
  44.         vehicleData = json.decode(result[i].vehicle)
  45.         if vehicleData.plate == plate then
  46.           found = true
  47.           cb(vehicleData)
  48.           break
  49.         end
  50.       end
  51.  
  52.       if not found then
  53.         cb(nil)
  54.       end
  55.     end
  56.   )
  57. end)
  58.  
  59. RegisterServerEvent('netr_garages:updateOwnedVehicle')
  60. AddEventHandler('netr_garages:updateOwnedVehicle', function(vehicleProps)
  61.  
  62.     local _source = source
  63.     local xPlayer = ESX.GetPlayerFromId(source)
  64.  
  65.     MySQL.Async.fetchAll(
  66.         'SELECT * FROM owned_vehicles WHERE owner = @owner',
  67.         {
  68.             ['@owner'] = xPlayer.identifier
  69.         },
  70.         function(result)
  71.  
  72.             local foundVehicleId = nil
  73.  
  74.             for i=1, #result, 1 do
  75.                
  76.                 local vehicle = json.decode(result[i].vehicle)
  77.                
  78.                 if vehicle.plate == vehicleProps.plate then
  79.                     foundVehicleId = result[i].id
  80.                     break
  81.                 end
  82.  
  83.             end
  84.  
  85.             if foundVehicleId ~= nil then
  86.  
  87.                 MySQL.Async.execute(
  88.                     'UPDATE owned_vehicles SET vehicle = @vehicle WHERE id = @id',
  89.                     {
  90.                         ['@vehicle'] = json.encode(vehicleProps),
  91.                         ['@id']      = foundVehicleId
  92.                     }
  93.                 )
  94.  
  95.             end
  96.  
  97.         end
  98.     )
  99.  
  100.  end)
  101.  
  102.  
  103. RegisterServerEvent('netr_garages:addCarToParking')
  104. AddEventHandler('netr_garages:addCarToParking', function(vehicleProps)
  105.  
  106.     local xPlayer = ESX.GetPlayerFromId(source)
  107.  
  108.     if vehicleProps ~= nil then
  109.  
  110.         MySQL.Async.execute(
  111.             'INSERT INTO `user_parkings` (`identifier`, `plate`, `vehicle`) VALUES (@identifier, @plate, @vehicle)',
  112.             {
  113.                 ['@identifier']   = xPlayer.identifier,
  114.         ['@plate']        = vehicleProps.plate,
  115.                 ['@vehicle']      = json.encode(vehicleProps)
  116.             }, function(rowsChanged)
  117.                 TriggerClientEvent('esx:showNotification', xPlayer.source, _U('veh_stored'))
  118.             end
  119.         )
  120.  
  121.     end
  122.  
  123. end)
  124.  
  125. RegisterServerEvent('netr_garages:removeCarFromParking')
  126. AddEventHandler('netr_garages:removeCarFromParking', function(plate)
  127.  
  128.     local xPlayer = ESX.GetPlayerFromId(source)
  129.  
  130.     if plate ~= nil then
  131.  
  132.         MySQL.Async.execute(
  133.             'DELETE FROM `user_parkings` WHERE `identifier` = @identifier AND `plate` = @plate',
  134.             {
  135.                 ['@identifier'] = xPlayer.identifier,
  136.         ['@plate'] = plate
  137.             }, function(rowsChanged)
  138.                 TriggerClientEvent('esx:showNotification', xPlayer.source, _U('veh_released'))
  139.             end
  140.         )
  141.  
  142.     end
  143.  
  144. end)
  145.  
  146.  
  147. RegisterServerEvent('netr_garages:getCustomPlate')
  148. AddEventHandler('netr_garages:getCustomPlate', function(plate)
  149.  
  150.     local xPlayer = ESX.GetPlayerFromId(source)
  151.  
  152.     if plate ~= nil then
  153.  
  154.     MySQL.Async.fetchAll(
  155.       'SELECT `plate_name` FROM custom_plate WHERE original_plate = @plate',
  156.       {
  157.         ['@plate'] = plate
  158.       },
  159.       function(result)
  160.         return result[1].plate_name
  161.     end)
  162.  
  163.     end
  164.  
  165. end)
  166.  
  167.  
  168. ESX.RegisterServerCallback('netr_garages:getVehiclesInGarage', function(source, cb)
  169.  
  170.     local xPlayer = ESX.GetPlayerFromId(source)
  171.  
  172.     MySQL.Async.fetchAll(
  173.         'SELECT * FROM `user_parkings` WHERE `identifier` = @identifier',
  174.         {
  175.             ['@identifier'] = xPlayer.identifier
  176.         },
  177.         function(result)
  178.  
  179.             local vehicles = {}
  180.  
  181.             for i=1, #result, 1 do
  182.         local vehicleData = json.decode(result[i].vehicle)
  183.         table.insert(vehicles, vehicleData)
  184.             end
  185.  
  186.             cb(vehicles)
  187.  
  188.         end
  189.     )
  190.  
  191. end)
  192.  
  193. --[[ runs everytime the server is restarted]]
  194. --[[
  195.  
  196.   adds all user owned cars into the parking garage table so people can pull their cars out
  197.  
  198. ]]
  199. function parkAllOwnedVehicles()
  200.  
  201.   MySQL.ready(function ()
  202.  
  203.     MySQL.Sync.execute(
  204.       'DELETE FROM `user_parkings`',
  205.       {
  206.       }, function(rowsChanged)
  207.       end
  208.     )
  209.  
  210.     print('deleteing vehs')
  211.  
  212.     local result = MySQL.Sync.fetchAll(
  213.       'SELECT * FROM owned_vehicles',
  214.       {})
  215.  
  216.     local foundVehicleId = nil
  217.  
  218.     for i=1, #result, 1 do
  219.    
  220.       local vehicle = result[i].vehicle
  221.       local identifier = result[i].owner
  222.  
  223.       MySQL.Sync.execute(
  224.         'INSERT INTO `user_parkings` (`identifier`, `plate`, `vehicle`) VALUES (@identifier, @plate, @vehicle)',
  225.         {
  226.           ['@identifier'] = identifier,
  227.           ['@plate'] = json.decode(vehicle).plate,
  228.           ['@vehicle']     = vehicle
  229.         })
  230.  
  231.     end
  232.    
  233.   end)
  234.  
  235.  
  236. end
  237.  
  238. parkAllOwnedVehicles()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top