feat(player/npwd): Update identifier & name

Still need to include a public facing phone number update
This commit is contained in:
Taso committed 2021-04-19 14:14:24 -04:00
1 parent 3e01fc456c
commit 6be602c95b
4 files changed
+93 -18

No files matched your search

+2 -2
View File
@@ -18,7 +18,7 @@ export const getSource = () => (global as any).source;
* @param source The source/netId for the player in question
**/
export function getIdentifier(source: number): string {
return getPlayer(source).identifier;
return getPlayer(source).getIdentifier();
}
/**
@@ -31,7 +31,7 @@ export async function getIdentifierByPhoneNumber(
fetch?: boolean,
): Promise<string | null> {
for (const [source, player] of Players) {
if (player.getPhoneNumber() === phoneNumber) return player.identifier;
if (player.getPhoneNumber() === phoneNumber) return player.getIdentifier();
}
// Whether we fetch from database if not found in online players
if (fetch) {
+60 -2
View File
@@ -1,6 +1,12 @@
import { getPlayer, getSource } from '../functions';
import { mainLogger } from '../sv_logger';
import { handlePlayerAdd, Players, PlayersByIdentifier } from './sv_players';
import { handlePlayerAdd, playerLogger, Players, PlayersByIdentifier } from './sv_players';
/**
* Essentially this whole file acts as a controller layer
* for player related actions. Everything here is registered,
* within the global scope like routes in a web app.
*/
on('playerJoining', async () => {
const _source = getSource();
@@ -11,7 +17,7 @@ on('playerJoining', async () => {
on('playerDropped', () => {
const _source = getSource();
// Get identifier for player to remove
const playerIdentifier = getPlayer(_source).identifier;
const playerIdentifier = getPlayer(_source).getIdentifier();
// Remove from player map by identifier
PlayersByIdentifier.delete(playerIdentifier);
// Remove from player map by source
@@ -40,3 +46,55 @@ on('onServerResourceStart', async (resource: string) => {
}
}
});
/**
* This sets the identifier for an NPWD player using source as the target
*
* @param source {number} - The source of the player to target
* @param identifier {string} - The identifier for this player to receive
*/
exports('setNpwdIdentifier', (source: number, identifier: string) => {
if (typeof source !== 'number') {
playerLogger.error('Source must be passed as a number to setNpwdIdentifier');
return;
}
const npwdPlayer = Players.get(source);
const playerLicense = npwdPlayer.getIdentifier();
// We need to remove the identifier mapped player first
PlayersByIdentifier.delete(playerLicense);
// Now we update the reference for the source map player & identifier
npwdPlayer.setIdentifier(identifier);
// Create a new identifier mapped player using updated reference
PlayersByIdentifier.set(identifier, npwdPlayer);
});
/**
* This sets the RP name for an NPWD player using source as the target
*
* @param source {number} - The source of the player to target
* @param firstname {string} - The RP first name for this player
* @param lastname {string} - The RP last name for this player
*/
exports('setNpwdName', (source: number, firstname: string, lastname: string) => {
if (typeof source !== 'number') {
playerLogger.error('Source must be passed as a number to setNpwdIdentifier');
return;
}
// I don't remember if JS treats maps as references or clones, because if they
// are references when we set, we don't need to update the player twice here but oh well
// exec time isn't much more.
const npwdPlayer = Players.get(source);
const npwdPlayerIdentifier = npwdPlayer.getIdentifier();
// Set lastname and firstname
npwdPlayer.setFirstName(firstname);
npwdPlayer.setLastName(lastname);
// Update identifier map
PlayersByIdentifier.set(npwdPlayerIdentifier, npwdPlayer);
});
+30 -13
View File
@@ -16,15 +16,15 @@ export async function handlePlayerAdd(pSource: number) {
const playerIdentifiers = getPlayerIdentifiers(pSource.toString());
// Parse specifically for license identifier as its
// guranteed
let playerIdentifer;
// guaranteed
let playerIdentifier;
for (const identifier of playerIdentifiers) {
if (identifier.includes('license:')) {
playerIdentifer = identifier.split(':')[1];
playerIdentifier = identifier.split(':')[1];
}
}
if (!playerIdentifer) {
if (!playerIdentifier) {
throw new Error('License identifier could not be found.');
}
@@ -32,12 +32,12 @@ export async function handlePlayerAdd(pSource: number) {
const username = GetPlayerName(pSource.toString());
playerLogger.info(`Started loading for ${username} (${pSource})`);
// Ensure phone number exists or generate
const phone_number = await generatePhoneNumber(playerIdentifer);
const phone_number = await generatePhoneNumber(playerIdentifier);
// Get player info to populate class instance
let playerInfo = { firstname: '', lastname: '' };
try {
playerInfo = await getPlayerInfo(playerIdentifer);
playerInfo = await getPlayerInfo(playerIdentifier);
} catch (e) {
playerLogger.debug(
`Failed to get player info, defaulting to phone number as profile, ${e.message}`,
@@ -49,7 +49,7 @@ export async function handlePlayerAdd(pSource: number) {
const { firstname, lastname } = playerInfo;
const newPlayer = new Player({
identifier: playerIdentifer,
identifier: playerIdentifier,
source: pSource,
username,
firstname,
@@ -58,7 +58,7 @@ export async function handlePlayerAdd(pSource: number) {
});
// Add to players by identifier map
PlayersByIdentifier.set(newPlayer.identifier, newPlayer);
PlayersByIdentifier.set(newPlayer.getIdentifier(), newPlayer);
// Add to players by source map
Players.set(pSource, newPlayer);
@@ -168,7 +168,7 @@ interface PlayerClassArgs {
export class Player {
public readonly source: number;
public readonly identifier: string;
private _identifier: string;
public readonly username: string;
private _firstname: string;
private _lastname: string;
@@ -176,7 +176,7 @@ export class Player {
constructor({ source, firstname, lastname, identifier, phoneNumber, username }: PlayerClassArgs) {
this.source = source;
this.identifier = identifier;
this._identifier = identifier;
this._firstname = firstname;
this._lastname = lastname;
this._phoneNumber = phoneNumber;
@@ -189,12 +189,27 @@ export class Player {
return this._firstname;
}
public getIdentifier(): string {
return this._identifier;
}
/**
* Set the identifier for a player, useful in systems with multicharacter
* players
* @param identifier {string} - Set the identifier for a player
*/
public setIdentifier(identifier: string): Player {
this._identifier = identifier;
return this;
}
/**
* Set the first name for a user
* @param firstname {string} The first name to set for the user
**/
public setFirstName(firstname: string): void {
public setFirstName(firstname: string): Player {
this._firstname = firstname;
return this;
}
/**
@@ -208,8 +223,9 @@ export class Player {
* Set the last name for a user
* @param lastname {string} The last name to set for the user
**/
public setLastName(lastname: string): void {
public setLastName(lastname: string): Player {
this._lastname = lastname;
return this;
}
/**
@@ -229,7 +245,8 @@ export class Player {
/**
* Set the stored phone number for a user
**/
public setPhoneNumber(number: string): void {
public setPhoneNumber(number: string): Player {
this._phoneNumber = number;
return this;
}
}
+1 -1
View File
@@ -80,7 +80,7 @@ onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => {
// This is used as username for the reports
await addListing(
player.identifier,
player.getIdentifier(),
player.username,
player.getName(),
player.getPhoneNumber(),