tweak(server/calls): Tweaked structure and added enum constants

This commit is contained in:
Taso committed 2021-04-18 15:38:51 -04:00
1 parent b051ae9fac
commit d93bdd20d8
6 files changed
+19 -176

No files matched your search

+2 -1
View File
@@ -1,6 +1,7 @@
import { CallEvents } from '../../../typings/call';
import { getSource } from '../utils/miscUtils';
import CallService, { callLogger } from './calls.service';
import CallService from './calls.service';
import { callLogger } from './calls.utils';
onNet(CallEvents.INITIALIZE_CALL, (receivingNumber: string) => {
const src = getSource();
+9 -5
View File
@@ -1,24 +1,28 @@
import { CallHistoryItem } from '../../../typings/call';
import { pool } from '../db';
import { FetchDefaultLimits } from '../utils/ServerConstants';
export class CallsRepo {
async saveCall(call: CallHistoryItem) {
async saveCall(call: CallHistoryItem): Promise<void> {
const query =
'INSERT INTO npwd_calls (identifier, transmitter, receiver, start) VALUES (?, ?, ?, ?)';
'INSERT INTO npwd_calls (identifier, transmitter, receiver, `start`) VALUES (?, ?, ?, ?)';
await pool.query(query, [call.identifier, call.transmitter, call.receiver, call.start]);
}
async updateCall(call: CallHistoryItem, isAccepted: boolean, end: number) {
async updateCall(call: CallHistoryItem, isAccepted: boolean, end: number): Promise<void> {
const query = 'UPDATE npwd_calls SET is_accepted=?, end=? WHERE identifier = ?';
await pool.query(query, [isAccepted, end, call.identifier]);
}
async fetchCalls(phoneNumber: string, limit = 50): Promise<CallHistoryItem[] | CallHistoryItem> {
async fetchCalls(
phoneNumber: string,
limit = FetchDefaultLimits.CALLS_FETCH_LIMIT,
): Promise<CallHistoryItem[] | CallHistoryItem> {
const query =
'SELECT * FROM npwd_calls WHERE receiver = ? OR transmitter = ? ORDER BY id DESC LIMIT ?';
const [result] = await pool.query(query, [phoneNumber, phoneNumber, limit]);
return <CallHistoryItem[]>result;
return <CallHistoryItem[] | CallHistoryItem>result;
}
}
+1 -3
View File
@@ -1,11 +1,9 @@
import Collection from '@discordjs/collection';
import { CallEvents, CallHistoryItem } from '../../../typings/call';
import { mainLogger } from '../sv_logger';
import CallsDB, { CallsRepo } from './calls.db';
import { v4 as uuidv4 } from 'uuid';
import PlayerService from '../players/player.service';
export const callLogger = mainLogger.child({ module: 'calls' });
import { callLogger } from './calls.utils';
class CallsService {
private callMap: Collection<string, CallHistoryItem>;
+3
View File
@@ -0,0 +1,3 @@
import { mainLogger } from '../sv_logger';
export const callLogger = mainLogger.child({ module: 'calls' });
-167
View File
@@ -1,167 +0,0 @@
import { CallEvents } from '../../typings/call';
import { CallHistoryItem } from '../../typings/call';
import { pool } from './db';
import { v4 as uuidv4 } from 'uuid';
import { mainLogger } from './sv_logger';
const callLogger = mainLogger.child({ module: 'calls' });
async function saveCall(call: CallHistoryItem) {
const query =
'INSERT INTO npwd_calls (identifier, transmitter, receiver, start) VALUES (?, ?, ?, ?)';
await pool.query(query, [call.identifier, call.transmitter, call.receiver, call.start]);
}
async function updateCall(call: CallHistoryItem, isAccepted: boolean, end: number) {
const query = 'UPDATE npwd_calls SET is_accepted=?, end=? WHERE identifier = ?';
await pool.query(query, [isAccepted, end, call.identifier]);
}
async function fetchCalls(phoneNumber: string): Promise<CallHistoryItem[]> {
const query = 'SELECT * FROM npwd_calls WHERE receiver = ? OR transmitter = ? ORDER BY id DESC';
const [result] = await pool.query(query, [phoneNumber, phoneNumber]);
const calls = <CallHistoryItem[]>result;
return calls;
}
let calls: Map<string, CallHistoryItem> = new Map();
onNet(CallEvents.INITIALIZE_CALL, async (phoneNumber: string) => {
const _source = getSource();
const startCallTimeUnix = new Date().getTime() / 1000;
const callIdentifier = uuidv4();
// the client that is calling
// TODO: Handle offline player calling
const transmitterNumber = getPlayer(_source).getPhoneNumber();
// player who is being called
const receiverIdentifier = await getIdentifierByPhoneNumber(phoneNumber, true);
const xReceiver = getPlayerFromIdentifier(receiverIdentifier);
const receiverNumber = phoneNumber;
if (!xReceiver.source) {
callLogger.error('Player you are trying to call is not online', {
source: _source,
});
}
callLogger.debug('Receiver Number:', receiverIdentifier);
callLogger.debug(`${_source} xReceiver.source`);
calls.set(transmitterNumber, {
identifier: callIdentifier,
transmitter: transmitterNumber,
transmitterSource: _source,
receiver: receiverNumber,
receiverSource: xReceiver.source,
start: Math.floor(startCallTimeUnix),
accepted: false,
});
callLogger.debug(`Call Set key: ${transmitterNumber}`);
const currentCall = calls.get(transmitterNumber);
await saveCall(currentCall);
// events
// client that is calling
emitNet(CallEvents.START_CALL, _source, transmitterNumber, receiverNumber, true);
// client that is being called
emitNet(CallEvents.START_CALL, xReceiver.source, transmitterNumber, receiverNumber, false);
});
onNet(CallEvents.ACCEPT_CALL, async (transmitterNumber: string) => {
const pSource = getSource();
try {
const currentCall = calls.get(transmitterNumber);
// Sanity check if call exists in current set
if (!currentCall) new Error('Transmitter number does not exist in set.');
const channelId = pSource;
await updateCall(currentCall, true, null);
// player who is being called
emitNet(CallEvents.WAS_ACCEPTED, pSource, channelId, currentCall, false);
// player who is calling
emitNet(CallEvents.WAS_ACCEPTED, currentCall.transmitterSource, channelId, currentCall, true);
currentCall.accepted = true;
} catch (e) {
callLogger.error('Accepting Call Error', e.message, {
source: pSource,
});
}
});
onNet(CallEvents.REJECTED, async (transmitterNumber: string, timestamp: number) => {
const pSource = getSource();
try {
const currentCall = calls.get(transmitterNumber);
// Sanity check if call exists in current set
if (!currentCall) new Error('Transmitter number does not exist in set.');
await updateCall(currentCall, false, timestamp);
// player who is called and initiasted the rejection.
emitNet(CallEvents.WAS_REJECTED, currentCall.receiverSource);
// player who is calling and recieved the rejection.
emitNet(CallEvents.WAS_REJECTED, currentCall.transmitterSource);
calls.delete(transmitterNumber);
} catch (e) {
callLogger.error(`Phone Call Rejected Event Error ${e.message}`, {
source: pSource,
});
}
});
onNet(CallEvents.END_CALL, async (transmitterNumber: string) => {
const pSource = getSource();
try {
const currentTimeUnix = new Date().getTime() / 1000;
const currentCall = calls.get(transmitterNumber);
// Sanity check if call exists in current set
if (!currentCall) new Error('Transmitter number does not exist in set.');
await updateCall(currentCall, false, currentTimeUnix);
// player who is being called
emitNet(CallEvents.WAS_ENDED, currentCall.receiverSource);
// player who is calling
emitNet(CallEvents.WAS_ENDED, currentCall.transmitterSource);
// ends animations if call is active
if (currentCall.accepted) {
emitNet(CallEvents.SEND_HANGUP_ANIM, currentCall.receiverSource);
emitNet(CallEvents.SEND_HANGUP_ANIM, currentCall.transmitterSource);
}
calls.delete(transmitterNumber);
} catch (e) {
callLogger.error(`Error ending Phone Call, ${e.message}`, {
source: pSource,
});
}
});
onNet(CallEvents.FETCH_CALLS, async () => {
const _source = getSource();
try {
const phoneNumber = getPlayer(_source).getPhoneNumber();
const calls = await fetchCalls(phoneNumber);
emitNet(CallEvents.SEND_HISTORY, _source, calls);
} catch (e) {
callLogger.error(`Failed to fetch calls, ${e.message}`, {
source: _source,
});
}
});
@@ -0,0 +1,4 @@
export enum FetchDefaultLimits {
NOTES_FETCH_LIMIT = 20,
CALLS_FETCH_LIMIT = 50,
}