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] 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