From 9fdb41e9f65737060a0779f86336561753bc48a0 Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Sun, 31 Aug 2025 19:17:01 -0400 Subject: [PATCH 1/3] Add job count management on player events Introduces event handlers to update job counts when players load, change jobs, or unload across multiple framework modules. Ensures job count tracking stays accurate by adding or removing counts based on player actions. (would like to update this to have some client event tables?) --- modules/framework/_default/server.lua | 10 ++++++++++ modules/framework/es_extended/server.lua | 3 +++ modules/framework/qb-core/server.lua | 3 +++ modules/framework/qbx_core/server.lua | 3 +++ 4 files changed, 19 insertions(+) diff --git a/modules/framework/_default/server.lua b/modules/framework/_default/server.lua index 0cedf7c..5dcdf95 100644 --- a/modules/framework/_default/server.lua +++ b/modules/framework/_default/server.lua @@ -53,4 +53,14 @@ Framework.RemoveJobCount = function(src, jobName) return true, #jobsRegisteredTable[jobName] end +AddEventHandler('community_bridge:Server:OnPlayerJobChange', function(src, newJobName) + local previousJob = Framework.SearchJobCountBySource(src) + if previousJob then Framework.RemoveJobCount(src, previousJob) end + if newJobName then Framework.AddJobCount(src, newJobName) end +end) + +AddEventHandler('community_bridge:Server:OnPlayerUnload', function(src) + Framework.RemoveJobCount(src) +end) + return Framework \ No newline at end of file diff --git a/modules/framework/es_extended/server.lua b/modules/framework/es_extended/server.lua index 6daf30f..40e809b 100644 --- a/modules/framework/es_extended/server.lua +++ b/modules/framework/es_extended/server.lua @@ -452,6 +452,9 @@ end RegisterNetEvent("esx:playerLoaded", function(src) src = src or source TriggerEvent("community_bridge:Server:OnPlayerLoaded", src) + local jobData = Framework.GetPlayerJobData(src) + if not jobData then return end + Framework.AddJobCount(src, jobData.jobName) end) RegisterNetEvent("esx:playerLogout", function(src) diff --git a/modules/framework/qb-core/server.lua b/modules/framework/qb-core/server.lua index 6b8049f..1ba3d10 100644 --- a/modules/framework/qb-core/server.lua +++ b/modules/framework/qb-core/server.lua @@ -557,6 +557,9 @@ end RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src) src = src or source TriggerEvent("community_bridge:Server:OnPlayerLoaded", src) + local jobData = Framework.GetPlayerJobData(src) + if not jobData then return end + Framework.AddJobCount(src, jobData.jobName) end) RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src) diff --git a/modules/framework/qbx_core/server.lua b/modules/framework/qbx_core/server.lua index 47a1ce2..de994dd 100644 --- a/modules/framework/qbx_core/server.lua +++ b/modules/framework/qbx_core/server.lua @@ -467,6 +467,9 @@ end RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src) src = src or source TriggerEvent("community_bridge:Server:OnPlayerLoaded", src) + local jobData = Framework.GetPlayerJobData(src) + if not jobData then return end + Framework.AddJobCount(src, jobData.jobName) end) RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src) From 014dba1012d57e61f0b2ecf16f7be0bfd2269972 Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Sun, 31 Aug 2025 22:49:12 -0400 Subject: [PATCH 2/3] Add job count tracking to framework Introduces job count tracking and retrieval functions to both client and server modules. The server now maintains and updates a global state of job counts, synchronizing changes to clients. Refactors job registration logic for accuracy and consistency, and adds utility functions for total job counts and player source retrieval. This usage with the state may be a problem on initial start for highly populated servers as 200 players load in for example... --- modules/framework/_default/client.lua | 27 +++++++++++ modules/framework/_default/server.lua | 68 ++++++++++++++++++++++----- 2 files changed, 82 insertions(+), 13 deletions(-) diff --git a/modules/framework/_default/client.lua b/modules/framework/_default/client.lua index 5d895be..2bcb7c6 100644 --- a/modules/framework/_default/client.lua +++ b/modules/framework/_default/client.lua @@ -1,3 +1,30 @@ +---@diagnostic disable: duplicate-set-field Framework = Framework or {} +local currentJobCounts = GlobalState.jobcounts or {} + +AddStateBagChangeHandler('jobcounts', 'global', function(bagName, key, value) + if not value then currentJobCounts = {} return end + currentJobCounts = value +end) + +---This is an internal function that will be used to retrieve job counts later. +---@param jobName string +---@return number +Framework.GetJobCount = function(jobName) + if not currentJobCounts[jobName] then return 0 end + return currentJobCounts[jobName] +end + +---This will allow passing a table of job names and returning a sum of the total count. +---@param tbl any +---@return number +Framework.GetJobCountTotal = function(tbl) + local total = 0 + for _, jobName in pairs(tbl) do + total = total + Framework.GetJobCount(jobName) + end + return total +end + return Framework \ No newline at end of file diff --git a/modules/framework/_default/server.lua b/modules/framework/_default/server.lua index 5dcdf95..40161ff 100644 --- a/modules/framework/_default/server.lua +++ b/modules/framework/_default/server.lua @@ -1,13 +1,33 @@ +---@diagnostic disable: duplicate-set-field Framework = Framework or {} local jobsRegisteredTable = {} local invertedJobsRegisteredTable = {} +local globalState = GlobalState + +globalState.jobcounts = {} ---This is an internal function that will be used to retrieve job counts later. ---@param jobName string ---@return number Framework.GetJobCount = function(jobName) - return #jobsRegisteredTable[jobName] or 0 + if not jobsRegisteredTable[jobName] then return 0 end + local count = 0 + for _ in pairs(jobsRegisteredTable[jobName]) do + count = count + 1 + end + return count +end + +---This will allow passing a table of job names and returning a sum of the total count. +---@param tbl any +---@return number +Framework.GetJobCountTotal = function(tbl) + local total = 0 + for _, jobName in pairs(tbl) do + total = total + Framework.GetJobCount(jobName) + end + return total end ---This will return a list of player sources for a given job. @@ -15,7 +35,11 @@ end ---@return table Framework.GetPlayerSourcesByJob = function(jobName) if not jobsRegisteredTable[jobName] then return {} end - return jobsRegisteredTable[jobName] + local sources = {} + for src in pairs(jobsRegisteredTable[jobName]) do + table.insert(sources, src) + end + return sources end ---This will update the cached tables for job counts. @@ -23,18 +47,25 @@ end ---@param src number ---@param jobName string Framework.AddJobCount = function(src, jobName) + if not src or not jobName then return false end if not jobsRegisteredTable[jobName] then jobsRegisteredTable[jobName] = {} end if jobsRegisteredTable[jobName][src] then return false end + jobsRegisteredTable[jobName][src] = true invertedJobsRegisteredTable[src] = jobName - return true, #jobsRegisteredTable[jobName] + + local newJobCounts = {} + for job, _ in pairs(jobsRegisteredTable) do + newJobCounts[job] = Framework.GetJobCount(job) + end + globalState.jobcounts = newJobCounts + + return true end ---This will return the job name for a given source. ----This is an internal function that will be used to retrieve job names in situations where player data is unavailable. ---@param src number Framework.SearchJobCountBySource = function(src) - if not invertedJobsRegisteredTable[src] then return nil end return invertedJobsRegisteredTable[src] end @@ -42,19 +73,30 @@ end ---@param src number ---@param jobName string | nil Framework.RemoveJobCount = function(src, jobName) - if not jobName then - jobName = Framework.SearchJobCountBySource(src) - end + if not src then return false end + + if not jobName then jobName = invertedJobsRegisteredTable[src] end if not jobName then return false end - if not jobsRegisteredTable[jobName] then return false end - if not jobsRegisteredTable[jobName][src] then return false end + invertedJobsRegisteredTable[src] = nil - jobsRegisteredTable[jobName][src] = nil - return true, #jobsRegisteredTable[jobName] + if jobsRegisteredTable[jobName] then + jobsRegisteredTable[jobName][src] = nil + end + + local newJobCounts = {} + for job, _ in pairs(jobsRegisteredTable) do + local count = Framework.GetJobCount(job) + if count > 0 then + newJobCounts[job] = count + end + end + globalState.jobcounts = newJobCounts + + return true end AddEventHandler('community_bridge:Server:OnPlayerJobChange', function(src, newJobName) - local previousJob = Framework.SearchJobCountBySource(src) + local previousJob = invertedJobsRegisteredTable[src] if previousJob then Framework.RemoveJobCount(src, previousJob) end if newJobName then Framework.AddJobCount(src, newJobName) end end) From e9fa5a2891274b9d176186595392077da0de6203 Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Mon, 1 Sep 2025 02:31:14 -0400 Subject: [PATCH 3/3] Fix type annotation for job count function Updated the type annotation for the 'tbl' parameter in Framework.GetJobCountTotal from 'any' to 'table' for improved clarity and correctness. --- modules/framework/_default/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/framework/_default/client.lua b/modules/framework/_default/client.lua index 2bcb7c6..0f6cbb0 100644 --- a/modules/framework/_default/client.lua +++ b/modules/framework/_default/client.lua @@ -17,7 +17,7 @@ Framework.GetJobCount = function(jobName) end ---This will allow passing a table of job names and returning a sum of the total count. ----@param tbl any +---@param tbl table ---@return number Framework.GetJobCountTotal = function(tbl) local total = 0