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()
|
ClearDrawOrigin()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get machine data with proper callback handling
|
-- Get machine data with optimized callback handling
|
||||||
function GetMachineData(entity)
|
function GetMachineData(entity)
|
||||||
local coords = GetEntityCoords(entity)
|
local coords = GetEntityCoords(entity)
|
||||||
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
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()
|
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
|
-- Initialize with default values
|
||||||
|
if not machineData[entityId] then
|
||||||
machineData[entityId] = {
|
machineData[entityId] = {
|
||||||
isRegistered = false,
|
isRegistered = false,
|
||||||
canManage = false,
|
canManage = false,
|
||||||
lastCheck = currentTime,
|
lastCheck = currentTime,
|
||||||
checking = true
|
checking = true
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
machineData[entityId].checking = true
|
||||||
|
machineData[entityId].lastCheck = currentTime
|
||||||
|
end
|
||||||
|
|
||||||
-- Check if machine is registered
|
-- Single callback to get all machine data at once (more efficient)
|
||||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
QBCore.Functions.TriggerCallback('vending:server:getMachineStatus', function(status)
|
||||||
if exists then
|
if status then
|
||||||
machineData[entityId].isRegistered = true
|
machineData[entityId].isRegistered = status.exists
|
||||||
|
machineData[entityId].canManage = status.canManage
|
||||||
-- Only check management if registered
|
|
||||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(result)
|
|
||||||
machineData[entityId].canManage = result
|
|
||||||
machineData[entityId].checking = false
|
|
||||||
end, coords)
|
|
||||||
else
|
else
|
||||||
machineData[entityId].isRegistered = false
|
machineData[entityId].isRegistered = false
|
||||||
machineData[entityId].canManage = false
|
machineData[entityId].canManage = false
|
||||||
|
end
|
||||||
machineData[entityId].checking = false
|
machineData[entityId].checking = false
|
||||||
end
|
|
||||||
end, coords)
|
end, coords)
|
||||||
end
|
|
||||||
|
|
||||||
-- Wait for callbacks to complete if currently checking
|
-- Short wait for callback to complete
|
||||||
if machineData[entityId].checking then
|
|
||||||
local timeout = 0
|
local timeout = 0
|
||||||
while machineData[entityId].checking and timeout < 50 do
|
while machineData[entityId].checking and timeout < 20 do -- Reduced timeout
|
||||||
Wait(10)
|
Wait(5) -- Shorter wait
|
||||||
timeout = timeout + 1
|
timeout = timeout + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If timeout reached, set checking to false to avoid deadlock
|
-- If timeout reached, set checking to false to avoid deadlock
|
||||||
if timeout >= 50 then
|
if timeout >= 20 then
|
||||||
machineData[entityId].checking = false
|
machineData[entityId].checking = false
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
return machineData[entityId]
|
return machineData[entityId]
|
||||||
end
|
end
|
||||||
|
@ -229,9 +229,9 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
||||||
local entity = data.entity
|
local entity = data.entity
|
||||||
local coords = GetEntityCoords(entity)
|
local coords = GetEntityCoords(entity)
|
||||||
|
|
||||||
-- Double-check if machine is registered before proceeding
|
-- Fast check using cached data
|
||||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||||
if not exists then
|
if machineData[entityId] and not machineData[entityId].isRegistered then
|
||||||
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
|
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -272,7 +272,6 @@ RegisterNetEvent('vending:client:openBuyMenu', function(data)
|
||||||
|
|
||||||
lib.showContext('vending_buy_menu')
|
lib.showContext('vending_buy_menu')
|
||||||
end, coords)
|
end, coords)
|
||||||
end, coords)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Open quantity dialog for buying items
|
-- Open quantity dialog for buying items
|
||||||
|
@ -304,9 +303,9 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
|
||||||
local entity = data.entity
|
local entity = data.entity
|
||||||
local coords = GetEntityCoords(entity)
|
local coords = GetEntityCoords(entity)
|
||||||
|
|
||||||
-- Double-check if player can manage this machine before proceeding
|
-- Fast check using cached data
|
||||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||||
if not canManage then
|
if machineData[entityId] and not machineData[entityId].canManage then
|
||||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -382,7 +381,6 @@ RegisterNetEvent('vending:client:openOwnerMenu', function(data)
|
||||||
|
|
||||||
lib.showContext('vending_owner_menu')
|
lib.showContext('vending_owner_menu')
|
||||||
end, coords)
|
end, coords)
|
||||||
end, coords)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Function to sell the vending machine
|
-- Function to sell the vending machine
|
||||||
|
@ -406,9 +404,9 @@ end
|
||||||
|
|
||||||
-- Open price menu
|
-- Open price menu
|
||||||
function openPriceMenu(coords)
|
function openPriceMenu(coords)
|
||||||
-- Double-check if player can manage this machine before proceeding
|
-- Fast check using cached data
|
||||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||||
if not canManage then
|
if machineData[entityId] and not machineData[entityId].canManage then
|
||||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -443,7 +441,6 @@ function openPriceMenu(coords)
|
||||||
|
|
||||||
lib.showContext('vending_price_menu')
|
lib.showContext('vending_price_menu')
|
||||||
end, coords)
|
end, coords)
|
||||||
end, coords)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Set price for specific item
|
-- Set price for specific item
|
||||||
|
@ -466,9 +463,9 @@ end
|
||||||
|
|
||||||
-- Open withdraw menu
|
-- Open withdraw menu
|
||||||
function openWithdrawMenu(coords, availableMoney)
|
function openWithdrawMenu(coords, availableMoney)
|
||||||
-- Double-check if player can manage this machine before proceeding
|
-- Fast check using cached data
|
||||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||||
if not canManage then
|
if machineData[entityId] and not machineData[entityId].canManage then
|
||||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -492,7 +489,6 @@ function openWithdrawMenu(coords, availableMoney)
|
||||||
if input and input[1] then
|
if input and input[1] then
|
||||||
TriggerServerEvent('vending:server:withdrawMoney', coords, tonumber(input[1]))
|
TriggerServerEvent('vending:server:withdrawMoney', coords, tonumber(input[1]))
|
||||||
end
|
end
|
||||||
end, coords)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Open stats menu
|
-- Open stats menu
|
||||||
|
@ -525,7 +521,7 @@ end
|
||||||
|
|
||||||
-- Open managers menu
|
-- Open managers menu
|
||||||
function openManagersMenu(coords)
|
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)
|
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
|
||||||
if not isOwner then
|
if not isOwner then
|
||||||
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter verwalten!', 'error')
|
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter verwalten!', 'error')
|
||||||
|
@ -598,7 +594,7 @@ end
|
||||||
|
|
||||||
-- Open add manager menu
|
-- Open add manager menu
|
||||||
function openAddManagerMenu(coords)
|
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)
|
QBCore.Functions.TriggerCallback('vending:server:isOwner', function(isOwner)
|
||||||
if not isOwner then
|
if not isOwner then
|
||||||
QBCore.Functions.Notify('Nur der Besitzer kann Verwalter hinzufügen!', 'error')
|
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 entity = data.entity
|
||||||
local coords = GetEntityCoords(entity)
|
local coords = GetEntityCoords(entity)
|
||||||
|
|
||||||
-- Double-check if machine is registered and player cannot manage it
|
-- Fast check using cached data
|
||||||
QBCore.Functions.TriggerCallback('vending:server:machineExists', function(exists)
|
local entityId = tostring(coords.x) .. tostring(coords.y) .. tostring(coords.z)
|
||||||
if not exists then
|
if machineData[entityId] then
|
||||||
|
if not machineData[entityId].isRegistered then
|
||||||
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
|
QBCore.Functions.Notify('Dieser Automat ist nicht registriert!', 'error')
|
||||||
return
|
return
|
||||||
end
|
elseif machineData[entityId].canManage then
|
||||||
|
|
||||||
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
|
||||||
if canManage then
|
|
||||||
QBCore.Functions.Notify('Du kannst deinen eigenen Automaten nicht aufbrechen!', 'error')
|
QBCore.Functions.Notify('Du kannst deinen eigenen Automaten nicht aufbrechen!', 'error')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
lib.registerContext({
|
lib.registerContext({
|
||||||
id = 'vending_robbery_confirm',
|
id = 'vending_robbery_confirm',
|
||||||
|
@ -678,8 +673,6 @@ RegisterNetEvent('vending:client:startRobbery', function(data)
|
||||||
})
|
})
|
||||||
|
|
||||||
lib.showContext('vending_robbery_confirm')
|
lib.showContext('vending_robbery_confirm')
|
||||||
end, coords)
|
|
||||||
end, coords)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Start robbery animation and progress
|
-- Start robbery animation and progress
|
||||||
|
@ -747,7 +740,7 @@ end)
|
||||||
|
|
||||||
-- Management menu (alternative opening method)
|
-- Management menu (alternative opening method)
|
||||||
RegisterNetEvent('vending:client:openManagement', function(machine)
|
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)
|
QBCore.Functions.TriggerCallback('vending:server:canManage', function(canManage)
|
||||||
if not canManage then
|
if not canManage then
|
||||||
QBCore.Functions.Notify('Du hast keine Berechtigung diesen Automaten zu verwalten!', 'error')
|
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
|
-- Nothing to do here, but good to have for completeness
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
|
@ -712,4 +712,39 @@ QBCore.Commands.Add('vendingdebug', 'Debug vending machines (Admin Only)', {}, f
|
||||||
end
|
end
|
||||||
end, 'admin')
|
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