Skip to content

Instantly share code, notes, and snippets.

@Stuyk
Created July 18, 2019 00:38
Show Gist options
  • Save Stuyk/51c15c803169c03a25b1be716846d46d to your computer and use it in GitHub Desktop.
Save Stuyk/51c15c803169c03a25b1be716846d46d to your computer and use it in GitHub Desktop.
import * as alt from 'alt';
import * as chat from 'chat';
import * as mongov from 'mongo-v';
console.log('=> Started Events');
const spawnLoc = {x: -1136.07, y: -3044.18, z: 14.1};
const loggedInPlayers = [];
alt.on('playerConnect', (player) => {
chat.setupPlayer(player); // Setup Extended Chat Resource
});
alt.on('playerDisconnect', (player) => {
console.log(player.name + ' has disconnected.');
// Don't worry about users who haven't logged in yet.
if (player.data === undefined)
return;
// Get the logged in player.
var index = loggedInPlayers.indexOf(player.data.username);
if (index === -1)
return;
// Remove the logged in player.
loggedInPlayers.splice(index, 1);
// Update their position.
alt.emit('updateDataByField', 'lastLocation', player.pos, player, 'accounts');
});
// Called when the player finishes logging in.
alt.on('completeLogin', (player) => {
if (loggedInPlayers.includes(player.data.username)) {
player.kick();
return;
}
player.sendMessage('{00FF00}Successfully Logged In.');
loggedInPlayers.push(player.data.username);
player.model = "a_f_y_hipster_01";
if (player.data.lastLocation === undefined) {
player.spawn(spawnLoc.x, spawnLoc.y, spawnLoc.z, 0);
} else {
player.spawn(player.data.lastLocation.x, player.data.lastLocation.y, player.data.lastLocation.z, 0);
}
});
// Used to update a single field for a player.
alt.on('updateDataByField', (fieldName, fieldValue, player, collectionName) => {
if (player.data === undefined)
throw new Error('Data does not exist for this player.');
player.data[fieldName] = fieldValue;
mongov.updateDocuments(JSON.stringify(player.data), collectionName, (res) => {
if (!res.success)
throw new Error(`Failed to update player data for ${player.data.username}`);
console.log(`Updated data for ${player.data.username}.`);
});
});
// Used to update an entire data set for a player.
alt.on('updateData', (player, collectionName) => {
if (player.data === undefined)
throw new Error('Data does not exist for this player.');
mongov.updateDocuments(JSON.stringify(player.data), collectionName, (res) => {
if (!res.success)
throw new Error(`Failed to update player data for ${player.data.username}`);
console.log(`Updated data for ${player.data.username}.`);
});
});
alt.on('dbConnect', (msg) => {
console.log(msg);
});
alt.on('dbDisconnect', (msg) => {
console.log(msg);
});
alt.on('dbError', (err) => {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment