let spawnPoints = require('./json-packet/spawn_points.json').SpawnPoints;
mp.events.add('playerDeath', (player) => {
player.spawn(spawnPoints[Math.floor(Math.random() * spawnPoints.length)]);
player.health = 100;
});
mp.events.add('playerJoin', (player) => {
player.call('showLoginDialog');
});
mp.events.add("playerQuit", (player) => {
});
const mysql = require('mysql');
var DB = mysql.createConnection(
{
host: 'localhost',
user: 'root',
password: '',
database: 'main_server'
});
mp.events.add('packagesLoaded', () => {
DB.connect(function(err){
if(err) {
console.error(`mysql connected failed.`);
return;
}
console.log('mysql connected successfully.');
});
});
mp.events.add('onRegisterAttempt', (player, data) => {
data = JSON.parse(data);
DB.query('SELECT id FROM `accounts` WHERE `social_id` = ?', [player.rgscId], function (error, results) { // Проверяем уникальность логина
if(results.length > 0) return player.call('showAuthError', ['Аккаунт с таким SocialID уже существует']); // Если такой логин уже есть, то возвращаем ошибку
DB.query('INSERT INTO `accounts`(`social_id`, `login`, `password`) VALUES (?,?,?)', [player.rgscId, data.login, data.password], function (error, results) { // Добавляем аккаунт в базу данных
});
DB.query('SELECT `admin_lvl`, `cash` FROM `accounts` WHERE `social_id` = ?', [player.rgscId], function (error, results)
{
if(!results.length)
{
player.kick(player);
return;
}
player.admin = results[0].admin_lvl;
player.cash = results[0].cash;
player.call('hideLoginDialog');
player.call('showPlayerHud', [player.cash, player.rgscId, player.id]);
});
});
});
mp.events.add('onLoginAttempt', (player, data) => {
data = JSON.parse(data);
DB.query('SELECT * FROM accounts WHERE login = ? LIMIT 1', [data.login], function (error, results) {
if(!results.length) return player.call('showAuthError', ['Введите правильный логин или пароль.']);
if (results[0].password === data.password)
{
player.admin = results[0].admin_lvl;
player.cash = results[0].cash;
player.call('hideLoginDialog');
player.call('showPlayerHud', [player.cash, player.rgscId, player.id]);
return;
}
else player.call('showAuthError', ['Неверный Логин или Пароль']);
});
});