Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions apps/game/client/cl_contacts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
import { ContactEvents } from '@typings/contact';
import { RegisterNuiProxy } from './cl_utils';

const QBCore = global.exports['qb-core'].GetCoreObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is supposed to be as standalone as possible, we cannot use any third party library. We have these exports for external use only.

const npwdExports = global.exports['npwd'];

type Vector = {
x: number;
y: number;
z: number;
};

RegisterNuiProxy(ContactEvents.PAY_CONTACT);
RegisterNuiProxy(ContactEvents.GET_CONTACTS);
RegisterNuiProxy(ContactEvents.ADD_CONTACT);
RegisterNuiProxy(ContactEvents.DELETE_CONTACT);
RegisterNuiProxy(ContactEvents.UPDATE_CONTACT);

const getVector = (coords: number[]) => {
const [x, y, z] = coords;
return {
x,
y,
z,
};
}

const getDistance = (v1: Vector, v2: Vector) => {
const dx = v1.x - v2.x;
const dy = v1.y - v2.y;
const dz = v1.z - v2.z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}

const GetClosestPlayer = () => {
const closestPlayers = QBCore.Functions.GetPlayersFromCoords()
let closestDistance = -1
let closestPlayer = -1
const coords = GetEntityCoords(PlayerPedId(), true)

for (let i = 0; i < closestPlayers.length; i++) {
if (closestPlayers[i] !== PlayerId()) {
const pos = GetEntityCoords(GetPlayerPed(closestPlayers[i]), true)
const distance = getDistance(getVector(pos), getVector(coords))

if (closestDistance === -1 || closestDistance > distance) {
closestPlayer = closestPlayers[i]
closestDistance = distance
}
}
}

return [closestPlayer, closestDistance]
}

on(ContactEvents.GIVE_CONTACT_DETAILS, () => {
const [player, distance] = GetClosestPlayer()

if (player !== -1 && distance < 2.5) {
const PlayerId = GetPlayerServerId(player)
emitNet(ContactEvents.GIVE_CONTACT_DETAILS, PlayerId)
} else {
QBCore.Functions.Notify("No one nearby!", "error")
}
});

// Opens the phone on the new contacts page with the number and name prefilled out
onNet(ContactEvents.RECEIVE_CONTACT_DETAILS, (sendingPlayerNumber: string, sendingPlayerName: string) => {
npwdExports.setPhoneVisible(true);

npwdExports.fillNewContact({ name: sendingPlayerName, number: sendingPlayerNumber })
});
10 changes: 10 additions & 0 deletions apps/game/server/contacts/contacts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Contact, ContactDeleteDTO, ContactEvents, PreDBContact, ContactPay } fr
import PlayerService from '../players/player.service';
import ContactService from './contacts.service';
import { contactsLogger } from './contacts.utils';
import { getSource } from '../utils/miscUtils';
import { onNetPromise } from '../lib/PromiseNetEvents/onNetPromise';
const exps = global.exports;

Expand Down Expand Up @@ -60,3 +61,12 @@ onNetPromise<ContactDeleteDTO, void>(ContactEvents.DELETE_CONTACT, (reqObj, resp
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
});
});

onNet(ContactEvents.GIVE_CONTACT_DETAILS, (targetPlayerId: number) => {
const src = getSource();
const sendingPlayer = PlayerService.getPlayer(src);
const sendingPlayerNumber = sendingPlayer.getPhoneNumber();
const sendingPlayerName = sendingPlayer.getName();

emitNet(ContactEvents.RECEIVE_CONTACT_DETAILS, targetPlayerId, sendingPlayerNumber, sendingPlayerName);
})
2 changes: 2 additions & 0 deletions typings/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export enum ContactEvents {
DELETE_CONTACT = 'npwd:deleteContact',
// Used to fill in information through an export event
ADD_CONTACT_EXPORT = 'npwd:addContactExport',
GIVE_CONTACT_DETAILS = 'npwd:GiveContactDetails', // Make sure to update your radial menu to use this event
RECEIVE_CONTACT_DETAILS = 'npwd:ReceiveContactDetails',
}

export interface AddContactExportData {
Expand Down