So data is send to MYSQL about users. Kills and so on.
But if user is kicked or banned the user isnt removed from MYSQL.
Also the nicknames isnt updated in MYSQL.
If i change my nickname the database doesnt reqonise the nick change and just keeps the old nickname and doesnt update kills / deaths for that specific user.
PHP Code:
#include < amxmodx >
#include < sqlx >
#include < cstrike >
#include < hamsandwich >
/* Database */
new Host[ ] = ""
new User[ ] = ""
new Pass[ ] = ""
new Db[ ] = ""
new MySQL_Query[ 512 ]
new Handle:MySQL_Tuple
new Handle:MySQL_Connection
new bool:g_Loaded[ 33 ] = false
new Kills[33], Deaths[33];
new Team[33], OldName[33][32]
new g_logfile[64];
new const VERSION[ ] = "0.0.3"
public plugin_init( ) {
register_plugin( "Scoreboard to MySQL", VERSION, "Airkish" )
register_clcmd("say /rs", "ResetScore")
register_event( "TeamInfo", "event_team_info", "a" );
}
public client_putinserver( id )
{
OldName[id] = "";
set_task(5.0, "Presql_Load", id)
g_Loaded[ id ] = false
}
public client_infochanged(id)
{
new newname[32],oldname[32]
get_user_info(id, "name", newname,31)
get_user_name(id, oldname, 31)
if(!equali(newname, oldname))
{
OldName[id] = oldname;
MySQL_UpdateName(id);
return PLUGIN_HANDLED;
}
return PLUGIN_CONTINUE;
}
public Presql_Load(id) {
if ( cs_get_user_team(id) == CS_TEAM_T) {
Team[id] = 1;
}
else if ( cs_get_user_team(id) == CS_TEAM_CT ) {
Team[id] = 2;
}
else {
Team[id] = 3;
}
MySQL_Load(id)
}
public client_death(Killer, Victim, wpnindex, hitplace, TK)
{
if(Killer == Victim) {
Deaths[Killer]++;
MySQL_Update(Killer);
return PLUGIN_HANDLED;
}
Kills[Killer]++;
Deaths[Victim]++;
MySQL_Update(Killer)
MySQL_Update(Victim)
return PLUGIN_CONTINUE;
}
public ResetScore(id) {
Kills[id] = 0;
Deaths[id] = 0;
MySQL_Update(id);
}
public event_team_info()
{
new id = read_data( 1 );
new team[12];
read_data( 2, team, sizeof team - 1 );
switch( team[0] )
{
case 'C': {
Team[id] = 1;
}
case 'T': {
Team[id] = 2;
}
case 'S': {
Team[id] = 3;
}
}
MySQL_Update(id);
}
public client_disconnected( id )
{
OldName[id] = "";
if( g_Loaded[ id ] )
MySQL_Delete( id )
}
public plugin_precache( )
{
get_time("online_players_%Y%m%d.log", g_logfile, charsmax(g_logfile));
log_errors("-------------------- == --------------------");
MySQL_Tuple = SQL_MakeDbTuple( Host, User, Pass, Db )
#if AMXX_VERSION_NUM >= 183
SQL_SetCharset(MySQL_Tuple,"utf8");
#endif
new ErrorCode
MySQL_Connection = SQL_Connect( MySQL_Tuple, ErrorCode, MySQL_Query, charsmax( MySQL_Query ) )
if( MySQL_Connection == Empty_Handle )
set_fail_state( MySQL_Query )
SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", "CREATE TABLE IF NOT EXISTS online_players (steamid VARCHAR(40), username VARCHAR(128) COLLATE utf8_lithuanian_ci NOT NULL, kills INT(11), deaths INT(11), team INT(1) )" )
SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", "DELETE FROM online_players" )
}
public MySQL_Load( id )
{
new Temp[ 1 ]
Temp[ 0 ] = id
formatex( MySQL_Query, charsmax( MySQL_Query ), "SELECT * FROM `online_players` WHERE `username` = '%s'", get_user_name_ex(id) )
SQL_ThreadQuery( MySQL_Tuple, "Load_PlayerInfo", MySQL_Query, Temp, sizeof( Temp ) )
}
public Load_PlayerInfo( FailState, Handle:Query, Error[ ], Errcode, Data[ ], DataSize )
{
if( FailState == TQUERY_CONNECT_FAILED )
{
new Players[ 32 ], PlayersNum
get_players( Players, PlayersNum, "ch" )
for( new i; i < PlayersNum; i++ )
log_errors( "Load - Could not connect to SQL database. [%d] %s", Errcode, Error )
}
else if( FailState == TQUERY_QUERY_FAILED )
{
new Players[ 32 ], PlayersNum
get_players( Players, PlayersNum, "ch" )
for( new i; i < PlayersNum; i++ )
{
log_errors( "Load Query failed. [%d] %s", Errcode, Error )
}
}
new id = Data[ 0 ]
if( !is_user_connected( id ) )
return
if( SQL_NumResults( Query ) < 1 )
{
formatex( MySQL_Query, charsmax( MySQL_Query ), "INSERT INTO `online_players` (`steamid`, `username`, `kills`, `deaths`, `team`) VALUES ('%s', '%s', '%d', '%d', '%d')", get_user_steamid_ex(id), get_user_name_ex(id), Kills[id], Deaths[id], Team[id])
SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", MySQL_Query )
log_errors("%s", MySQL_Query)
g_Loaded[ id ] = true
}
}
public MySQL_Update( id ) {
formatex( MySQL_Query, charsmax( MySQL_Query ), "UPDATE `online_players` SET `kills` = '%d', `deaths` = '%d', `team` = '%d' WHERE `username` = '%s'", Kills[id], Deaths[id], Team[id], get_user_name_ex(id) )
SQL_ThreadQuery(MySQL_Tuple, "SQL_TrashHandler", MySQL_Query);
}
public MySQL_UpdateName(id) {
replace_all(OldName[id], 32, "'", "\'");
replace_all(OldName[id], 32, "^"", "\^"");
formatex( MySQL_Query, charsmax( MySQL_Query ), "UPDATE `online_players` SET `username` = '%s' WHERE `username` = '%s'", get_user_name_ex(id), OldName[id] )
SQL_ThreadQuery(MySQL_Tuple, "SQL_TrashHandler", MySQL_Query);
}
public MySQL_Delete( const id )
{
formatex( MySQL_Query, charsmax( MySQL_Query ), "DELETE from `online_players` WHERE `username` = '%s'", get_user_name_ex(id) )
SQL_ThreadQuery(MySQL_Tuple, "SQL_TrashHandler", MySQL_Query);
}
public SQL_TrashHandler( FailState,Handle:Query, Error[ ], Errcode,Data[ ], DataSize )
{
if( FailState == TQUERY_CONNECT_FAILED )
log_errors( "Load - Could not connect to SQL database. [%d] %s", Errcode, Error )
else if( FailState == TQUERY_QUERY_FAILED )
log_errors( "Load Query failed. [%d] %s", Errcode, Error )
SQL_FreeHandle( Query )
}
public plugin_end( )
{
SQL_FreeHandle( MySQL_Connection )
}
stock log_errors(const szText[], any:...)
{
static szMessage[256];
vformat(szMessage, 256, szText, 2);
log_to_file(g_logfile, szMessage);
return PLUGIN_HANDLED;
}
stock get_user_name_ex(id)
{
new szName[33];
get_user_name(id, szName, charsmax(szName));
replace_all(szName, charsmax(szName), "'", "\'");
replace_all(szName, charsmax(szName), "^"", "\^"");
return szName;
}
stock get_user_steamid_ex(id) {
new steamid[33]
get_user_authid( id, steamid, charsmax(steamid))
return steamid;
}