Skip to content

Commit

Permalink
chore: improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Sep 4, 2024
1 parent cbc1315 commit 9cedac4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
5 changes: 3 additions & 2 deletions lib/client/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class PlayerInterface {
}
}

export type OxPlayerClient = typeof OxPlayer & InstanceType<typeof PlayerInterface>;
const player = new PlayerInterface() as OxPlayerClient;
export type OxPlayer = typeof OxPlayer & PlayerInterface;

const player = new PlayerInterface() as OxPlayer;

export function GetPlayer() {
return player;
Expand Down
8 changes: 4 additions & 4 deletions lib/server/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OxAccount } from 'server/accounts/class';
import type { OxAccount as _OxAccount } from 'server/accounts/class';

class AccountInterface {
constructor(public accountId: number) {}
Expand All @@ -14,12 +14,12 @@ AccountInterface.prototype.toString = function () {
return JSON.stringify(this, null, 2);
};

export type OxAccountServer = OxAccount & AccountInterface;
export type OxAccount = _OxAccount & AccountInterface;

function CreateAccountInstance(account?: OxAccount) {
function CreateAccountInstance(account?: _OxAccount) {
if (!account) return;

return new AccountInterface(account.accountId) as OxAccountServer;
return new AccountInterface(account.accountId) as OxAccount;
}

export async function GetAccount(accountId: number) {
Expand Down
10 changes: 5 additions & 5 deletions lib/server/player.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OxPlayer } from 'server/player/class';
import type { OxPlayer as _OxPlayer } from 'server/player/class';
import type { Dict } from 'types';
import { GetCharacterAccount } from './account';

Expand Down Expand Up @@ -46,9 +46,9 @@ PlayerInterface.prototype.toString = function () {
return JSON.stringify(this, null, 2);
};

export type OxPlayerServer = InstanceType<typeof OxPlayer> & InstanceType<typeof PlayerInterface>;
export type OxPlayer = _OxPlayer & PlayerInterface;

function CreatePlayerInstance(player?: InstanceType<typeof OxPlayer>) {
function CreatePlayerInstance(player?: _OxPlayer) {
if (!player) return;

return new PlayerInterface(
Expand All @@ -59,7 +59,7 @@ function CreatePlayerInstance(player?: InstanceType<typeof OxPlayer>) {
player.username,
player.identifier,
player.ped
) as OxPlayerServer;
) as OxPlayer;
}

export function GetPlayer(playerId: string | number) {
Expand All @@ -70,7 +70,7 @@ export function GetPlayerFromUserId(userId: number) {
return CreatePlayerInstance(exports.ox_core.GetPlayerFromUserId(userId));
}

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

for (const id in players) players[id] = CreatePlayerInstance(players[id]);
Expand Down
8 changes: 4 additions & 4 deletions lib/server/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OxVehicle } from 'server/vehicle/class';
import type { OxVehicle as _OxVehicle } from 'server/vehicle/class';
import type { VehicleProperties } from '@overextended/ox_lib';

class VehicleInterface {
Expand Down Expand Up @@ -45,9 +45,9 @@ VehicleInterface.prototype.toString = function () {
return JSON.stringify(this, null, 2);
};

export type OxVehicleServer = InstanceType<typeof OxVehicle> & InstanceType<typeof VehicleInterface>;
export type OxVehicle = _OxVehicle & VehicleInterface;

function CreateVehicleInstance(vehicle?: InstanceType<typeof OxVehicle>) {
function CreateVehicleInstance(vehicle?: _OxVehicle) {
if (!vehicle) return;

return new VehicleInterface(
Expand All @@ -61,7 +61,7 @@ function CreateVehicleInstance(vehicle?: InstanceType<typeof OxVehicle>) {
vehicle.vin,
vehicle.owner,
vehicle.group
) as OxVehicleServer;
) as OxVehicle;
}

export function GetVehicle(entityId: number) {
Expand Down
14 changes: 6 additions & 8 deletions server/player/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import { PayAccountInvoice } from 'accounts';
import type { Character, Dict, NewCharacter, PlayerMetadata, OxGroup, CharacterLicense } from 'types';
import { GetGroupPermissions } from '../../common';

export type PlayerInstance = InstanceType<typeof OxPlayer>;

export class OxPlayer extends ClassInterface {
source: number | string;
userId: number;
Expand All @@ -44,8 +42,8 @@ export class OxPlayer extends ClassInterface {
#groups: Dict<number>;
#licenses: Dict<CharacterLicense>;

protected static members: Dict<PlayerInstance> = {};
protected static keys: Dict<Dict<PlayerInstance>> = {
protected static members: Dict<OxPlayer> = {};
protected static keys: Dict<Dict<OxPlayer>> = {
userId: {},
charId: {},
};
Expand Down Expand Up @@ -89,12 +87,12 @@ export class OxPlayer extends ClassInterface {
}

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

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

for (const id in this.members) {
const player = this.members[id].filter(filter);
Expand Down
8 changes: 3 additions & 5 deletions server/vehicle/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { GetVehicleData, GetVehicleNetworkType } from '../../common/vehicles';
import { setVehicleProperties } from '@overextended/ox_lib/server';
import { Vector3 } from '@nativewrappers/fivem';

export type VehicleInstance = InstanceType<typeof OxVehicle>;

export class OxVehicle extends ClassInterface {
entity: number;
netId: number;
Expand All @@ -23,8 +21,8 @@ export class OxVehicle extends ClassInterface {
#metadata: Dict<any>;
#stored: string | null;

protected static members: Dict<VehicleInstance> = {};
protected static keys: Dict<Dict<VehicleInstance>> = {
protected static members: Dict<OxVehicle> = {};
protected static keys: Dict<Dict<OxVehicle>> = {
id: {},
netId: {},
vin: {},
Expand Down Expand Up @@ -55,7 +53,7 @@ export class OxVehicle extends ClassInterface {
}

/** Gets all instances of OxVehicle. */
static getAll(): Dict<VehicleInstance> {
static getAll(): Dict<OxVehicle> {
return this.members;
}

Expand Down

0 comments on commit 9cedac4

Please sign in to comment.