5 files changed
+100
-12
No files matched your search
@@ -1,3 +1,30 @@
|
|||||||
|
---@diagnostic disable: duplicate-set-field
|
||||||
Framework = Framework or {}
|
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 table
|
||||||
|
---@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
|
return Framework
|
||||||
@@ -1,13 +1,33 @@
|
|||||||
|
---@diagnostic disable: duplicate-set-field
|
||||||
Framework = Framework or {}
|
Framework = Framework or {}
|
||||||
|
|
||||||
local jobsRegisteredTable = {}
|
local jobsRegisteredTable = {}
|
||||||
local invertedJobsRegisteredTable = {}
|
local invertedJobsRegisteredTable = {}
|
||||||
|
local globalState = GlobalState
|
||||||
|
|
||||||
|
globalState.jobcounts = {}
|
||||||
|
|
||||||
---This is an internal function that will be used to retrieve job counts later.
|
---This is an internal function that will be used to retrieve job counts later.
|
||||||
---@param jobName string
|
---@param jobName string
|
||||||
---@return number
|
---@return number
|
||||||
Framework.GetJobCount = function(jobName)
|
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
|
end
|
||||||
|
|
||||||
---This will return a list of player sources for a given job.
|
---This will return a list of player sources for a given job.
|
||||||
@@ -15,7 +35,11 @@ end
|
|||||||
---@return table
|
---@return table
|
||||||
Framework.GetPlayerSourcesByJob = function(jobName)
|
Framework.GetPlayerSourcesByJob = function(jobName)
|
||||||
if not jobsRegisteredTable[jobName] then return {} end
|
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
|
end
|
||||||
|
|
||||||
---This will update the cached tables for job counts.
|
---This will update the cached tables for job counts.
|
||||||
@@ -23,18 +47,25 @@ end
|
|||||||
---@param src number
|
---@param src number
|
||||||
---@param jobName string
|
---@param jobName string
|
||||||
Framework.AddJobCount = function(src, jobName)
|
Framework.AddJobCount = function(src, jobName)
|
||||||
|
if not src or not jobName then return false end
|
||||||
if not jobsRegisteredTable[jobName] then jobsRegisteredTable[jobName] = {} end
|
if not jobsRegisteredTable[jobName] then jobsRegisteredTable[jobName] = {} end
|
||||||
if jobsRegisteredTable[jobName][src] then return false end
|
if jobsRegisteredTable[jobName][src] then return false end
|
||||||
|
|
||||||
jobsRegisteredTable[jobName][src] = true
|
jobsRegisteredTable[jobName][src] = true
|
||||||
invertedJobsRegisteredTable[src] = jobName
|
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
|
end
|
||||||
|
|
||||||
---This will return the job name for a given source.
|
---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
|
---@param src number
|
||||||
Framework.SearchJobCountBySource = function(src)
|
Framework.SearchJobCountBySource = function(src)
|
||||||
if not invertedJobsRegisteredTable[src] then return nil end
|
|
||||||
return invertedJobsRegisteredTable[src]
|
return invertedJobsRegisteredTable[src]
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -42,15 +73,36 @@ end
|
|||||||
---@param src number
|
---@param src number
|
||||||
---@param jobName string | nil
|
---@param jobName string | nil
|
||||||
Framework.RemoveJobCount = function(src, jobName)
|
Framework.RemoveJobCount = function(src, jobName)
|
||||||
if not jobName then
|
if not src then return false end
|
||||||
jobName = Framework.SearchJobCountBySource(src)
|
|
||||||
end
|
if not jobName then jobName = invertedJobsRegisteredTable[src] end
|
||||||
if not jobName then return false 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
|
invertedJobsRegisteredTable[src] = nil
|
||||||
jobsRegisteredTable[jobName][src] = nil
|
if jobsRegisteredTable[jobName] then
|
||||||
return true, #jobsRegisteredTable[jobName]
|
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
|
end
|
||||||
|
|
||||||
|
AddEventHandler('community_bridge:Server:OnPlayerJobChange', function(src, newJobName)
|
||||||
|
local previousJob = invertedJobsRegisteredTable[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
|
return Framework
|
||||||
@@ -452,6 +452,9 @@ end
|
|||||||
RegisterNetEvent("esx:playerLoaded", function(src)
|
RegisterNetEvent("esx:playerLoaded", function(src)
|
||||||
src = src or source
|
src = src or source
|
||||||
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
|
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
|
||||||
|
local jobData = Framework.GetPlayerJobData(src)
|
||||||
|
if not jobData then return end
|
||||||
|
Framework.AddJobCount(src, jobData.jobName)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNetEvent("esx:playerLogout", function(src)
|
RegisterNetEvent("esx:playerLogout", function(src)
|
||||||
|
|||||||
@@ -557,6 +557,9 @@ end
|
|||||||
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
|
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
|
||||||
src = src or source
|
src = src or source
|
||||||
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
|
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
|
||||||
|
local jobData = Framework.GetPlayerJobData(src)
|
||||||
|
if not jobData then return end
|
||||||
|
Framework.AddJobCount(src, jobData.jobName)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
|
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
|
||||||
|
|||||||
@@ -467,6 +467,9 @@ end
|
|||||||
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
|
RegisterNetEvent("QBCore:Server:OnPlayerLoaded", function(src)
|
||||||
src = src or source
|
src = src or source
|
||||||
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
|
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
|
||||||
|
local jobData = Framework.GetPlayerJobData(src)
|
||||||
|
if not jobData then return end
|
||||||
|
Framework.AddJobCount(src, jobData.jobName)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
|
RegisterNetEvent("QBCore:Server:OnPlayerUnload", function(src)
|
||||||
|
|||||||
Reference in new issue
Block a user