From 7a0567efac6d4e93983e9c89e4ca86d66b89a640 Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Fri, 23 May 2025 04:13:17 -0400 Subject: [PATCH 1/2] feat(core): add functions to retrieve online players by job name or type with optional on-duty check Adds two utility functions to retrieve online players by either job name or job type, with optional filtering for on-duty status. While the logic is a bit nested, this approach avoids unnecessary function splitting and keeps related behavior in a single place. Although I'm not entirely satisfied with the nesting, this structure felt like a reasonable trade-off between readability and reusability. This method could also serve as a foundation for deprecating older job-check functions, should the maintainer choose to consolidate similar logic in the future. --- server/functions.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/server/functions.lua b/server/functions.lua index a7f203a..60e7ed6 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -150,6 +150,52 @@ function QBCore.Functions.GetPlayersOnDuty(job) return players, count end +---Gets a list of all online players of a specified job and the number, with an option to check if they are on duty. +---@param job string +---@param checkOnDuty boolean +---@return table, number +function QBCore.Functions.GetPlayersByJobName(job, checkOnDuty) + local players = {} + local count = 0 + for src, Player in pairs(QBCore.Players) do + if Player.PlayerData.job.name == job then + if checkOnDuty then + if Player.PlayerData.job.onduty then + players[#players + 1] = src + count += 1 + end + else + players[#players + 1] = src + count += 1 + end + end + end + return players, count +end + +---Gets a list of all online players with a specified job type and the number, with an option to check if they are on duty. +---@param jobType string +---@param checkOnDuty boolean +---@return table, number +function QBCore.Functions.GetPlayersByJobType(jobType, checkOnDuty) + local players = {} + local count = 0 + for src, Player in pairs(QBCore.Players) do + if Player.PlayerData.job.type == jobType then + if checkOnDuty then + if Player.PlayerData.job.onduty then + players[#players + 1] = src + count += 1 + end + else + players[#players + 1] = src + count += 1 + end + end + end + return players, count +end + ---Returns only the amount of players on duty for the specified job ---@param job string ---@return number From d19f34ba096cfe716aefa8851cf589bd66ea8e9f Mon Sep 17 00:00:00 2001 From: MrNewb <47620135+MrNewb@users.noreply.github.com> Date: Sun, 22 Jun 2025 15:45:03 -0400 Subject: [PATCH 2/2] update --- server/functions.lua | 89 +++++++++++++------------------------------- 1 file changed, 25 insertions(+), 64 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 7f0abea..c01aed2 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -132,66 +132,34 @@ function QBCore.Functions.GetQBPlayers() return QBCore.Players end +--- Gets a list of all online players of a specified job or job type and the number +--- @param job string +--- @param checkOnDuty boolean If true, only players on duty will be returned +function QBCore.Functions.GetPlayersByJob(job, checkOnDuty) + local players = {} + local count = 0 + for src, Player in pairs(QBCore.Players) do + local playerData = Player.PlayerData + if playerData.job.name == job or playerData.job.type == job then + if checkOnDuty then + if playerData.job.onduty then + players[#players + 1] = src + count += 1 + end + else + players[#players + 1] = src + count += 1 + end + end + end + return players, count +end + ---Gets a list of all on duty players of a specified job and the number ---@param job string ---@return table, number function QBCore.Functions.GetPlayersOnDuty(job) - local players = {} - local count = 0 - for src, Player in pairs(QBCore.Players) do - if Player.PlayerData.job.name == job then - if Player.PlayerData.job.onduty then - players[#players + 1] = src - count += 1 - end - end - end - return players, count -end - ----Gets a list of all online players of a specified job and the number, with an option to check if they are on duty. ----@param job string ----@param checkOnDuty boolean ----@return table, number -function QBCore.Functions.GetPlayersByJobName(job, checkOnDuty) - local players = {} - local count = 0 - for src, Player in pairs(QBCore.Players) do - if Player.PlayerData.job.name == job then - if checkOnDuty then - if Player.PlayerData.job.onduty then - players[#players + 1] = src - count += 1 - end - else - players[#players + 1] = src - count += 1 - end - end - end - return players, count -end - ----Gets a list of all online players with a specified job type and the number, with an option to check if they are on duty. ----@param jobType string ----@param checkOnDuty boolean ----@return table, number -function QBCore.Functions.GetPlayersByJobType(jobType, checkOnDuty) - local players = {} - local count = 0 - for src, Player in pairs(QBCore.Players) do - if Player.PlayerData.job.type == jobType then - if checkOnDuty then - if Player.PlayerData.job.onduty then - players[#players + 1] = src - count += 1 - end - else - players[#players + 1] = src - count += 1 - end - end - end + local players, count = QBCore.Functions.GetPlayersByJob(job, true) return players, count end @@ -199,14 +167,7 @@ end ---@param job string ---@return number function QBCore.Functions.GetDutyCount(job) - local count = 0 - for _, Player in pairs(QBCore.Players) do - if Player.PlayerData.job.name == job then - if Player.PlayerData.job.onduty then - count += 1 - end - end - end + local _, count = QBCore.Functions.GetPlayersByJob(job, true) return count end