Facebook
From Blush Tern, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 204
  1. ESX              = nil
  2. local Categories = {}
  3. local Vehicles   = {}
  4.  
  5. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  6.  
  7. TriggerEvent('esx_phone:registerNumber', 'cardealer', _U('dealer_customers'), false, false)
  8. TriggerEvent('esx_society:registerSociety', 'cardealer', _U('car_dealer'), 'society_cardealer', 'society_cardealer', 'society_cardealer', {type = 'private'})
  9.  
  10. Citizen.CreateThread(function()
  11.         local char = Config.PlateLetters
  12.         char = char + Config.PlateNumbers
  13.         if Config.PlateUseSpace then char = char + 1 end
  14.  
  15.         if char > 8 then
  16.                 print(('esx_vehicleshop: ^1WARNING^7 plate character count reached, %s/8 characters.'):format(char))
  17.         end
  18. end)
  19.  
  20. function RemoveOwnedVehicle(plate)
  21.         MySQL.Async.execute('DELETE FROM owned_vehicles WHERE plate = @plate', {
  22.                 ['@plate'] = plate
  23.         })
  24. end
  25.  
  26. MySQL.ready(function()
  27.         Categories     = MySQL.Sync.fetchAll('SELECT * FROM vehicle_categories')
  28.         local vehicles = MySQL.Sync.fetchAll('SELECT * FROM vehicles')
  29.  
  30.         for i=1, #vehicles, 1 do
  31.                 local vehicle = vehicles[i]
  32.  
  33.                 for j=1, #Categories, 1 do
  34.                         if Categories[j].name == vehicle.category then
  35.                                 vehicle.categoryLabel = Categories[j].label
  36.                                 break
  37.                         end
  38.                 end
  39.  
  40.                 table.insert(Vehicles, vehicle)
  41.         end
  42.  
  43.         -- send information after db has loaded, making sure everyone gets vehicle information
  44.         TriggerClientEvent('esx_vehicleshop:sendCategories', -1, Categories)
  45.         TriggerClientEvent('esx_vehicleshop:sendVehicles', -1, Vehicles)
  46. end)
  47.  
  48. RegisterServerEvent('esx_vehicleshop:setVehicleOwned')
  49. AddEventHandler('esx_vehicleshop:setVehicleOwned', function (vehicleProps,vehicleData)
  50.         local _source = source
  51.         local xPlayer = ESX.GetPlayerFromId(_source)
  52.  
  53.         MySQL.Async.execute(
  54.     'INSERT INTO owned_vehicles (vehicle, owner, plate, modelname) VALUES (@vehicle, @owner, @plate, @modelname)',
  55.     {
  56.       ['@vehicle'] = json.encode(vehicleProps),
  57.       ['@owner']   = xPlayer.identifier,
  58.       ['@plate'] = vehicleProps.plate,
  59.       ['@modelname'] = vehicleData.model
  60.     }, function (rowsChanged)
  61.                 TriggerClientEvent('esx:showNotification', _source, _U('vehicle_belongs', vehicleProps.plate))
  62.         end)
  63. end)
  64.  
  65. RegisterServerEvent('esx_vehicleshop:setVehicleOwnedPlayerId')
  66. AddEventHandler('esx_vehicleshop:setVehicleOwnedPlayerId', function (playerId, vehicleProps,vehicleData)
  67.         local xPlayer = ESX.GetPlayerFromId(playerId)
  68.  
  69.         MySQL.Async.execute(
  70.     'INSERT INTO owned_vehicles (vehicle, owner, plate, modelname) VALUES (@vehicle, @owner, @plate, @modelname)',
  71.     {
  72.       ['@vehicle'] = json.encode(vehicleProps),
  73.       ['@owner']   = xPlayer.identifier,
  74.       ['@plate'] = vehicleProps.plate,
  75.       ['@modelname'] = vehicleDatal.model
  76.     }, function (rowsChanged)
  77.                 TriggerClientEvent('esx:showNotification', playerId, _U('vehicle_belongs', vehicleProps.plate))
  78.         end)
  79. end)
  80.  
  81. RegisterServerEvent('esx_vehicleshop:setVehicleOwnedSociety')
  82. AddEventHandler('esx_vehicleshop:setVehicleOwnedSociety', function (society, vehicleProps)
  83.         local _source = source
  84.         local xPlayer = ESX.GetPlayerFromId(_source)
  85.  
  86.         MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)',
  87.         {
  88.                 ['@owner']   = 'society:' .. society,
  89.                 ['@plate']   = vehicleProps.plate,
  90.                 ['@vehicle'] = json.encode(vehicleProps),
  91.         }, function (rowsChanged)
  92.  
  93.         end)
  94. end)
  95.  
  96. RegisterServerEvent('esx_vehicleshop:sellVehicle')
  97. AddEventHandler('esx_vehicleshop:sellVehicle', function (vehicle)
  98.         MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
  99.                 ['@vehicle'] = vehicle
  100.         }, function (result)
  101.                 local id = result[1].id
  102.  
  103.                 MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
  104.                         ['@id'] = id
  105.                 })
  106.         end)
  107. end)
  108.  
  109. RegisterServerEvent('esx_vehicleshop:addToList')
  110. AddEventHandler('esx_vehicleshop:addToList', function(target, model, plate)
  111.         local xPlayer, xTarget = ESX.GetPlayerFromId(source), ESX.GetPlayerFromId(target)
  112.         local dateNow = os.date('%Y-%m-%d %H:%M')
  113.  
  114.         if xPlayer.job.name ~= 'cardealer' then
  115.                 print(('esx_vehicleshop: %s attempted to add a sold vehicle to list!'):format(xPlayer.identifier))
  116.                 return
  117.         end
  118.  
  119.         MySQL.Async.execute('INSERT INTO vehicle_sold (client, model, plate, soldby, date) VALUES (@client, @model, @plate, @soldby, @date)', {
  120.                 ['@client'] = xTarget.getName(),
  121.                 ['@model'] = model,
  122.                 ['@plate'] = plate,
  123.                 ['@soldby'] = xPlayer.getName(),
  124.                 ['@date'] = dateNow
  125.         })
  126. end)
  127.  
  128. ESX.RegisterServerCallback('esx_vehicleshop:getSoldVehicles', function (source, cb)
  129.  
  130.         MySQL.Async.fetchAll('SELECT * FROM vehicle_sold', {}, function(result)
  131.                 cb(result)
  132.         end)
  133. end)
  134.  
  135. RegisterServerEvent('esx_vehicleshop:rentVehicle')
  136. AddEventHandler('esx_vehicleshop:rentVehicle', function (vehicle, plate, playerName, basePrice, rentPrice, target)
  137.         local xPlayer = ESX.GetPlayerFromId(target)
  138.  
  139.         MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
  140.                 ['@vehicle'] = vehicle
  141.         }, function (result)
  142.                 local id    = result[1].id
  143.                 local price = result[1].price
  144.                 local owner = xPlayer.identifier
  145.  
  146.                 MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
  147.                         ['@id'] = id
  148.                 })
  149.  
  150.                 MySQL.Async.execute('INSERT INTO rented_vehicles (vehicle, plate, player_name, base_price, rent_price, owner) VALUES (@vehicle, @plate, @player_name, @base_price, @rent_price, @owner)',
  151.                 {
  152.                         ['@vehicle']     = vehicle,
  153.                         ['@plate']       = plate,
  154.                         ['@player_name'] = playerName,
  155.                         ['@base_price']  = basePrice,
  156.                         ['@rent_price']  = rentPrice,
  157.                         ['@owner']       = owner
  158.                 })
  159.         end)
  160. end)
  161.  
  162. RegisterServerEvent('esx_vehicleshop:getStockItem')
  163. AddEventHandler('esx_vehicleshop:getStockItem', function (itemName, count)
  164.         local _source = source
  165.         local xPlayer = ESX.GetPlayerFromId(_source)
  166.         local sourceItem = xPlayer.getInventoryItem(itemName)
  167.  
  168.         TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function (inventory)
  169.                 local item = inventory.getItem(itemName)
  170.  
  171.                 -- is there enough in the society?
  172.                 if count > 0 and item.count >= count then
  173.  
  174.                         -- can the player carry the said amount of x item?
  175.                         if sourceItem.limit ~= -1 and (sourceItem.count + count) > sourceItem.limit then
  176.                                 TriggerClientEvent('esx:showNotification', _source, _U('player_cannot_hold'))
  177.                         else
  178.                                 inventory.removeItem(itemName, count)
  179.                                 xPlayer.addInventoryItem(itemName, count)
  180.                                 TriggerClientEvent('esx:showNotification', _source, _U('have_withdrawn', count, item.label))
  181.                         end
  182.                 else
  183.                         TriggerClientEvent('esx:showNotification', _source, _U('not_enough_in_society'))
  184.                 end
  185.         end)
  186. end)
  187.  
  188. RegisterServerEvent('esx_vehicleshop:putStockItems')
  189. AddEventHandler('esx_vehicleshop:putStockItems', function (itemName, count)
  190.         local _source = source
  191.         local xPlayer = ESX.GetPlayerFromId(_source)
  192.  
  193.         TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function (inventory)
  194.                 local item = inventory.getItem(itemName)
  195.  
  196.                 if item.count >= 0 then
  197.                         xPlayer.removeInventoryItem(itemName, count)
  198.                         inventory.addItem(itemName, count)
  199.                         TriggerClientEvent('esx:showNotification', _source, _U('have_deposited', count, item.label))
  200.                 else
  201.                         TriggerClientEvent('esx:showNotification', _source, _U('invalid_amount'))
  202.                 end
  203.         end)
  204. end)
  205.  
  206. ESX.RegisterServerCallback('esx_vehicleshop:getCategories', function (source, cb)
  207.         cb(Categories)
  208. end)
  209.  
  210. ESX.RegisterServerCallback('esx_vehicleshop:getVehicles', function (source, cb)
  211.         cb(Vehicles)
  212. end)
  213.  
  214. ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function (source, cb, vehicleModel)
  215.         local xPlayer     = ESX.GetPlayerFromId(source)
  216.         local vehicleData = nil
  217.  
  218.         for i=1, #Vehicles, 1 do
  219.                 if Vehicles[i].model == vehicleModel then
  220.                         vehicleData = Vehicles[i]
  221.                         break
  222.                 end
  223.         end
  224.  
  225.         if xPlayer.getMoney() >= vehicleData.price then
  226.                 xPlayer.removeMoney(vehicleData.price)
  227.                 cb(true)
  228.         else
  229.                 cb(false)
  230.         end
  231. end)
  232.  
  233. ESX.RegisterServerCallback('esx_vehicleshop:buyVehicleSociety', function (source, cb, society, vehicleModel)
  234.         local vehicleData = nil
  235.  
  236.         for i=1, #Vehicles, 1 do
  237.                 if Vehicles[i].model == vehicleModel then
  238.                         vehicleData = Vehicles[i]
  239.                         break
  240.                 end
  241.         end
  242.  
  243.         TriggerEvent('esx_addonaccount:getSharedAccount', 'society_' .. society, function (account)
  244.                 if account.money >= vehicleData.price then
  245.                         account.removeMoney(vehicleData.price)
  246.  
  247.                         MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', {
  248.                                 ['@vehicle'] = vehicleData.model,
  249.                                 ['@price']   = vehicleData.price
  250.                         }, function(rowsChanged)
  251.                                 cb(true)
  252.                         end)
  253.  
  254.                 else
  255.                         cb(false)
  256.                 end
  257.         end)
  258. end)
  259.  
  260. ESX.RegisterServerCallback('esx_vehicleshop:getCommercialVehicles', function (source, cb)
  261.         MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles ORDER BY vehicle ASC', {}, function (result)
  262.                 local vehicles = {}
  263.  
  264.                 for i=1, #result, 1 do
  265.                         table.insert(vehicles, {
  266.                                 name  = result[i].vehicle,
  267.                                 price = result[i].price
  268.                         })
  269.                 end
  270.  
  271.                 cb(vehicles)
  272.         end)
  273. end)
  274.  
  275.  
  276. RegisterServerEvent('esx_vehicleshop:returnProvider')
  277. AddEventHandler('esx_vehicleshop:returnProvider', function(vehicleModel)
  278.         local _source = source
  279.  
  280.         MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
  281.                 ['@vehicle'] = vehicleModel
  282.         }, function (result)
  283.  
  284.                 if result[1] then
  285.                         local id    = result[1].id
  286.                         local price = ESX.Math.Round(result[1].price * 0.75)
  287.  
  288.                         TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account)
  289.                                 account.addMoney(price)
  290.                         end)
  291.  
  292.                         MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
  293.                                 ['@id'] = id
  294.                         })
  295.  
  296.                         TriggerClientEvent('esx:showNotification', _source, _U('vehicle_sold_for', vehicleModel, ESX.Math.GroupDigits(price)))
  297.                 else
  298.  
  299.                         print(('esx_vehicleshop: %s attempted selling an invalid vehicle!'):format(GetPlayerIdentifiers(_source)[1]))
  300.                 end
  301.  
  302.         end)
  303. end)
  304.  
  305. ESX.RegisterServerCallback('esx_vehicleshop:getRentedVehicles', function (source, cb)
  306.         MySQL.Async.fetchAll('SELECT * FROM rented_vehicles ORDER BY player_name ASC', {}, function (result)
  307.                 local vehicles = {}
  308.  
  309.                 for i=1, #result, 1 do
  310.                         table.insert(vehicles, {
  311.                                 name       = result[i].vehicle,
  312.                                 plate      = result[i].plate,
  313.                                 playerName = result[i].player_name
  314.                         })
  315.                 end
  316.  
  317.                 cb(vehicles)
  318.         end)
  319. end)
  320.  
  321. ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function (source, cb, plate)
  322.         MySQL.Async.fetchAll('SELECT * FROM rented_vehicles WHERE plate = @plate', {
  323.                 ['@plate'] = plate
  324.         }, function (result)
  325.                 if result[1] ~= nil then
  326.                         local vehicle   = result[1].vehicle
  327.                         local basePrice = result[1].base_price
  328.  
  329.                         MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', {
  330.                                 ['@vehicle'] = vehicle,
  331.                                 ['@price']   = basePrice
  332.                         })
  333.  
  334.                         MySQL.Async.execute('DELETE FROM rented_vehicles WHERE plate = @plate', {
  335.                                 ['@plate'] = plate
  336.                         })
  337.  
  338.                         RemoveOwnedVehicle(plate)
  339.                         cb(true)
  340.                 else
  341.                         cb(false)
  342.                 end
  343.         end)
  344. end)
  345.  
  346. ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function (source, cb, plate, model)
  347.         local resellPrice = 0
  348.  
  349.         -- calculate the resell price
  350.         for i=1, #Vehicles, 1 do
  351.                 if GetHashKey(Vehicles[i].model) == model then
  352.                         resellPrice = ESX.Math.Round(Vehicles[i].price / 100 * Config.ResellPercentage)
  353.                         break
  354.                 end
  355.         end
  356.  
  357.         if resellPrice == 0 then
  358.                 print(('esx_vehicleshop: %s attempted to sell an unknown vehicle!'):format(GetPlayerIdentifiers(source)[1]))
  359.                 cb(false)
  360.         else
  361.                 MySQL.Async.fetchAll('SELECT * FROM rented_vehicles WHERE plate = @plate', {
  362.                         ['@plate'] = plate
  363.                 }, function (result)
  364.                         if result[1] then -- is it a rented vehicle?
  365.                                 cb(false) -- it is, don't let the player sell it since he doesn't own it
  366.                         else
  367.                                 local xPlayer = ESX.GetPlayerFromId(source)
  368.  
  369.                                 MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND @plate = plate', {
  370.                                         ['@owner'] = xPlayer.identifier,
  371.                                         ['@plate'] = plate
  372.                                 }, function (result)
  373.                                         if result[1] then -- does the owner match?
  374.                                                 local vehicle = json.decode(result[1].vehicle)
  375.  
  376.                                                 if vehicle.model == model then
  377.                                                         if vehicle.plate == plate then
  378.                                                                 xPlayer.addMoney(resellPrice)
  379.                                                                 RemoveOwnedVehicle(plate)
  380.                                                                 cb(true)
  381.                                                         else
  382.                                                                 print(('esx_vehicleshop: %s attempted to sell an vehicle with plate mismatch!'):format(xPlayer.identifier))
  383.                                                                 cb(false)
  384.                                                         end
  385.                                                 else
  386.                                                         print(('esx_vehicleshop: %s attempted to sell an vehicle with model mismatch!'):format(xPlayer.identifier))
  387.                                                         cb(false)
  388.                                                 end
  389.                                         else
  390.                                                 if xPlayer.job.grade_name == 'boss' then
  391.                                                         MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND @plate = plate', {
  392.                                                                 ['@owner'] = 'society:' .. xPlayer.job.name,
  393.                                                                 ['@plate'] = plate
  394.                                                         }, function (result)
  395.                                                                 if result[1] then
  396.                                                                         local vehicle = json.decode(result[1].vehicle)
  397.  
  398.                                                                         if vehicle.model == model then
  399.                                                                                 if vehicle.plate == plate then
  400.                                                                                         xPlayer.addMoney(resellPrice)
  401.                                                                                         RemoveOwnedVehicle(plate)
  402.                                                                                         cb(true)
  403.                                                                                 else
  404.                                                                                         print(('esx_vehicleshop: %s attempted to sell an vehicle with plate mismatch!'):format(xPlayer.identifier))
  405.                                                                                         cb(false)
  406.                                                                                 end
  407.                                                                         else
  408.                                                                                 print(('esx_vehicleshop: %s attempted to sell an vehicle with model mismatch!'):format(xPlayer.identifier))
  409.                                                                                 cb(false)
  410.                                                                         end
  411.                                                                 else
  412.                                                                         cb(false)
  413.                                                                 end
  414.                                                         end)
  415.                                                 else
  416.                                                         cb(false)
  417.                                                 end
  418.                                         end
  419.                                 end)
  420.                         end
  421.                 end)
  422.         end
  423. end)
  424.  
  425.  
  426. ESX.RegisterServerCallback('esx_vehicleshop:getStockItems', function (source, cb)
  427.         TriggerEvent('esx_addoninventory:getSharedInventory', 'society_cardealer', function(inventory)
  428.                 cb(inventory.items)
  429.         end)
  430. end)
  431.  
  432. ESX.RegisterServerCallback('esx_vehicleshop:getPlayerInventory', function (source, cb)
  433.         local xPlayer = ESX.GetPlayerFromId(source)
  434.         local items = xPlayer.inventory
  435.  
  436.         cb({items = items})
  437. end)
  438.  
  439. ESX.RegisterServerCallback('esx_vehicleshop:isPlateTaken', function (source, cb, plate)
  440.         MySQL.Async.fetchAll('SELECT 1 FROM owned_vehicles WHERE plate = @plate', {
  441.                 ['@plate'] = plate
  442.         }, function (result)
  443.                 cb(result[1] ~= nil)
  444.         end)
  445. end)
  446.  
  447. ESX.RegisterServerCallback('esx_vehicleshop:retrieveJobVehicles', function (source, cb, type)
  448.         local xPlayer = ESX.GetPlayerFromId(source)
  449.  
  450.         MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND type = @type AND job = @job', {
  451.                 ['@owner'] = xPlayer.identifier,
  452.                 ['@type'] = type,
  453.                 ['@job'] = xPlayer.job.name
  454.         }, function (result)
  455.                 cb(result)
  456.         end)
  457. end)
  458.  
  459. RegisterServerEvent('esx_vehicleshop:setJobVehicleState')
  460. AddEventHandler('esx_vehicleshop:setJobVehicleState', function(plate, state)
  461.         local xPlayer = ESX.GetPlayerFromId(source)
  462.  
  463.         MySQL.Async.execute('UPDATE owned_vehicles SET `stored` = @stored WHERE plate = @plate AND job = @job', {
  464.                 ['@stored'] = state,
  465.                 ['@plate'] = plate,
  466.                 ['@job'] = xPlayer.job.name
  467.         }, function(rowsChanged)
  468.                 if rowsChanged == 0 then
  469.                         print(('esx_vehicleshop: %s exploited the garage!'):format(xPlayer.identifier))
  470.                 end
  471.         end)
  472. end)
  473.  
  474. function PayRent(d, h, m)
  475.         MySQL.Async.fetchAll('SELECT * FROM rented_vehicles', {}, function (result)
  476.                 for i=1, #result, 1 do
  477.                         local xPlayer = ESX.GetPlayerFromIdentifier(result[i].owner)
  478.  
  479.                         -- message player if connected
  480.                         if xPlayer ~= nil then
  481.                                 xPlayer.removeAccountMoney('bank', result[i].rent_price)
  482.                                 TriggerClientEvent('esx:showNotification', xPlayer.source, _U('paid_rental', ESX.Math.GroupDigits(result[i].rent_price)))
  483.                         else -- pay rent either way
  484.                                 MySQL.Sync.execute('UPDATE users SET bank = bank - @bank WHERE identifier = @identifier',
  485.                                 {
  486.                                         ['@bank']       = result[i].rent_price,
  487.                                         ['@identifier'] = result[i].owner
  488.                                 })
  489.                         end
  490.  
  491.                         TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account)
  492.                                 account.addMoney(result[i].rent_price)
  493.                         end)
  494.                 end
  495.         end)
  496. end
  497.  
  498. TriggerEvent('cron:runAt', 22, 00, PayRent)
  499.