mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
Merge pull request #155 from project-error/tweak/remove-esx-code
feat(standalone): Allows for the phone to be free from framework requirements
This commit is contained in:
15 files changed
+349
-146
No files matched your search
@@ -19,7 +19,7 @@ function fetchOnInitialize() {
|
|||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
app: 'PHONE',
|
app: 'PHONE',
|
||||||
method: 'setPhoneReady',
|
method: 'setPhoneReady',
|
||||||
data: true,
|
data: isPhoneReady,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -110,7 +110,8 @@ async function Phone(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onNet('esx:playerLoaded', fetchOnInitialize);
|
// triggerd when the player is ready
|
||||||
|
onNet(events.PLAYER_IS_READY, fetchOnInitialize);
|
||||||
|
|
||||||
AddEventHandler('onResourceStop', function (resource: string) {
|
AddEventHandler('onResourceStop', function (resource: string) {
|
||||||
if (resource === GetCurrentResourceName()) {
|
if (resource === GetCurrentResourceName()) {
|
||||||
|
|||||||
@@ -1,57 +1,94 @@
|
|||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import { ESX } from './server';
|
import config from '../utils/config';
|
||||||
import { XPlayer } from 'esx.js/@types/server';
|
import { mainLogger } from './sv_logger';
|
||||||
|
import { Player, Players, PlayersByIdentifier } from './players/sv_players';
|
||||||
|
|
||||||
interface IPhoneNumber {
|
export function getPlayer(source: number): Player {
|
||||||
phone_number: string;
|
const player = Players.get(source);
|
||||||
}
|
if (!player) mainLogger.error(`Could not find player from source: ${source}`);
|
||||||
|
return player;
|
||||||
interface Identifier {
|
|
||||||
identifier: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function usePhoneNumber(identifier: string): Promise<string> {
|
|
||||||
const query = 'SELECT phone_number FROM users WHERE identifier = ?';
|
|
||||||
const [results] = await pool.query(query, [identifier]);
|
|
||||||
const phoneNumber = <IPhoneNumber[]>results;
|
|
||||||
return phoneNumber[0].phone_number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getSource = () => (global as any).source;
|
export const getSource = () => (global as any).source;
|
||||||
|
|
||||||
// we might need to run a db query on this.
|
// we might need to run a db query on this.
|
||||||
// to make it more standalone
|
// to make it more standalone
|
||||||
|
/**
|
||||||
|
* Return the identifier for an online player
|
||||||
|
* @param source The source/netId for the player in question
|
||||||
|
**/
|
||||||
export function getIdentifier(source: number): string {
|
export function getIdentifier(source: number): string {
|
||||||
return ESX.GetPlayerFromId(source).getIdentifier();
|
return getPlayer(source).identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getIdentifierByPhoneNumber(phoneNumber: string): Promise<string> {
|
/**
|
||||||
const query = 'SELECT identifier FROM users WHERE phone_number = ?';
|
* Return the attached identifier for a given phone number
|
||||||
const [results] = await pool.query(query, [phoneNumber]);
|
* @param phoneNumber The phone number to return identifier for
|
||||||
const identifier = <Identifier[]>results;
|
* @param fetch Whether or not to query the database if a given player is offline
|
||||||
return identifier[0].identifier;
|
**/
|
||||||
|
export async function getIdentifierByPhoneNumber(
|
||||||
|
phoneNumber: string,
|
||||||
|
fetch?: boolean,
|
||||||
|
): Promise<string | null> {
|
||||||
|
for (const [source, player] of Players) {
|
||||||
|
if (player.getPhoneNumber() === phoneNumber) return player.identifier;
|
||||||
|
}
|
||||||
|
// Whether we fetch from database if not found in online players
|
||||||
|
if (fetch) {
|
||||||
|
const query = `SELECT identifier FROM users WHERE phone_number = ?`;
|
||||||
|
const [results] = await pool.query(query, [phoneNumber]);
|
||||||
|
// Get identifier from results
|
||||||
|
return (results as { identifier: string }[])[0].identifier;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the player phoneNumber for a passed identifier
|
* Returns the player phoneNumber for a passed identifier
|
||||||
* @param identifier The players phone number
|
* @param identifier The players phone number
|
||||||
*/
|
*/
|
||||||
export async function getPlayerFromIdentifier(
|
export function getPlayerFromIdentifier(identifier: string) {
|
||||||
identifier: string
|
const player = PlayersByIdentifier.get(identifier);
|
||||||
): Promise<XPlayer> {
|
if (!player) mainLogger.error(`Could not find player from identifier: ${identifier}`);
|
||||||
return new Promise((res, rej) => {
|
return player;
|
||||||
const xPlayers = ESX.GetPlayers();
|
}
|
||||||
|
|
||||||
for (const player of xPlayers) {
|
function getRandomPhoneNumber() {
|
||||||
const xPlayer = ESX.GetPlayerFromId(player);
|
let randomNumber: string = null;
|
||||||
if (
|
|
||||||
xPlayer.getIdentifier() != null &&
|
if (!config.general.useDashNumber) {
|
||||||
xPlayer.getIdentifier() == identifier
|
randomNumber = Math.floor(Math.random() * 10000000).toString(); // 10000000 creates a number with 7 characters.
|
||||||
) {
|
} else {
|
||||||
res(xPlayer);
|
randomNumber = Math.floor(Math.random() * 10000000)
|
||||||
|
.toString()
|
||||||
|
.replace(/(\d{3})(\d{4})/, '$1-$2');
|
||||||
|
// The numbers inside {} in replace() can be changed to how many digits you want on each side of the dash.
|
||||||
|
// Example: 123-4567
|
||||||
|
}
|
||||||
|
mainLogger.debug(`Getting random number: ${randomNumber}`);
|
||||||
|
|
||||||
|
return randomNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function generatePhoneNumber(identifier: string): Promise<void> {
|
||||||
|
const getQuery = `SELECT phone_number FROM users WHERE identifier = ?`;
|
||||||
|
const [results] = await pool.query(getQuery, [identifier]);
|
||||||
|
const result = <any[]>results;
|
||||||
|
const phoneNumber = result[0]?.phone_number;
|
||||||
|
|
||||||
|
if (!phoneNumber) {
|
||||||
|
let existingId;
|
||||||
|
let newNumber;
|
||||||
|
do {
|
||||||
|
newNumber = getRandomPhoneNumber();
|
||||||
|
try {
|
||||||
|
existingId = await getIdentifierByPhoneNumber(newNumber);
|
||||||
|
} catch (e) {
|
||||||
|
existingId = false;
|
||||||
}
|
}
|
||||||
}
|
} while (existingId);
|
||||||
|
mainLogger.debug(`Inserting number into Database: ${newNumber}`);
|
||||||
rej(new Error('Call Target Identifier was not found in xPlayers array'));
|
const query = 'UPDATE users SET phone_number = ? WHERE identifier = ?';
|
||||||
});
|
await pool.query(query, [newNumber, identifier]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { generatePhoneNumber, getPlayer, getSource } from '../functions';
|
||||||
|
import { mainLogger } from '../sv_logger';
|
||||||
|
import {
|
||||||
|
getPlayerInfo,
|
||||||
|
handlePlayerAdd,
|
||||||
|
Player,
|
||||||
|
playerLogger,
|
||||||
|
Players,
|
||||||
|
PlayersByIdentifier,
|
||||||
|
} from './sv_players';
|
||||||
|
|
||||||
|
on('playerJoining', async () => {
|
||||||
|
const _source = getSource();
|
||||||
|
await handlePlayerAdd(_source);
|
||||||
|
mainLogger.debug(`Event Source Type: ${typeof _source}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle removing from player maps when player disconnects
|
||||||
|
on('playerDropped', () => {
|
||||||
|
const _source = getSource();
|
||||||
|
// Get identifier for player to remove
|
||||||
|
const playerIdentifier = getPlayer(_source).identifier;
|
||||||
|
// 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) {
|
||||||
|
mainLogger.debug(`Type for getPlayers: ${typeof player}`);
|
||||||
|
await handlePlayerAdd(parseInt(player));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
import { mainLogger } from '../sv_logger';
|
||||||
|
import { pool } from '../db';
|
||||||
|
import { generatePhoneNumber } from '../functions';
|
||||||
|
import events from '../../utils/events';
|
||||||
|
|
||||||
|
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
|
||||||
|
// guranteed
|
||||||
|
let playerIdentifer;
|
||||||
|
for (const identifier of playerIdentifiers) {
|
||||||
|
if (identifier.includes('license:')) {
|
||||||
|
playerIdentifer = identifier.split(':')[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!playerIdentifer) {
|
||||||
|
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
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add to players by identifier map
|
||||||
|
PlayersByIdentifier.set(newPlayer.identifier, 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(events.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 };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
public readonly 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 = firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 = lastname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import './sv_notes';
|
|||||||
import './sv_photo';
|
import './sv_photo';
|
||||||
import './sv_messages';
|
import './sv_messages';
|
||||||
import './sv_call';
|
import './sv_call';
|
||||||
|
import './players/sv_player_events';
|
||||||
import { mainLogger } from './sv_logger';
|
import { mainLogger } from './sv_logger';
|
||||||
|
|
||||||
export let ESX: Server = null;
|
export let ESX: Server = null;
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ async function getTransfer(transferId: number): Promise<Transfer> {
|
|||||||
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
|
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = await getIdentifier(_source);
|
const _identifier = getIdentifier(_source);
|
||||||
const transfer = await fetchAllTransactions(_identifier);
|
const transfer = await fetchAllTransactions(_identifier);
|
||||||
|
|
||||||
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer);
|
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer);
|
||||||
|
|||||||
+16
-15
@@ -1,13 +1,7 @@
|
|||||||
import { ESX } from './server';
|
|
||||||
import events from '../utils/events';
|
import events from '../utils/events';
|
||||||
import {
|
import { getIdentifierByPhoneNumber, getPlayer, getSource } from './functions';
|
||||||
getIdentifierByPhoneNumber,
|
|
||||||
usePhoneNumber,
|
|
||||||
getIdentifier,
|
|
||||||
getSource,
|
|
||||||
} from './functions';
|
|
||||||
|
|
||||||
import { getPlayerFromIdentifier } from './functions'
|
import { getPlayerFromIdentifier } from './functions';
|
||||||
import { ICall } from '../../phone/src/common/typings/call';
|
import { ICall } from '../../phone/src/common/typings/call';
|
||||||
|
|
||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
@@ -42,13 +36,17 @@ onNet(events.PHONE_INITIALIZE_CALL, async (phoneNumber: string, timestamp: numbe
|
|||||||
const callIdentifier = uuidv4();
|
const callIdentifier = uuidv4();
|
||||||
|
|
||||||
// the client that is calling
|
// the client that is calling
|
||||||
const xTransmitter = ESX.GetPlayerFromId(_source);
|
// TODO: Handle offline player calling
|
||||||
const transmitterNumber = await usePhoneNumber(xTransmitter.getIdentifier());
|
const transmitterNumber = getPlayer(_source).getPhoneNumber();
|
||||||
|
|
||||||
// player who is being called
|
// player who is being called
|
||||||
const receiverIdentifier = await getIdentifierByPhoneNumber(phoneNumber);
|
const receiverIdentifier = await getIdentifierByPhoneNumber(phoneNumber, true);
|
||||||
const xReceiver = await getPlayerFromIdentifier(receiverIdentifier);
|
const xReceiver = getPlayerFromIdentifier(receiverIdentifier);
|
||||||
const receiverNumber = phoneNumber;
|
const receiverNumber = phoneNumber;
|
||||||
|
|
||||||
|
callLogger.debug('Receiver Number:', receiverIdentifier)
|
||||||
|
|
||||||
|
callLogger.debug(`${_source} xReceiver.source`);
|
||||||
|
|
||||||
calls.set(transmitterNumber, {
|
calls.set(transmitterNumber, {
|
||||||
identifier: callIdentifier,
|
identifier: callIdentifier,
|
||||||
@@ -60,6 +58,8 @@ onNet(events.PHONE_INITIALIZE_CALL, async (phoneNumber: string, timestamp: numbe
|
|||||||
accepted: false,
|
accepted: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
callLogger.debug(`Call Set key: ${transmitterNumber}`);
|
||||||
|
|
||||||
const currentCall = calls.get(transmitterNumber);
|
const currentCall = calls.get(transmitterNumber);
|
||||||
await saveCall(currentCall);
|
await saveCall(currentCall);
|
||||||
|
|
||||||
@@ -107,8 +107,10 @@ onNet(events.PHONE_CALL_REJECTED, async (transmitterNumber: string, timestamp: n
|
|||||||
|
|
||||||
// player who is called and initiasted the rejection.
|
// player who is called and initiasted the rejection.
|
||||||
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.receiverSource);
|
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.receiverSource);
|
||||||
|
|
||||||
// player who is calling and recieved the rejection.
|
// player who is calling and recieved the rejection.
|
||||||
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.transmitterSource);
|
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.transmitterSource);
|
||||||
|
calls.delete(transmitterNumber);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
callLogger.error(`Phone Call Rejected Event Error ${e.message}`, {
|
callLogger.error(`Phone Call Rejected Event Error ${e.message}`, {
|
||||||
source: pSource,
|
source: pSource,
|
||||||
@@ -144,8 +146,7 @@ onNet(events.PHONE_END_CALL, async (transmitterNumber: string, timestamp: number
|
|||||||
onNet(events.PHONE_CALL_FETCH_CALLS, async () => {
|
onNet(events.PHONE_CALL_FETCH_CALLS, async () => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const identifier = await getIdentifier(_source);
|
const phoneNumber = getPlayer(_source).getPhoneNumber();
|
||||||
const phoneNumber = await usePhoneNumber(identifier);
|
|
||||||
const calls = await fetchCalls(phoneNumber);
|
const calls = await fetchCalls(phoneNumber);
|
||||||
emitNet(events.PHONE_CALL_SEND_HISTORY, _source, calls);
|
emitNet(events.PHONE_CALL_SEND_HISTORY, _source, calls);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { ESX } from './server';
|
|
||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import events from '../utils/events';
|
import events from '../utils/events';
|
||||||
import { getIdentifier, getSource } from './functions';
|
import { getIdentifier, getSource } from './functions';
|
||||||
@@ -89,7 +88,7 @@ onNet(events.CONTACTS_ADD_CONTACT, async (number: string, display: string, avata
|
|||||||
onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
|
onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = ESX.GetPlayerFromId(_source).getIdentifier();
|
const _identifier = getIdentifier(_source);
|
||||||
await updateContact(contact, _identifier);
|
await updateContact(contact, _identifier);
|
||||||
|
|
||||||
emitNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, _source);
|
emitNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, _source);
|
||||||
@@ -113,7 +112,7 @@ onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
|
|||||||
onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
|
onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = await getIdentifier(_source);
|
const _identifier = getIdentifier(_source);
|
||||||
await deleteContact(contact, _identifier);
|
await deleteContact(contact, _identifier);
|
||||||
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, _source);
|
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, _source);
|
||||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
|
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
|
||||||
|
|||||||
@@ -1,60 +1,12 @@
|
|||||||
import { ESX } from './server';
|
|
||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import config from '../utils/config';
|
import config from '../utils/config';
|
||||||
import { getIdentifier, getIdentifierByPhoneNumber, getSource, usePhoneNumber } from './functions';
|
import { getIdentifier, getSource } from './functions';
|
||||||
import { getOrCreateProfile } from './sv_twitter';
|
|
||||||
import { mainLogger } from './sv_logger';
|
import { mainLogger } from './sv_logger';
|
||||||
import events from '../utils/events';
|
|
||||||
|
|
||||||
//db = DatabaseConfig //helper variable for use in server function
|
|
||||||
|
|
||||||
//we pass the server configuration to the phone so that we can have phone or
|
|
||||||
//app specific settings configured by the server owner
|
|
||||||
//ESX.RegisterServerCallback('phone:phoneConfig', function(source, cb) {
|
|
||||||
// cb(Config)
|
|
||||||
//});
|
|
||||||
|
|
||||||
interface Credentials {
|
interface Credentials {
|
||||||
phone_number: string;
|
phone_number: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate phone number
|
|
||||||
|
|
||||||
function getRandomPhoneNumber() {
|
|
||||||
let randomNumber: string = null;
|
|
||||||
|
|
||||||
if (!config.general.useDashNumber) {
|
|
||||||
randomNumber = Math.floor(Math.random() * 10000000).toString(); // 10000000 creates a number with 7 characters.
|
|
||||||
} else {
|
|
||||||
randomNumber = Math.floor(Math.random() * 10000000)
|
|
||||||
.toString()
|
|
||||||
.replace(/(\d{3})(\d{4})/, '$1-$2'); // ;
|
|
||||||
// The numbers inside {} in replace() can be changed to how many digits you want on each side of the dash.
|
|
||||||
// Example: 123-4567
|
|
||||||
}
|
|
||||||
mainLogger.verbose(`Getting random number: ${randomNumber}`);
|
|
||||||
|
|
||||||
return randomNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generatePhoneNumber(identifier: string) {
|
|
||||||
if (!(await usePhoneNumber(identifier))) {
|
|
||||||
let existingId;
|
|
||||||
let newNumber;
|
|
||||||
do {
|
|
||||||
newNumber = getRandomPhoneNumber();
|
|
||||||
try {
|
|
||||||
existingId = await getIdentifierByPhoneNumber(newNumber);
|
|
||||||
} catch (e) {
|
|
||||||
existingId = false;
|
|
||||||
}
|
|
||||||
} while (existingId);
|
|
||||||
mainLogger.verbose(`Inserting number into Database: ${newNumber}`);
|
|
||||||
const query = 'UPDATE users SET phone_number = ? WHERE identifier = ?';
|
|
||||||
await pool.query(query, [newNumber, identifier]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getCredentials(identifier: string): Promise<string> {
|
async function getCredentials(identifier: string): Promise<string> {
|
||||||
const query = 'SELECT phone_number FROM users WHERE identifier = ?';
|
const query = 'SELECT phone_number FROM users WHERE identifier = ?';
|
||||||
const [result] = await pool.query(query, [identifier]);
|
const [result] = await pool.query(query, [identifier]);
|
||||||
@@ -63,22 +15,6 @@ async function getCredentials(identifier: string): Promise<string> {
|
|||||||
return number[0].phone_number;
|
return number[0].phone_number;
|
||||||
}
|
}
|
||||||
|
|
||||||
onNet('esx:playerLoaded', async (playerId: number, xPlayer: any) => {
|
|
||||||
const _source = getSource();
|
|
||||||
const identifier = xPlayer.identifier;
|
|
||||||
try {
|
|
||||||
await generatePhoneNumber(identifier);
|
|
||||||
} catch (e) {
|
|
||||||
mainLogger.error(`Failed to generate a phone number, ${e.message}`, {
|
|
||||||
source: _source,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure a twitter profile exists for this user before
|
|
||||||
// we load the phone
|
|
||||||
await getOrCreateProfile(identifier);
|
|
||||||
});
|
|
||||||
|
|
||||||
onNet('phone:getCredentials', async () => {
|
onNet('phone:getCredentials', async () => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
@@ -90,4 +26,4 @@ onNet('phone:getCredentials', async () => {
|
|||||||
source: _source,
|
source: _source,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -408,7 +408,7 @@ onNet(
|
|||||||
async (phoneNumbers: string[], label: string = null) => {
|
async (phoneNumbers: string[], label: string = null) => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = await getIdentifier(_source);
|
const _identifier = getIdentifier(_source);
|
||||||
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
|
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
|
||||||
|
|
||||||
if (result.error && result.duplicate) {
|
if (result.error && result.duplicate) {
|
||||||
@@ -443,7 +443,7 @@ onNet(
|
|||||||
for (const participantId of result.identifiers) {
|
for (const participantId of result.identifiers) {
|
||||||
// we don't broadcast to the source of the event.
|
// we don't broadcast to the source of the event.
|
||||||
if (participantId !== _identifier) {
|
if (participantId !== _identifier) {
|
||||||
const participantPlayer = await getPlayerFromIdentifier(participantId);
|
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
|
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -467,7 +467,7 @@ onNet(
|
|||||||
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
|
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = await getIdentifier(_source);
|
const _identifier = getIdentifier(_source);
|
||||||
const messages = await getMessages(_identifier, groupId);
|
const messages = await getMessages(_identifier, groupId);
|
||||||
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, _source, messages);
|
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, _source, messages);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -493,7 +493,7 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
|
|||||||
for (const participantId of userParticipants) {
|
for (const participantId of userParticipants) {
|
||||||
// we don't broadcast to the source of the event.
|
// we don't broadcast to the source of the event.
|
||||||
if (participantId !== _identifier) {
|
if (participantId !== _identifier) {
|
||||||
const participantPlayer = await getPlayerFromIdentifier(participantId);
|
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||||
emitNet(events.MESSAGES_CREATE_MESSAGE_BROADCAST, participantPlayer.source, {
|
emitNet(events.MESSAGES_CREATE_MESSAGE_BROADCAST, participantPlayer.source, {
|
||||||
groupName,
|
groupName,
|
||||||
groupId,
|
groupId,
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
|
|||||||
onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
|
onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
|
||||||
const pSource = getSource();
|
const pSource = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = await getIdentifier(pSource);
|
const _identifier = getIdentifier(pSource);
|
||||||
const notes = await fetchAllNotes(_identifier);
|
const notes = await fetchAllNotes(_identifier);
|
||||||
emitNet(events.NOTE_SEND_NOTE, pSource, notes);
|
emitNet(events.NOTE_SEND_NOTE, pSource, notes);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -86,7 +86,7 @@ onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
|
|||||||
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
|
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
|
||||||
const pSource = getSource();
|
const pSource = getSource();
|
||||||
try {
|
try {
|
||||||
const _identifier = await getIdentifier(pSource);
|
const _identifier = getIdentifier(pSource);
|
||||||
await updateNote(note, _identifier);
|
await updateNote(note, _identifier);
|
||||||
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource);
|
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import events from '../utils/events';
|
import events from '../utils/events';
|
||||||
import { ESX } from './server';
|
import { getIdentifier, getPlayer, getSource } from './functions';
|
||||||
import { getIdentifier, getSource } from './functions';
|
|
||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import { usePhoneNumber } from './functions';
|
|
||||||
import { mainLogger } from './sv_logger';
|
import { mainLogger } from './sv_logger';
|
||||||
import { MarketplaceListing } from '../../phone/src/common/typings/marketplace';
|
import { MarketplaceListing } from '../../phone/src/common/typings/marketplace';
|
||||||
import { reportListingToDiscord } from './discord';
|
import { reportListingToDiscord } from './discord';
|
||||||
@@ -81,15 +79,16 @@ onNet(events.SELLOUT_FETCH_LISTING, async () => {
|
|||||||
onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
|
onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
try {
|
try {
|
||||||
const xPlayer = ESX.GetPlayerFromId(_source);
|
const player = getPlayer(_source);
|
||||||
const _identifier = xPlayer.getIdentifier();
|
|
||||||
const name = xPlayer.getName();
|
|
||||||
|
|
||||||
// This is used as username for the reports
|
// This is used as username for the reports
|
||||||
const playerName = GetPlayerName(_source);
|
await addListing(
|
||||||
|
player.identifier,
|
||||||
const phoneNumber = await usePhoneNumber(_identifier);
|
player.username,
|
||||||
await addListing(_identifier, playerName, name, phoneNumber, listing);
|
player.getName(),
|
||||||
|
player.getPhoneNumber(),
|
||||||
|
listing,
|
||||||
|
);
|
||||||
|
|
||||||
emitNet(events.SELLOUT_ADD_LISTING_SUCCESS, _source);
|
emitNet(events.SELLOUT_ADD_LISTING_SUCCESS, _source);
|
||||||
emitNet(events.SELLOUT_ACTION_RESULT, _source, {
|
emitNet(events.SELLOUT_ACTION_RESULT, _source, {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import { ESX } from './server';
|
|
||||||
import { getIdentifier, getSource } from './functions';
|
import { getIdentifier, getSource } from './functions';
|
||||||
import { NewTweet, Tweet, Profile } from '../../phone/src/common/typings/twitter';
|
import { NewTweet, Tweet, Profile } from '../../phone/src/common/typings/twitter';
|
||||||
import events from '../utils/events';
|
import events from '../utils/events';
|
||||||
@@ -38,7 +37,7 @@ const SELECT_FIELDS = `
|
|||||||
npwd_twitter_tweets.createdAt,
|
npwd_twitter_tweets.createdAt,
|
||||||
npwd_twitter_tweets.updatedAt,
|
npwd_twitter_tweets.updatedAt,
|
||||||
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
|
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
|
||||||
`
|
`;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the latest 100 tweets
|
* Retrieve the latest 100 tweets
|
||||||
@@ -204,7 +203,6 @@ export async function getOrCreateProfile(identifier: string): Promise<Profile> {
|
|||||||
return profile || (await createDefaultProfile(identifier));
|
return profile || (await createDefaultProfile(identifier));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function updateProfile(identifier: string, profile: Profile) {
|
async function updateProfile(identifier: string, profile: Profile) {
|
||||||
const { avatar_url, profile_name, bio, location, job } = profile;
|
const { avatar_url, profile_name, bio, location, job } = profile;
|
||||||
const query = `
|
const query = `
|
||||||
@@ -299,7 +297,7 @@ async function doesRetweetExist(tweetId: number, identifier: string): Promise<bo
|
|||||||
SELECT COUNT(id) as count
|
SELECT COUNT(id) as count
|
||||||
FROM npwd_twitter_tweets
|
FROM npwd_twitter_tweets
|
||||||
WHERE (id = ? OR retweet = ?) AND identifier = ?
|
WHERE (id = ? OR retweet = ?) AND identifier = ?
|
||||||
`
|
`;
|
||||||
const [results] = await pool.query(query, [tweetId, tweetId, identifier]);
|
const [results] = await pool.query(query, [tweetId, tweetId, identifier]);
|
||||||
const counts = <any[]>results;
|
const counts = <any[]>results;
|
||||||
return counts[0].count > 0;
|
return counts[0].count > 0;
|
||||||
@@ -308,7 +306,7 @@ async function doesRetweetExist(tweetId: number, identifier: string): Promise<bo
|
|||||||
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
const identifier = getIdentifier(_source);
|
const identifier = getIdentifier(_source);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const profile = await getOrCreateProfile(identifier);
|
const profile = await getOrCreateProfile(identifier);
|
||||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
||||||
@@ -429,7 +427,7 @@ onNet(events.TWITTER_RETWEET, async (tweetId: number) => {
|
|||||||
|
|
||||||
// alert the player that they have already retweeted
|
// alert the player that they have already retweeted
|
||||||
// this post (or that they are the original poster)
|
// this post (or that they are the original poster)
|
||||||
if(await doesRetweetExist(tweetId, identifier)) {
|
if (await doesRetweetExist(tweetId, identifier)) {
|
||||||
emitNet(events.TWITTER_RETWEET_EXISTS, _source, {
|
emitNet(events.TWITTER_RETWEET_EXISTS, _source, {
|
||||||
message: 'TWITTER_RETWEET_EXISTS',
|
message: 'TWITTER_RETWEET_EXISTS',
|
||||||
type: 'error',
|
type: 'error',
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export interface IPlayer {
|
||||||
|
identifier: string;
|
||||||
|
source: number;
|
||||||
|
name: string;
|
||||||
|
phone_number: string;
|
||||||
|
}
|
||||||
@@ -96,6 +96,7 @@ export default {
|
|||||||
NOTE_ACTION_RESULT: 'phone:notesActionResult',
|
NOTE_ACTION_RESULT: 'phone:notesActionResult',
|
||||||
|
|
||||||
// DO NOT CHANGE WHATS BELOW, PLEASE - CHIP
|
// DO NOT CHANGE WHATS BELOW, PLEASE - CHIP
|
||||||
|
PLAYER_IS_READY: 'phone:playerIsReady',
|
||||||
OPEN_APP_CONTACTS: 'phone:app:CONTACTS',
|
OPEN_APP_CONTACTS: 'phone:app:CONTACTS',
|
||||||
OPEN_APP_BANK: 'phone:app:BANK',
|
OPEN_APP_BANK: 'phone:app:BANK',
|
||||||
OPEN_APP_NOTES: 'phone:app:NOTES',
|
OPEN_APP_NOTES: 'phone:app:NOTES',
|
||||||
|
|||||||
Reference in new issue
Block a user