Skip to content

Commit

Permalink
feat: added streamer race cp and callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Sep 16, 2022
1 parent 9175925 commit e2ce36f
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/controllers/streamer/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./mapIcon";
export * from "./pickup";
export * from "./checkpoint";
export * from "./raceCP";
140 changes: 140 additions & 0 deletions src/controllers/streamer/raceCP/baseRaceCP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { BasePlayer } from "@/controllers/player";
import { IDynamicRaceCp } from "@/interfaces";

import { logger } from "@/logger";
import {
CreateDynamicRaceCP,
CreateDynamicRaceCPEx,
DestroyDynamicCP,
GetPlayerVisibleDynamicRaceCP,
IsPlayerInDynamicRaceCP,
IsValidDynamicCP,
StreamerDistances,
TogglePlayerAllDynamicRaceCPs,
TogglePlayerDynamicRaceCP,
} from "omp-wrapper-streamer";

import { raceCPBus, raceCPHooks } from "./raceCPBus";

export class DynamicRaceCP {
private sourceInfo: IDynamicRaceCp;
private _id = -1;
public get id(): number {
return this._id;
}
constructor(checkPoint: IDynamicRaceCp) {
this.sourceInfo = checkPoint;
}
public create(): void | this {
if (this.id !== -1)
return logger.warn("[StreamerRaceCP]: Unable to create checkpoint again");
let { streamdistance, worldid, interiorid, playerid, areaid, priority } =
this.sourceInfo;
const { type, size, x, y, z, nextx, nexty, nextz, extended } =
this.sourceInfo;

if (type < 0 || type > 8)
return logger.error("[StreamerRaceCP]: Invalid type");

if (size < 0) return logger.error("[StreamerRaceCP]: Invalid size");

streamdistance ??= StreamerDistances.RACE_CP_SD;
priority ??= 0;

if (extended) {
if (typeof worldid === "number") worldid = [-1];
else worldid ??= [-1];
if (typeof interiorid === "number") interiorid = [-1];
else interiorid ??= [-1];
if (typeof playerid === "number") playerid = [-1];
else playerid ??= [-1];
if (typeof areaid === "number") areaid = [-1];
else areaid ??= [-1];

this._id = CreateDynamicRaceCPEx(
type,
x,
y,
z,
nextx,
nexty,
nextz,
size,
streamdistance,
worldid,
interiorid,
playerid,
areaid,
priority
);
} else {
if (Array.isArray(worldid)) worldid = -1;
else worldid ??= -1;
if (Array.isArray(interiorid)) interiorid = -1;
else interiorid ??= -1;
if (Array.isArray(playerid)) playerid = -1;
else playerid ??= -1;
if (Array.isArray(areaid)) areaid = -1;
else areaid ??= -1;

this._id = CreateDynamicRaceCP(
type,
x,
y,
z,
nextx,
nexty,
nextz,
size,
worldid,
interiorid,
playerid,
streamdistance,
areaid,
priority
);
}

raceCPBus.emit(raceCPHooks.created, this);
return this;
}
public destroy(): void | this {
if (this.id === -1)
return logger.warn(
"[StreamerRaceCP]: Unable to destroy the checkpoint before create"
);
DestroyDynamicCP(this.id);
raceCPBus.emit(raceCPHooks.destroyed, this);
return this;
}
public static isValid(checkpoint: DynamicRaceCP): boolean {
return IsValidDynamicCP(checkpoint.id);
}
public togglePlayer<P extends BasePlayer>(
player: P,
toggle: boolean
): void | this {
if (this.id === -1)
return logger.warn(
"[StreamerRaceCP]: Unable to toggle the player before create"
);
TogglePlayerDynamicRaceCP(player.id, this.id, toggle);
return this;
}
public static togglePlayerAll<P extends BasePlayer>(
player: P,
toggle: boolean
): number {
return TogglePlayerAllDynamicRaceCPs(player.id, toggle);
}
public isPlayerIn<P extends BasePlayer>(player: P): boolean {
if (this.id === -1) return false;
return IsPlayerInDynamicRaceCP(player.id, this.id);
}
public static getPlayerVisible<P extends BasePlayer, C extends DynamicRaceCP>(
player: P,
checkpoints: Map<number, C>
): C | undefined {
return checkpoints.get(GetPlayerVisibleDynamicRaceCP(player.id));
}
}
2 changes: 2 additions & 0 deletions src/controllers/streamer/raceCP/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./baseRaceCP";
export * from "./raceCPEvent";
8 changes: 8 additions & 0 deletions src/controllers/streamer/raceCP/raceCPBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EventBus } from "@/utils/eventBus";

export enum raceCPHooks {
created = "OnRaceCPCreate",
destroyed = "OnRaceCPDestroy",
}

export const raceCPBus = new EventBus();
50 changes: 50 additions & 0 deletions src/controllers/streamer/raceCP/raceCPEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BasePlayer } from "@/controllers/player";
import {
OnPlayerEnterDynamicRaceCP,
OnPlayerLeaveDynamicRaceCP,
} from "omp-wrapper-streamer";
import { DynamicRaceCP } from "./baseRaceCP";
import { raceCPBus, raceCPHooks } from "./raceCPBus";

export abstract class DynamicRaceCPEvent<
P extends BasePlayer,
R extends DynamicRaceCP
> {
public readonly raceCPs = new Map<number, R>();
private readonly players;

constructor(playersMap: Map<number, P>) {
this.players = playersMap;
raceCPBus.on(raceCPHooks.created, (checkpoint: R) => {
this.raceCPs.set(checkpoint.id, checkpoint);
});
raceCPBus.on(raceCPHooks.destroyed, (checkpoint: R) => {
this.raceCPs.delete(checkpoint.id);
});
OnPlayerEnterDynamicRaceCP(
(playerid: number, checkpointid: number): number => {
const cp = this.raceCPs.get(checkpointid);
if (!cp) return 0;
const p = this.players.get(playerid);
if (!p) return 0;
return this.onPlayerEnter(p, cp);
}
);
OnPlayerLeaveDynamicRaceCP(
(playerid: number, checkpointid: number): number => {
const cp = this.raceCPs.get(checkpointid);
if (!cp) return 0;
const p = this.players.get(playerid);
if (!p) return 0;
return this.onPlayerLeave(p, cp);
}
);
}

protected abstract onPlayerEnter(player: P, checkpoint: R): number;
protected abstract onPlayerLeave(player: P, checkpoint: R): number;

public getRaceCPsArr(): Array<R> {
return [...this.raceCPs.values()];
}
}
7 changes: 7 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,10 @@ export interface IDynamicPickup extends IDynamicCommon {
export interface IDynamicCheckPoint extends IDynamicCommon {
size: number;
}

export interface IDynamicRaceCp extends IDynamicCheckPoint {
type: number;
nextx: number;
nexty: number;
nextz: number;
}

0 comments on commit e2ce36f

Please sign in to comment.