mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-05 06:28:51 +00:00
refactor(server/player): Player singleton architecture
This commit is contained in:
1 parent
19e4ba0543
commit
33a02bb203
1 file changed
+62
-5
@@ -22,23 +22,40 @@ class PlayerService {
|
|||||||
this.playerDB = playerDB;
|
this.playerDB = playerDB;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a Player instance to the source & identifier maps
|
||||||
|
* @param source - The player's source
|
||||||
|
* @param player - The player instance to use as a value
|
||||||
|
*/
|
||||||
|
|
||||||
addPlayerToMaps(source: number, player: Player) {
|
addPlayerToMaps(source: number, player: Player) {
|
||||||
this.playersBySource.set(source, player);
|
this.playersBySource.set(source, player);
|
||||||
this.playersByIdentifier.set(player.getIdentifier(), player);
|
this.playersByIdentifier.set(player.getIdentifier(), player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a player from both map by source & identifier
|
||||||
|
* @param source - The player's source
|
||||||
|
*/
|
||||||
deletePlayerFromMaps(source: number) {
|
deletePlayerFromMaps(source: number) {
|
||||||
const identifier = this.playersBySource.get(source).getIdentifier();
|
const identifier = this.playersBySource.get(source).getIdentifier();
|
||||||
this.playersByIdentifier.delete(identifier);
|
this.playersByIdentifier.delete(identifier);
|
||||||
this.playersBySource.delete(source);
|
this.playersBySource.delete(source);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
getPlayer(source: number): Player {
|
* Returns the player instance for a given source
|
||||||
|
* Will return null if no player is found online with that source
|
||||||
|
**/
|
||||||
|
getPlayer(source: number): Player | null {
|
||||||
const player = this.playersBySource.get(source);
|
const player = this.playersBySource.get(source);
|
||||||
if (!player) throw new Error(`Could not find player associated with source: ${source}`);
|
if (!player) return null;
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the player instance for a given source
|
||||||
|
* Will return null if no player is found online with that source
|
||||||
|
**/
|
||||||
getIdentifier(source: number): string {
|
getIdentifier(source: number): string {
|
||||||
return this.getPlayer(source).getIdentifier();
|
return this.getPlayer(source).getIdentifier();
|
||||||
}
|
}
|
||||||
@@ -54,7 +71,32 @@ class PlayerService {
|
|||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
getIdentifierFromPhoneNumber(phoneNumber: string) {}
|
/**
|
||||||
|
* Will return the given identifier from a phone number
|
||||||
|
* @param phoneNumber - The phone number of the player
|
||||||
|
* @param fetch - Whether to fetch for the identifier if they are offline
|
||||||
|
**/
|
||||||
|
async getIdentifierFromPhoneNumber(phoneNumber: string, fetch?: boolean): Promise<string | null> {
|
||||||
|
const onlinePlayer = this.playersBySource.find(
|
||||||
|
(player: Player) => player.getPhoneNumber() === phoneNumber,
|
||||||
|
);
|
||||||
|
// Return the player if they are online
|
||||||
|
if (onlinePlayer) return onlinePlayer.getIdentifier();
|
||||||
|
if (fetch) {
|
||||||
|
const fetchResult: string | null = await this.playerDB
|
||||||
|
.fetchIdentifierFromPhoneNumber(phoneNumber)
|
||||||
|
.catch((e) => {
|
||||||
|
playerLogger.error(
|
||||||
|
`Failed to fetch identifier from phone number for ${phoneNumber}, error: ${e.message}`,
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return fetchResult;
|
||||||
|
}
|
||||||
|
// Return null if all else doesn't return
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We call this function on the event `playerJoined`,
|
* We call this function on the event `playerJoined`,
|
||||||
@@ -95,6 +137,13 @@ class PlayerService {
|
|||||||
emitNet(PhoneEvents.PLAYER_IS_READY, pSource, true);
|
emitNet(PhoneEvents.PLAYER_IS_READY, pSource, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate a basic Player instance from the constructor
|
||||||
|
* @param src - The player source
|
||||||
|
* @param identifier - The primary identifier
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
async createNewPlayer({
|
async createNewPlayer({
|
||||||
src,
|
src,
|
||||||
identifier,
|
identifier,
|
||||||
@@ -121,7 +170,9 @@ class PlayerService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* We call this function whenever we receive a `npwd:newPlayer`
|
* We call this function whenever we receive a `npwd:newPlayer`
|
||||||
* event while mutlchar is enabled.
|
* event while multichar is enabled.
|
||||||
|
* @param NewPlayerDTO - A DTO with all the new info required to instantiate a new player
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
async handleNewPlayerEvent({ source: src, identifier, firstname, lastname }: PlayerAddData) {
|
async handleNewPlayerEvent({ source: src, identifier, firstname, lastname }: PlayerAddData) {
|
||||||
const player = await this.createNewPlayer({ src, identifier });
|
const player = await this.createNewPlayer({ src, identifier });
|
||||||
@@ -153,11 +204,17 @@ class PlayerService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unload event handler
|
||||||
|
* @param src - Source of player being unloaded
|
||||||
|
* @param playerDropped - Whether the player disconnected or is being unloaded for multichar
|
||||||
|
**/
|
||||||
handleUnloadPlayerEvent(src: number, playerDropped?: boolean) {
|
handleUnloadPlayerEvent(src: number, playerDropped?: boolean) {
|
||||||
this.deletePlayerFromMaps(src);
|
this.deletePlayerFromMaps(src);
|
||||||
|
|
||||||
playerLogger.info(`Unloaded NPWD Player, source: (${src})`);
|
playerLogger.info(`Unloaded NPWD Player, source: (${src})`);
|
||||||
|
|
||||||
|
// We emit PLAYER_IS_READY false if the player wasn't disconnected
|
||||||
if (!playerDropped) {
|
if (!playerDropped) {
|
||||||
emitNet(PhoneEvents.PLAYER_IS_READY, src, false);
|
emitNet(PhoneEvents.PLAYER_IS_READY, src, false);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user