Vocк precisarб da include do BlueG(a_mysql) e da include a_samp.
pawn Код:
// - Includes
#include <a_samp>
#include <a_mysql>
// - Variaveis
new connect;
new Query[512];
// - Enum
enum aInfo
{
bool:Logged,
Killed,
Died,
Connections,
Money,
Bank
}
new AccountInfo[MAX_PLAYERS][aInfo];
#define SQL_Host "" // Change to your host
#define SQL_User "" // Change to your use
#define SQL_DataBase "" // Change to your database
#define SQL_Password "" // Change to your password
#define TableName "Accounts" // in my case will be 'Accounts' / no meu caso serб 'Accounts'
#define DIALOG_REGISTER 0
#define DIALOG_LOGIN 1
// - Forward's
forward MySQL_CheckAccount(playerid);
forward R_@MySQL_CheckAccount(playerid);
forward MySQL_CheckAccountPass(playerid, password[]);
forward R_@MySQL_CheckAccountPass(playerid);
forward MySQL_LoadAccount(playerid);
forward R_@MySQL_LoadAccount(playerid);
forward MySQL_SaveAccount(playerid);
// - CallBacks
public OnFilterScriptInit()
{
connect = mysql_connect(SQL_Host, SQL_User, SQL_DataBase, SQL_Password);
static x;
x = -1;
for(; ++x ^ 3;)
{
if(!connect)
printf("MySQL connection attempt %d failed!", x);
else break;
}
mysql_debug(1);
return 1;
}
public OnFilterScriptExit()
return mysql_debug(0), mysql_close(connect);
public OnPlayerRequestSpawn(playerid)
return (AccountInfo[playerid][Logged] ? 1 : 0);
public OnPlayerConnect(playerid)
{
AccountInfo[playerid][Logged] = false;
AccountInfo[playerid][Killed] = 0;
AccountInfo[playerid][Died] = 0;
AccountInfo[playerid][Connections] = 0;
AccountInfo[playerid][Money] = 0;
AccountInfo[playerid][Bank] = 0;
return MySQL_CheckAccount(playerid);
}
public OnPlayerDisconnect(playerid, reason)
return MySQL_SaveAccount(playerid);
public MySQL_CheckAccount(playerid)
{
format(Query,sizeof(Query),"SELECT * FROM `%s` WHERE Nick='%s'", TableName, _@PlayerName(playerid));
mysql_function_query(connect, Query, true, "R_@MySQL_CheckAccount", "d", playerid);
return 1;
}
public R_@MySQL_CheckAccount(playerid)
{
static rows,fields;
cache_get_data(rows, fields, connect);
if(rows)
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Olб vejo que nгo й novo por aqui,\ndigite sua senha para continuar:", "Logar", "Sair");
else
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Registro", "Olб vejo que й novo por aqui,\ndigite uma senha para continuar", "Registrar", "Sair");
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case 0:
{
if(response)
{
if(!strlen(inputtext))
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registro", "Vocк ainda nгo й registrado, \nPor favor digite uma senha:", "Logar", "Sair");
}
format(Query, sizeof(Query), "INSERT INTO `Contas` VALUES ('%s','%s','%d','%d','0','1500','2000');", _@PlayerName(playerid), inputtext, AccountInfo[playerid][Killed], AccountInfo[playerid][Died]);
mysql_function_query(connect, Query, false, "", "", "");
SendClientMessage(playerid, -1, "Registrado com sucesso!");
SetPlayerHealth(playerid, 100);
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Parabйns, conta criada com sucesso! \nAgora digite sua senha para continuar:", "Logar", "Sair");
}
else
Kick(playerid);
return 1;
}
case 1:
{
if(response)
{
if(!strlen(inputtext))
{
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Vocк ja й registrado, \nPor favor digite sua senha:", "Logar", "Sair");
SendClientMessage(playerid,0xFF0000AA,"Senha incorreta digite-a novamente");
}
MySQL_CheckAccountPass(playerid, inputtext);
}
else
Kick(playerid);
return 1;
}
}
return 1;
}
public MySQL_CheckAccountPass(playerid, password[])
{
format(Query,sizeof(Query),"SELECT * FROM `%s` WHERE Nick='%s' and Pass='%s'", TableName, _@PlayerName(playerid), password);
mysql_function_query(connect, Query, true, "R_@MySQL_CheckAccountPass", "d", playerid);
return 1;
}
public R_@MySQL_CheckAccountPass(playerid)
{
static rows,fields;
cache_get_data(rows, fields, connect);
if(rows)
{
MySQL_LoadAccount(playerid);
AccountInfo[playerid][Connections]++;
MySQL_SaveAccount(playerid);
SendClientMessage(playerid, -1, "Seja bem vindo novamente ao nosso servidor!");
}
else
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Senha errada digite a sua senha novamente:", "Logar", "Sair");
return 1;
}
public MySQL_LoadAccount(playerid)
{
format(Query,sizeof(Query),"SELECT * FROM `%s` WHERE Nick='%s'", TableName, _@PlayerName(playerid));
mysql_function_query(connect,Query,true,"R_@MySQL_LoadAccount", "d", playerid);
return 1;
}
public R_@MySQL_LoadAccount(playerid)
{
static rows,fields;
cache_get_data(rows,fields,connect);
if(rows)
{
cache_get_field_content(0,"Money",Query,connect);
AccountInfo[playerid][Money] = strval(Query);
cache_get_field_content(0,"Bank",Query,connect);
AccountInfo[playerid][Bank] = strval(Query);
cache_get_field_content(0,"Killed",Query,connect);
AccountInfo[playerid][Killed] = strval(Query);
cache_get_field_content(0,"Died",Query,connect);
AccountInfo[playerid][Died] = strval(Query);
}
return 1;
}
public MySQL_SaveAccount(playerid)
{
format(Query,sizeof(Query),"UPDATE `%s` SET Money='%d', Bank='%d', Killed='%d', Died='%d', Connections='%d' WHERE Nick='%s'", AccountInfo[playerid][Money], AccountInfo[playerid][Bank], AccountInfo[playerid][Killed], AccountInfo[playerid][Died], _@PlayerName(playerid));
mysql_function_query(connect,Query,false,"","","");
return 1;
}
// - Stocks
_@PlayerName(playerid)
{
new _@pN[24];
GetPlayerName(playerid, _@pN, sizeof(_@pN));
return _@pN;
}