#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#pragma tabsize 0
#define DB_NONE 0
#define DB_CONNECTING 1
#define DB_READY 2
#define TASK_BANK 652512
new Handle:Conexiune = Empty_Handle
new Handle:SqlConnection
new g_Error[512], g_db_status
new Host[64], User[64], Pass[64], Db[64];
new V1[33],V2[33];
public plugin_init()
{
new configsDi[64], configfile[128];
get_configsdir(configsDi, 63)
format(configfile, 127, "%s/sql.cfg", configsDi)
if(file_exists(configfile))
{
server_cmd("exec %s", configfile)
}
else log_amx("[AMXX] pointslevelmode.amxx : sql.cfg not found in the config folder.")
g_db_status = DB_NONE
set_task(0.1, "MySql_Init")
}
public MySql_Init()
{
if(g_db_status == DB_CONNECTING)
return
g_db_status = DB_CONNECTING
new ErrorCode
get_cvar_string("amx_sql_host", Host, 63)
get_cvar_string("amx_sql_user", User, 63)
get_cvar_string("amx_sql_pass", Pass, 63)
get_cvar_string("amx_sql_db", Db, 63)
Conexiune = SQL_MakeDbTuple(Host,User,Pass,Db)
SqlConnection = SQL_Connect(Conexiune,ErrorCode,g_Error,charsmax(g_Error))
if(SqlConnection == Empty_Handle)
set_fail_state(g_Error)
new Handle:Queries
Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS bank (user varchar(32),lvl INT(11),xpr INT(11))")
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
g_db_status = DB_NONE
}
else g_db_status = DB_READY
SQL_FreeHandle(Queries)
SQL_FreeHandle(SqlConnection)
}
public Load_MySql(id)
{
if(g_db_status != DB_READY)
return
new Data[1], szTemp[512]
Data[0] = id
new szName[32];
get_user_name(id, szName, charsmax(szName))
format(szTemp,charsmax(szTemp),"SELECT * FROM bank WHERE bank.user = ^"%s^";", szName)
SQL_ThreadQuery(Conexiune,"register_client",szTemp,Data,1)
}
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
{
log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
}
else if(FailState == TQUERY_QUERY_FAILED)
{
log_amx("Load Query failed. [%d] %s", Errcode, Error)
}
new id, szTemp[512]
id = Data[0]
if(SQL_NumResults(Query) < 1)
{
new szName[32]
get_user_name(id, szName, charsmax(szName))
format(szTemp,charsmax(szTemp),"INSERT INTO `bank` ( `user` , `lvl`, `xpr`)VALUES ('%s','1','0');",szName)
SQL_ThreadQuery(Conexiune,"IgnoreHandle",szTemp)
}
else
{
V1[id] = SQL_ReadResult(Query, 1);
V2[id] = SQL_ReadResult(Query, 2);
}
return PLUGIN_HANDLED
}
public Savebank(id)
{
id -= TASK_BANK
if(!is_user_connected(id))
return
if(g_db_status != DB_READY)
return
new szTemp[512]
new szNume[32]
get_user_name(id, szNume, charsmax(szNume))
if(V1[id]>0 || V2[id]>0)
{
format(szTemp,charsmax(szTemp),"UPDATE `bank` SET `lvl` = '%i', `xpr` = '%i' WHERE `bank`.`user` = '%s';",V1[id],V2[id],szNume)
SQL_ThreadQuery(Conexiune,"IgnoreHandle",szTemp)
}else{
format(szTemp,charsmax(szTemp),"UPDATE `bank` SET `lvl` = '1', `xpr` = '0' WHERE `bank`.`user` = '%s';",szNume)
SQL_ThreadQuery(Conexiune,"IgnoreHandle",szTemp)
}
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}
public Save_MySql(id)
{
if(!is_user_connected(id))
return
if(g_db_status != DB_READY)
return
new szTemp[512]
new szNume[32]
get_user_name(id, szNume, charsmax(szNume))
if(V1[id]>0 || V2[id]>0)
{
format(szTemp,charsmax(szTemp),"UPDATE `bank` SET `lvl` = '%i', `xpr` = '%i' WHERE `bank`.`user` = '%s';",V1[id],V2[id],szNume)
SQL_ThreadQuery(Conexiune,"IgnoreHandle",szTemp)
}else{
format(szTemp,charsmax(szTemp),"UPDATE `bank` SET `lvl` = '1', `xpr` = '0' WHERE `bank`.`user` = '%s';",szNume)
SQL_ThreadQuery(Conexiune,"IgnoreHandle",szTemp)
}
}
public plugin_end()
{
if(g_db_status == DB_READY)
SQL_FreeHandle(Conexiune)
}
public client_disconnect(id) {
if(Conexiune != Empty_Handle)
Save_MySql(id);
remove_task(id+TASK_BANK)
}
public client_putinserver(id)
{
V1[id] = 0;/*variabila ta ,te sfatuiesc sa o pui pe 0 ca sa nu se creeze bug am patit-o o sa o descarce dp din baza*/
V2[id] = 0;//idem ca sus
Load_MySql(id)
set_task(60.0, "Savebank", id+TASK_BANK, _, _, "b")
return PLUGIN_CONTINUE;
}