refactor(esx_vehicleshop): main/client.lua

This commit is contained in:
Csoki
2022-12-30 10:41:57 +01:00
parent 6212076bd4
commit 2a659184d6
+57 -79
View File
@@ -1,42 +1,28 @@
local HasAlreadyEnteredMarker, IsInShopMenu = false, false local HasAlreadyEnteredMarker, IsInShopMenu = false, false
local CurrentAction, CurrentActionMsg, LastZone, currentDisplayVehicle, CurrentVehicleData local CurrentAction, CurrentActionMsg, LastZone, currentDisplayVehicle, CurrentVehicleData
local CurrentActionData, Vehicles, Categories = {}, {}, {} local CurrentActionData, Vehicles, Categories = {}, {}, {}
local VehiclesByModel = {}
local vehiclesByCategory = {}
function getVehicleFromModel(model) function getVehicleFromModel(model)
for i = 1, #Vehicles do return VehiclesByModel[model]
local vehicle = Vehicles[i]
if vehicle.model == model then
return vehicle
end
end
end end
function getVehicles()
ESX.TriggerServerCallback('esx_vehicleshop:getCategories', function(categories)
Categories = categories
end)
ESX.TriggerServerCallback('esx_vehicleshop:getVehicles', function(vehicles)
Vehicles = vehicles
end)
end
AddEventHandler("onResourceStart", getVehicles)
function PlayerManagement() function PlayerManagement()
if Config.EnablePlayerManagement then if not Config.EnablePlayerManagement then
if ESX.PlayerData.job.name == 'cardealer' then return
Config.Zones.ShopEntering.Type = 1 end
if ESX.PlayerData.job.grade_name == 'boss' then if ESX.PlayerData.job.name ~= 'cardealer' then
Config.Zones.BossActions.Type = 1 Config.Zones.ShopEntering.Type = -1
end Config.Zones.BossActions.Type = -1
Config.Zones.ResellVehicle.Type = -1
return
end
Config.Zones.ShopEntering.Type = 1
else if ESX.PlayerData.job.grade_name == 'boss' then
Config.Zones.ShopEntering.Type = -1 Config.Zones.BossActions.Type = 1
Config.Zones.BossActions.Type = -1
Config.Zones.ResellVehicle.Type = -1
end
end end
end end
@@ -45,21 +31,35 @@ AddEventHandler('esx:playerLoaded', function(xPlayer)
ESX.PlayerData = xPlayer ESX.PlayerData = xPlayer
PlayerManagement() PlayerManagement()
getVehicles() TriggerServerEvent("esx_vehicleshop:getVehiclesAndCategories")
end) end)
RegisterNetEvent('esx_vehicleshop:sendCategories') RegisterNetEvent('esx_vehicleshop:updateVehiclesAndCategories', function(vehicles, categories, vehiclesByModel)
AddEventHandler('esx_vehicleshop:sendCategories', function(categories)
Categories = categories
end)
RegisterNetEvent('esx_vehicleshop:sendVehicles')
AddEventHandler('esx_vehicleshop:sendVehicles', function(vehicles)
Vehicles = vehicles Vehicles = vehicles
Categories = categories
VehiclesByModel = vehiclesByModel
table.sort(Vehicles, function(a, b)
return a.name < b.name
end)
for _, vehicle in ipairs(Vehicles) do
if IsModelInCdimage(joaat(vehicle.model)) then
local category = vehicle.category
if not vehiclesByCategory[category] then
vehiclesByCategory[category] = {}
end
table.insert(vehiclesByCategory[category], vehicle)
else
print(('[^3WARNING^7] Ignoring vehicle ^5%s^7 due to invalid Model'):format(vehicle.model))
end
end
end) end)
RegisterNetEvent('esx:setJob', PlayerManagement)
RegisterNetEvent('esx:setJob') AddEventHandler('esx:setJob', PlayerManagement)
function DeleteDisplayVehicleInsideShop() function DeleteDisplayVehicleInsideShop()
local attempt = 0 local attempt = 0
@@ -81,7 +81,7 @@ function ReturnVehicleProvider()
ESX.TriggerServerCallback('esx_vehicleshop:getCommercialVehicles', function(vehicles) ESX.TriggerServerCallback('esx_vehicleshop:getCommercialVehicles', function(vehicles)
local elements = {} local elements = {}
for k,v in ipairs(vehicles) do for k, v in ipairs(vehicles) do
local returnPrice = ESX.Math.Round(v.price * 0.75) local returnPrice = ESX.Math.Round(v.price * 0.75)
local vehicleLabel = getVehicleFromModel(v.vehicle).label local vehicleLabel = getVehicleFromModel(v.vehicle).label
@@ -135,28 +135,9 @@ function OpenShopMenu()
SetEntityVisible(playerPed, false) SetEntityVisible(playerPed, false)
SetEntityCoords(playerPed, Config.Zones.ShopInside.Pos) SetEntityCoords(playerPed, Config.Zones.ShopInside.Pos)
local vehiclesByCategory = {}
local elements = {} local elements = {}
local firstVehicleData = nil local firstVehicleData = nil
for i=1, #Categories, 1 do
vehiclesByCategory[Categories[i].name] = {}
end
for i=1, #Vehicles, 1 do
if IsModelInCdimage(joaat(Vehicles[i].model)) then
table.insert(vehiclesByCategory[Vehicles[i].category], Vehicles[i])
else
print(('[^3WARNING^7] Ignoring vehicle ^5%s^7 due to invalid Model'):format(Vehicles[i].model))
end
end
for k,v in pairs(vehiclesByCategory) do
table.sort(v, function(a, b)
return a.name < b.name
end)
end
for i=1, #Categories, 1 do for i=1, #Categories, 1 do
local category = Categories[i] local category = Categories[i]
local categoryVehicles = vehiclesByCategory[category.name] local categoryVehicles = vehiclesByCategory[category.name]
@@ -439,7 +420,7 @@ function OpenPopVehicleMenu()
local vehicleLabel = getVehicleFromModel(v.vehicle).label local vehicleLabel = getVehicleFromModel(v.vehicle).label
table.insert(elements, { table.insert(elements, {
label = ('%s [MSRP <span style="color:green;">%s</span>]'):format(vehicleLabel, TranslateCap('generic_shopitem', ESX.Math.GroupDigits(v.price))), label = ('%s [<span style="color:green;">%s</span>]'):format(vehicleLabel, TranslateCap('generic_shopitem', ESX.Math.GroupDigits(v.price))),
value = v.vehicle value = v.vehicle
}) })
end end
@@ -630,21 +611,18 @@ function OpenPutStocksMenu()
end) end)
end end
AddEventHandler('esx_vehicleshop:hasEnteredMarker', function(zone) function hasEnteredMarker(zone)
if zone == 'ShopEntering' then if zone == 'ShopEntering' then
if not Config.EnablePlayerManagement then
if Config.EnablePlayerManagement then
if ESX.PlayerData.job ~= nil and ESX.PlayerData.job.name == 'cardealer' then
CurrentAction = 'reseller_menu'
CurrentActionMsg = TranslateCap('shop_menu')
CurrentActionData = {}
end
else
CurrentAction = 'shop_menu' CurrentAction = 'shop_menu'
CurrentActionMsg = TranslateCap('shop_menu') CurrentActionMsg = TranslateCap('shop_menu')
CurrentActionData = {} CurrentActionData = {}
end end
if ESX.PlayerData.job ~= nil and ESX.PlayerData.job.name == 'cardealer' then
CurrentAction = 'reseller_menu'
CurrentActionMsg = TranslateCap('shop_menu')
CurrentActionData = {}
end
elseif zone == 'GiveBackVehicle' and Config.EnablePlayerManagement then elseif zone == 'GiveBackVehicle' and Config.EnablePlayerManagement then
local playerPed = PlayerPedId() local playerPed = PlayerPedId()
@@ -696,15 +674,15 @@ AddEventHandler('esx_vehicleshop:hasEnteredMarker', function(zone)
CurrentActionMsg = TranslateCap('shop_menu') CurrentActionMsg = TranslateCap('shop_menu')
CurrentActionData = {} CurrentActionData = {}
end end
end) end
AddEventHandler('esx_vehicleshop:hasExitedMarker', function(zone) function hasExitedMarker(zone)
if not IsInShopMenu then if not IsInShopMenu then
ESX.UI.Menu.CloseAll() ESX.UI.Menu.CloseAll()
end end
ESX.HideUI() ESX.HideUI()
CurrentAction = nil CurrentAction = nil
end) end
AddEventHandler('onResourceStop', function(resource) AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then if resource == GetCurrentResourceName() then
@@ -736,8 +714,8 @@ if Config.EnablePlayerManagement then
end end
-- Create Blips -- Create Blips
CreateThread(function() if Config.Blip.show then
if Config.Blip.show then CreateThread(function()
local blip = AddBlipForCoord(Config.Zones.ShopEntering.Pos) local blip = AddBlipForCoord(Config.Zones.ShopEntering.Pos)
SetBlipSprite (blip, Config.Blip.Sprite) SetBlipSprite (blip, Config.Blip.Sprite)
@@ -748,8 +726,8 @@ CreateThread(function()
BeginTextCommandSetBlipName('STRING') BeginTextCommandSetBlipName('STRING')
AddTextComponentSubstringPlayerName(TranslateCap('car_dealer')) AddTextComponentSubstringPlayerName(TranslateCap('car_dealer'))
EndTextCommandSetBlipName(blip) EndTextCommandSetBlipName(blip)
end end)
end) end
-- Enter / Exit marker events & Draw Markers -- Enter / Exit marker events & Draw Markers
CreateThread(function() CreateThread(function()
@@ -777,12 +755,12 @@ CreateThread(function()
if (isInMarker and not HasAlreadyEnteredMarker) or (isInMarker and LastZone ~= currentZone) then if (isInMarker and not HasAlreadyEnteredMarker) or (isInMarker and LastZone ~= currentZone) then
HasAlreadyEnteredMarker, LastZone = true, currentZone HasAlreadyEnteredMarker, LastZone = true, currentZone
LastZone = currentZone LastZone = currentZone
TriggerEvent('esx_vehicleshop:hasEnteredMarker', currentZone) hasEnteredMarker(currentZone)
end end
if not isInMarker and HasAlreadyEnteredMarker then if not isInMarker and HasAlreadyEnteredMarker then
HasAlreadyEnteredMarker = false HasAlreadyEnteredMarker = false
TriggerEvent('esx_vehicleshop:hasExitedMarker', LastZone) hasExitedMarker(LastZone)
end end
if letSleep then if letSleep then