Skip to content

Commit

Permalink
sort categories & vehicles by name
Browse files Browse the repository at this point in the history
  • Loading branch information
TonybynMp4 committed Mar 15, 2024
1 parent 0ac4235 commit b27e38b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
86 changes: 53 additions & 33 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local config = require 'config.client'
local sharedConfig = require 'config.shared'
local VEHICLES = exports.qbx_core:GetVehiclesByName()
local CATEGORIES = qbx.table.mapBySubfield(VEHICLES, 'category')
local VEHICLES_HASH = exports.qbx_core:GetVehiclesByHash()
local testDriveVeh = 0
local inTestDrive = false
Expand Down Expand Up @@ -182,44 +183,49 @@ end
---@param targetVehicle number
local function openVehCatsMenu(category, targetVehicle)
local vehMenu = {}
for k, v in pairs(VEHICLES) do
if VEHICLES[k].category == category then
if not VEHICLES[k].shop then
lib.print.debug('Vehicle '..k..' does not have a shop set')
goto continue
end

if type(VEHICLES[k].shop) == 'table' then
for _, shop in pairs(VEHICLES[k].shop) do
if shop == insideShop then
vehMenu[#vehMenu + 1] = {
title = v.brand..' '..v.name,
description = locale('menus.veh_price')..lib.math.groupdigits(v.price),
serverEvent = 'qbx_vehicleshop:server:swapVehicle',
args = {
toVehicle = v.model,
targetVehicle = targetVehicle,
ClosestShop = insideShop
}
for k, v in pairs(CATEGORIES[category]) do
if not v.shop then
lib.print.debug('Vehicle '..k..' does not have a shop set')
goto continue
end

if type(v.shop) == 'table' then
for _, shop in pairs(v.shop) do
if shop == insideShop then
vehMenu[#vehMenu + 1] = {
title = v.brand..' '..v.name,
description = locale('menus.veh_price')..lib.math.groupdigits(v.price),
serverEvent = 'qbx_vehicleshop:server:swapVehicle',
args = {
toVehicle = v.model,
targetVehicle = targetVehicle,
ClosestShop = insideShop
}
end
end
elseif VEHICLES[k].shop == insideShop then
vehMenu[#vehMenu + 1] = {
title = v.brand..' '..v.name,
description = locale('menus.veh_price')..lib.math.groupdigits(v.price),
serverEvent = 'qbx_vehicleshop:server:swapVehicle',
args = {
toVehicle = v.model,
targetVehicle = targetVehicle,
ClosestShop = insideShop
}
}
end
end
elseif v.shop == insideShop then
vehMenu[#vehMenu + 1] = {
title = v.brand..' '..v.name,
description = locale('menus.veh_price')..lib.math.groupdigits(v.price),
serverEvent = 'qbx_vehicleshop:server:swapVehicle',
args = {
toVehicle = v.model,
targetVehicle = targetVehicle,
ClosestShop = insideShop
}
}
end
::continue::
end

table.sort(vehMenu, function(a, b)
local _, aName = string.strsplit(' ', string.upper(a.title), 2)
local _, bName = string.strsplit(' ', string.upper(b.title), 2)
return aName < bName
end)

lib.registerContext({
id = 'openVehCats',
title = config.shops[insideShop].categories[category],
Expand All @@ -234,12 +240,26 @@ end
---@param args table<string, any>
local function openVehicleCategoryMenu(args)
local categoryMenu = {}
for k, v in pairs(config.shops[insideShop].categories) do
local sortedCategories = {}
local categories = config.shops[insideShop].categories

for k, v in pairs(categories) do
sortedCategories[#sortedCategories + 1] = {
category = k,
label = v
}
end

table.sort(sortedCategories, function(a, b)
return string.upper(a.label) < string.upper(b.label)
end)

for i = 1, #sortedCategories do
categoryMenu[#categoryMenu + 1] = {
title = v,
title = sortedCategories[i].label,
arrow = true,
onSelect = function()
openVehCatsMenu(k, args.targetVehicle)
openVehCatsMenu(sortedCategories[i].category, args.targetVehicle)
end
}
end
Expand Down
22 changes: 11 additions & 11 deletions config/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return {
vec3(0.0, 0.0, 0.0),
},
size = vec3(0.0, 0.0, 0.0), -- Size of the vehicles zones (x, y, z)
targetDistance = 1, -- Defines targeting distance. Only works if useTarget is enabled
targetDistance = 2 -- Defines targeting distance. Only works if useTarget is enabled
},
blip = {
label = '', -- Blip label
Expand Down Expand Up @@ -70,7 +70,7 @@ return {
vector3(-31.29, -1095.37, 25.0)
},
size = vector3(3, 3, 4), -- size of the vehicles zones (x, y, z)
targetDistance = 1, -- Defines targeting distance. Only works if targeting is enabled
targetDistance = 2 -- Defines targeting distance. Only works if targeting is enabled
},
blip = {
label = 'Premium Deluxe Motorsport South', -- Blip label
Expand All @@ -85,7 +85,7 @@ return {
sports = 'Sportives',
coupes = 'Coupées',
suvs = 'SUV',
offroad = '4x4',
offroad = 'Tout-Terrains',
muscle = 'Muscle-cars',
compacts = 'Compactes',
motorcycles = 'Motos',
Expand Down Expand Up @@ -189,7 +189,7 @@ return {
vector3(-1244.39, -362.71, 35.0)
},
size = vector3(3, 3, 4), -- size of the vehicles zones (x, y, z)
targetDistance = 1, -- Defines targeting distance. Only works if targeting is enabled
targetDistance = 2 -- Defines targeting distance. Only works if targeting is enabled
},
blip = {
label = 'Premium Deluxe Motorsport North', -- Blip label
Expand All @@ -208,7 +208,7 @@ return {
sports = 'Sportives',
coupes = 'Coupées',
suvs = 'SUV',
offroad = '4x4',
offroad = 'Tout-Terrains',
muscle = 'Muscle-cars',
compacts = 'Compactes',
motorcycles = 'Motos',
Expand Down Expand Up @@ -275,14 +275,14 @@ return {
vector3(-787.86, -247.81 ,39),
},
size = vector3(3, 3, 4), -- size of the vehicles zones (x, y, z)
targetDistance = 1, -- Defines targeting distance. Only works if targeting is enabled
targetDistance = 2 -- Defines targeting distance. Only works if targeting is enabled
},
categories = {
sportsclassics = 'Sportives-Classiques',
sedans = 'Berlines',
coupes = 'Coupées',
suvs = 'SUV',
offroad = '4x4',
offroad = 'Tout-Terrains',
sports = 'Sportives',
muscle = 'Muscle-cars',
compacts = 'Compactes',
Expand Down Expand Up @@ -342,15 +342,15 @@ return {
vector3(-492.56, -2721.6, 5.5),
},
size = vector3(3, 3, 4), -- size of the vehicles zones (x, y, z)
targetDistance = 1, -- Defines targeting distance. Only works if targeting is enabled
targetDistance = 2 -- Defines targeting distance. Only works if targeting is enabled
},
categories = {
sportsclassics = 'Sportives-Classiques',
sedans = 'Berlines',
sports = 'Sportives',
coupes = 'Coupées',
suvs = 'SUV',
offroad = '4x4',
offroad = 'Tout-Terrains',
muscle = 'Muscle-cars',
compacts = 'Compactes',
motorcycles = 'Motos',
Expand Down Expand Up @@ -433,14 +433,14 @@ return {
vector3(-668.96, -2462.63, 10.5),
},
size = vector3(3, 3, 4), -- size of the vehicles zones (x, y, z)
targetDistance = 1, -- Defines targeting distance. Only works if targeting is enabled
targetDistance = 2 -- Defines targeting distance. Only works if targeting is enabled
},
categories = {
sportsclassics = 'Sportives-Classiques',
sedans = 'Berlines',
coupes = 'Coupées',
suvs = 'SUV',
offroad = '4x4',
offroad = 'Tout-Terrains',
muscle = 'Muscle-cars',
compacts = 'Compactes',
motorcycles = 'Motos',
Expand Down

0 comments on commit b27e38b

Please sign in to comment.