Skip to content

Commit

Permalink
Merge pull request #1501 from Kenshiin13/getextendedplayers-refactor
Browse files Browse the repository at this point in the history
refactor(es_extended/server/functions): refactor ESX.GetExtendedPlayers
  • Loading branch information
Arctos2win authored Nov 24, 2024
2 parents d7f346f + 3a61ae5 commit 556d82e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
30 changes: 16 additions & 14 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,15 @@ end

ESX.GetPlayers = GetPlayers

local function checkTable(key, val, player, xPlayers)
local function checkTable(key, val, xPlayer, xPlayers)
for valIndex = 1, #val do
local value = val[valIndex]
if not xPlayers[value] then
xPlayers[value] = {}
end

if (key == "job" and player.job.name == value) or player[key] == value then
xPlayers[value][#xPlayers[value] + 1] = player
if (key == "job" and xPlayer.job.name == value) or xPlayer[key] == value then
xPlayers[value][#xPlayers[value] + 1] = xPlayer
end
end
end
Expand All @@ -293,20 +293,22 @@ end
---@param val? string|table
---@return table
function ESX.GetExtendedPlayers(key, val)
if not key then
return ESX.Table.ToArray(ESX.Players)
end

local xPlayers = {}
if type(val) == "table" then
for _, v in pairs(ESX.Players) do
checkTable(key, val, v, xPlayers)
for i, xPlayer in pairs(ESX.Players) do
checkTable(key, val, xPlayer, xPlayers)
end
else
for _, v in pairs(ESX.Players) do
if key then
if (key == "job" and v.job.name == val) or v[key] == val then
xPlayers[#xPlayers + 1] = v
end
else
xPlayers[#xPlayers + 1] = v
end

return xPlayers
end

for i, xPlayer in pairs(ESX.Players) do
if (key == "job" and xPlayer.job.name == val) or xPlayer[key] == val then
xPlayers[#xPlayers + 1] = xPlayer
end
end

Expand Down
8 changes: 8 additions & 0 deletions [core]/es_extended/shared/modules/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,11 @@ function ESX.Table.Sort(t, order)
end
end
end

function ESX.Table.ToArray(table)
local array = {}
for _, v in pairs(table) do
array[#array + 1] = v
end
return array
end

0 comments on commit 556d82e

Please sign in to comment.