PHP Code:
#include <amxmodx>
#include <sqlx>
#include <cstrike>
#define PLUGIN "Player_stats"
#define VERSION "1.0"
#define AUTHOR ""
new Host[] = "hostname"
new User[] = "username"
new Pass[] = "password"
new Db[] = "database"
new Handle:g_SqlTuple
new g_Error[512]
new iSurvive[33];
new iHeadshot[33];
new iSuicide[33];
new iKills[33];
//pcvar's
new cSurvive;
new cHeadshot;
new cSuicide;
new cKills;
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("DeathMsg", "Event_DeathMsg", "a")
register_logevent( "eRound_end", 2, "1=Round_End" );
cSurvive = register_cvar("ps_survive", "1")
cHeadshot = register_cvar("ps_headshot", "2")
cSuicide = register_cvar("ps_suicide", "1")
cKills = register_cvar("ps_kills", "1")
set_task(1.0, "MySql_Init")
}
public MySql_Init()
{
g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,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 tutorial (steamid varchar(32),stats INT(11))")
if(!SQL_Execute(Queries))
{
SQL_QueryError(Queries,g_Error,charsmax(g_Error))
set_fail_state(g_Error)
}
SQL_FreeHandle(Queries)
SQL_FreeHandle(SqlConnection)
}
public Load_MySql(id)
{
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(g_SqlTuple == Empty_Handle)
set_fail_state(g_Error)
new szSteamId[32], szTemp[512]
get_user_authid(id, szSteamId, charsmax(szSteamId))
new Data[1]
Data[0] = id
//we will now select from the table `tutorial` where the steamid match
format(szTemp,charsmax(szTemp),"SELECT * FROM `tutorial` WHERE (`tutorial`.`steamid` = '%s')", szSteamId)
SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
SQL_FreeHandle(SqlConnection)
}
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 szSteamId[32]
get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
// if its still pending we can't do anything with it
if (equal(szSteamId,"ID_PENDING"))
return PLUGIN_HANDLED
new szTemp[512]
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(g_SqlTuple == Empty_Handle)
// stop the plugin with an error message
set_fail_state(g_Error)
// now we will insturt the values into our table.
format(szTemp,charsmax(szTemp),"INSERT INTO `tutorial` ( `steamid` , `exp`)VALUES ('%s','0');",szSteamId)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
// and of course, free the connection
SQL_FreeHandle(SqlConnection)
}
else
{
// if there are results found
iSurvive[id] = SQL_ReadResult(Query, 1)
}
return PLUGIN_HANDLED
}
public Save_MySql(id)
{
// ok, we're ready to connect
new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
if(g_SqlTuple == Empty_Handle)
// stop the plugin with an error message
set_fail_state(g_Error)
new szSteamId[32], szName[32], szTemp[512]
get_user_authid(id, szSteamId, charsmax(szSteamId))
get_user_name(id, szName, charsmax(szName))
// Here we will update the user hes information in the database where the steamid matches.
format(szTemp,charsmax(szTemp),"UPDATE `tutorial` SET `exp` = '%i' WHERE `tutorial`.`steamid` = '%s';",iSurvive[id], szSteamId)
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
// and of course, free the connection
SQL_FreeHandle(SqlConnection)
}
public Event_DeathMsg()
{
new iKiller = read_data(1)
new iVictim = read_data(2)
if(is_user_alive(iKiller))
{
if(read_data(3))
{
iHeadshot[iKiller] += get_pcvar_num(cHeadshot)
}
else
{
iKills[iKiller] += get_pcvar_num(cKills)
}
}
iSuicide[iVictim] -= get_pcvar_num(cSuicide)
}
public eRound_end()
{
new iPlayers[32], iNum, iPid;
get_players( iPlayers, iNum, "a" );
for( new i; i < iNum; i++ )
{
iPid = iPlayers[i];
new CsTeams:team = cs_get_user_team(iPid);
if( team == CS_TEAM_T && is_user_alive(iPid) )
{
iSurvive[iPid] += get_pcvar_num(cSurvive)
}
}
}