Skip to content

Commit

Permalink
feat: unify some attributes in the player event class
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Feb 11, 2023
1 parent 56f6342 commit e6baccc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/controllers/gamemode/settings.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Used to split circular dependency tips that ignore annoyance
export const defaultCharset = "utf8";
export const defaultLocale = "en_US";
32 changes: 23 additions & 9 deletions src/controllers/player/basePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,15 @@ import {
AllowPlayerWeapons,
ArePlayerWeaponsAllowed,
} from "omp-wrapper";
import { defaultCharset } from "../gamemode/settings";
import { defaultCharset, defaultLocale } from "../gamemode/settings";
import { playerBus, playerHooks } from "./playerBus";

export abstract class BasePlayer {
private _id: number;
private _isNpc: boolean;

isRecording = false;

/* Note: The locale and character set must be assigned at application level development time.
Otherwise i18n will be problematic. */

locale?: string;
charset: string = defaultCharset;
private _locale: string = defaultLocale;
private _charset: string = defaultCharset;
private _isRecording = false;

lastDrunkLevel = 0;
lastFps = 0;
Expand All @@ -92,6 +88,24 @@ export abstract class BasePlayer {
get id(): number {
return this._id;
}
get locale(): string {
return this._locale;
}
set locale(value: string) {
playerBus.emit(playerHooks.setLocale, { player: this, value });
}
get charset(): string {
return this._charset;
}
set charset(value: string) {
playerBus.emit(playerHooks.setCharset, { player: this, value });
}
get isRecording() {
return this._isRecording;
}
set isRecording(value) {
playerBus.emit(playerHooks.setIsRecording, { player: this, value });
}

constructor(id: number) {
this._id = id;
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/player/playerBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { EventBus } from "@/utils/eventBus";
export enum playerHooks {
pause = "OnPlayerPause",
create = "OnPlayerEventCreate",
setLocale = "OnPlayerSetLocale",
setCharset = "OnPlayerSetCharset",
setIsRecording = "OnPlayerSetIsRecording",
}

export const playerBus = new EventBus();
14 changes: 12 additions & 2 deletions src/controllers/player/playerEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export abstract class BasePlayerEvent<P extends BasePlayer> {
this.onResume && this.onResume(p, now - p.lastUpdateTick);
}
p.lastUpdateTick = now;
this.fpsHeartbeat(p);
BasePlayerEvent.fpsHeartbeat(p);
}
const pFn = promisifyCallback(this, "throttleUpdate", "OnPlayerUpdate");
const res = pFn(p);
Expand Down Expand Up @@ -311,6 +311,16 @@ export abstract class BasePlayerEvent<P extends BasePlayer> {
player.isPaused = true;
this.onPause && this.onPause(player, player.lastUpdateTick);
});

playerBus.on(playerHooks.setLocale, ({ player, value }) => {
Reflect.set(player, "_locale", value);
});
playerBus.on(playerHooks.setCharset, ({ player, value }) => {
Reflect.set(player, "_charset", value);
});
playerBus.on(playerHooks.setIsRecording, ({ player, value }) => {
Reflect.set(player, "_isRecording", value);
});
}
findPlayerById(playerid: number) {
return this.players.get(playerid);
Expand All @@ -325,7 +335,7 @@ export abstract class BasePlayerEvent<P extends BasePlayer> {
(player: P) => this.onUpdate && this.onUpdate(player),
60
);
private fpsHeartbeat = throttle((player: P) => {
private static fpsHeartbeat = throttle((player: BasePlayer) => {
if (!BasePlayer.isConnected(player)) return;
const nowDrunkLevel = player.getDrunkLevel();
if (nowDrunkLevel < 100) {
Expand Down

0 comments on commit e6baccc

Please sign in to comment.