mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
feat(resources): remove all esx code, almost
This commit is contained in:
1 parent
6b6080c4b2
commit
c99d0780ae
12 files changed
+121
-84
No files matched your search
@@ -5,6 +5,13 @@ import { phoneCloseAnim, phoneOpenAnim, removePhoneProp } from './functions';
|
||||
let isPhoneOpen = false;
|
||||
let isPhoneReady = false;
|
||||
|
||||
while (true) {
|
||||
if (NetworkIsPlayerActive(PlayerId())) {
|
||||
emitNet(events.PHONE_IS_READY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* * * * * * * * * * * * *
|
||||
*
|
||||
* Phone initialize data
|
||||
@@ -19,7 +26,7 @@ function fetchOnInitialize() {
|
||||
JSON.stringify({
|
||||
app: 'PHONE',
|
||||
method: 'setPhoneReady',
|
||||
data: true,
|
||||
data: isPhoneReady,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -110,7 +117,8 @@ async function Phone(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
onNet('esx:playerLoaded', fetchOnInitialize);
|
||||
// triggerd when the player is ready
|
||||
onNet(events.PHONE_IS_READY, fetchOnInitialize);
|
||||
|
||||
AddEventHandler('onResourceStop', function (resource: string) {
|
||||
if (resource === GetCurrentResourceName()) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IPlayer } from '../typings/players';
|
||||
import { pool } from './db';
|
||||
import { ESX } from './server';
|
||||
import { XPlayer } from 'esx.js/@types/server';
|
||||
import { Players } from './sv_main';
|
||||
|
||||
interface IPhoneNumber {
|
||||
phone_number: string;
|
||||
@@ -10,11 +10,27 @@ interface Identifier {
|
||||
identifier: string;
|
||||
}
|
||||
|
||||
export async function usePhoneNumber(identifier: string): Promise<string> {
|
||||
const query = 'SELECT phone_number FROM users WHERE identifier = ?';
|
||||
export async function usePlayer(
|
||||
identifier: string,
|
||||
): Promise<Pick<IPlayer, 'name' | 'phone_number'>> {
|
||||
const query = `SELECT firstname, lastname, phone_number FROM users WHERE identifier = ?`;
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
const phoneNumber = <IPhoneNumber[]>results;
|
||||
return phoneNumber[0].phone_number;
|
||||
const creds = <{ firstname: string; lastname: string; phone_number: string }[]>results;
|
||||
|
||||
const name = `${creds[0].firstname} ${creds[0].lastname}`;
|
||||
const phone_number = creds[0].phone_number;
|
||||
|
||||
return { name, phone_number };
|
||||
}
|
||||
|
||||
export function usePhoneNumber(identifier: string) {
|
||||
const player = getPlayerFromIdentifier(identifier);
|
||||
return player?.phone_number || null;
|
||||
}
|
||||
|
||||
export function useName(identifer: string) {
|
||||
const player = getPlayerFromIdentifier(identifer);
|
||||
return player?.name || null;
|
||||
}
|
||||
|
||||
export const getSource = () => (global as any).source;
|
||||
@@ -22,7 +38,8 @@ export const getSource = () => (global as any).source;
|
||||
// we might need to run a db query on this.
|
||||
// to make it more standalone
|
||||
export function getIdentifier(source: number): string {
|
||||
return ESX.GetPlayerFromId(source).getIdentifier();
|
||||
const player = getPlayerBySource(source);
|
||||
return player?.identifier || null;
|
||||
}
|
||||
|
||||
export async function getIdentifierByPhoneNumber(phoneNumber: string): Promise<string> {
|
||||
@@ -32,26 +49,19 @@ export async function getIdentifierByPhoneNumber(phoneNumber: string): Promise<s
|
||||
return identifier[0].identifier;
|
||||
}
|
||||
|
||||
export const getPlayerBySource = (source: any) => {
|
||||
return Players.get(source);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the player phoneNumber for a passed identifier
|
||||
* @param identifier The players phone number
|
||||
*/
|
||||
export async function getPlayerFromIdentifier(
|
||||
identifier: string
|
||||
): Promise<XPlayer> {
|
||||
return new Promise((res, rej) => {
|
||||
const xPlayers = ESX.GetPlayers();
|
||||
|
||||
for (const player of xPlayers) {
|
||||
const xPlayer = ESX.GetPlayerFromId(player);
|
||||
if (
|
||||
xPlayer.getIdentifier() != null &&
|
||||
xPlayer.getIdentifier() == identifier
|
||||
) {
|
||||
res(xPlayer);
|
||||
}
|
||||
export function getPlayerFromIdentifier(identifier: string) {
|
||||
for (const player of Array.from(Players.entries())) {
|
||||
if (player[1].identifier === identifier) {
|
||||
return player[1];
|
||||
}
|
||||
|
||||
rej(new Error('Call Target Identifier was not found in xPlayers array'));
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -78,7 +78,7 @@ async function getTransfer(transferId: number): Promise<Transfer> {
|
||||
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const _identifier = getIdentifier(_source);
|
||||
const transfer = await fetchAllTransactions(_identifier);
|
||||
|
||||
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer);
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { ESX } from './server';
|
||||
import events from '../utils/events';
|
||||
import {
|
||||
getIdentifierByPhoneNumber,
|
||||
usePhoneNumber,
|
||||
getIdentifier,
|
||||
getSource,
|
||||
} from './functions';
|
||||
import { getIdentifierByPhoneNumber, usePhoneNumber, getIdentifier, getSource } from './functions';
|
||||
|
||||
import { getPlayerFromIdentifier } from './functions'
|
||||
import { getPlayerFromIdentifier } from './functions';
|
||||
import { ICall } from '../../phone/src/common/typings/call';
|
||||
|
||||
import { pool } from './db';
|
||||
@@ -42,12 +36,11 @@ onNet(events.PHONE_INITIALIZE_CALL, async (phoneNumber: string, timestamp: numbe
|
||||
const callIdentifier = uuidv4();
|
||||
|
||||
// the client that is calling
|
||||
const xTransmitter = ESX.GetPlayerFromId(_source);
|
||||
const transmitterNumber = await usePhoneNumber(xTransmitter.getIdentifier());
|
||||
const transmitterNumber = usePhoneNumber(getIdentifier(_source));
|
||||
|
||||
// player who is being called
|
||||
const receiverIdentifier = await getIdentifierByPhoneNumber(phoneNumber);
|
||||
const xReceiver = await getPlayerFromIdentifier(receiverIdentifier);
|
||||
const xReceiver = getPlayerFromIdentifier(receiverIdentifier);
|
||||
const receiverNumber = phoneNumber;
|
||||
|
||||
calls.set(transmitterNumber, {
|
||||
@@ -144,8 +137,8 @@ onNet(events.PHONE_END_CALL, async (transmitterNumber: string, timestamp: number
|
||||
onNet(events.PHONE_CALL_FETCH_CALLS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = await getIdentifier(_source);
|
||||
const phoneNumber = await usePhoneNumber(identifier);
|
||||
const identifier = getIdentifier(_source);
|
||||
const phoneNumber = usePhoneNumber(identifier);
|
||||
const calls = await fetchCalls(phoneNumber);
|
||||
emitNet(events.PHONE_CALL_SEND_HISTORY, _source, calls);
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { ESX } from './server';
|
||||
import { pool } from './db';
|
||||
import events from '../utils/events';
|
||||
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) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = ESX.GetPlayerFromId(_source).getIdentifier();
|
||||
const _identifier = getIdentifier(_source);
|
||||
await updateContact(contact, _identifier);
|
||||
|
||||
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) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const _identifier = getIdentifier(_source);
|
||||
await deleteContact(contact, _identifier);
|
||||
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, _source);
|
||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
|
||||
|
||||
+47
-21
@@ -1,10 +1,10 @@
|
||||
import { ESX } from './server';
|
||||
import { pool } from './db';
|
||||
import config from '../utils/config';
|
||||
import { getIdentifier, getIdentifierByPhoneNumber, getSource, usePhoneNumber } from './functions';
|
||||
import { getOrCreateProfile } from './sv_twitter';
|
||||
import { getIdentifier, getIdentifierByPhoneNumber, getSource, usePlayer } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
import { IPlayer } from '../typings/players';
|
||||
import events from '../utils/events';
|
||||
import { getOrCreateProfile } from './sv_twitter';
|
||||
|
||||
//db = DatabaseConfig //helper variable for use in server function
|
||||
|
||||
@@ -18,6 +18,43 @@ interface Credentials {
|
||||
phone_number: string;
|
||||
}
|
||||
|
||||
export const Players = new Map<string, IPlayer>();
|
||||
|
||||
// gets the identifers
|
||||
onNet(events.PHONE_IS_READY, async () => {
|
||||
const pSource = (global as any).source;
|
||||
|
||||
let identifier = null;
|
||||
|
||||
for (let i = 0; i < GetNumPlayerIdentifiers(pSource); i++) {
|
||||
const identifiers = GetPlayerIdentifier(pSource, i);
|
||||
|
||||
if (identifiers.includes('license')) {
|
||||
identifier = identifiers.split(':')[1];
|
||||
}
|
||||
}
|
||||
|
||||
console.log('PHONE IS READY: ', identifier);
|
||||
|
||||
if (!identifier) {
|
||||
throw new Error('Identifier could not be found.');
|
||||
}
|
||||
|
||||
try {
|
||||
await generatePhoneNumber(identifier);
|
||||
} catch (e) {
|
||||
mainLogger.error(`Failed to generate a phone number, ${e.message}`, {
|
||||
source: pSource,
|
||||
});
|
||||
}
|
||||
|
||||
const { name, phone_number } = await usePlayer(identifier);
|
||||
|
||||
Players.set(pSource, { identifier, source: pSource, name, phone_number });
|
||||
|
||||
emitNet(events.PHONE_IS_READY, pSource);
|
||||
});
|
||||
|
||||
// Generate phone number
|
||||
|
||||
function getRandomPhoneNumber() {
|
||||
@@ -38,7 +75,12 @@ function getRandomPhoneNumber() {
|
||||
}
|
||||
|
||||
async function generatePhoneNumber(identifier: string) {
|
||||
if (!(await usePhoneNumber(identifier))) {
|
||||
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 {
|
||||
@@ -63,22 +105,6 @@ async function getCredentials(identifier: string): Promise<string> {
|
||||
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 () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
@@ -90,4 +116,4 @@ onNet('phone:getCredentials', async () => {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -404,7 +404,7 @@ onNet(
|
||||
async (phoneNumbers: string[], label: string = null) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const _identifier = getIdentifier(_source);
|
||||
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
|
||||
|
||||
if (result.error && result.duplicate) {
|
||||
@@ -439,7 +439,7 @@ onNet(
|
||||
for (const participantId of result.identifiers) {
|
||||
// we don't broadcast to the source of the event.
|
||||
if (participantId !== _identifier) {
|
||||
const participantPlayer = await getPlayerFromIdentifier(participantId);
|
||||
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
|
||||
}
|
||||
}
|
||||
@@ -463,7 +463,7 @@ onNet(
|
||||
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const _identifier = getIdentifier(_source);
|
||||
const messages = await getMessages(_identifier, groupId);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, _source, messages);
|
||||
} catch (e) {
|
||||
@@ -489,7 +489,7 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
|
||||
for (const participantId of userParticipants) {
|
||||
// we don't broadcast to the source of the event.
|
||||
if (participantId !== _identifier) {
|
||||
const participantPlayer = await getPlayerFromIdentifier(participantId);
|
||||
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_BROADCAST, participantPlayer.source, {
|
||||
groupName,
|
||||
groupId,
|
||||
|
||||
@@ -51,7 +51,7 @@ onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
|
||||
onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const _identifier = await getIdentifier(pSource);
|
||||
const _identifier = getIdentifier(pSource);
|
||||
const notes = await fetchAllNotes(_identifier);
|
||||
emitNet(events.NOTE_SEND_NOTE, pSource, notes);
|
||||
} catch (e) {
|
||||
@@ -84,7 +84,7 @@ onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
|
||||
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const _identifier = await getIdentifier(pSource);
|
||||
const _identifier = getIdentifier(pSource);
|
||||
await updateNote(note, _identifier);
|
||||
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource);
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import events from '../utils/events';
|
||||
import { ESX } from './server';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { getIdentifier, getPlayerBySource, getSource } from './functions';
|
||||
import { pool } from './db';
|
||||
import { usePhoneNumber } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
import { MarketplaceListing } from '../../phone/src/common/typings/marketplace';
|
||||
import { reportListingToDiscord } from './discord';
|
||||
@@ -82,15 +81,12 @@ onNet(events.SELLOUT_FETCH_LISTING, async () => {
|
||||
onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const xPlayer = ESX.GetPlayerFromId(_source);
|
||||
const _identifier = xPlayer.getIdentifier();
|
||||
const name = xPlayer.getName();
|
||||
const player = getPlayerBySource(_source);
|
||||
|
||||
// This is used as username for the reports
|
||||
const playerName = GetPlayerName(_source);
|
||||
const username = GetPlayerName(_source);
|
||||
|
||||
const phoneNumber = await usePhoneNumber(_identifier);
|
||||
await addListing(_identifier, playerName, name, phoneNumber, listing);
|
||||
await addListing(player.identifier, username, player.name, player.phone_number, listing);
|
||||
|
||||
emitNet(events.SELLOUT_ADD_LISTING_SUCCESS, _source);
|
||||
emitNet(events.SELLOUT_ACTION_RESULT, _source, {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { pool } from './db';
|
||||
import { ESX } from './server';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { NewTweet, Tweet, Profile } from '../../phone/src/common/typings/twitter';
|
||||
import events from '../utils/events';
|
||||
@@ -38,7 +37,7 @@ const SELECT_FIELDS = `
|
||||
npwd_twitter_tweets.createdAt,
|
||||
npwd_twitter_tweets.updatedAt,
|
||||
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
|
||||
`
|
||||
`;
|
||||
|
||||
/**
|
||||
* Retrieve the latest 100 tweets
|
||||
@@ -204,7 +203,6 @@ export async function getOrCreateProfile(identifier: string): Promise<Profile> {
|
||||
return profile || (await createDefaultProfile(identifier));
|
||||
}
|
||||
|
||||
|
||||
async function updateProfile(identifier: string, profile: Profile) {
|
||||
const { avatar_url, profile_name, bio, location, job } = profile;
|
||||
const query = `
|
||||
@@ -299,7 +297,7 @@ async function doesRetweetExist(tweetId: number, identifier: string): Promise<bo
|
||||
SELECT COUNT(id) as count
|
||||
FROM npwd_twitter_tweets
|
||||
WHERE (id = ? OR retweet = ?) AND identifier = ?
|
||||
`
|
||||
`;
|
||||
const [results] = await pool.query(query, [tweetId, tweetId, identifier]);
|
||||
const counts = <any[]>results;
|
||||
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 () => {
|
||||
const _source = getSource();
|
||||
const identifier = getIdentifier(_source);
|
||||
|
||||
|
||||
try {
|
||||
const profile = await getOrCreateProfile(identifier);
|
||||
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
|
||||
// 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, {
|
||||
message: 'TWITTER_RETWEET_EXISTS',
|
||||
type: 'error',
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface IPlayer {
|
||||
identifier: string;
|
||||
source: number;
|
||||
name: string;
|
||||
phone_number: string;
|
||||
}
|
||||
@@ -95,6 +95,7 @@ export default {
|
||||
NOTE_ACTION_RESULT: 'phone:notesActionResult',
|
||||
|
||||
// DO NOT CHANGE WHATS BELOW, PLEASE - CHIP
|
||||
PHONE_IS_READY: 'phone:isReady',
|
||||
OPEN_APP_CONTACTS: 'phone:app:CONTACTS',
|
||||
OPEN_APP_BANK: 'phone:app:BANK',
|
||||
OPEN_APP_NOTES: 'phone:app:NOTES',
|
||||
|
||||
Reference in new issue
Block a user