mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
refactor(server/players): Take a different approach to multichar
This changes how multicharacter support is implemented entirely. Instead there are now events for creating a new player & unloading a player. These should be called whenever a character is switched. Documentation will be added for `npwd:newPlayer` & `npwd:unloadPlayer`
This commit is contained in:
1 parent
2ec049ca12
commit
268bd4f4a4
11 files changed
+391
-377
No files matched your search
+6
-1
@@ -12,7 +12,12 @@
|
|||||||
"vertical": "top"
|
"vertical": "top"
|
||||||
},
|
},
|
||||||
"general": {
|
"general": {
|
||||||
"useDashNumber": true
|
"useDashNumber": true,
|
||||||
|
"enableMultiChar": false
|
||||||
|
},
|
||||||
|
"database": {
|
||||||
|
"playerTable": "users",
|
||||||
|
"identifierColumn": "identifier"
|
||||||
},
|
},
|
||||||
"twitter": {
|
"twitter": {
|
||||||
"showNotifications": true,
|
"showNotifications": true,
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import { config } from './server';
|
import { config } from './server';
|
||||||
import { mainLogger } from './sv_logger';
|
import { mainLogger } from './sv_logger';
|
||||||
import { Player, Players, PlayersByIdentifier } from './players/sv_players';
|
import { Players, PlayersByIdentifier } from './players/player.provider';
|
||||||
|
import { Player } from './players/player.class';
|
||||||
|
|
||||||
export function getPlayer(source: number): Player {
|
export function getPlayer(source: number): Player {
|
||||||
const player = Players.get(source);
|
const player = Players.get(source);
|
||||||
@@ -35,7 +36,7 @@ export async function getIdentifierByPhoneNumber(
|
|||||||
}
|
}
|
||||||
// Whether we fetch from database if not found in online players
|
// Whether we fetch from database if not found in online players
|
||||||
if (fetch) {
|
if (fetch) {
|
||||||
const query = `SELECT identifier FROM users WHERE phone_number = ?`;
|
const query = `SELECT ${config.database.identifierColumn} FROM ${config.database.playerTable} WHERE phone_number = ?`;
|
||||||
const [results] = await pool.query(query, [phoneNumber]);
|
const [results] = await pool.query(query, [phoneNumber]);
|
||||||
// Get identifier from results
|
// Get identifier from results
|
||||||
return (results as { identifier: string }[])[0].identifier;
|
return (results as { identifier: string }[])[0].identifier;
|
||||||
@@ -71,26 +72,33 @@ function getRandomPhoneNumber() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function generatePhoneNumber(identifier: string): Promise<string> {
|
export async function generatePhoneNumber(identifier: string): Promise<string> {
|
||||||
const getQuery = `SELECT phone_number FROM users WHERE identifier = ?`;
|
const getQuery = `SELECT phone_number FROM ${config.database.playerTable} WHERE ${config.database.identifierColumn} = ?`;
|
||||||
const [results] = await pool.query(getQuery, [identifier]);
|
const [results] = await pool.query(getQuery, [identifier]);
|
||||||
const result = <any[]>results;
|
const result = <any[]>results;
|
||||||
const phoneNumber = result[0]?.phone_number;
|
const phoneNumber = result[0]?.phone_number;
|
||||||
|
|
||||||
if (!phoneNumber) {
|
// Generate a new phone number if there isn't one present
|
||||||
let existingId;
|
if (!phoneNumber)
|
||||||
let newNumber;
|
if (!phoneNumber) {
|
||||||
do {
|
let existingId;
|
||||||
newNumber = getRandomPhoneNumber();
|
let newNumber;
|
||||||
try {
|
|
||||||
existingId = await getIdentifierByPhoneNumber(newNumber);
|
do {
|
||||||
} catch (e) {
|
newNumber = getRandomPhoneNumber();
|
||||||
existingId = false;
|
try {
|
||||||
}
|
existingId = await getIdentifierByPhoneNumber(newNumber);
|
||||||
} while (existingId);
|
} catch (e) {
|
||||||
mainLogger.debug(`Inserting number into Database: ${newNumber}`);
|
existingId = false;
|
||||||
const query = 'UPDATE users SET phone_number = ? WHERE identifier = ?';
|
}
|
||||||
await pool.query(query, [newNumber, identifier]);
|
} while (existingId);
|
||||||
return newNumber;
|
|
||||||
}
|
mainLogger.debug(
|
||||||
|
`Generated a new number for identifier: ${identifier}, number: ${phoneNumber}`,
|
||||||
|
);
|
||||||
|
const query = `UPDATE ${config.database.playerTable} SET phone_number = ? WHERE ${config.database.identifierColumn} = ?`;
|
||||||
|
await pool.query(query, [newNumber, identifier]);
|
||||||
|
return newNumber;
|
||||||
|
}
|
||||||
|
|
||||||
return phoneNumber;
|
return phoneNumber;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import { CreatePlayerInstance } from './player.interfaces';
|
||||||
|
|
||||||
|
export class Player {
|
||||||
|
public readonly source: number;
|
||||||
|
public readonly username: string;
|
||||||
|
private _phoneNumber: string;
|
||||||
|
private _identifier: string;
|
||||||
|
private _firstname: string | null;
|
||||||
|
private _lastname: string | null;
|
||||||
|
|
||||||
|
constructor({ source, identifier, phoneNumber, username }: CreatePlayerInstance) {
|
||||||
|
this.source = source;
|
||||||
|
this._identifier = identifier;
|
||||||
|
this._phoneNumber = phoneNumber;
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the stored firstname for a user
|
||||||
|
*/
|
||||||
|
public getFirstName(): string | null {
|
||||||
|
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): Player {
|
||||||
|
this._firstname = firstname;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the stored lastname for a user
|
||||||
|
**/
|
||||||
|
public getLastName(): string | null {
|
||||||
|
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): Player {
|
||||||
|
this._lastname = lastname;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the full name of the user
|
||||||
|
**/
|
||||||
|
public getName(): string | null {
|
||||||
|
if (!this._firstname || !this._lastname) return null;
|
||||||
|
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): Player {
|
||||||
|
this._phoneNumber = number;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import { getSource } from '../functions';
|
||||||
|
import {
|
||||||
|
handleNewPlayerEvent,
|
||||||
|
handleNewPlayerJoined,
|
||||||
|
handleUnloadPlayerEvent,
|
||||||
|
playerLogger,
|
||||||
|
} from './player.service';
|
||||||
|
import config from '../../utils/config';
|
||||||
|
import { PlayerAddData } from './player.interfaces';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 server.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// If multicharacter mode is disabled, we instantiate a new
|
||||||
|
// NPWD player by ourselves without waiting for an export
|
||||||
|
|
||||||
|
if (!config.general.enableMultiChar) {
|
||||||
|
on('playerJoining', async () => {
|
||||||
|
const _source = getSource();
|
||||||
|
await handleNewPlayerJoined(_source);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle removing from player maps when player disconnects
|
||||||
|
on('playerDropped', () => {
|
||||||
|
const source = getSource();
|
||||||
|
// Get identifier for player to remove
|
||||||
|
handleUnloadPlayerEvent(source);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Can use this to debug the player table if needed. Disabled by default
|
||||||
|
// RegisterCommand(
|
||||||
|
// 'getPlayers',
|
||||||
|
// () => {
|
||||||
|
// playerLogger.debug(Players);
|
||||||
|
// },
|
||||||
|
// false,
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Used for debug purposes, if the resource is restarted and the multicharacter option is false
|
||||||
|
// we will handle all the currently online players
|
||||||
|
if (!config.general.enableMultiChar) {
|
||||||
|
on('onServerResourceStart', async (resource: string) => {
|
||||||
|
if (resource === GetCurrentResourceName()) {
|
||||||
|
// Workaround till https://github.com/citizenfx/fivem/pull/682
|
||||||
|
// is merged
|
||||||
|
// @ts-ignore
|
||||||
|
const onlinePlayers: string[] = getPlayers();
|
||||||
|
for (const player of onlinePlayers) {
|
||||||
|
await handleNewPlayerJoined(parseInt(player));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// For multicharacter frameworks, we enable these events for
|
||||||
|
// instantiating/deleting a player. The config option must be set to true
|
||||||
|
// for these to be available
|
||||||
|
if (config.general.enableMultiChar) {
|
||||||
|
// This has to be an event as FXServer does not yet support exports which return a promise.
|
||||||
|
on('npwd:newPlayer', async (playerDTO: PlayerAddData) => {
|
||||||
|
playerLogger.debug('Receive newPlayer event, data:');
|
||||||
|
playerLogger.debug(playerDTO);
|
||||||
|
await handleNewPlayerEvent(playerDTO);
|
||||||
|
});
|
||||||
|
|
||||||
|
on('npwd:unloadPlayer', (source: number) => {
|
||||||
|
playerLogger.debug(`Received unloadPlayer event for ${source}`);
|
||||||
|
handleUnloadPlayerEvent(source);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
export interface PlayerAddData {
|
||||||
|
source: number;
|
||||||
|
identifier: string;
|
||||||
|
firstname?: string;
|
||||||
|
lastname?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreatePlayerInstance {
|
||||||
|
source: number;
|
||||||
|
username: string;
|
||||||
|
identifier: string;
|
||||||
|
phoneNumber: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// Map of players by source
|
||||||
|
import { Player } from './player.class';
|
||||||
|
|
||||||
|
export const Players = new Map<number, Player>();
|
||||||
|
// Map of players by identifier
|
||||||
|
export const PlayersByIdentifier = new Map<string, Player>();
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
import { mainLogger } from '../sv_logger';
|
||||||
|
import { generatePhoneNumber, getPlayer, getPlayerFromIdentifier } from '../functions';
|
||||||
|
import { PhoneEvents } from '../../../typings/phone';
|
||||||
|
import { Player } from './player.class';
|
||||||
|
import { Players, PlayersByIdentifier } from './player.provider';
|
||||||
|
import { PlayerAddData } from './player.interfaces';
|
||||||
|
|
||||||
|
export const playerLogger = mainLogger.child({
|
||||||
|
module: 'player',
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We call this function on the event `playerJoined`,
|
||||||
|
* only if the multicharacter option is set to false in the config.
|
||||||
|
* @param pSource - The source of the player to handle
|
||||||
|
*/
|
||||||
|
export async function handleNewPlayerJoined(pSource: number) {
|
||||||
|
const playerIdentifiers = getPlayerIdentifiers(pSource.toString());
|
||||||
|
|
||||||
|
// Parse specifically for license identifier as its
|
||||||
|
// guaranteed
|
||||||
|
let playerIdentifier;
|
||||||
|
for (const identifier of playerIdentifiers) {
|
||||||
|
if (identifier.includes('license:')) {
|
||||||
|
playerIdentifier = identifier.split(':')[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!playerIdentifier) {
|
||||||
|
throw new Error('License identifier could not be found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const username = GetPlayerName(pSource.toString());
|
||||||
|
playerLogger.info(`Started loading for ${username} (${pSource})`);
|
||||||
|
// Ensure phone number exists or generate
|
||||||
|
const phone_number = await generatePhoneNumber(playerIdentifier);
|
||||||
|
|
||||||
|
const newPlayer = new Player({
|
||||||
|
identifier: playerIdentifier,
|
||||||
|
source: pSource,
|
||||||
|
username,
|
||||||
|
phoneNumber: phone_number,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add to players by identifier map
|
||||||
|
PlayersByIdentifier.set(newPlayer.getIdentifier(), newPlayer);
|
||||||
|
|
||||||
|
// Add to players by source map
|
||||||
|
Players.set(pSource, newPlayer);
|
||||||
|
|
||||||
|
playerLogger.info('NPWD Player Loaded!');
|
||||||
|
playerLogger.debug(newPlayer);
|
||||||
|
|
||||||
|
// Emit to client that player is ready
|
||||||
|
emitNet(PhoneEvents.PLAYER_IS_READY, pSource);
|
||||||
|
} catch (e) {
|
||||||
|
playerLogger.error(`Failed to create a new player, ${e.message}`, {
|
||||||
|
source: pSource,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We call this function whenever we receive a `npwd:newPlayer`
|
||||||
|
* event while mutlchar is enabled.
|
||||||
|
*/
|
||||||
|
export async function handleNewPlayerEvent({
|
||||||
|
source,
|
||||||
|
identifier,
|
||||||
|
firstname,
|
||||||
|
lastname,
|
||||||
|
}: PlayerAddData) {
|
||||||
|
try {
|
||||||
|
const username = GetPlayerName(source.toString());
|
||||||
|
|
||||||
|
// Generate or return a phone number if already exists
|
||||||
|
const phoneNumber = await generatePhoneNumber(identifier);
|
||||||
|
|
||||||
|
const player = new Player({
|
||||||
|
source,
|
||||||
|
identifier,
|
||||||
|
phoneNumber,
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (firstname) player.setFirstName(firstname);
|
||||||
|
if (lastname) player.setLastName(lastname);
|
||||||
|
|
||||||
|
// Set up player maps
|
||||||
|
Players.set(source, player);
|
||||||
|
PlayersByIdentifier.set(identifier, player);
|
||||||
|
|
||||||
|
playerLogger.info(`New NPWD Player added through event (${source}) (${identifier})`);
|
||||||
|
playerLogger.debug(player);
|
||||||
|
} catch (e) {
|
||||||
|
playerLogger.error(`Failed to create a new player through event, ${e.message}`, {
|
||||||
|
source,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleUnloadPlayerEvent(source: number) {
|
||||||
|
if (typeof source !== 'number') {
|
||||||
|
return playerLogger.error('Source must be passed as a number to npwd:handleUnloadPlayer');
|
||||||
|
}
|
||||||
|
const identifier = getPlayer(source).getIdentifier();
|
||||||
|
|
||||||
|
// Remove from maps
|
||||||
|
Players.delete(source);
|
||||||
|
PlayersByIdentifier.delete(identifier);
|
||||||
|
|
||||||
|
playerLogger.info(`Unloaded NPWD Player, source: (${source})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const clean = (input: string) => (input ? input.replace(/[^0-9a-z]/gi, '') : input);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a profile name by the player's name and/or phone number
|
||||||
|
* @param identifier - player's identifier
|
||||||
|
* @param delimiter - What we split the profile name by, defaults to -
|
||||||
|
*/
|
||||||
|
export async function generateProfileName(
|
||||||
|
identifier: string,
|
||||||
|
delimiter: string = '_',
|
||||||
|
): Promise<string | null> {
|
||||||
|
const player = getPlayerFromIdentifier(identifier);
|
||||||
|
|
||||||
|
const firstname = clean(player.getFirstName());
|
||||||
|
const lastname = clean(player.getLastName());
|
||||||
|
const phone_number = clean(player.getPhoneNumber());
|
||||||
|
|
||||||
|
if (firstname && lastname) {
|
||||||
|
return `${firstname}${delimiter}${lastname}`;
|
||||||
|
} else if (firstname) {
|
||||||
|
return firstname;
|
||||||
|
} else if (lastname) {
|
||||||
|
return lastname;
|
||||||
|
} else if (phone_number) {
|
||||||
|
return phone_number;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate default profile names to use for apps like twitter.
|
||||||
|
* @param identifier - The identifier to generate the profiles from.
|
||||||
|
*/
|
||||||
|
export async function getDefaultProfileNames(identifier: string): Promise<string[]> {
|
||||||
|
const player = getPlayerFromIdentifier(identifier);
|
||||||
|
|
||||||
|
const firstname = clean(player.getFirstName());
|
||||||
|
const lastname = clean(player.getLastName());
|
||||||
|
|
||||||
|
const phone_number = clean(player.getPhoneNumber());
|
||||||
|
|
||||||
|
const defaultProfileNames = [];
|
||||||
|
|
||||||
|
if (firstname && lastname) {
|
||||||
|
defaultProfileNames.push(`${firstname}_${lastname}`);
|
||||||
|
} else if (firstname) {
|
||||||
|
defaultProfileNames.push(firstname);
|
||||||
|
} else if (lastname) {
|
||||||
|
defaultProfileNames.push(lastname);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (phone_number) {
|
||||||
|
defaultProfileNames.push(phone_number);
|
||||||
|
}
|
||||||
|
return defaultProfileNames;
|
||||||
|
}
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
import { getPlayer, getSource } from '../functions';
|
|
||||||
import { mainLogger } from '../sv_logger';
|
|
||||||
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();
|
|
||||||
await handlePlayerAdd(_source);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle removing from player maps when player disconnects
|
|
||||||
on('playerDropped', () => {
|
|
||||||
const _source = getSource();
|
|
||||||
// Get identifier for player to remove
|
|
||||||
const playerIdentifier = getPlayer(_source).getIdentifier();
|
|
||||||
// Remove from player map by identifier
|
|
||||||
PlayersByIdentifier.delete(playerIdentifier);
|
|
||||||
// Remove from player map by source
|
|
||||||
Players.delete(_source);
|
|
||||||
|
|
||||||
mainLogger.info(`Unloaded NPWD Player, source: (${_source})`);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Can use this to debug the player table if needed. Disabled by default
|
|
||||||
// RegisterCommand(
|
|
||||||
// 'getPlayers',
|
|
||||||
// () => {
|
|
||||||
// playerLogger.debug(Players);
|
|
||||||
// },
|
|
||||||
// false,
|
|
||||||
// );
|
|
||||||
|
|
||||||
on('onServerResourceStart', async (resource: string) => {
|
|
||||||
if (resource === GetCurrentResourceName()) {
|
|
||||||
// Workaround till https://github.com/citizenfx/fivem/pull/682
|
|
||||||
// is merged
|
|
||||||
// @ts-ignore
|
|
||||||
const onlinePlayers: string[] = getPlayers();
|
|
||||||
for (const player of onlinePlayers) {
|
|
||||||
await handlePlayerAdd(parseInt(player));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// We need to alias this as webpack or something
|
|
||||||
// is being a PITA
|
|
||||||
const exp = global.exports;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
exp('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
|
|
||||||
*/
|
|
||||||
exp('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);
|
|
||||||
});
|
|
||||||
@@ -1,252 +0,0 @@
|
|||||||
import { mainLogger } from '../sv_logger';
|
|
||||||
import { pool } from '../db';
|
|
||||||
import { generatePhoneNumber } from '../functions';
|
|
||||||
import { PhoneEvents } from '../../../typings/phone';
|
|
||||||
|
|
||||||
export const playerLogger = mainLogger.child({
|
|
||||||
module: 'player',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Map of players by source
|
|
||||||
export const Players = new Map<number, Player>();
|
|
||||||
// Map of players by identifier
|
|
||||||
export const PlayersByIdentifier = new Map<string, Player>();
|
|
||||||
|
|
||||||
export async function handlePlayerAdd(pSource: number) {
|
|
||||||
const playerIdentifiers = getPlayerIdentifiers(pSource.toString());
|
|
||||||
|
|
||||||
// Parse specifically for license identifier as its
|
|
||||||
// guaranteed
|
|
||||||
let playerIdentifier;
|
|
||||||
for (const identifier of playerIdentifiers) {
|
|
||||||
if (identifier.includes('license:')) {
|
|
||||||
playerIdentifier = identifier.split(':')[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!playerIdentifier) {
|
|
||||||
throw new Error('License identifier could not be found.');
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const username = GetPlayerName(pSource.toString());
|
|
||||||
playerLogger.info(`Started loading for ${username} (${pSource})`);
|
|
||||||
// Ensure phone number exists or generate
|
|
||||||
const phone_number = await generatePhoneNumber(playerIdentifier);
|
|
||||||
|
|
||||||
// Get player info to populate class instance
|
|
||||||
let playerInfo = { firstname: '', lastname: '' };
|
|
||||||
try {
|
|
||||||
playerInfo = await getPlayerInfo(playerIdentifier);
|
|
||||||
} catch (e) {
|
|
||||||
playerLogger.debug(
|
|
||||||
`Failed to get player info, defaulting to phone number as profile, ${e.message}`,
|
|
||||||
{
|
|
||||||
source: pSource,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const { firstname, lastname } = playerInfo;
|
|
||||||
|
|
||||||
const newPlayer = new Player({
|
|
||||||
identifier: playerIdentifier,
|
|
||||||
source: pSource,
|
|
||||||
username,
|
|
||||||
firstname,
|
|
||||||
lastname,
|
|
||||||
phoneNumber: phone_number,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add to players by identifier map
|
|
||||||
PlayersByIdentifier.set(newPlayer.getIdentifier(), newPlayer);
|
|
||||||
|
|
||||||
// Add to players by source map
|
|
||||||
Players.set(pSource, newPlayer);
|
|
||||||
|
|
||||||
playerLogger.info('NPWD Player Loaded!');
|
|
||||||
playerLogger.debug(newPlayer);
|
|
||||||
|
|
||||||
// Emit to client that player is ready
|
|
||||||
emitNet(PhoneEvents.PLAYER_IS_READY, pSource);
|
|
||||||
} catch (e) {
|
|
||||||
playerLogger.error(`Failed to create a new player, ${e.message}`, {
|
|
||||||
source: pSource,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PlayerQueryResp {
|
|
||||||
firstname: string;
|
|
||||||
lastname: string;
|
|
||||||
phone_number: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get initial player data to populate new class instance
|
|
||||||
// NOTE: We should make this more generic and as easily configurable as possible
|
|
||||||
export async function getPlayerInfo(identifier: string): Promise<PlayerQueryResp> {
|
|
||||||
const query = `SELECT firstname, lastname, phone_number
|
|
||||||
FROM users
|
|
||||||
WHERE identifier = ?`;
|
|
||||||
|
|
||||||
const [results] = await pool.query(query, [identifier]);
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
const phone_number = creds[0].phone_number;
|
|
||||||
return { firstname, lastname, phone_number };
|
|
||||||
}
|
|
||||||
|
|
||||||
const clean = (input: string) => (input ? input.replace(/[^0-9a-z]/gi, '') : input);
|
|
||||||
|
|
||||||
async function getCleanedPlayerInfo(identifier: string): Promise<PlayerQueryResp> {
|
|
||||||
const { firstname, lastname, phone_number } = await getPlayerInfo(identifier);
|
|
||||||
return {
|
|
||||||
firstname: clean(firstname),
|
|
||||||
lastname: clean(lastname),
|
|
||||||
phone_number: clean(phone_number),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a profile name by the player's name and/or phone number
|
|
||||||
* @param identifier - player's identifier
|
|
||||||
*/
|
|
||||||
export async function generateProfileName(
|
|
||||||
identifier: string,
|
|
||||||
delimiter: string = '_',
|
|
||||||
): Promise<string | null> {
|
|
||||||
const { firstname, lastname, phone_number } = await getCleanedPlayerInfo(identifier);
|
|
||||||
|
|
||||||
if (firstname && lastname) {
|
|
||||||
return `${firstname}${delimiter}${lastname}`;
|
|
||||||
} else if (firstname) {
|
|
||||||
return firstname;
|
|
||||||
} else if (lastname) {
|
|
||||||
return lastname;
|
|
||||||
} else if (phone_number) {
|
|
||||||
return phone_number;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getDefaultProfileNames(identifier: string): Promise<string[]> {
|
|
||||||
const { firstname, lastname, phone_number } = await getCleanedPlayerInfo(identifier);
|
|
||||||
let defaultProfileNames = [];
|
|
||||||
|
|
||||||
if (firstname && lastname) {
|
|
||||||
defaultProfileNames.push(`${firstname}_${lastname}`);
|
|
||||||
} else if (firstname) {
|
|
||||||
defaultProfileNames.push(firstname);
|
|
||||||
} else if (lastname) {
|
|
||||||
defaultProfileNames.push(lastname);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (phone_number) {
|
|
||||||
defaultProfileNames.push(phone_number);
|
|
||||||
}
|
|
||||||
return defaultProfileNames;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach to playerJoining event as this is when
|
|
||||||
// the permanent source is assigned for the user
|
|
||||||
|
|
||||||
interface PlayerClassArgs {
|
|
||||||
source: number;
|
|
||||||
username: string;
|
|
||||||
firstname: string;
|
|
||||||
lastname: string;
|
|
||||||
identifier: string;
|
|
||||||
phoneNumber: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
private _identifier: string;
|
|
||||||
public readonly username: string;
|
|
||||||
private _firstname: string;
|
|
||||||
private _lastname: string;
|
|
||||||
private _phoneNumber: string;
|
|
||||||
|
|
||||||
constructor({ source, firstname, lastname, identifier, phoneNumber, username }: PlayerClassArgs) {
|
|
||||||
this.source = source;
|
|
||||||
this._identifier = identifier;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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): Player {
|
|
||||||
this._firstname = firstname;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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): Player {
|
|
||||||
this._lastname = lastname;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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): Player {
|
|
||||||
this._phoneNumber = number;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,7 +5,7 @@ import { NewTweet, Tweet, Profile } from '../../typings/twitter';
|
|||||||
import { config } from './server';
|
import { config } from './server';
|
||||||
import { reportTweetToDiscord } from './discord';
|
import { reportTweetToDiscord } from './discord';
|
||||||
import { mainLogger } from './sv_logger';
|
import { mainLogger } from './sv_logger';
|
||||||
import { generateProfileName, getDefaultProfileNames } from './players/sv_players';
|
import { generateProfileName, getDefaultProfileNames } from './players/player.service';
|
||||||
import { TwitterEvents } from '../../typings/twitter';
|
import { TwitterEvents } from '../../typings/twitter';
|
||||||
|
|
||||||
const twitterLogger = mainLogger.child({ module: 'twitter' });
|
const twitterLogger = mainLogger.child({ module: 'twitter' });
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ interface Debug {
|
|||||||
|
|
||||||
interface General {
|
interface General {
|
||||||
useDashNumber: boolean;
|
useDashNumber: boolean;
|
||||||
|
enableMultiChar: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NotificationConfig {
|
interface NotificationConfig {
|
||||||
@@ -34,7 +35,13 @@ interface BankConfig {
|
|||||||
showNotifications: boolean;
|
showNotifications: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface DatabaseConfig {
|
||||||
|
playerTable: string;
|
||||||
|
identifierColumn: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IServerConfig {
|
export interface IServerConfig {
|
||||||
|
database: DatabaseConfig;
|
||||||
Locale: string;
|
Locale: string;
|
||||||
KeyTogglePhone: number;
|
KeyTogglePhone: number;
|
||||||
KeyTakeCall: number;
|
KeyTakeCall: number;
|
||||||
|
|||||||
Reference in new issue
Block a user