-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added streamer race cp and callbacks
- Loading branch information
Showing
6 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./baseRaceCP"; | ||
export * from "./raceCPEvent"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters