#include <a_samp>
#include <a_mysql>
#include <sscanf2>
#include <zcmd>
#define function%0(%1) forward %0(%1); public %0(%1)
#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_DATABASE "samptestes"
#define MYSQL_PASSWORD ""
#define Dialog_Login 9998
#define Dialog_Register 9999
new MySQL:DBConn;
enum TPlayerData
{
IDp,
PlayerLevel,
PlayerMoney,
PlayerScore,
PlayerCash,
PlayerName[MAX_PLAYER_NAME],
PlayerPass[24],
pAttempsLogin,
pAttempsRegister,
bool:pLogged,
}
new APlayerData[MAX_PLAYERS][TPlayerData];
main()
{
print("\n|======================================|");
print("|Registro e Login em MySQL para testes.|");
print("|======================================|\n");
return 1;
}
public OnGameModeInit()
{
SetGameModeText("GMTESTS");
DataBaseInit();
return 1;
}
public OnGameModeExit()
{
mysql_close(DBConn);
return 1;
}
public OnPlayerRequestClass(playerid, classid)
{
SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
return 1;
}
public OnPlayerConnect(playerid)
{
new Query[80];
TogglePlayerSpectating(playerid, 1);
mysql_format(DBConn, Query, sizeof(Query), "SELECT `password`, `id` FROM `Players` WHERE `name`='%e'", GetPlayerNameEx(playerid));
mysql_tquery(DBConn, Query, "CheckAccount", "i", playerid);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
if(APlayerData[playerid][pLogged] == true && reason >= 0)
{
SaveAccount(playerid);
}
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
new Query[125];
switch(dialogid)
{
case Dialog_Register:
{
if(!response)
return Kick(playerid);
if(strlen(inputtext) < 4 || strlen(inputtext) > 24)
{
SendClientMessage(playerid, 0xFF0000AA, "[SERVER] Escolha uma senha entre 4 a 24 caracteres.");
TogglePlayerSpectating(playerid, 1);
ShowPlayerDialog(playerid, Dialog_Register, DIALOG_STYLE_INPUT, "Registro", "Digite uma senha para se registrar em nosso servidor", "Registrar", "Sair");
APlayerData[playerid][pAttempsRegister]++;
} else {
TogglePlayerSpectating(playerid, 0);
mysql_format(DBConn, Query, sizeof(Query), "INSERT INTO `Players`(`name`,`password`) VALUES ('%e', '%e')", GetPlayerNameEx(playerid), inputtext);
mysql_tquery(DBConn, Query, "EnterData", "i", playerid);
}
if(APlayerData[playerid][pAttempsRegister] == 3)
{
SendClientMessage(playerid, 0xFF0000AA, "[SERVER] Limite de tentativas de registro excedida.");
APlayerData[playerid][pAttempsRegister] = 0;
SetTimerEx("KickPlayerinTime", 200, false, "i", playerid);
}
}
case Dialog_Login:
{
if(!response)
return Kick(playerid);
if(!strcmp(APlayerData[playerid][PlayerPass], inputtext, true, 24))
{
TogglePlayerSpectating(playerid, 0);
mysql_format(DBConn, Query, sizeof(Query), "SELECT * FROM Players WHERE name='%e'", GetPlayerNameEx(playerid));
mysql_tquery(DBConn, Query, "LoadAccount", "i", playerid);
SendClientMessage(playerid, 0x80FF00AA, "[Server] Logado com sucesso.");
} else {
TogglePlayerSpectating(playerid, 1);
SendClientMessage(playerid, 0xFF0000AA, "[SERVER] Senha errada, tente novamente.");
APlayerData[playerid][pAttempsLogin]++;
ShowPlayerDialog(playerid, Dialog_Login, DIALOG_STYLE_PASSWORD, "Login", "Digite sua senha para entrar em nosso servidor.", "Confirmar", "Sair");
}
if(APlayerData[playerid][pAttempsLogin] == 3)
{
SendClientMessage(playerid, 0xFF0000AA, "[SERVER] Limite de tentativas de login excedida.");
APlayerData[playerid][pAttempsLogin] = 0;
SetTimerEx("KickPlayerinTime", 200, false, "i", playerid);
}
}
}
return 1;
}
function SaveAccount(playerid)
{
if(APlayerData[playerid][pLogged] == false)
return 0;
new Query[200];
APlayerData[playerid][PlayerMoney] = GetPlayerMoney(playerid);
APlayerData[playerid][PlayerScore] = GetPlayerScore(playerid);
mysql_format(DBConn, Query, sizeof(Query), "UPDATE Players SET \
name='%e',\
password='%e',\
admin='%d',\
score='%d',\
money='%d', \
cash='%d' WHERE id='%d'", APlayerData[playerid][PlayerName],
APlayerData[playerid][PlayerPass],
APlayerData[playerid][PlayerLevel],
APlayerData[playerid][PlayerScore],
APlayerData[playerid][PlayerMoney],
APlayerData[playerid][PlayerCash],
APlayerData[playerid][IDp]);
mysql_query(DBConn, Query);
printf("[MYSQL] Dados do Jogador %s ID %d salvo com sucesso", GetPlayerNameEx(playerid), APlayerData[playerid][IDp]);
return 1;
}
function LoadAccount(playerid)
{
APlayerData[playerid][pLogged] = true;
cache_get_value_int(0, "id", APlayerData[playerid][IDp]);
cache_get_value_name(0, "name", APlayerData[playerid][PlayerName]);
cache_get_value_name(0, "password", APlayerData[playerid][PlayerPass]);
cache_get_value_int(0, "admin", APlayerData[playerid][PlayerLevel]);
cache_get_value_int(0, "score", APlayerData[playerid][PlayerScore]);
cache_get_value_int(0, "money", APlayerData[playerid][PlayerMoney]);
cache_get_value_int(0, "cash", APlayerData[playerid][PlayerCash]);
SetPlayerScore(playerid, APlayerData[playerid][PlayerScore]);
GivePlayerMoney(playerid, APlayerData[playerid][PlayerMoney]);
SetSpawnInfo(playerid, 0, 122, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
SpawnPlayer(playerid);
return 1;
}
function EnterData(playerid)
{
new Query[50];
APlayerData[playerid][IDp] = cache_insert_id();
printf("[MYSQL] Jogador %s registrado como ID %d", GetPlayerNameEx(playerid), APlayerData[playerid][IDp]);
mysql_format(DBConn, Query, sizeof(Query), "SELECT * FROM Players WHERE id='%i'", APlayerData[playerid][IDp]);
mysql_query(DBConn,Query);
LoadAccount(playerid);
return 1;
}
function CheckAccount(playerid)
{
if(cache_num_rows() > 0)
{
cache_get_value_name(0, "password", APlayerData[playerid][PlayerPass], 24);
ShowPlayerDialog(playerid, Dialog_Login, DIALOG_STYLE_PASSWORD, "Login", "Digite sua senha para entrar em nosso servidor.", "Confirmar", "Sair");
}else{
ShowPlayerDialog(playerid, Dialog_Register, DIALOG_STYLE_INPUT, "Registro", "Digite uma senha para se registrar em nosso servidor", "Registrar", "Sair");
}
return 1;
}
function DataBaseInit()
{
DBConn = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
if(mysql_errno() == 0)
{
printf("[MySQL] Database '%s' conectada com sucesso!", MYSQL_DATABASE);
print("[MySQL] Verificando tabelas...");
mysql_query(DBConn, "CREATE TABLE IF NOT EXISTS Players (\
id int NOT NULL AUTO_INCREMENT,\
name varchar(25) NOT NULL,\
password varchar(255) NOT NULL,\
admin int DEFAULT 0,\
score int DEFAULT 500,\
money int DEFAULT 1000,\
cash int DEFAULT 0,\
PRIMARY KEY(id));", false);
print("[MySQL] Tabela 'Players' verificada com sucesso!");
}
else
{
printf("[MySQL] ERRO: Não foi possível se conectar a database '%s'!", MYSQL_DATABASE);
SendRconCommand("exit");
}
return 1;
}
function KickPlayerinTime(playerid) return Kick(playerid);
stock GetPlayerNameEx(playerid)
{
new PlayerName2[MAX_PLAYER_NAME];
GetPlayerName(playerid, PlayerName2, sizeof(PlayerName2));
return PlayerName2;
}