Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GetOnDuty exports #1162

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ function QBCore.Functions.GetClosestObject(coords)
return closestObject, closestDistance
end

function QBCore.Functions.GetDutyCount(jobstring, istype)
return GlobalState.dutyInfo[istype and 'type_'..jobstring or jobstring] or 0
end

-- Vehicle

function QBCore.Functions.LoadModel(model)
Expand Down
16 changes: 16 additions & 0 deletions server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ RegisterNetEvent('QBCore:CallCommand', function(command, args)
end
end)

RegisterNetEvent('QBCore:Server:OnPlayerUnload', function (source)
QBCore.Functions.CalculateDutyInfo(source)
end)

RegisterNetEvent('QBCore:Server:PlayerLoaded', function ()
QBCore.Functions.CalculateDutyInfo()
end)

RegisterNetEvent('QBCore:Server:OnJobUpdate', function ()
QBCore.Functions.CalculateDutyInfo()
end)

RegisterNetEvent('QBCore:Server:PlayerDropped', function (Player)
QBCore.Functions.CalculateDutyInfo(Player.PlayerData.source)
end)

-- Use this for player vehicle spawning
-- Vehicle server-side spawning callback (netId)
-- use the netid on the client with the NetworkGetEntityFromNetworkId native
Expand Down
30 changes: 20 additions & 10 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,31 @@ function QBCore.Functions.GetPlayersOnDuty(job)
return players, count
end

---Returns only the amount of players on duty for the specified job
---@param job string
---Returns only the amount of players on duty for the specified job or job type
---@param jobstring string
---@param istype? boolean
---@return number
function QBCore.Functions.GetDutyCount(job)
local count = 0
function QBCore.Functions.GetDutyCount(jobstring, istype)
return GlobalState.dutyInfo[istype and 'type_'..jobstring or jobstring] or 0
end

--- Recalculate the duty info for all players
--- @param exclude number?
function QBCore.Functions.CalculateDutyInfo(exclude)
if exclude then exclude = tonumber(exclude) end
if not GlobalState.dutyInfo then GlobalState.dutyInfo = {} end
local dutyInfo = {}
for _, Player in pairs(QBCore.Players) do
if Player.PlayerData.job.name == job then
if Player.PlayerData.job.onduty then
count += 1
end
end
if exclude and exclude == Player.PlayerData.source then goto continue end
local job = Player.PlayerData.job
dutyInfo[job.name] = (dutyInfo[job.name] or 0) + (job.onduty and 1 or 0)
if job.type and job.type ~= 'none' then dutyInfo['type_'..job.type] = (dutyInfo['type_'..job.type] or 0) + (job.onduty and 1 or 0) end
GlobalState:set('dutyInfo', dutyInfo, true)
::continue::
end
return count
end


--- @param source number source player's server ID.
--- @param coords vector The coordinates to calculate the distance from. Can be a table with x, y, z fields or a vector3. If not provided, the source player's Ped's coordinates are used.
--- @return string closestPlayer - The Player that is closest to the source player (or the provided coordinates). Returns -1 if no Players are found.
Expand Down
Loading