Skip to content

Commit

Permalink
refactor: use qbx lib
Browse files Browse the repository at this point in the history
  • Loading branch information
solareon committed Feb 13, 2024
1 parent 39029a2 commit db4963c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
20 changes: 10 additions & 10 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ local function showVehicleFinanceMenu(data)
{
title = 'Finance Information',
icon = 'circle-info',
description = string.format('Name: %s\nPlate: %s\nRemaining Balance: $%s\nRecurring Payment Amount: $%s\nPayments Left: %s', vehLabel, data.vehiclePlate, CommaValue(data.balance), CommaValue(data.paymentAmount), data.paymentsLeft),
description = string.format('Name: %s\nPlate: %s\nRemaining Balance: $%s\nRecurring Payment Amount: $%s\nPayments Left: %s', vehLabel, data.vehiclePlate, lib.math.groupdigits(data.balance), lib.math.groupdigits(data.paymentAmount), data.paymentsLeft),
readOnly = true,
},
{
Expand Down Expand Up @@ -138,7 +138,7 @@ end
---@return string
local function getVehPrice(closestVehicle)
local vehicle = config.shops[insideShop].showroomVehicles[closestVehicle].vehicle
return CommaValue(VEHICLES[vehicle].price)
return lib.math.groupdigits(VEHICLES[vehicle].price)
end

--- Fetches the brand of a vehicle from QB Shared
Expand Down Expand Up @@ -390,7 +390,7 @@ local function openVehicleSellMenu()
end
}
end

if config.finance.enable then
options[#options + 1] = {
title = Lang:t('menus.finance_header'),
Expand Down Expand Up @@ -428,7 +428,7 @@ local function startTestDriveTimer(time)
inTestDrive = false
exports.qbx_core:Notify(Lang:t('general.testdrive_complete'), 'success')
end
DrawText2D(Lang:t('general.testdrive_timer')..math.ceil(time - secondsLeft / 1000), vec2(1.0, 1.38), 1.0, 1.0, 0.5)
qbx.drawText2d({ text = Lang:t('general.testdrive_timer')..math.ceil(time - secondsLeft / 1000), coords = vec2(1.0, 1.38), scale = 0.5})
Wait(0)
end
end)
Expand Down Expand Up @@ -498,7 +498,7 @@ end
---@param coords vector4
---@return number vehicleEntity
local function createShowroomVehicle(model, coords)
lib.requestModel(model, requestModelTimeout)
lib.requestModel(model, 1000)
local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, false, false)
SetModelAsNoLongerNeeded(model)
SetVehicleOnGroundProperly(veh)
Expand Down Expand Up @@ -586,7 +586,7 @@ RegisterNetEvent('qbx_vehicleshop:client:testDrive', function(args)

inTestDrive = true
local testDrive = config.shops[insideShop].testDrive
local plate = 'TEST'..RandomNumber(4)
local plate = 'TEST'..lib.string.random('1111')
local netId = lib.callback.await('qbx_vehicleshop:server:spawnVehicle', false, args.vehicle, testDrive.spawn, plate)
testDriveVeh = netId
exports.qbx_core:Notify(Lang:t('general.testdrive_timenoti'), testDrive.limit, 'inform')
Expand All @@ -602,11 +602,11 @@ RegisterNetEvent('qbx_vehicleshop:client:swapVehicle', function(data)

local closestVehicle = lib.getClosestVehicle(dataClosestVehicle.coords.xyz, 5, false)
if not closestVehicle then return end

if not IsModelInCdimage(data.toVehicle) then
lib.print.error(('Failed to find model for "%s". Vehicle might not be streamed?'):format(data.toVehicle))
return
end
return
end

DeleteEntity(closestVehicle)
while DoesEntityExist(closestVehicle) do
Expand All @@ -633,7 +633,7 @@ RegisterNetEvent('qbx_vehicleshop:client:buyShowroomVehicle', function(vehicle,
end)

lib.callback.register('qbx_vehicleshop:client:confirmTrade', function(vehicle, sellAmount)
local input = lib.inputDialog((Lang:t('general.transfervehicle_confirm')):format(VEHICLES_HASH[vehicle].brand,VEHICLES_HASH[vehicle].name, CommaValue(sellAmount) or 0),{
local input = lib.inputDialog((Lang:t('general.transfervehicle_confirm')):format(VEHICLES_HASH[vehicle].brand,VEHICLES_HASH[vehicle].name, lib.math.groupdigits(sellAmount) or 0),{
{
type = 'checkbox',
label = 'Confirm'
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version '1.0.0'

shared_script {
'@ox_lib/init.lua',
'@qbx_core/modules/utils.lua',
'@qbx_core/modules/lib.lua',
'@qbx_core/shared/locale.lua',
'locales/en.lua',
'locales/*.lua',
Expand Down
22 changes: 11 additions & 11 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end)
local function calculateFinance(vehiclePrice, downPayment, paymentamount)
local balance = vehiclePrice - downPayment
local vehPaymentAmount = balance / paymentamount
return math.round(balance), math.round(vehPaymentAmount)
return qbx.math.round(balance), qbx.math.round(vehPaymentAmount)
end

---@class FinancedVehicle
Expand All @@ -76,13 +76,13 @@ local function calculateNewFinance(paymentAmount, vehData)
local minusPayment = vehData.paymentsLeft - 1
local newPaymentsLeft = newBalance / minusPayment
local newPayment = newBalance / newPaymentsLeft
return math.round(newBalance), math.round(newPayment), newPaymentsLeft
return qbx.math.round(newBalance), qbx.math.round(newPayment), newPaymentsLeft
end

local function generatePlate()
local plate
repeat
plate = GenerateRandomPlate('11AAA111')
plate = qbx.generateRandomPlate('11AAA111')
until not DoesVehicleEntityExist(plate)
return plate:upper()
end
Expand All @@ -100,7 +100,7 @@ lib.callback.register('qbx_vehicleshop:server:GetVehiclesByName', function(sourc
end)

lib.callback.register('qbx_vehicleshop:server:spawnVehicle', function(source, model, coords, plate)
local netId = SpawnVehicle(source, model, coords, true)
local netId = qbx.spawnVehicle({model = model, spawnSource = coords, warp = GetPlayerPed(source)})
if not netId or netId == 0 then return end
local veh = NetworkGetEntityFromNetworkId(netId)
if not veh or veh == 0 then return end
Expand Down Expand Up @@ -189,7 +189,7 @@ RegisterNetEvent('qbx_vehicleshop:server:financePayment', function(paymentAmount
end

if paymentAmount < minPayment then
exports.qbx_core:Notify(src, Lang:t('error.minimumallowed')..CommaValue(minPayment), 'error')
exports.qbx_core:Notify(src, Lang:t('error.minimumallowed')..lib.math.groupdigits(minPayment), 'error')
return
end

Expand Down Expand Up @@ -255,7 +255,7 @@ RegisterNetEvent('qbx_vehicleshop:server:financeVehicle', function(downPayment,
local src = source
local player = exports.qbx_core:GetPlayer(src)
local vehiclePrice = coreVehicles[vehicle].price
local minDown = tonumber(math.round((sharedConfig.finance.minimumDown / 100) * vehiclePrice)) --[[@as number]]
local minDown = tonumber(qbx.math.round((sharedConfig.finance.minimumDown / 100) * vehiclePrice)) --[[@as number]]
downPayment = tonumber(downPayment) --[[@as number]]
paymentAmount = tonumber(paymentAmount) --[[@as number]]

Expand Down Expand Up @@ -314,9 +314,9 @@ local function sellShowroomVehicleTransact(src, target, price, downPayment)

target.Functions.RemoveMoney(currencyType, downPayment, 'vehicle-bought-in-showroom')

local commission = math.round(price * config.commissionRate)
local commission = qbx.math.round(price * config.commissionRate)
player.Functions.AddMoney('bank', price * config.commissionRate)
exports.qbx_core:Notify(src, Lang:t('success.earned_commission', {amount = CommaValue(commission)}), 'success')
exports.qbx_core:Notify(src, Lang:t('success.earned_commission', {amount = lib.math.groupdigits(commission)}), 'success')

exports['Renewed-Banking']:addAccountMoney(player.PlayerData.job.name, price)
exports.qbx_core:Notify(target.PlayerData.source, Lang:t('success.purchased'), 'success')
Expand Down Expand Up @@ -369,7 +369,7 @@ RegisterNetEvent('qbx_vehicleshop:server:sellfinanceVehicle', function(downPayme
downPayment = tonumber(downPayment) --[[@as number]]
paymentAmount = tonumber(paymentAmount) --[[@as number]]
local vehiclePrice = coreVehicles[vehicle].price
local minDown = tonumber(math.round((sharedConfig.finance.minimumDown / 100) * vehiclePrice)) --[[@as number]]
local minDown = tonumber(qbx.math.round((sharedConfig.finance.minimumDown / 100) * vehiclePrice)) --[[@as number]]

if downPayment > vehiclePrice then
return exports.qbx_core:Notify(src, Lang:t('error.notworth'), 'error')
Expand Down Expand Up @@ -493,8 +493,8 @@ lib.addCommand('transfervehicle', {help = Lang:t('general.command_transfervehicl
end
UpdateVehicleEntityOwner(targetcid, targetlicense, plate)
TriggerClientEvent('vehiclekeys:client:SetOwner', buyerId, plate)
local sellerMessage = sellAmount > 0 and Lang:t('success.soldfor') .. CommaValue(sellAmount) or Lang:t('success.gifted')
local buyerMessage = sellAmount > 0 and Lang:t('success.boughtfor') .. CommaValue(sellAmount) or Lang:t('success.received_gift')
local sellerMessage = sellAmount > 0 and Lang:t('success.soldfor') .. lib.math.groupdigits(sellAmount) or Lang:t('success.gifted')
local buyerMessage = sellAmount > 0 and Lang:t('success.boughtfor') .. lib.math.groupdigits(sellAmount) or Lang:t('success.received_gift')
exports.qbx_core:Notify(src, sellerMessage, 'success')
exports.qbx_core:Notify(buyerId, buyerMessage, 'success')
end, GetEntityModel(vehicle), sellAmount)
Expand Down

0 comments on commit db4963c

Please sign in to comment.