Skip to content

Commit

Permalink
feat(server)!: GetPlayers as array
Browse files Browse the repository at this point in the history
Reverts to old (Lua) behaviour.
  • Loading branch information
thelindat committed Jun 8, 2024
1 parent 4c94add commit a514104
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/server/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export function GetPlayerFromUserId(userId: number) {
return CreatePlayerInstance(exports.ox_core.GetPlayerFromUserId(userId));
}

export function GetPlayers(filter?: Dict<any>) {
export function GetPlayers(filter?: Dict<any>): OxPlayerServer[] {
const players = exports.ox_core.GetPlayers(filter);

for (const id in players) players[id] = CreatePlayerInstance(players[id]);

return players as Dict<PlayerInterface & OxPlayer>;
return players;
}

export function GetPlayerFromFilter(filter: Dict<any>) {
Expand Down
20 changes: 9 additions & 11 deletions server/player/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) {
const groups = filter.groups;
private filter(criteria: Dict<any>) {
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];
Expand All @@ -88,17 +84,19 @@ export class OxPlayer extends ClassInterface {
}

/** Gets all instances of OxPlayer, optionally comparing against a filter. */
static getAll(filter?: Dict<any>): Dict<OxPlayer> {
static getAll(filter?: Dict<any>, asArray?: false): Dict<PlayerInstance>;
static getAll(filter?: Dict<any>, asArray?: true): PlayerInstance[];
static getAll(filter?: Dict<any>, asArray = false): Dict<PlayerInstance> | PlayerInstance[] {
if (!filter) return this.members;

const obj: Dict<OxPlayer> = {};
const obj: Dict<PlayerInstance> = {};

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. */
Expand Down Expand Up @@ -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));

0 comments on commit a514104

Please sign in to comment.