From a51410409b3efddc00ec8458b58b36208e2d020a Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sat, 8 Jun 2024 10:01:26 +1000 Subject: [PATCH] feat(server)!: GetPlayers as array Reverts to old (Lua) behaviour. --- lib/server/player.lua | 4 ++-- lib/server/player.ts | 4 ++-- server/player/class.ts | 20 +++++++++----------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/server/player.lua b/lib/server/player.lua index 70dbb6bf..25787954 100644 --- a/lib/server/player.lua +++ b/lib/server/player.lua @@ -83,8 +83,8 @@ end function Ox.GetPlayers(filter) local players = exports.ox_core:GetPlayers(filter) - for id, player in pairs(players) do - players[id] = CreatePlayerInstance(player) + for i = 1, #players do + players[i] = CreatePlayerInstance(player) end return players diff --git a/lib/server/player.ts b/lib/server/player.ts index 445e02e5..7b51e1dc 100644 --- a/lib/server/player.ts +++ b/lib/server/player.ts @@ -65,12 +65,12 @@ export function GetPlayerFromUserId(userId: number) { return CreatePlayerInstance(exports.ox_core.GetPlayerFromUserId(userId)); } -export function GetPlayers(filter?: Dict) { +export function GetPlayers(filter?: Dict): OxPlayerServer[] { const players = exports.ox_core.GetPlayers(filter); for (const id in players) players[id] = CreatePlayerInstance(players[id]); - return players as Dict; + return players; } export function GetPlayerFromFilter(filter: Dict) { diff --git a/server/player/class.ts b/server/player/class.ts index 03e455c9..fdc20381 100644 --- a/server/player/class.ts +++ b/server/player/class.ts @@ -61,14 +61,10 @@ export class OxPlayer extends ClassInterface { } /** Compares player fields and metadata to a filter, returning the player if all values match. */ - private filter(filter: Dict) { - const groups = filter.groups; + private filter(criteria: Dict) { + const { groups, ...filter } = criteria; - if (groups) { - if (!this.getGroup(groups)) return; - - delete filter.groups; - } + if (groups && !this.getGroup(groups)) return; for (const key in filter) { const value = filter[key]; @@ -88,17 +84,19 @@ export class OxPlayer extends ClassInterface { } /** Gets all instances of OxPlayer, optionally comparing against a filter. */ - static getAll(filter?: Dict): Dict { + static getAll(filter?: Dict, asArray?: false): Dict; + static getAll(filter?: Dict, asArray?: true): PlayerInstance[]; + static getAll(filter?: Dict, asArray = false): Dict | PlayerInstance[] { if (!filter) return this.members; - const obj: Dict = {}; + const obj: Dict = {}; for (const id in this.members) { const player = this.members[id].filter(filter); if (player) obj[id] = player; } - return obj; + return asArray ? Object.values(obj) : obj; } /** Saves all players to the database, and optionally kicks them from the server. */ @@ -611,4 +609,4 @@ OxPlayer.init(); exports('SaveAllPlayers', (arg: any) => OxPlayer.saveAll(arg)); exports('GetPlayerFromUserId', (arg: any) => OxPlayer.getFromUserId(arg)); exports('GetPlayerFromFilter', (arg: any) => OxPlayer.getFromFilter(arg)); -exports(`GetPlayers`, (arg: any) => OxPlayer.getAll(arg)); +exports(`GetPlayers`, (arg: any) => OxPlayer.getAll(arg, true));