enhance(server/player): Several enhancements

Attach a readonly username property to Player class

Implement setters/getters for Player class to ensure future
compatibility for servers who implement a character switching system

Attach a fetch argument to getIdentifierByPhoneNumber that will force a
DB query if player is not online
This commit is contained in:
Taso committed 2021-03-08 20:18:20 -05:00
1 parent e5fac800ee
commit 6e9c3e6d1a
4 files changed
+112 -32

No files matched your search

+79 -15
View File
@@ -12,21 +12,23 @@ export const Players = new Map<number, Player>();
// Map of players by identifier
export const PlayersByIdentifier = new Map<string, Player>();
interface PlayerInfo {
interface PlayerQueryResp {
firstname: string;
lastname: string;
phone_number: string;
}
// Get initial player data to populate new class instance
async function getPlayerInfo(identifer: string): Promise<PlayerInfo> {
// NOTE: We should make this more generic and as easily configurable as possible
async function getPlayerInfo(identifier: string): Promise<PlayerQueryResp> {
const query = `SELECT firstname, lastname, phone_number
FROM users
WHERE identifier = ?`;
const [results] = await pool.query(query, [identifer]);
const [results] = await pool.query(query, [identifier]);
const creds = <PlayerInfo[]>results;
if (!creds[0]) throw new Error('Could not get player information');
const creds = <PlayerQueryResp[]>results;
if (!creds[0]) throw new Error(`Could not get player information for ${identifier}`);
const firstname = creds[0].firstname;
const lastname = creds[0].lastname;
@@ -35,10 +37,13 @@ async function getPlayerInfo(identifer: string): Promise<PlayerInfo> {
return { firstname, lastname, phone_number };
}
// Attach to playerJoining event as this is when
// the permanent source is assigned for the user
on('playerJoining', async () => {
const pSource = (global as any).source;
const playerIdentifiers = getPlayerIdentifiers(pSource);
// Parse specifically for license identifier as its
// guranteed
let playerIdentifer;
@@ -53,15 +58,18 @@ on('playerJoining', async () => {
}
try {
playerLogger.debug('Loading NPWD player');
const username = GetPlayerName(pSource);
playerLogger.info(`Started loading for ${username} (${pSource})`);
// Ensure phone number exists or generate
await generatePhoneNumber(playerIdentifer);
// Get player info to populate class instance
const { firstname, lastname, phone_number } = await getPlayerInfo(playerIdentifer);
const newPlayer = new Player({
identifier: playerIdentifer,
source: pSource,
username,
firstname,
lastname,
phoneNumber: phone_number,
@@ -73,7 +81,8 @@ on('playerJoining', async () => {
// Add to players by source map
Players.set(pSource, newPlayer);
playerLogger.debug('Loaded NPWD Player');
playerLogger.info('NPWD Player Loaded!');
playerLogger.debug(newPlayer);
// Emit to client that player is ready
emitNet(events.PLAYER_IS_READY, pSource);
@@ -99,25 +108,80 @@ on('playerDropped', () => {
interface PlayerClassArgs {
source: number;
username: string;
firstname: string;
lastname: string;
identifier: string;
phoneNumber: string;
}
// Can add methods here to expand in the future
// Public getters/setters are for servers with multicharacter systems
// who need to switch certain information when character is changed
export class Player {
public readonly source: number;
public readonly identifier: string;
public firstname: string;
public lastname: string;
public phoneNumber: string;
public readonly username: string;
private _firstname: string;
private _lastname: string;
private _phoneNumber: string;
constructor({ source, firstname, lastname, identifier, phoneNumber }: PlayerClassArgs) {
constructor({ source, firstname, lastname, identifier, phoneNumber, username }: PlayerClassArgs) {
this.source = source;
this.identifier = identifier;
this.firstname = firstname;
this.lastname = lastname;
this.phoneNumber = phoneNumber;
this._firstname = firstname;
this._lastname = lastname;
this._phoneNumber = phoneNumber;
this.username = username;
}
/**
* Returns the stored firstname for a user
**/
public getFirstName(): string {
return this._firstname;
}
/**
* Set the first name for a user
* @param firstname {string} The first name to set for the user
**/
public setFirstName(firstname: string): void {
this._firstname = name;
}
/**
* Returns the stored lastname for a user
**/
public getLastName(): string {
return this._lastname;
}
/**
* Set the last name for a user
* @param lastname {string} The last name to set for the user
**/
public setLastName(lastname: string): void {
this._lastname = name;
}
/**
* Get the full name of the user
**/
public getName(): string {
return `${this._firstname} ${this._lastname}`;
}
/**
* Get the stored phone number for a user
**/
public getPhoneNumber(): string {
return this._phoneNumber;
}
/**
* Set the stored phone number for a user
**/
public setPhoneNumber(number: string): void {
this._phoneNumber = number;
}
}