PHP Code:
// Includes
#include <amxmodx>
#include <cstrike>
#include <amxmisc>
#include <sqlx>
// Pragma
#pragma semicolon 1
// Plugin info
new const PLUGIN[] = "MT Pts";
new const VERSION[] = "1.0";
new const AUTHOR[] = "Cooler";
// MySQL Init ---------------------------------
new Host[] = "";
new User[] = "";
new Pass[] = "";
new Db[] = "";
new TABLE[] = "";
// ---------------------------------------------------
// Bool
new g_Points[33];
new Handle:g_SqlConnection;
new Handle:g_SqlTuple;
new g_Error[512];
new g_sql_ready = false;
new bool:g_loaded[33];
new maxplayers;
public plugin_init()
{
// Register the plugin
register_plugin(PLUGIN, VERSION, AUTHOR);
maxplayers = get_maxplayers();
set_task(0.1, "MySql_Init");
}
public MySql_Init()
{
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db);
new ErrorCode;
g_SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error));
if(g_SqlConnection == Empty_Handle)
{
set_fail_state(g_Error);
}
new Handle:Queries;
Queries = SQL_PrepareQuery(g_SqlConnection,"CREATE TABLE IF NOT EXISTS %s (name varchar(64), points INT(11))", TABLE);
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error));
set_fail_state(g_Error);
}
SQL_FreeHandle(Queries);
g_sql_ready = true;
for(new i=1; i<=maxplayers; i++)
{
Load_MySql(i);
}
}
public Load_MySql(id)
{
if(g_sql_ready)
{
if(g_SqlTuple == Empty_Handle)
{
set_fail_state(g_Error);
}
new szName[32], szTemp[512];
get_user_name(id, szName, charsmax(szName));
new Data[1];
Data[0] = id;
format(szTemp,charsmax(szTemp),"SELECT * FROM `%s` WHERE (`%s`.`name` = '%s')", TABLE, TABLE, szName);
SQL_ThreadQuery(g_SqlTuple,"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;
id = Data[0];
if(SQL_NumResults(Query) < 1)
{
//.if there are no results found
new szName[32], szQuotedName[64];
get_user_name(id, szName, 31); // get user's name
SQL_QuoteString(g_SqlConnection, szQuotedName, 63, szName);
new szTemp[512];
// now we will insert the values into our table.
format(szTemp,charsmax(szTemp),"INSERT INTO `%s` ( `name` , `points`) VALUES ('%s','0');", TABLE, szQuotedName);
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp);
}
else
{
// if there are results found
g_Points[id] = SQL_ReadResult(Query, 2);
}
g_loaded[id] = true;
return PLUGIN_HANDLED;
}
public Save_MySql(id)
{
if(g_loaded[id])
{
new szTemp[512], szName[32], szQuotedName[64];
get_user_name(id, szName, 31);
SQL_QuoteString(g_SqlConnection, szQuotedName, 63, szName);
// Here we will update the user hes information in the database where the steamid matches.
format(szTemp,charsmax(szTemp),"UPDATE `%s` SET `name` = '%s', `points` = '%i'",TABLE, szQuotedName,g_Points[id]);
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp);
}
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
SQL_FreeHandle(Query);
return PLUGIN_HANDLED;
}
// When client enter on server
public client_putinserver(id)
{
if (!is_user_bot(id))
{
// Set the custom HUD display task
set_task(1.0, "ShowHud", id+TASK_SHOWHUD, _, _, "b");
}
Load_MySql(id);
}
// When client disconnect
public client_disconnect(id)
{
Save_MySql(id);
remove_task(id+TASK_SHOWHUD);
}
public plugin_end()
{
if(g_SqlConnection != Empty_Handle)
{
SQL_FreeHandle(g_SqlConnection);
}
}
Its not even creating a database... and it doesnt save my points, what shall I do ?