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

Expose arrestedPlayers table globally #3234

Merged
merged 3 commits into from
Sep 23, 2023
Merged
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
36 changes: 36 additions & 0 deletions gamemode/modules/police/sv_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ function plyMeta:unArrest(unarrester, teleportOverride)
hook.Call("playerUnArrested", DarkRP.hooks, self, unarrester, teleportOverride)
end

function DarkRP.iterateArrestedPlayers()
local index = nil
local function iterator()
local found_player = nil
index = next(arrestedPlayers, index)

if index == nil then return end

found_player = player.GetBySteamID(index)
-- player.GetBySteamID returns false when the player is not in the
-- server. In that case, skip the player.
if not found_player then return iterator() end

return found_player
end
return iterator
end

function DarkRP.arrestedPlayers()
local result = {}
for ply in DarkRP.iterateArrestedPlayers() do
table.insert(result, ply)
end

return result
end


function DarkRP.arrestedPlayerCount()
local count = 0

for _ in DarkRP.iterateArrestedPlayers() do count = count + 1 end

return count
end

--[[---------------------------------------------------------------------------
Chat commands
---------------------------------------------------------------------------]]
Expand Down
48 changes: 48 additions & 0 deletions gamemode/modules/police/sv_interface.lua
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,54 @@ DarkRP.PLAYER.unArrest = DarkRP.stub{
metatable = DarkRP.PLAYER
}

DarkRP.iterateArrestedPlayers = DarkRP.stub{
name = "iterateArrestedPlayers",
description = "An iterator that walks over the arrested players. Use as follows: for arrestedPlayer in DarkRP.iterateArrestedPlayers() do print(arrestedPlayer) end",
parameters = {
},
returns = {
{
name = "arrestedPlayer",
description = "Much like the next function, this returns the next arrested player until the table is fully iterated.",
type = "Player"
}
},
metatable = DarkRP,
realm = "Server"
}

DarkRP.arrestedPlayers = DarkRP.stub{
name = "arrestedPlayers",
description = "Returns a table that contains all arrested players. NOTE: This function is defined using DarkRP.iterateArrestedPlayers. It might be more efficient to use that function instead, because this function builds the table anew.",
parameters = {
},
returns = {
{
name = "arrestedPlayers",
description = "An array of arrested players, in no particular order.",
type = "table"
}
},
metatable = DarkRP,
realm = "Server"
}

DarkRP.arrestedPlayerCount = DarkRP.stub{
name = "arrestedPlayerCount",
description = "Returns the amount of players that are currently arrested.",
parameters = {
},
returns = {
{
name = "arrestedPlayerCount",
description = "The amount of arrested players.",
type = "number"
}
},
metatable = DarkRP,
realm = "Server"
}

DarkRP.hookStub{
name = "playerArrested",
description = "When a player is arrested.",
Expand Down