Skip to content

Commit

Permalink
refactor(core)!: remove partial generics and instances param
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Jan 31, 2024
1 parent adc3586 commit 1adf6c5
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 124 deletions.
22 changes: 10 additions & 12 deletions packages/core/src/controllers/netstats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,34 @@ export class NetStats {
static getNetworkStats(): string {
return f.GetNetworkStats();
}
static getPlayerNetworkStats<P extends Player>(player: P): string {
static getPlayerNetworkStats(player: Player): string {
return f.GetPlayerNetworkStats(player.id);
}
static getBytesReceived<P extends Player>(player: P): number {
static getBytesReceived(player: Player): number {
return f.NetStats_BytesReceived(player.id);
}
static getBytesSent<P extends Player>(player: P): number {
static getBytesSent(player: Player): number {
return f.NetStats_BytesSent(player.id);
}
static getConnectionStatus<P extends Player>(
player: P
): ConnectionStatusEnum {
static getConnectionStatus(player: Player): ConnectionStatusEnum {
return f.NetStats_ConnectionStatus(player.id);
}
static getConnectedTime<P extends Player>(player: P): number {
static getConnectedTime(player: Player): number {
return f.NetStats_GetConnectedTime(player.id);
}
static getIpPort<P extends Player>(player: P): string {
static getIpPort(player: Player): string {
return f.NetStats_GetIpPort(player.id);
}
static getMessagesReceived<P extends Player>(player: P): number {
static getMessagesReceived(player: Player): number {
return f.NetStats_MessagesReceived(player.id);
}
static getMessagesRecvPerSecond<P extends Player>(player: P): number {
static getMessagesRecvPerSecond(player: Player): number {
return f.NetStats_MessagesRecvPerSecond(player.id);
}
static getMessagesSent<P extends Player>(player: P): number {
static getMessagesSent(player: Player): number {
return f.NetStats_MessagesSent(player.id);
}
static getPacketLossPercent<P extends Player>(player: P): number {
static getPacketLossPercent(player: Player): number {
return f.NetStats_PacketLossPercent(player.id);
}
}
66 changes: 34 additions & 32 deletions packages/core/src/controllers/vehicle/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CarModTypeEnum, VehicleModelInfoEnum } from "core/enums";
import { LimitsEnum } from "core/enums";
import type { TPos } from "core/types";
import type { IVehicle } from "core/interfaces";
import type { Player } from "../player";
import { Player } from "../player";
import { isValidPaintJob, isValidVehComponent } from "core/utils/vehicleUtils";
import * as v from "core/wrapper/native/functions";

Expand Down Expand Up @@ -143,19 +143,19 @@ export class Vehicle {
if (this.id === -1) return 0;
return v.SetVehicleHealth(this.id, health);
}
isPlayerIn<P extends Player>(player: P): boolean {
isPlayerIn(player: Player): boolean {
if (this.id === -1) return false;
return v.IsPlayerInVehicle(player.id, this.id);
}
putPlayerIn<P extends Player>(player: P, seatid: number): number {
putPlayerIn(player: Player, seatId: number): number {
if (this.id === -1) return 0;
if (seatid < 0) return 0;
if (seatid > 4) {
if (seatId < 0) return 0;
if (seatId > 4) {
logger.warn(
"[Vehicle]: If the seat is invalid or is taken, will cause a crash when they EXIT the vehicle."
);
}
return v.PutPlayerInVehicle(player.id, this.id, seatid);
return v.PutPlayerInVehicle(player.id, this.id, seatId);
}
getZAngle(): number {
if (this.id === -1) return 0;
Expand Down Expand Up @@ -216,14 +216,14 @@ export class Vehicle {
return v.GetVehicleModel(this.id);
}
static getModelInfo(
vehiclemodel: number,
infotype: VehicleModelInfoEnum
vehicleModel: number,
infoType: VehicleModelInfoEnum
): TPos {
return v.GetVehicleModelInfo(vehiclemodel, infotype);
return v.GetVehicleModelInfo(vehicleModel, infoType);
}
getModelInfo(infotype: VehicleModelInfoEnum): void | TPos {
getModelInfo(infoType: VehicleModelInfoEnum): void | TPos {
if (this.id === -1) return;
return Vehicle.getModelInfo(this.getModel(), infotype);
return Vehicle.getModelInfo(this.getModel(), infoType);
}
getRotationQuat() {
if (this.id === -1) return;
Expand All @@ -234,38 +234,38 @@ export class Vehicle {
if (this.id === -1) return 0;
return v.SetVehicleToRespawn(this.id);
}
isStreamedIn<P extends Player>(forplayer: P): boolean {
isStreamedIn(forPlayer: Player): boolean {
if (this.id === -1) return false;
return v.IsVehicleStreamedIn(this.id, forplayer.id);
return v.IsVehicleStreamedIn(this.id, forPlayer.id);
}
setParamsCarDoors(
driver: boolean,
passenger: boolean,
backleft: boolean,
backright: boolean
backLeft: boolean,
backRight: boolean
): number {
if (this.id === -1) return 0;
return v.SetVehicleParamsCarDoors(
this.id,
driver,
passenger,
backleft,
backright
backLeft,
backRight
);
}
setParamsCarWindows(
driver: boolean,
passenger: boolean,
backleft: boolean,
backright: boolean
backLeft: boolean,
backRight: boolean
) {
if (this.id === -1) return 0;
return v.SetVehicleParamsCarWindows(
this.id,
driver,
passenger,
backleft,
backright
backLeft,
backRight
);
}
getParamsCarDoors() {
Expand Down Expand Up @@ -307,17 +307,17 @@ export class Vehicle {
if (this.id === -1) return -2;
return v.GetVehicleParamsSirenState(this.id);
}
setParamsForPlayer<P extends Player>(
player: P,
setParamsForPlayer(
player: Player,
objective: boolean,
doorslocked: boolean
doorsLocked: boolean
): number {
if (this.id === -1) return 0;
return v.SetVehicleParamsForPlayer(
this.id,
player.id,
objective,
doorslocked
doorsLocked
);
}
isTrailerAttached(): boolean {
Expand All @@ -331,17 +331,19 @@ export class Vehicle {
v.ChangeVehiclePaintjob(this.id, paintjobId);
return 1;
}
attachTrailer<V extends Vehicle>(trailer: V): number {
attachTrailer(trailer: Vehicle): number {
if (this.id === -1) return 0;
return v.AttachTrailerToVehicle(trailer.id, this.id);
}
detachTrailer() {
if (this.id === -1) return;
if (this.isTrailerAttached()) v.DetachTrailerFromVehicle(this.id);
}
getTrailer<V extends Vehicle>(vehicles: Array<V>): V | undefined {
getTrailer() {
if (this.id === -1) return;
return vehicles.find((_v) => _v.id === v.GetVehicleTrailer(this.id));
return Vehicle.getInstances().find(
(_v) => _v.id === v.GetVehicleTrailer(this.id)
);
}
isValid(): boolean {
return v.IsValidVehicle(this.id);
Expand Down Expand Up @@ -370,13 +372,13 @@ export class Vehicle {
return w.GetVehicleSirenState(this.id);
}
static getModelsUsed = w.GetVehicleModelsUsed;
getDriver<P extends Player>(players: Map<number, P>): P | undefined {
getDriver() {
if (this.id === -1) return;
return players.get(w.GetVehicleDriver(this.id));
return Player.players.get(w.GetVehicleDriver(this.id));
}
getLastDriver<P extends Player>(players: Map<number, P>): P | undefined {
getLastDriver() {
if (this.id === -1) return;
return players.get(w.GetVehicleLastDriver(this.id));
return Player.players.get(w.GetVehicleLastDriver(this.id));
}
static getModelCount = w.GetVehicleModelCount;
isSirenEnabled(): boolean {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Player } from "core/controllers/player/entity";
import type {
IDynamicCircle,
IDynamicCylinder,
Expand All @@ -8,9 +9,9 @@ import type {
} from "core/interfaces";

export type TEventName = string | string[];
export type TEventFunc<EventInstance, P> = (
export type TEventFunc<EventInstance> = (
this: EventInstance,
player: P,
player: Player,
...args: string[]
) => boolean | number | void | Promise<boolean | number | void>;

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/utils/helperUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { I18n } from "core/controllers/i18n";
import type { Player } from "../controllers/player/entity";
import { Player } from "../controllers/player/entity";
import { LimitsEnum } from "../enums";
import type { IDialog } from "../interfaces";
import { rgba } from "./colorUtils";
Expand Down Expand Up @@ -30,11 +30,10 @@ export const SendClientMessage = (
};

export const SendClientMessageToAll = (
fn: Array<Player>,
color: string | number,
msg: string
): number => {
fn.forEach((player) => SendClientMessage(player, color, msg));
Player.getInstances().forEach((p) => SendClientMessage(p, color, msg));
return 1;
};

Expand All @@ -54,11 +53,12 @@ export const SendPlayerMessageToPlayer = (
};

export const SendPlayerMessageToAll = (
fn: Array<Player>,
senderId: number,
message: string
): number => {
fn.forEach((player) => SendPlayerMessageToPlayer(player, senderId, message));
Player.getInstances().forEach((p) => {
SendPlayerMessageToPlayer(p, senderId, message);
});
return 1;
};

Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/wrapper/native/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2122,9 +2122,9 @@ export const SetPlayerChatBubble = (
export const PutPlayerInVehicle = (
playerId: number,
vehicleId: number,
seatid: number
seatId: number
): number => {
return callNative("PutPlayerInVehicle", "iii", playerId, vehicleId, seatid);
return callNative("PutPlayerInVehicle", "iii", playerId, vehicleId, seatId);
};

export const GetPlayerVehicleID = (playerId: number): number => {
Expand Down Expand Up @@ -2277,6 +2277,10 @@ export const SetPlayerWorldBounds = (
);
};

export const ClearPlayerWorldBounds = (playerId: number) => {
return Boolean(callNative("ClearPlayerWorldBounds", "i", playerId));
};

export const SetPlayerMarkerForPlayer = (
playerId: number,
showplayerid: number,
Expand Down Expand Up @@ -2991,13 +2995,13 @@ export const UpdateVehicleDamageStatus = (

export const GetVehicleModelInfo = (
vehiclemodel: number,
infotype: VehicleModelInfoEnum
infoType: VehicleModelInfoEnum
) => {
const values: number[] = callNative(
"GetVehicleModelInfo",
"iiFFF",
vehiclemodel,
infotype
infoType
);
if (values.length < 3) {
throw new Error("ModelID " + vehiclemodel + " not found");
Expand Down
18 changes: 6 additions & 12 deletions packages/core/src/wrapper/streamer/actor/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ export class DynamicActor {
isValid(): boolean {
return s.IsValidDynamicActor(this.id);
}
isStreamedIn<P extends Player>(forplayer: P): boolean {
isStreamedIn(forPlayer: Player): boolean {
if (this.id === -1) return false;
return s.IsDynamicActorStreamedIn(this.id, forplayer.id);
return s.IsDynamicActorStreamedIn(this.id, forPlayer.id);
}
getVirtualWorld(): void | number {
if (this.id === -1)
Expand Down Expand Up @@ -199,21 +199,15 @@ export class DynamicActor {
);
return s.SetDynamicActorInvulnerable(this.id, invulnerable);
}
getPlayerTarget<P extends Player, A extends DynamicActor>(
player: P,
actors: Map<number, A>
): void | A {
getPlayerTarget(player: Player) {
if (this.id === -1) return undefined;
const actorId = s.GetPlayerTargetDynamicActor(player.id);
return actors.get(actorId);
return DynamicActor.actors.get(actorId);
}
getPlayerCameraTarget<P extends Player, A extends DynamicActor>(
player: P,
actors: Map<number, A>
): void | A {
getPlayerCameraTarget(player: Player) {
if (this.id === -1) return undefined;
const actorId = s.GetPlayerCameraTargetDynActor(player.id);
return actors.get(actorId);
return DynamicActor.actors.get(actorId);
}
getSkin(): void | number {
if (this.id === -1)
Expand Down
Loading

0 comments on commit 1adf6c5

Please sign in to comment.