forked from Simnation/Main
ed
This commit is contained in:
parent
1301dfa7ad
commit
64fecd2fa0
2 changed files with 267 additions and 240 deletions
|
@ -26,53 +26,53 @@ function DrawText3D(x, y, z, text)
|
|||
ClearDrawOrigin()
|
||||
end
|
||||
|
||||
-- Get machine data with proper callback handling
|
||||
-- Get machine data with optimized callback handling
|
||||
function GetMachineData(entity)
|
||||
local coords = GetEntityCoords(entity)
|
||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||
|
||||
-- Check if we need to refresh the data
|
||||
-- Return cached data if available and not expired
|
||||
local currentTime = GetGameTimer()
|
||||
if not machineData[entityId] or (currentTime - machineData[entityId].lastCheck > 10000) then
|
||||
if machineData[entityId] and (currentTime - machineData[entityId].lastCheck < 10000) then
|
||||
return machineData[entityId]
|
||||
end
|
||||
|
||||
-- Initialize with default values
|
||||
if not machineData[entityId] then
|
||||
machineData[entityId] = {
|
||||
isRegistered = false,
|
||||
canManage = false,
|
||||
lastCheck = currentTime,
|
||||
checking = true
|
||||
}
|
||||
else
|
||||
machineData[entityId].checking = true
|
||||
machineData[entityId].lastCheck = currentTime
|
||||
end
|
||||
|
||||
-- Check if machine is registered
|
||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
||||
if exists then
|
||||
machineData[entityId].isRegistered = true
|
||||
|
||||
-- Only check management if registered
|
||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(result)
|
||||
machineData[entityId].canManage = result
|
||||
machineData[entityId].checking = false
|
||||
end, coords)
|
||||
-- Single callback to get all machine data at once (more efficient)
|
||||
QBCore.Functions.TriggerCallback('vending:server:getMachineStatus', function(status)
|
||||
if status then
|
||||
machineData[entityId].isRegistered = status.exists
|
||||
machineData[entityId].canManage = status.canManage
|
||||
else
|
||||
machineData[entityId].isRegistered = false
|
||||
machineData[entityId].canManage = false
|
||||
end
|
||||
machineData[entityId].checking = false
|
||||
end
|
||||
end, coords)
|
||||
end
|
||||
|
||||
-- Wait for callbacks to complete if currently checking
|
||||
if machineData[entityId].checking then
|
||||
-- Short wait for callback to complete
|
||||
local timeout = 0
|
||||
while machineData[entityId].checking and timeout < 50 do
|
||||
Wait(10)
|
||||
while machineData[entityId].checking and timeout < 20 do -- Reduced timeout
|
||||
Wait(5) -- Shorter wait
|
||||
timeout = timeout + 1
|
||||
end
|
||||
|
||||
-- If timeout reached, set checking to false to avoid deadlock
|
||||
if timeout >= 50 then
|
||||
if timeout >= 20 then
|
||||
machineData[entityId].checking = false
|
||||
end
|
||||
end
|
||||
|
||||
return machineData[entityId]
|
||||
end
|
||||
|
@ -229,9 +229,9 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
|||
local entity = data.entity
|
||||
local coords = GetEntityCoords(entity)
|
||||
|
||||
-- Double-check if machine is registered before proceeding
|
||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
||||
if not exists then
|
||||
-- Fast check using cached data
|
||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||
if machineData[entityId] and not machineData[entityId].isRegistered then
|
||||
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
|
||||
return
|
||||
end
|
||||
|
@ -272,7 +272,6 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
|||
|
||||
lib.showContext('vending_buy_menu')
|
||||
end, coords)
|
||||
end, coords)
|
||||
end)
|
||||
|
||||
-- Open quantity dialog for buying items
|
||||
|
@ -304,9 +303,9 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
|
|||
local entity = data.entity
|
||||
local coords = GetEntityCoords(entity)
|
||||
|
||||
-- Double-check if player can manage this machine before proceeding
|
||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
||||
if not canManage then
|
||||
-- Fast check using cached data
|
||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||
if machineData[entityId] and not machineData[entityId].canManage then
|
||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||
return
|
||||
end
|
||||
|
@ -382,7 +381,6 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
|
|||
|
||||
lib.showContext('vending_owner_menu')
|
||||
end, coords)
|
||||
end, coords)
|
||||
end)
|
||||
|
||||
-- Function to sell the vending machine
|
||||
|
@ -406,9 +404,9 @@ end
|
|||
|
||||
-- Open price menu
|
||||
function openPriceMenu(coords)
|
||||
-- Double-check if player can manage this machine before proceeding
|
||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
||||
if not canManage then
|
||||
-- Fast check using cached data
|
||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||
if machineData[entityId] and not machineData[entityId].canManage then
|
||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||
return
|
||||
end
|
||||
|
@ -443,7 +441,6 @@ function openPriceMenu(coords)
|
|||
|
||||
lib.showContext('vending_price_menu')
|
||||
end, coords)
|
||||
end, coords)
|
||||
end
|
||||
|
||||
-- Set price for specific item
|
||||
|
@ -466,9 +463,9 @@ end
|
|||
|
||||
-- Open withdraw menu
|
||||
function openWithdrawMenu(coords, availableMoney)
|
||||
-- Double-check if player can manage this machine before proceeding
|
||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
||||
if not canManage then
|
||||
-- Fast check using cached data
|
||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||
if machineData[entityId] and not machineData[entityId].canManage then
|
||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||
return
|
||||
end
|
||||
|
@ -492,7 +489,6 @@ function openWithdrawMenu(coords, availableMoney)
|
|||
if input and input[1] then
|
||||
TriggerServerEvent('vending:server:withdrawMoney', coords, tonumber(input[1]))
|
||||
end
|
||||
end, coords)
|
||||
end
|
||||
|
||||
-- Open stats menu
|
||||
|
@ -525,7 +521,7 @@ end
|
|||
|
||||
-- Open managers menu
|
||||
function openManagersMenu(coords)
|
||||
-- Double-check if player is owner of this machine before proceeding
|
||||
-- Fast check for owner status
|
||||
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
|
||||
if not isOwner then
|
||||
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter verwalten!', 'error')
|
||||
|
@ -598,7 +594,7 @@ end
|
|||
|
||||
-- Open add manager menu
|
||||
function openAddManagerMenu(coords)
|
||||
-- Double-check if player is owner of this machine before proceeding
|
||||
-- Fast check for owner status
|
||||
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
|
||||
if not isOwner then
|
||||
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter hinzufügen!', 'error')
|
||||
|
@ -644,18 +640,17 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
|
|||
local entity = data.entity
|
||||
local coords = GetEntityCoords(entity)
|
||||
|
||||
-- Double-check if machine is registered and player cannot manage it
|
||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
||||
if not exists then
|
||||
-- Fast check using cached data
|
||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||
if machineData[entityId] then
|
||||
if not machineData[entityId].isRegistered then
|
||||
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
|
||||
return
|
||||
end
|
||||
|
||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
||||
if canManage then
|
||||
elseif machineData[entityId].canManage then
|
||||
QBCore.Functions.Notify('Du kannst deinen eigenen Automaten nicht aufbrechen!', 'error')
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
lib.registerContext({
|
||||
id = 'vending_robbery_confirm',
|
||||
|
@ -678,8 +673,6 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
|
|||
})
|
||||
|
||||
lib.showContext('vending_robbery_confirm')
|
||||
end, coords)
|
||||
end, coords)
|
||||
end)
|
||||
|
||||
-- Start robbery animation and progress
|
||||
|
@ -747,7 +740,7 @@ end)
|
|||
|
||||
-- Management menu (alternative opening method)
|
||||
RegisterNetEvent('vending:client:openManagement', function(machine)
|
||||
-- Double-check if player can manage this machine
|
||||
-- Fast check for management permissions
|
||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
||||
if not canManage then
|
||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||
|
@ -871,4 +864,3 @@ AddEventHandler('onResourceStop', function(resourceName)
|
|||
-- Nothing to do here, but good to have for completeness
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
|
@ -712,4 +712,39 @@ QBCore.Commands.Add('vendingdebug', 'Debug vending machines (Admin Only)', {}, f
|
|||
end
|
||||
end, 'admin')
|
||||
|
||||
-- Combined callback for faster machine status checks
|
||||
QBCore.Functions.CreateCallback('vending:server:getMachineStatus', function(source, cb, coords)
|
||||
local src = source
|
||||
local Player = QBCore.Functions.GetPlayer(src)
|
||||
if not Player then
|
||||
cb(nil)
|
||||
return
|
||||
end
|
||||
|
||||
local machineId = getMachineIdByCoords(coords)
|
||||
if not machineId then
|
||||
cb({exists = false, canManage = false})
|
||||
return
|
||||
end
|
||||
|
||||
local machine = vendingMachines[machineId]
|
||||
local canManage = false
|
||||
|
||||
-- Check if player is owner
|
||||
if machine.owner == Player.PlayerData.citizenid then
|
||||
canManage = true
|
||||
else
|
||||
-- Check if player is manager
|
||||
if machine.managers then
|
||||
for _, manager in pairs(machine.managers) do
|
||||
if manager == Player.PlayerData.citizenid then
|
||||
canManage = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cb({exists = true, canManage = canManage})
|
||||
end)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue