Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
|
12-06-2019
, 12:55
Re: Player Data Update - Add More Info
|
#2
|
Uhn you do not need to get time in plugin, since you can use NOW() function in mysql string to retrieve correct date
PHP Code:
#include < amxmodx >
#include < sqlx >
#define TABLE_NAME "player_info"
/* Database */
new Host[] = ""
new User[] = ""
new Pass[] = ""
new Db[] = ""
new MySQL_Query[ 512 ]
new Handle:MySQL_Tuple
new Handle:MySQL_Connection
public plugin_init( ) {
register_plugin( "PlayerInfo", "v1", "???" )
}
public client_putinserver( id )
{
MySQL_Load(id);
}
public plugin_precache( )
{
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 )
formatex( MySQL_Query, charsmax( MySQL_Query ), "CREATE TABLE IF NOT EXISTS %s (id int NOT NULL AUTO_INCREMENT, username VARCHAR(128), steam_id VARCHAR(20), ip_adress VARCHAR(15), count INT NOT NULL, server VARCHAR(25), date DATE, time TIME, primary key (id) )", TABLE_NAME )
SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", MySQL_Query )
}
public MySQL_Load( id )
{
new szAuth[32];
get_user_authid(id, szAuth, 31);
new Temp[ 1 ]
Temp[ 0 ] = id
formatex( MySQL_Query, charsmax( MySQL_Query ), "SELECT `steam_id` FROM %s WHERE `steam_id` = '%s'", TABLE_NAME, szAuth )
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 ) { return PLUGIN_HANDLED; }
else if( FailState == TQUERY_QUERY_FAILED ) { return PLUGIN_HANDLED; }
new id = Data[ 0 ]
if( !is_user_connected( id ) )
return PLUGIN_HANDLED;
new szAuth[32], szName[32], szIP[23], currentTime[9], currentDate[11], szServer[23];
get_user_authid(id, szAuth, 31);
get_user_name(id, szName, 31);
get_user_ip(id, szIP, 22, 1);
get_time("%H:%M:%S", currentTime, 8);
get_time("%Y/%m/%d", currentDate, 10);
get_cvar_string("net_address",szServer, 22);
if( SQL_NumResults( Query ) < 1 )
{
formatex( MySQL_Query, charsmax( MySQL_Query ), "INSERT INTO %s (`username`, `steam_id`, `ip_adress`, `count`, `date`, `time`, `server`) VALUES ('%s', '%s', '%s', 1, '%s', '%s', '%s')", TABLE_NAME, szName, szAuth, szIP, currentDate, currentTime, szServer )
SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", MySQL_Query )
}
else {
formatex( MySQL_Query, charsmax( MySQL_Query ), "UPDATE %s SET `date` = '%s', `time` = '%s', `count` = `count` + 1, `server` = '%s', WHERE `steam_id` = '%s'", TABLE_NAME, currentDate, currentTime, szServer, szAuth )
SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", MySQL_Query )
}
return PLUGIN_CONTINUE;
}
public SQL_TrashHandler( FailState,Handle:Query, Error[ ], Errcode,Data[ ], DataSize )
{
if( FailState == TQUERY_CONNECT_FAILED )
return PLUGIN_HANDLED;
else if( FailState == TQUERY_QUERY_FAILED )
return PLUGIN_HANDLED;
SQL_FreeHandle( Query )
return PLUGIN_CONTINUE;
}
public plugin_end( )
{
SQL_FreeHandle( MySQL_Connection )
}
I recommend only to use inserts instead of update, ,since you can use mysql COUNT function to get the number of connections of a player. Also use steamid as primary key is a good choice in your case
__________________
Last edited by ^SmileY; 12-06-2019 at 13:52.
|
|