Major improvements, see desc

- Fixed selling same vehicle multiple times
- Fixed selling <no> vehicle
- Major security improvements
- Removed buying vehicle for society
- Code cleanup
- Fixed depopping vehicle not working (not respecting network owner)
This commit is contained in:
ElPumpo
2020-02-08 12:36:18 +01:00
parent 5e6cd29ca8
commit 14fbc69357
11 changed files with 371 additions and 523 deletions
+187 -222
View File
@@ -1,5 +1,5 @@
ESX = nil
local Categories, Vehicles = {}, {}
local categories, vehicles = {}, {}
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
@@ -12,7 +12,7 @@ Citizen.CreateThread(function()
if Config.PlateUseSpace then char = char + 1 end
if char > 8 then
print(('esx_vehicleshop: ^1WARNING^7 plate character count reached, %s/8 characters.'):format(char))
print(('[esx_vehicleshop] [^3WARNING^7] Plate character count reached, %s/8 characters!'):format(char))
end
end)
@@ -23,132 +23,117 @@ function RemoveOwnedVehicle(plate)
end
MySQL.ready(function()
Categories = MySQL.Sync.fetchAll('SELECT * FROM vehicle_categories')
local vehicles = MySQL.Sync.fetchAll('SELECT * FROM vehicles')
MySQL.Async.fetchAll('SELECT * FROM vehicle_categories', {}, function(_categories)
categories = _categories
for i=1, #vehicles, 1 do
local vehicle = vehicles[i]
MySQL.Async.fetchAll('SELECT * FROM vehicles', {}, function(_vehicles)
vehicles = _vehicles
for j=1, #Categories, 1 do
if Categories[j].name == vehicle.category then
vehicle.categoryLabel = Categories[j].label
break
for k,v in ipairs(vehicles) do
for k2,v2 in ipairs(categories) do
if v2.name == v.category then
vehicles[k].categoryLabel = v2.label
break
end
end
end
end
table.insert(Vehicles, vehicle)
-- send information after db has loaded, making sure everyone gets vehicle information
TriggerClientEvent('esx_vehicleshop:sendCategories', -1, categories)
TriggerClientEvent('esx_vehicleshop:sendVehicles', -1, vehicles)
end)
end)
end)
function getVehicleLabelFromModel(model)
for k,v in ipairs(vehicles) do
if v.model == model then
return v.name
end
end
-- send information after db has loaded, making sure everyone gets vehicle information
TriggerClientEvent('esx_vehicleshop:sendCategories', -1, Categories)
TriggerClientEvent('esx_vehicleshop:sendVehicles', -1, Vehicles)
end)
return
end
RegisterServerEvent('esx_vehicleshop:setVehicleOwned')
AddEventHandler('esx_vehicleshop:setVehicleOwned', function(vehicleProps)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
RegisterNetEvent('esx_vehicleshop:setVehicleOwnedPlayerId')
AddEventHandler('esx_vehicleshop:setVehicleOwnedPlayerId', function(playerId, vehicleProps, model, label)
local xPlayer, xTarget = ESX.GetPlayerFromId(source), ESX.GetPlayerFromId(playerId)
MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)',
{
['@owner'] = xPlayer.identifier,
['@plate'] = vehicleProps.plate,
['@vehicle'] = json.encode(vehicleProps)
}, function(rowsChanged)
xPlayer.showNotification(_U('vehicle_belongs', vehicleProps.plate))
end)
end)
if xPlayer.job.name == 'cardealer' and xTarget then
MySQL.Async.fetchAll('SELECT id FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
['@vehicle'] = model
}, function(result)
if result[1] then
local id = result[1].id
RegisterServerEvent('esx_vehicleshop:setVehicleOwnedPlayerId')
AddEventHandler('esx_vehicleshop:setVehicleOwnedPlayerId', function(playerId, vehicleProps)
local xPlayer = ESX.GetPlayerFromId(playerId)
MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
['@id'] = id
}, function(rowsChanged)
if rowsChanged == 1 then
MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)', {
['@owner'] = xTarget.identifier,
['@plate'] = vehicleProps.plate,
['@vehicle'] = json.encode(vehicleProps)
}, function(rowsChanged)
xPlayer.showNotification(_U('vehicle_set_owned', vehicleProps.plate, xTarget.getName()))
xTarget.showNotification(_U('vehicle_belongs', vehicleProps.plate))
end)
MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)',
{
['@owner'] = xPlayer.identifier,
['@plate'] = vehicleProps.plate,
['@vehicle'] = json.encode(vehicleProps)
}, function(rowsChanged)
xPlayer.showNotification(_U('vehicle_belongs', vehicleProps.plate))
end)
end)
local dateNow = os.date('%Y-%m-%d %H:%M')
RegisterServerEvent('esx_vehicleshop:setVehicleOwnedSociety')
AddEventHandler('esx_vehicleshop:setVehicleOwnedSociety', function(society, vehicleProps)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)', {
['@owner'] = 'society:' .. society,
['@plate'] = vehicleProps.plate,
['@vehicle'] = json.encode(vehicleProps),
}, function(rowsChanged) end)
end)
RegisterServerEvent('esx_vehicleshop:sellVehicle')
AddEventHandler('esx_vehicleshop:sellVehicle', function(vehicle)
MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
['@vehicle'] = vehicle
}, function(result)
local id = result[1].id
MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
['@id'] = id
})
end)
end)
RegisterServerEvent('esx_vehicleshop:addToList')
AddEventHandler('esx_vehicleshop:addToList', function(target, model, plate)
local xPlayer, xTarget = ESX.GetPlayerFromId(source), ESX.GetPlayerFromId(target)
local dateNow = os.date('%Y-%m-%d %H:%M')
if xPlayer.job.name == 'cardealer' then
MySQL.Async.execute('INSERT INTO vehicle_sold (client, model, plate, soldby, date) VALUES (@client, @model, @plate, @soldby, @date)', {
['@client'] = xTarget.getName(),
['@model'] = model,
['@plate'] = plate,
['@soldby'] = xPlayer.getName(),
['@date'] = dateNow
})
else
print(('esx_vehicleshop: %s attempted to add a sold vehicle to list!'):format(xPlayer.identifier))
MySQL.Async.execute('INSERT INTO vehicle_sold (client, model, plate, soldby, date) VALUES (@client, @model, @plate, @soldby, @date)', {
['@client'] = xTarget.getName(),
['@model'] = label,
['@plate'] = vehicleProps.plate,
['@soldby'] = xPlayer.getName(),
['@date'] = dateNow
})
end
end)
end
end)
end
end)
ESX.RegisterServerCallback('esx_vehicleshop:getSoldVehicles', function(source, cb)
MySQL.Async.fetchAll('SELECT * FROM vehicle_sold', {}, function(result)
MySQL.Async.fetchAll('SELECT client, model, plate, soldby, date FROM vehicle_sold', {}, function(result)
cb(result)
end)
end)
RegisterServerEvent('esx_vehicleshop:rentVehicle')
AddEventHandler('esx_vehicleshop:rentVehicle', function(vehicle, plate, playerName, basePrice, rentPrice, target)
local xPlayer = ESX.GetPlayerFromId(target)
RegisterNetEvent('esx_vehicleshop:rentVehicle')
AddEventHandler('esx_vehicleshop:rentVehicle', function(vehicle, plate, rentPrice, playerId)
local xPlayer, xTarget = ESX.GetPlayerFromId(source), ESX.GetPlayerFromId(playerId)
MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
['@vehicle'] = vehicle
}, function(result)
local id = result[1].id
local price = result[1].price
local owner = xPlayer.identifier
if xPlayer.job.name == 'cardealer' and xTarget then
MySQL.Async.fetchAll('SELECT id, price FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
['@vehicle'] = vehicle
}, function(result)
if result[1] then
local price = result[1].price
MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
['@id'] = id
})
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)', {
['@vehicle'] = vehicle,
['@plate'] = plate,
['@player_name'] = playerName,
['@base_price'] = basePrice,
['@rent_price'] = rentPrice,
['@owner'] = owner
})
end)
MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
['@id'] = result[1].id
}, function(rowsChanged)
if rowsChanged == 1 then
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)', {
['@vehicle'] = vehicle,
['@plate'] = plate,
['@player_name'] = xTarget.getName(),
['@base_price'] = price,
['@rent_price'] = rentPrice,
['@owner'] = xTarget.identifier
}, function(rowsChanged2)
xPlayer.showNotification(_U('vehicle_set_rented', plate, xTarget.getName()))
end)
end
end)
end
end)
end
end)
RegisterServerEvent('esx_vehicleshop:getStockItem')
RegisterNetEvent('esx_vehicleshop:getStockItem')
AddEventHandler('esx_vehicleshop:getStockItem', function(itemName, count)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
@@ -173,7 +158,7 @@ AddEventHandler('esx_vehicleshop:getStockItem', function(itemName, count)
end)
end)
RegisterServerEvent('esx_vehicleshop:putStockItems')
RegisterNetEvent('esx_vehicleshop:putStockItems')
AddEventHandler('esx_vehicleshop:putStockItems', function(itemName, count)
local _source = source
local xPlayer = ESX.GetPlayerFromId(_source)
@@ -192,98 +177,107 @@ AddEventHandler('esx_vehicleshop:putStockItems', function(itemName, count)
end)
ESX.RegisterServerCallback('esx_vehicleshop:getCategories', function(source, cb)
cb(Categories)
cb(categories)
end)
ESX.RegisterServerCallback('esx_vehicleshop:getVehicles', function(source, cb)
cb(Vehicles)
cb(vehicles)
end)
ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function(source, cb, vehicleModel)
ESX.RegisterServerCallback('esx_vehicleshop:buyVehicle', function(source, cb, model, plate)
local xPlayer = ESX.GetPlayerFromId(source)
local vehicleData
local modelPrice
for i=1, #Vehicles, 1 do
if Vehicles[i].model == vehicleModel then
vehicleData = Vehicles[i]
for k,v in ipairs(vehicles) do
if model == v.model then
modelPrice = v.price
break
end
end
if vehicleData and xPlayer.getMoney() >= vehicleData.price then
xPlayer.removeMoney(vehicleData.price)
cb(true)
if modelPrice and xPlayer.getMoney() >= modelPrice then
xPlayer.removeMoney(modelPrice)
MySQL.Async.execute('INSERT INTO owned_vehicles (owner, plate, vehicle) VALUES (@owner, @plate, @vehicle)', {
['@owner'] = xPlayer.identifier,
['@plate'] = plate,
['@vehicle'] = json.encode({model = GetHashKey(model)})
}, function(rowsChanged)
xPlayer.showNotification(_U('vehicle_belongs', plate))
cb(true)
end)
else
cb(false)
end
end)
ESX.RegisterServerCallback('esx_vehicleshop:buyVehicleSociety', function(source, cb, society, vehicleModel)
local vehicleData
ESX.RegisterServerCallback('esx_vehicleshop:getCommercialVehicles', function(source, cb)
MySQL.Async.fetchAll('SELECT price, vehicle FROM cardealer_vehicles ORDER BY vehicle ASC', {}, function(result)
cb(result)
end)
end)
for i=1, #Vehicles, 1 do
if Vehicles[i].model == vehicleModel then
vehicleData = Vehicles[i]
break
ESX.RegisterServerCallback('esx_vehicleshop:buyCarDealerVehicle', function(source, cb, model)
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer.job.name == 'cardealer' then
local modelPrice
for k,v in ipairs(vehicles) do
if model == v.model then
modelPrice = v.price
break
end
end
if modelPrice then
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account)
if account.money >= modelPrice then
account.removeMoney(modelPrice)
MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', {
['@vehicle'] = model,
['@price'] = modelPrice
}, function(rowsChanged)
cb(true)
end)
else
cb(false)
end
end)
end
end
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_' .. society, function(account)
if account.money >= vehicleData.price then
account.removeMoney(vehicleData.price)
MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', {
['@vehicle'] = vehicleData.model,
['@price'] = vehicleData.price
}, function(rowsChanged)
cb(true)
end)
else
cb(false)
end
end)
end)
ESX.RegisterServerCallback('esx_vehicleshop:getCommercialVehicles', function(source, cb)
MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles ORDER BY vehicle ASC', {}, function(result)
local vehicles = {}
for i=1, #result, 1 do
table.insert(vehicles, {
name = result[i].vehicle,
price = result[i].price
})
end
cb(vehicles)
end)
end)
RegisterServerEvent('esx_vehicleshop:returnProvider')
RegisterNetEvent('esx_vehicleshop:returnProvider')
AddEventHandler('esx_vehicleshop:returnProvider', function(vehicleModel)
local _source = source
local xPlayer = ESX.GetPlayerFromId(source)
MySQL.Async.fetchAll('SELECT * FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
['@vehicle'] = vehicleModel
}, function(result)
if result[1] then
local id = result[1].id
local price = ESX.Math.Round(result[1].price * 0.75)
if xPlayer.job.name == 'cardealer' then
MySQL.Async.fetchAll('SELECT id, price FROM cardealer_vehicles WHERE vehicle = @vehicle LIMIT 1', {
['@vehicle'] = vehicleModel
}, function(result)
if result[1] then
local id = result[1].id
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account)
account.addMoney(price)
end)
MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
['@id'] = id
}, function(rowsChanged)
if rowsChanged == 1 then
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_cardealer', function(account)
local price = ESX.Math.Round(result[1].price * 0.75)
local vehicleLabel = getVehicleLabelFromModel(vehicleModel)
MySQL.Async.execute('DELETE FROM cardealer_vehicles WHERE id = @id', {
['@id'] = id
})
xPlayer.showNotification(_U('vehicle_sold_for', vehicleModel, ESX.Math.GroupDigits(price)))
else
print(('esx_vehicleshop: %s attempted selling an invalid vehicle!'):format(GetPlayerIdentifiers(_source)[1]))
end
end)
account.addMoney(price)
xPlayer.showNotification(_U('vehicle_sold_for', vehicleLabel, ESX.Math.GroupDigits(price)))
end)
end
end)
else
print(('[esx_vehicleshop] [^3WARNING^7] %s attempted selling an invalid vehicle!'):format(xPlayer.identifier))
end
end)
end
end)
ESX.RegisterServerCallback('esx_vehicleshop:getRentedVehicles', function(source, cb)
@@ -303,24 +297,24 @@ ESX.RegisterServerCallback('esx_vehicleshop:getRentedVehicles', function(source,
end)
ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function(source, cb, plate)
MySQL.Async.fetchAll('SELECT * FROM rented_vehicles WHERE plate = @plate', {
MySQL.Async.fetchAll('SELECT base_price, vehicle FROM rented_vehicles WHERE plate = @plate', {
['@plate'] = plate
}, function(result)
if result[1] then
local vehicle = result[1].vehicle
local basePrice = result[1].base_price
MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', {
['@vehicle'] = vehicle,
['@price'] = basePrice
})
MySQL.Async.execute('DELETE FROM rented_vehicles WHERE plate = @plate', {
['@plate'] = plate
})
RemoveOwnedVehicle(plate)
cb(true)
}, function(rowsChanged)
MySQL.Async.execute('INSERT INTO cardealer_vehicles (vehicle, price) VALUES (@vehicle, @price)', {
['@vehicle'] = vehicle,
['@price'] = basePrice
})
RemoveOwnedVehicle(plate)
cb(true)
end)
else
cb(false)
end
@@ -328,18 +322,18 @@ ESX.RegisterServerCallback('esx_vehicleshop:giveBackVehicle', function(source, c
end)
ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function(source, cb, plate, model)
local resellPrice = 0
local resellPrice
-- calculate the resell price
for i=1, #Vehicles, 1 do
if GetHashKey(Vehicles[i].model) == model then
resellPrice = ESX.Math.Round(Vehicles[i].price / 100 * Config.ResellPercentage)
for i=1, #vehicles, 1 do
if GetHashKey(vehicles[i].model) == model then
resellPrice = ESX.Math.Round(vehicles[i].price / 100 * Config.ResellPercentage)
break
end
end
if resellPrice == 0 then
print(('esx_vehicleshop: %s attempted to sell an unknown vehicle!'):format(GetPlayerIdentifiers(source)[1]))
if not resellPrice then
print(('[esx_vehicleshop] [^3WARNING^7] %s attempted to sell an unknown vehicle!'):format(GetPlayerIdentifiers(source)[1]))
cb(false)
else
MySQL.Async.fetchAll('SELECT * FROM rented_vehicles WHERE plate = @plate', {
@@ -363,40 +357,11 @@ ESX.RegisterServerCallback('esx_vehicleshop:resellVehicle', function(source, cb,
RemoveOwnedVehicle(plate)
cb(true)
else
print(('esx_vehicleshop: %s attempted to sell an vehicle with plate mismatch!'):format(xPlayer.identifier))
print(('[esx_vehicleshop] [^3WARNING^7] %s attempted to sell an vehicle with plate mismatch!'):format(xPlayer.identifier))
cb(false)
end
else
print(('esx_vehicleshop: %s attempted to sell an vehicle with model mismatch!'):format(xPlayer.identifier))
cb(false)
end
else
if xPlayer.job.grade_name == 'boss' then
MySQL.Async.fetchAll('SELECT * FROM owned_vehicles WHERE owner = @owner AND @plate = plate', {
['@owner'] = 'society:' .. xPlayer.job.name,
['@plate'] = plate
}, function(result)
if result[1] then
local vehicle = json.decode(result[1].vehicle)
if vehicle.model == model then
if vehicle.plate == plate then
xPlayer.addMoney(resellPrice)
RemoveOwnedVehicle(plate)
cb(true)
else
print(('esx_vehicleshop: %s attempted to sell an vehicle with plate mismatch!'):format(xPlayer.identifier))
cb(false)
end
else
print(('esx_vehicleshop: %s attempted to sell an vehicle with model mismatch!'):format(xPlayer.identifier))
cb(false)
end
else
cb(false)
end
end)
else
print(('[esx_vehicleshop] [^3WARNING^7] %s attempted to sell an vehicle with model mismatch!'):format(xPlayer.identifier))
cb(false)
end
end
@@ -439,7 +404,7 @@ ESX.RegisterServerCallback('esx_vehicleshop:retrieveJobVehicles', function(sourc
end)
end)
RegisterServerEvent('esx_vehicleshop:setJobVehicleState')
RegisterNetEvent('esx_vehicleshop:setJobVehicleState')
AddEventHandler('esx_vehicleshop:setJobVehicleState', function(plate, state)
local xPlayer = ESX.GetPlayerFromId(source)
@@ -449,7 +414,7 @@ AddEventHandler('esx_vehicleshop:setJobVehicleState', function(plate, state)
['@job'] = xPlayer.job.name
}, function(rowsChanged)
if rowsChanged == 0 then
print(('esx_vehicleshop: %s exploited the garage!'):format(xPlayer.identifier))
print(('[esx_vehicleshop] [^3WARNING^7] %s exploited the garage!'):format(xPlayer.identifier))
end
end)
end)