Skip to content

Instantly share code, notes, and snippets.

@Stuyk
Created July 18, 2019 00:37
Show Gist options
  • Save Stuyk/0a482b0fe80f1fc34c2da7000d5c9dad to your computer and use it in GitHub Desktop.
Save Stuyk/0a482b0fe80f1fc34c2da7000d5c9dad 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 Commands');
// Example of data being created for reference
/*
var exampleData = {
username: '',
password: '',
lastLocation: {x: 0, y: 0, z: 0}
};
*/
chat.registerCmd('register', (player, arg) => {
if (player.data !== undefined) {
player.sendMessage('{FF0000}Cannot re-register after logging in.');
return;
}
// Check if there are enough arguments for this command.
if (arg.length <= 1 || arg.length >= 4) {
player.sendMessage('{FF0000}/register [username] [pwd] [pwd]');
return;
}
// Check username length and password length.
if (arg[1].length <= 5 || arg[2].length <= 5 || arg[0].length <= 5) {
player.sendMessage('{FF0000}User and Password must be at least 6 characters.');
return;
}
// Check if the two password provided match each other.
if (arg[1] !== arg[2]) {
player.sendMessage('{FF0000}Password mismatch.');
return;
}
// Get the documents by the fieldName of 'username' and the field value of arg[0];
mongov.getDocuments('username', arg[0], 'accounts', (res) => {
const documents = JSON.parse(res);
// There is an account already.
if (documents.length >= 1) {
player.sendMessage('{FF0000}That account is already taken.');
return;
}
// Register a new account.
const newPlayerData = {
username: arg[0],
password: arg[1]
};
// Add the account.
mongov.insertDocuments(JSON.stringify(newPlayerData), 'accounts');
player.sendMessage('{00FF00}Registered your new account. Use /login');
});
});
chat.registerCmd('login', (player, arg) => {
if (player.data !== undefined) {
player.sendMessage('{FF0000}Cannot re-login after logging in.');
return;
}
if (arg.length <= 1 || arg.length >= 3) {
player.sendMessage('{FF0000}/login [username] [pwd]');
return;
}
player.sendMessage('{00FF00}Attempting Login');
mongov.getDocuments('username', arg[0], 'accounts', (res) => {
const documents = JSON.parse(res);
// There is an account already.
if (documents.length <= 0) {
player.sendMessage('{FF0000}Account was not found.');
return;
}
// Password does not match.
if (documents[0].password !== arg[1]) {
player.sendMessage('{FF0000}Username or password mismatch.');
return;
}
// Finish login by assigning the data to the player.
// It can then be accessed by doing 'player.data.x'
// x being from the example at the top of this file.
player.data = documents[0];
alt.emit('completeLogin', player);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment