diff --git a/resources/[standalone]/ps-multijob/client/cl_main.lua b/resources/[standalone]/ps-multijob/client/cl_main.lua index 15efc8ae7..2808a3e48 100644 --- a/resources/[standalone]/ps-multijob/client/cl_main.lua +++ b/resources/[standalone]/ps-multijob/client/cl_main.lua @@ -1,5 +1,7 @@ local QBCore = exports['qb-core']:GetCoreObject() local isJobMenuOpen = false +local lastJobCheck = 0 +local checkInterval = 60000 -- Check for new jobs every 60 seconds local function GetJobs() local p = promise.new() @@ -20,6 +22,19 @@ local function OpenUI() jobs = GetJobs(), side = Config.Side, }) + lastJobCheck = GetGameTimer() -- Reset the timer when we open the UI +end + +-- Function to check for job updates +local function CheckForJobUpdates() + if isJobMenuOpen and (GetGameTimer() - lastJobCheck) > checkInterval then + lastJobCheck = GetGameTimer() + local jobs = GetJobs() + SendNUIMessage({ + action = 'updateJobList', + jobs = jobs + }) + end end RegisterNUICallback('selectjob', function(data, cb) @@ -81,3 +96,19 @@ RegisterNetEvent('ps-multijob:refreshJobs', function() OpenUI() -- Refresh the UI with updated job data end end) + +-- Request job updates from server periodically +Citizen.CreateThread(function() + while true do + Citizen.Wait(checkInterval) + TriggerServerEvent('ps-multijob:requestJobUpdate') + end +end) + +-- Check for job updates while menu is open +Citizen.CreateThread(function() + while true do + Citizen.Wait(1000) -- Check every second if we need to update + CheckForJobUpdates() + end +end) diff --git a/resources/[standalone]/ps-multijob/server/sv_main.lua b/resources/[standalone]/ps-multijob/server/sv_main.lua index a35e76c7b..78f52f034 100644 --- a/resources/[standalone]/ps-multijob/server/sv_main.lua +++ b/resources/[standalone]/ps-multijob/server/sv_main.lua @@ -1,4 +1,5 @@ local QBCore = exports['qb-core']:GetCoreObject() +local jobUpdateInterval = 60000 -- 60 seconds local function GetJobs(citizenid) local p = promise.new() @@ -38,6 +39,12 @@ local function AddJob(citizenid, job, grade) citizenid = citizenid, jobdata = json.encode(jobs), }) + + -- Notify the player if they're online + local Player = QBCore.Functions.GetPlayerByCitizenId(citizenid) + if Player then + TriggerClientEvent('ps-multijob:refreshJobs', Player.PlayerData.source) + end end exports("AddJob", AddJob) @@ -93,6 +100,12 @@ local function UpdateJobRank(citizenid, job, grade) if Player.PlayerData.job.name == job then UpdatePlayerJob(Player, job, grade) end + + -- Notify the player if they're online + local OnlinePlayer = QBCore.Functions.GetPlayerByCitizenId(citizenid) + if OnlinePlayer then + TriggerClientEvent('ps-multijob:refreshJobs', OnlinePlayer.PlayerData.source) + end end exports("UpdateJobRank", UpdateJobRank) @@ -126,6 +139,12 @@ local function RemoveJob(citizenid, job) citizenid = citizenid, jobdata = json.encode(jobs), }) + + -- Notify the player if they're online + local OnlinePlayer = QBCore.Functions.GetPlayerByCitizenId(citizenid) + if OnlinePlayer then + TriggerClientEvent('ps-multijob:refreshJobs', OnlinePlayer.PlayerData.source) + end end exports("RemoveJob", RemoveJob) @@ -257,6 +276,15 @@ RegisterNetEvent("ps-multijob:removeJob",function(job, grade) RemoveJob(Player.PlayerData.citizenid, job) end) +-- Handle client requests for job updates +RegisterNetEvent('ps-multijob:requestJobUpdate', function() + local source = source + local Player = QBCore.Functions.GetPlayer(source) + if Player then + TriggerClientEvent('ps-multijob:refreshJobs', source) + end +end) + -- QBCORE EVENTS RegisterNetEvent('ps-multijob:server:removeJob', function(targetCitizenId) @@ -307,3 +335,20 @@ RegisterNetEvent("jobs_creator:injectJobs", function(jobs) end end end) + +-- Periodically check for job updates and notify all players +Citizen.CreateThread(function() + while true do + Citizen.Wait(jobUpdateInterval) + + -- Check for any job updates in the database + local Players = QBCore.Functions.GetPlayers() + for i = 1, #Players do + local Player = QBCore.Functions.GetPlayer(Players[i]) + if Player then + -- This will trigger the client to refresh their job list if the menu is open + TriggerClientEvent('ps-multijob:refreshJobs', Players[i]) + end + end + end +end)