Skip to content

Commit

Permalink
feat: vehicleId statebag on vehicle spawn, safety of plate generation…
Browse files Browse the repository at this point in the history
… refactor: rename generatePlate to generateUniquePlate chore: MySQL to lint
  • Loading branch information
artur-michalak committed May 18, 2024
1 parent 997404a commit 5eeb76a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"cache",
"QBX",
"locale",
"qbx"
"qbx",
"MySQL"
]
}
4 changes: 2 additions & 2 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ end)
--- Buys the selected vehicle
---@param vehicle number
---@param plate string
RegisterNetEvent('qbx_vehicleshop:client:buyShowroomVehicle', function(vehicle, plate)
RegisterNetEvent('qbx_vehicleshop:client:buyShowroomVehicle', function(vehicle, plate, vehicleId)
local tempShop = insideShop -- temp hacky way of setting the shop because it changes after the callback has returned since you are outside the zone
local netId = lib.callback.await('qbx_vehicleshop:server:spawnVehicle', false, vehicle, config.shops[tempShop].vehicleSpawn, plate)
local netId = lib.callback.await('qbx_vehicleshop:server:spawnVehicle', false, vehicle, config.shops[tempShop].vehicleSpawn, plate, vehicleId)
local veh = NetToVeh(netId)
local props = lib.getVehicleProperties(veh)
props.plate = plate
Expand Down
35 changes: 18 additions & 17 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ local function calculateNewFinance(paymentAmount, vehData)
return qbx.math.round(newBalance), qbx.math.round(newPayment), newPaymentsLeft
end

local function generatePlate()
local plate
repeat
plate = qbx.generateRandomPlate('11AAA111')
until not DoesVehicleEntityExist(plate)
return plate:upper()
local function generateUniquePlate()
while true do
local plate = qbx.generateRandomPlate('11AAA111')
if not DoesVehicleEntityExist(plate) then return plate end
Wait(0)
end
end

-- Callbacks
Expand All @@ -106,11 +106,13 @@ lib.callback.register('qbx_vehicleshop:server:GetVehiclesByName', function(sourc
end
end)

lib.callback.register('qbx_vehicleshop:server:spawnVehicle', function(source, model, coords, plate)
lib.callback.register('qbx_vehicleshop:server:spawnVehicle', function(source, model, coords, plate, vehicleId)
local netId, veh = qbx.spawnVehicle({model = model, spawnSource = coords, warp = GetPlayerPed(source)})
if not netId or netId == 0 then return end
if not veh or veh == 0 then return end

if vehicleId then Entity(veh).state:set('vehicleid', vehicleId, false) end

SetVehicleNumberPlateText(veh, plate)
TriggerClientEvent('vehiclekeys:client:SetOwner', source, plate)
return netId
Expand Down Expand Up @@ -239,18 +241,17 @@ RegisterNetEvent('qbx_vehicleshop:server:buyShowroomVehicle', function(vehicle)
local vehiclePrice = coreVehicles[vehicle].price
local currencyType = findChargeableCurrencyType(vehiclePrice, player.PlayerData.money.cash, player.PlayerData.money.bank)
if not currencyType then
exports.qbx_core:Notify(src, locale('error.notenoughmoney'), 'error')
return
return exports.qbx_core:Notify(src, locale('error.notenoughmoney'), 'error')
end

local plate = generatePlate()
exports.qbx_vehicles:CreateVehicleEntity({
local plate = generateUniquePlate()
local vehicleId = exports.qbx_vehicles:CreateVehicleEntity({
citizenId = player.PlayerData.citizenid,
model = vehicle,
plate = plate,
})
exports.qbx_core:Notify(src, locale('success.purchased'), 'success')
TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', src, vehicle, plate)
TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', src, vehicle, plate, vehicleId)
player.Functions.RemoveMoney(currencyType, vehiclePrice, 'vehicle-bought-in-showroom')
end)

Expand Down Expand Up @@ -279,7 +280,7 @@ RegisterNetEvent('qbx_vehicleshop:server:financeVehicle', function(downPayment,
return exports.qbx_core:Notify(src, locale('error.notenoughmoney'), 'error')
end

local plate = generatePlate()
local plate = generateUniquePlate()
local balance, vehPaymentAmount = calculateFinance(vehiclePrice, downPayment, paymentAmount)
local cid = player.PlayerData.citizenid
local timer = (config.finance.paymentInterval * 60)
Expand Down Expand Up @@ -342,17 +343,17 @@ RegisterNetEvent('qbx_vehicleshop:server:sellShowroomVehicle', function(data, pl
local vehicle = data
local vehiclePrice = coreVehicles[vehicle].price
local cid = target.PlayerData.citizenid
local plate = generatePlate()
local plate = generateUniquePlate()

if not sellShowroomVehicleTransact(src, target, vehiclePrice, vehiclePrice) then return end

exports.qbx_vehicles:CreateVehicleEntity({
local vehicleId = exports.qbx_vehicles:CreateVehicleEntity({
citizenId = cid,
model = vehicle,
plate = plate
})

TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', target.PlayerData.source, vehicle, plate)
TriggerClientEvent('qbx_vehicleshop:client:buyShowroomVehicle', target.PlayerData.source, vehicle, plate, vehicleId)
end)

-- Finance vehicle to customer
Expand Down Expand Up @@ -385,7 +386,7 @@ RegisterNetEvent('qbx_vehicleshop:server:sellfinanceVehicle', function(downPayme

local cid = target.PlayerData.citizenid
local timer = (config.finance.paymentInterval * 60)
local plate = generatePlate()
local plate = generateUniquePlate()
local balance, vehPaymentAmount = calculateFinance(vehiclePrice, downPayment, paymentAmount)

if not sellShowroomVehicleTransact(src, target, vehiclePrice, downPayment) then return end
Expand Down

0 comments on commit 5eeb76a

Please sign in to comment.