Merge branch 'master' of github.com:Re-Ignited-Development/new-phone-who-dis into feature/messages

This commit is contained in:
erik-sn committed 2020-11-18 16:50:53 -05:00
commit b87baeb12e
52 files changed
+1214 -210

No files matched your search

+1
View File
@@ -6,6 +6,7 @@ import './sv_main';
import './sv_contacts';
import './sv_sellout';
import './sv_bank';
import './sv_notes';
export let ESX: ESXServer = null;
+48 -36
View File
@@ -2,18 +2,9 @@ import events from '../utils/events';
import { ESX } from './server';
import { getSource } from "./functions";
import { pool } from './db';
import { resourceLimits } from 'worker_threads';
interface Transfer {
targetID: string;
amount: number;
message: string;
}
interface Credentials {
name: string;
balance: number;
}
import { Transfer, Credentials } from '../../phone/src/common/interfaces/bank';
import { useIdentifier } from './functions';
import { type } from 'os';
async function fetchAllTransactions(identifier: string): Promise<Transfer[]> {
@@ -33,63 +24,84 @@ function fetchCredentials(): Credentials {
balance
}
console.log(result.name, result.balance);
return result;
}
async function addTransfer(identifier: string, transfer: Transfer): Promise<any> {
const xPlayer = ESX.GetPlayerFromId(getSource())
const bankBalance = xPlayer.getAccount('bank').money;
console.log(bankBalance)
if (transfer.amount > bankBalance) {
console.log("poor man")
emitNet(events.BANK_TRANSACTION_ALERT, getSource, 'You do not have enough money on your account', 'error')
}
else if (transfer.amount <= bankBalance) {
const query = "INSERT INTO npwd_bank_transfers (identifier, target, amount, message) VALUES (?, ?, ?, ?)"
const sourceName = xPlayer.getName()
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
await pool.query(query, [
if (transfer.transferAmount > bankBalance) {
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false)
}
else if (transfer.transferAmount < bankBalance) {
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), true);
xTarget.addAccountMoney('bank', transfer.transferAmount);
xPlayer.removeAccountMoney('bank', transfer.transferAmount);
const query = "INSERT INTO npwd_bank_transfers (identifier, target, amount, message, type, source) VALUES (?, ?, ?, ?, ?, ?)"
const [results] = await pool.query(query, [
identifier,
transfer.targetID,
transfer.amount,
transfer.message
transfer.transferAmount,
transfer.message,
"Transfer",
sourceName,
])
emitNet(events.BANK_TRANSACTION_ALERT, getSource, 'You succesfully transferred money to someone', 'success')
const insertData = <any>results;
return await getTransfer(insertData.insertId)
}
}
/**
* Gets a transfer from its ID
* @param transferId - transfer id
*/
async function getTransfer(transferId: number): Promise<Transfer> {
const query = "SELECT * FROM npwd_bank_transfers WHERE id = ?"
const [ results ] = await pool.query(query, [transferId]);
const transfers = <Transfer[]>results;
const transfer = transfers[0];
return {...transfer};
}
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
try {
const _source = (global as any).source;
const _identifier = ESX.GetPlayerFromId(getSource()).getIdentifier()
const _identifier = await useIdentifier()
const transfer = await fetchAllTransactions(_identifier);
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer)
console.log("WOORK")
} catch (error) {
console.log('Failed to fetch transactions', error)
console.log("Failed to fetch transactions");
}
})
onNet(events.BANK_ADD_TRANSFER, (transfer: Transfer) => {
onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => {
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
const targetSource = xTarget.source;
try {
const _identifier = ESX.GetPlayerFromId(getSource()).getIdentifier()
addTransfer(_identifier, transfer)
const _identifier = await useIdentifier()
const transferNotify = await addTransfer(_identifier, transfer)
emitNet(events.BANK_TRANSACTION_NOTIFICATION, targetSource, transferNotify)
emitNet(events.BANK_ADD_TRANSFER_SUCCESS, getSource());
} catch (error) {
console.log('Failed to add transfer', error)
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false)
}
})
onNet(events.BANK_GET_CREDENTIALS, () => {
try {
const credentials = fetchCredentials()
console.log("New log: ", credentials.balance, credentials.name)
emitNet(events.BANK_SEND_CREDENTIALS, getSource(), credentials)
} catch (error) {
console.log(error)
console.log("Failed to fetch credentials");
}
})
+52 -5
View File
@@ -1,12 +1,20 @@
import { ESX } from "./server";
import { ESX, getSource } from "./server";
import { pool } from "./db";
import events from "../utils/events";
import { useIdentifier } from './functions'
interface Contacts {
id?: number;
display: string;
number: string;
avatar: string;
}
interface ContactId {
id: number;
}
async function fetchAllContacts(identifier: string): Promise<Contacts[]> {
const query = "SELECT * FROM npwd_phone_contacts WHERE identifier = ?";
const [results] = await pool.query(query, [identifier]);
@@ -26,6 +34,25 @@ async function addContact(
await pool.query(query, [identifier, number, display, avatar]);
}
async function updateContact(contact: Contacts, identifier: string): Promise<any> {
const query = "UPDATE npwd_phone_contacts SET number = ?, display = ?, avatar = ? WHERE id = ? AND identifier = ?"
await pool.query(query, [
contact.number,
contact.display,
contact.avatar,
contact.id,
identifier
])
}
async function deleteContact(contact: ContactId, identifier: string): Promise<any> {
const query = "DELETE FROM npwd_phone_contacts WHERE id = ? AND identifier = ?"
await pool.query(query, [
contact.id,
identifier
])
}
onNet(events.CONTACTS_GET_CONTACTS, async () => {
try {
const _source = (global as any).source;
@@ -39,16 +66,36 @@ onNet(events.CONTACTS_GET_CONTACTS, async () => {
}
})
onNet(events.CONTACTS_ADD_CONTACT, (number: string, display: string, avatar: string) => {
onNet(events.CONTACTS_ADD_CONTACT, async (number: string, display: string, avatar: string) => {
try {
const _source = (global as any).source;
const xPlayer = ESX.GetPlayerFromId(_source);
const _identifier = xPlayer.getIdentifier()
const _identifier = await useIdentifier()
addContact(_identifier, number, display, avatar);
//emit(events.CONTACTS_GET_CONTACTS);
emitNet(events.CONTACTS_ADD_CONTACT_SUCCESS, _source)
} catch(error) {
console.log("Failed to add contact: ", error);
}
});
onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
try {
const _source = (global as any)._source
const _identifier = await useIdentifier()
updateContact(contact, _identifier)
emitNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, _source)
} catch (error) {
console.log(error)
}
})
onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
try {
const _identifier = await useIdentifier()
deleteContact(contact, _identifier)
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, getSource())
} catch (error) {
console.log("DELETE CONTACT ERROR", error)
}
})
+72
View File
@@ -0,0 +1,72 @@
import events from "../utils/events";
import { ESX, getSource } from "./server";
import { pool } from "./db";
import { useIdentifier } from "./functions";
import { Note, NoteId } from "../../phone/src/common/interfaces/notes";
async function addNote(identifier: string, note: Note): Promise<any> {
const query =
"INSERT INTO npwd_notes (identifier, title, content) VALUES (?, ?, ?)";
await pool.query(query, [identifier, note.title, note.content]);
}
async function fetchAllNotes(identifier: string): Promise<Note[]> {
const query =
"SELECT * FROM npwd_notes WHERE identifier = ? ORDER BY id DESC";
const [result] = await pool.query(query, [identifier]);
const notes = <Note[]>result;
return notes;
}
async function deleteNote(noteId: number, identifier: string) {
const query = "DELETE FROM npwd_notes WHERE id = ? AND identifier = ?";
await pool.query(query, [noteId, identifier]);
}
async function updateNote(note: Note, identifier: string): Promise<any> {
const query =
"UPDATE npwd_notes SET title = ?, content = ? WHERE id = ? AND identifier = ?";
await pool.query(query, [note.title, note.content, note.id, identifier]);
}
onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
try {
const _identifier = await useIdentifier();
addNote(_identifier, note);
emitNet(events.NOTE_SEND_NOTE_SUCCESS, getSource());
} catch (error) {
console.log("NOTES ERROR: ", error);
}
});
onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
try {
const _source = (global as any).source;
const _identifier = await useIdentifier();
const notes = await fetchAllNotes(_identifier);
emitNet(events.NOTE_SEND_NOTE, _source, notes);
} catch (error) {
console.log("NOTE ERROR: ", error);
}
});
onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
try {
const _identifier = await useIdentifier();
const id = noteId.id;
await deleteNote(id, _identifier);
} catch (error) {
console.log("NOTE ERROR:", error);
}
});
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
const _source = (global as any).source;
try {
const _identifier = await useIdentifier();
updateNote(note, _identifier);
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, _source);
} catch (error) {
emitNet(events.NOTE_UPDATE_NOTE_FAILURE, _source);
}
});