#include < amxmodx >
#include < cstrike >
#include < sqlx >
#pragma tabsize 0;
new SQL_TABLE [ ] = "users_stats";
new iFrags [33], iDeaths [33], iMoney [33], iLastConnected [33] [32], PlayerAuthID [33] [32], PlayerName [33] [32],
Handle: iSqlTuple, SQLHost [32], SQLUser [32], SQLPassword [32], SQLDatabase [32], cSqlHost, cSqlUser,
cSqlPassword, cSqlDatabase, MaxPlayers;
public plugin_init ( )
{
register_event ( "DeathMsg", "EventDeathMsg", "a" );
register_logevent ( "leRoundEnd", 2, "1=Round_End" );
cSqlHost = register_cvar ( "sql_host", "host" );
cSqlUser = register_cvar ( "sql_user", "user" );
cSqlPassword = register_cvar ( "sql_password", "pw" );
cSqlDatabase = register_cvar ( "sql_database", "db" );
MaxPlayers = get_maxplayers ( );
}
public plugin_cfg ( )
{
get_pcvar_string ( cSqlHost, SQLHost, charsmax ( SQLHost ) );
get_pcvar_string ( cSqlUser, SQLUser, charsmax ( SQLUser ) );
get_pcvar_string ( cSqlPassword, SQLPassword, charsmax ( SQLPassword ) );
get_pcvar_string ( cSqlDatabase, SQLDatabase, charsmax ( SQLDatabase ) );
iSqlTuple = SQL_MakeDbTuple ( SQLHost, SQLUser, SQLPassword, SQLDatabase );
static Query [10048], Len;
Len += formatex ( Query [Len], charsmax ( Query ), "CREATE TABLE IF NOT EXISTS `%s`", SQL_TABLE );
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "(`steamid` varchar(32) NOT NULL, " );
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "`nickname` varchar(32) NOT NULL,");
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "`frags` int(11) NOT NULL,");
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "`deaths` int(11) NOT NULL,");
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "`money` int(11) NOT NULL,");
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "`last_seen_online` varchar(32) NOT NULL,");
Len += formatex ( Query [Len], charsmax ( Query ) - Len, "`ID` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY)" );
SQL_ThreadQuery ( iSqlTuple, "CreateTableThread", Query );
}
public CreateTableThread ( FailState, Handle:Query, Error [ ], Errcode, Data [ ], DataSize, Float:Queuetime )
{
if ( FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED )
{
set_fail_state ( "[SQL] Query Error" );
}
if ( Errcode )
{
log_amx ( "[SQL] Error: %s", Error );
}
}
public LoadData ( id )
{
static Query [10048], Data [1];
Data [0] = id;
formatex ( Query, charsmax ( Query ), "SELECT * FROM `%s` WHERE steamid = ^"%s^";", SQL_TABLE, PlayerAuthID [id] );
SQL_ThreadQuery ( iSqlTuple, "QuerySelectData", Query, Data, 1 );
}
public QuerySelectData ( FailState, Handle: Query, Error [ ], Errcode, Data [ ], DataSize, Float: Queuetime )
{
if ( FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED )
{
log_amx ( "[SQL] %s", Error );
return;
}
else
{
new id = Data [0];
if ( SQL_NumRows ( Query ) < 1 )
{
new Query [256];
formatex ( Query, charsmax ( Query ), "INSERT INTO `%s` (`steamid`,`nickname`,`frags`,`deaths`,`money`,`last_seen_online`) VALUES (^"%s^",^"%s^",^"0^",^"0^",^"0^",^"N/A^");", SQL_TABLE, PlayerAuthID [id], PlayerName [id] );
SQL_ThreadQuery ( iSqlTuple, "QuerySetData", Query );
}
else
{
iFrags [id] = SQL_ReadResult ( Query, SQL_FieldNameToNum ( Query, "frags" ) );
iDeaths [id] = SQL_ReadResult ( Query, SQL_FieldNameToNum ( Query, "deaths" ) );
iMoney [id] = SQL_ReadResult ( Query, SQL_FieldNameToNum ( Query, "money" ) );
}
}
}
public SaveData ( id )
{
new Query [256], sQuery [3000];
formatex ( Query, charsmax ( Query ), "UPDATE `%s` SET ", SQL_TABLE );
add ( sQuery, charsmax ( sQuery ), Query );
formatex ( Query, charsmax ( Query ), "nickname = ^"%s^", ", PlayerName [id] );
add ( sQuery, charsmax ( sQuery ), Query );
formatex ( Query, charsmax ( Query ), "frags = ^"%i^", ", iFrags [id] );
add ( sQuery, charsmax ( sQuery ), Query );
formatex ( Query, charsmax ( Query ), "deaths = ^"%i^", ", iDeaths [id] );
add ( sQuery, charsmax ( sQuery ), Query );
formatex ( Query, charsmax ( Query ), "money = ^"%i^", ", iMoney [id] );
add ( sQuery, charsmax ( sQuery ), Query );
formatex ( Query, charsmax ( Query ), "last_seen_online = ^"%s^" WHERE steamid = ^"%s^";", iLastConnected [id], PlayerAuthID [id] );
add ( sQuery, charsmax ( sQuery ), Query );
SQL_ThreadQuery ( iSqlTuple, "QuerySetData", sQuery );
}
public QuerySetData ( FailState, Handle: Query, Error [ ], Errcode, Data [ ], DataSize, Float:Queuetime )
{
if ( FailState == TQUERY_CONNECT_FAILED || FailState == TQUERY_QUERY_FAILED )
{
log_amx ( "[SQL] %s", Error );
return;
}
}
public client_putinserver ( id )
{
get_user_name ( id, PlayerName [id], charsmax ( PlayerName [ ] ) );
get_user_authid ( id, PlayerAuthID [id], charsmax ( PlayerAuthID [ ] ) );
iFrags [id] = 0;
iDeaths [id] = 0;
iMoney [id] = 0;
iLastConnected [id] = "N/A";
LoadData ( id );
}
public client_disconnect ( id )
{
iMoney [id] += cs_get_user_money ( id );
new iTime [32];
get_time ( "%m/%d/%Y - %H:%M:%S", iTime, charsmax ( iTime ) )
copy ( iLastConnected [id], charsmax ( iLastConnected [ ] ), iTime )
SaveData ( id );
}
public EventDeathMsg ( )
{
new Killer = read_data ( 1 );
new Victim = read_data ( 2 );
if ( !is_user_alive ( Killer ) || Killer == Victim || get_user_team ( Killer ) == get_user_team ( Victim ) )
return
iFrags [Killer] ++;
iDeaths [Victim] ++;
}
public leRoundEnd ( )
{
for ( new i = 1; i <= MaxPlayers; i ++ )
{
if ( is_user_connected ( i ) )
{
iMoney += cs_get_user_money ( i );
SaveData ( i );
}
}
}
public plugin_end ( ) SQL_FreeHandle ( iSqlTuple );