Files
new-phone-who-dis/resources/client/cl_main.ts
T
J Francisco Rader 1a09fa57ae Fix: Workaround for ESX errors pertaining to player loading (#153)
* Set phone ready before sending any init events + Show initializing phone message if not ready yet

* Send phone ready to nui
2021-03-06 19:25:32 -05:00

204 lines
4.7 KiB
TypeScript

import config from '../utils/config';
import events from '../utils/events';
import { phoneCloseAnim, phoneOpenAnim, removePhoneProp } from './functions';
let isPhoneOpen = false;
let isPhoneReady = false;
/* * * * * * * * * * * * *
*
* Phone initialize data
*
* * * * * * * * * * * * */
function fetchOnInitialize() {
isPhoneReady = true;
emitNet(events.CONTACTS_GET_CONTACTS);
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE);
SendNuiMessage(
JSON.stringify({
app: 'PHONE',
method: 'setPhoneReady',
data: true,
}),
);
}
RegisterKeyMapping('phone', 'Open Phone', 'keyboard', 'f1');
/* * * * * * * * * * * * *
*
* Phone Visibility Handling
*
* * * * * * * * * * * * */
const showPhone = async (): Promise<void> => {
isPhoneOpen = true;
await phoneOpenAnim(); // Animation starts before the phone is open
emitNet('phone:getCredentials');
SetCursorLocation(0.9, 0.922); //Experimental
SendNuiMessage(
JSON.stringify({
app: 'PHONE',
method: 'setVisibility',
data: true,
}),
);
SetNuiFocus(true, true);
};
const hidePhone = async (): Promise<void> => {
isPhoneOpen = false;
SendNuiMessage(
//Hides phone
JSON.stringify({
app: 'PHONE',
method: 'setVisibility',
data: false,
}),
);
await phoneCloseAnim();
SetNuiFocus(false, false);
};
/* * * * * * * * * * * * *
*
* Register Command and Keybinding
*
* * * * * * * * * * * * */
RegisterCommand(
'phone',
async () => {
//-- Toggles Phone
await Phone();
},
false,
);
RegisterCommand(
'phone:restart',
async () => {
await hidePhone();
SendNuiMessage(
JSON.stringify({
app: 'PHONE',
method: 'startRestart',
data: {},
}),
);
},
false,
);
/* * * * * * * * * * * * *
*
* Misc. Helper Functions
*
* * * * * * * * * * * * */
async function Phone(): Promise<void> {
fetchOnInitialize();
if (config.PhoneAsItem) {
// TODO: Do promise callback here
// const hasPhoneItem = await emitNetPromise('phone:hasPhoneItem')
// if (!hasPhoneItem) return
}
if (isPhoneOpen) {
await hidePhone();
} else {
await showPhone();
}
}
onNet('esx:playerLoaded', fetchOnInitialize);
AddEventHandler('onResourceStop', function (resource: string) {
if (resource === GetCurrentResourceName()) {
SendNuiMessage(
JSON.stringify({
app: 'PHONE',
method: 'setVisibility',
data: false,
}),
);
SetNuiFocus(false, false);
removePhoneProp(); //Deletes the phone incase it was attached.
ClearPedTasks(PlayerPedId()); //Leave here until launch as it'll fix any stuck animations.
}
});
onNet('phone:sendCredentials', (number: string) => {
SendNuiMessage(
JSON.stringify({
app: 'SIMCARD',
method: 'setNumber',
data: number,
}),
);
});
// DO NOT CHANGE THIS EITHER, PLEASE - CHIP
// ^ AND WHAT ARE YOU GOING TO DO HUH? - KIDZ
/* * * * * * * * * * * * *
*
* NUI Service Callback Registration
*
* * * * * * * * * * * * */
RegisterNuiCallbackType(events.OPEN_APP_LISTINGS);
on(`__cfx_nui:${events.OPEN_APP_LISTINGS}`, (data: any, cb: Function) => {
emitNet(events.SELLOUT_FETCH_LISTING);
cb();
});
RegisterNuiCallbackType(events.OPEN_APP_NOTES);
on(`__cfx_nui:${events.OPEN_APP_NOTES}`, (data: any, cb: Function) => {
emitNet(events.NOTE_FETCH_ALL_NOTES);
cb();
});
RegisterNuiCallbackType(events.OPEN_APP_BANK);
on(`__cfx_nui:${events.OPEN_APP_BANK}`, (data: any, cb: Function) => {
emitNet(events.BANK_FETCH_TRANSACTIONS);
emitNet(events.BANK_GET_CREDENTIALS);
cb();
});
RegisterNuiCallbackType(events.OPEN_APP_CAMERA);
on(`__cfx_nui:${events.OPEN_APP_CAMERA}`, (data: any, cb: Function) => {
emitNet(events.CAMERA_FETCH_PHOTOS);
cb();
});
RegisterNuiCallbackType(events.OPEN_APP_DAILER);
on(`__cfx_nui:${events.OPEN_APP_DAILER}`, (data: any, cb: Function) => {
emitNet(events.PHONE_CALL_FETCH_CALLS);
cb();
});
RegisterNuiCallbackType('phone:close');
on(`__cfx_nui:phone:close`, async (data: any, cb: Function) => {
await hidePhone();
cb();
}); // Called for when the phone is closed via the UI.
// setTick(async () => {
// while (config.SwimDestroy) {
// await Delay(config.RunRate * 1000);
// if (IsPedSwimming(PlayerPedId())) {
// let chance = Math.floor(Math.random() * 100 + 1);
// if (chance <= config.DestoryChance) {
// countPhone((countPhone: boolean) => {
// if (countPhone) {
// ESX.ShowNotification("Your phone is ruined from the water!");
// destroyedPhone = true;
// }
// });
// }
// if (destroyedPhone) {
// await Delay(config.DestroyPhoneReCheck * 60000);
// }
// }
// }
// });