Files

119 lines
3.6 KiB
Lua
Raw Permalink Normal View History

2025-08-31 22:49:12 -04:00
---@diagnostic disable: duplicate-set-field
2025-05-09 19:06:50 -05:00
Framework = Framework or {}
2025-08-07 00:47:51 -04:00
local jobsRegisteredTable = {}
local invertedJobsRegisteredTable = {}
2025-08-31 22:49:12 -04:00
local globalState = GlobalState
globalState.jobcounts = {}
2025-08-07 00:47:51 -04:00
---@description This will get the name of the in use resource.
2025-09-10 12:27:12 -04:00
---@return string
Framework.GetResourceName = function()
return 'default'
end
---@description This is an internal function that will be used to retrieve job counts later.
2025-08-07 00:47:51 -04:00
---@param jobName string
---@return number
Framework.GetJobCount = function(jobName)
2025-08-31 22:49:12 -04:00
if not jobsRegisteredTable[jobName] then return 0 end
local count = 0
for _ in pairs(jobsRegisteredTable[jobName]) do
count = count + 1
end
return count
end
---@description This will allow passing a table of job names and returning a sum of the total count.
2025-08-31 22:49:12 -04:00
---@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
2025-08-07 00:47:51 -04:00
end
---@description This will return a list of player sources for a given job.
2025-08-07 00:47:51 -04:00
---@param jobName string
---@return table
Framework.GetPlayerSourcesByJob = function(jobName)
if not jobsRegisteredTable[jobName] then return {} end
2025-08-31 22:49:12 -04:00
local sources = {}
for src in pairs(jobsRegisteredTable[jobName]) do
table.insert(sources, src)
end
return sources
2025-08-07 00:47:51 -04:00
end
---@description This will update the cached tables for job counts.
2025-08-07 00:47:51 -04:00
---This is used to track how many players are in a job.
---@param src number
---@param jobName string
Framework.AddJobCount = function(src, jobName)
2025-08-31 22:49:12 -04:00
if not src or not jobName then return false end
2025-08-07 00:47:51 -04:00
if not jobsRegisteredTable[jobName] then jobsRegisteredTable[jobName] = {} end
if jobsRegisteredTable[jobName][src] then return false end
2025-08-31 22:49:12 -04:00
2025-08-07 00:47:51 -04:00
jobsRegisteredTable[jobName][src] = true
invertedJobsRegisteredTable[src] = jobName
2025-08-31 22:49:12 -04:00
local newJobCounts = {}
for job, _ in pairs(jobsRegisteredTable) do
newJobCounts[job] = Framework.GetJobCount(job)
end
globalState.jobcounts = newJobCounts
return true
2025-08-07 00:47:51 -04:00
end
---@description This will return the job name for a given source.
2025-08-07 00:47:51 -04:00
---@param src number
Framework.SearchJobCountBySource = function(src)
return invertedJobsRegisteredTable[src]
end
---@description This will remove the job count for a given source.
2025-08-07 00:47:51 -04:00
---@param src number
---@param jobName string | nil
Framework.RemoveJobCount = function(src, jobName)
2025-08-31 22:49:12 -04:00
if not src then return false end
if not jobName then jobName = invertedJobsRegisteredTable[src] end
2025-08-07 00:47:51 -04:00
if not jobName then return false end
2025-08-31 22:49:12 -04:00
2025-08-07 00:47:51 -04:00
invertedJobsRegisteredTable[src] = nil
2025-08-31 22:49:12 -04:00
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
2025-08-07 00:47:51 -04:00
end
---@description Event handler for when a player's job changes, updates job counts accordingly
---@param src number
---@param newJobName string
2025-08-31 19:17:01 -04:00
AddEventHandler('community_bridge:Server:OnPlayerJobChange', function(src, newJobName)
2025-08-31 22:49:12 -04:00
local previousJob = invertedJobsRegisteredTable[src]
2025-08-31 19:17:01 -04:00
if previousJob then Framework.RemoveJobCount(src, previousJob) end
if newJobName then Framework.AddJobCount(src, newJobName) end
end)
---@description Event handler for when a player unloads, removes them from job counts
---@param src number
2025-08-31 19:17:01 -04:00
AddEventHandler('community_bridge:Server:OnPlayerUnload', function(src)
Framework.RemoveJobCount(src)
end)
return Framework