24.02.2018, 02:11
(
Последний раз редактировалось GuiKommander; 24.02.2018 в 14:37.
)
PHP код:
#include <a_samp>
#include <a_mysql>
main(){}
#define mysql_host "localhost"
#define mysql_username "root"
#define mysql_database "samp"
#define mysql_password ""
#define REGISTRO 0
#define LOGIN 1
enum PlayerInfo
{
Nome[MAX_PLAYER_NAME],
Senha[40],
Dinheiro,
Level,
Admin
}
new pInfo[MAX_PLAYERS][PlayerInfo];
new MySQL:Conexao;
public OnGameModeInit()
{
new Query[200];
Conexao = mysql_connect(mysql_host, mysql_username, mysql_password, mysql_database);
if(!Conexao) return print("Nгo foi possнvel conectar ao banco de dados.");
else print("Conectado com sucesso ao banco de dados.");
format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS `contas`(`Nome` VARCHAR(20) NOT NULL, `Senha` VARCHAR(20) NOT NULL, `Dinheiro` INT NOT NULL DEFAULT 500, `Level` INT NOT NULL DEFAULT 1, `Admin` INT NOT NULL DEFAULT 0)");
mysql_query(Conexao, Query);
return 1;
}
public OnPlayerConnect(playerid)
{
new Query[200], row = cache_get_row_count(row);
GetPlayerName(playerid, pInfo[playerid][Nome], 20);
format(Query, sizeof(Query), "SELECT * FROM `contas` WHERE `Nome`='%s'", pInfo[playerid][Nome]);
//format(Query, sizeof(Query), "SELECT `Senha` FROM `contas` WHERE `Nome`='%s'", pInfo[playerid][Nome]);
mysql_query(Conexao, Query);
cache_get_value_name(0, "Senha", pInfo[playerid][Senha], 30);
if(row > 0)
{
ShowPlayerDialog(playerid, LOGIN, DIALOG_STYLE_INPUT, "Login", "Digite sua senha para logar", "Login", "Sair");
}
else
{
ShowPlayerDialog(playerid, REGISTRO, DIALOG_STYLE_INPUT, "Registro", "Digite uma senha para registrar", "Registrar", "Sair");
}
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
SalvarConta(playerid);
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(dialogid == REGISTRO)
{
if(strlen(inputtext) < 4 || strlen(inputtext) > 20)
{
ShowPlayerDialog(playerid, REGISTRO, DIALOG_STYLE_INPUT, "Registro", "Digite uma senha de 4 a 20 caracteres para se registrar", "Registrar", "Sair");
}
else
{
new Query[200];
GetPlayerName(playerid, pInfo[playerid][Nome], 20);
format(Query, sizeof(Query), "INSERT INTO * FROM `contas` (`Nome`, `Senha`, `Dinheiro`, `Level`, `Admin`) VALUES ('%s', '%s', '%d', '%d', '%d')", pInfo[playerid][Nome], inputtext, pInfo[playerid][Dinheiro], pInfo[playerid][Level], pInfo[playerid][Admin]);
mysql_query(Conexao, Query);
SpawnPlayer(playerid);
cache_get_value_name_int(0, "`Dinheiro`", pInfo[playerid][Dinheiro]);
cache_get_value_name_int(0, "`Level`", pInfo[playerid][Level]);
cache_get_value_name_int(0, "`Admin`", pInfo[playerid][Admin]);
pInfo[playerid][Dinheiro] = GivePlayerMoney(playerid, 500);
pInfo[playerid][Level] = SetPlayerScore(playerid, 1);
pInfo[playerid][Admin] = 0;
SendClientMessage(playerid, -1, "| INFO | Registrado com sucesso.");
}
}
if(dialogid == LOGIN)
{
if(!strcmp(pInfo[playerid][Senha], inputtext, true, 30))
{
new Query[200];
GetPlayerName(playerid, pInfo[playerid][Nome], 20);
SendClientMessage(playerid, -1, "| INFO | Logado com sucesso.");
format(Query, sizeof(Query), "SELECT * FROM `contas` WHERE `Nome`='%s'", pInfo[playerid][Nome]);
GivePlayerMoney(playerid, pInfo[playerid][Dinheiro]);
SetPlayerScore(playerid, pInfo[playerid][Level]);
pInfo[playerid][Admin] = 1;
SpawnPlayer(playerid);
}
else
{
ShowPlayerDialog(playerid, LOGIN, DIALOG_STYLE_INPUT, "Login", "Senha incorreta, digite a senha certa!", "Login", "Sair");
}
}
return 0;
}
static SalvarConta(playerid)
{
new Query[200];
GetPlayerName(playerid, pInfo[playerid][Nome], 20);
format(Query, sizeof(Query), "UPDATE `contas` SET `Nome`'%s', `Senha`'%s', `Dinheiro`'%d', `Level`'%d', `Admin`'%d'",
pInfo[playerid][Nome],
pInfo[playerid][Senha],
pInfo[playerid][Dinheiro] = GetPlayerMoney(playerid),
pInfo[playerid][Level] = GetPlayerScore(playerid),
pInfo[playerid][Admin] = pInfo[playerid][Admin]);
mysql_query(Conexao, Query);
}