Code: Select all
/* Plugin generated by AMXX-Studio */
#include < amxmodx >
#include < amxmisc >
#include < sockets >
#include < regex >
#define PLUGIN "GT Player Statistics"
#define VERSION "1.7.5"
#pragma tabsize 0
enum (+= 1000)
{
TASK_CHANGE = 1000,
TASK_CLOSE,
TASK_DISPLAY,
TASK_VERIFY,
TASK_DISCONNECT
}
enum _:iDatasId
{
iFirstSeen,
iLastSeen,
iScore,
iMinutesPlayed,
iScorePerMinute,
iRank
}
enum _:iDatas
{
szPrefix[ 32 ],
szPattern[ 128 ]
}
new const g_szDatas[ iDatasId ][ iDatas ] =
{
{ "First Seen:", "\w{3} [0-3][0-9], [12][019][0-9][0-9]" },
{ "Last Seen:", "Online Now|(((Yester|To)day)|(\w{3} [0-3][0-9], [12][019][0-9][0-9])) (1[0-2]|[0-9]):[0-5]\d [A|P]M" },
{ "Score:", "\n*\s*(\d+)\s*\n*" },
{ "Minutes Played:", "\n*\s*(\d+)\s*\n*" },
{ "Score per Minute:", "\s+(\d+\.?\d*)" /*"\n*\s*(\d+\.?\d*)\s*\n*"*/ },
{ "Rank on Server:", "#\d+ out of \d+" }
}
new g_szPlayerDatas[ iDatasId ][ 32 ][ 33 ];
new const g_szTag[ ] = "|GT Player Statistics:";
new const g_szHost[ ] = "www.gametracker.com";
new const g_szPlayer[ ] = "/player";
new const g_szServerNotFound[ ] = "No Statistics Available";
new const g_szPlayerNotFound[ ] = "Player Not Found";
new const g_szPlayerSearchFor[ ] = "Player Search for";
new g_szServerIp[ 32 ];
//new const g_szServerIp[ ] = "193.203.39.152:28015";
new g_szRequest[33][ 128 ];
new g_szData[33][ 4096 ];
new g_szName[ 33 ][ 32 ];
new g_iSocket[ 33 ];
new g_iPacketNum[ 33 ];
new g_iError[ 33 ];
public plugin_init( )
{
register_plugin( PLUGIN, VERSION, "Askhanar" );
register_clcmd( "say", "HookClCmdSayOrSayTeam" );
register_clcmd( "say_team", "HookClCmdSayOrSayTeam" );
register_clcmd( "gt_stats", "ClCmdGTStats" );
register_clcmd( "amx_gtstats", "ClCmdAmxGTStats" );
get_user_ip( 0, g_szServerIp, sizeof ( g_szServerIp ) -1, 0 ); //--| Ii luam ip la sv cu tot cu port!
register_menucmd( register_menuid( "GTStatsMenu" ), (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9), "GTStatsMenuHandler" );
set_task( 300.0, "task_DisconnectAllPlayers", TASK_DISCONNECT );
// Add your code here...
}
public task_DisconnectAllPlayers( )
{
static iPlayers[ 32 ];
static iPlayersNum;
get_players( iPlayers, iPlayersNum, "ch" );
if( !iPlayersNum )
return;
static id, i;
for( i = 0; i < iPlayersNum; i++ )
{
id = iPlayers[ i ];
remove_task( id + TASK_CHANGE );
remove_task( id + TASK_DISPLAY );
if( g_iSocket[ id ] > 0 )
{
socket_close( g_iSocket[ id ] );
g_iSocket[ id ] = 0
g_iPacketNum[ id ] = 0;
g_iError[ id ] = 0;
copy( g_szData[ id ], sizeof( g_szData[ ] ) -1, "" );
}
}
}
public client_disconnected( id )
{
if( is_user_bot( id ) )
return;
remove_task( id + TASK_CHANGE );
remove_task( id + TASK_CLOSE );
remove_task( id + TASK_DISPLAY );
socket_close( g_iSocket[ id ] );
g_iSocket[ id ] = 0;
g_iPacketNum[ id ] = 0;
g_iError[ id ] = 0;
}
public HookClCmdSayOrSayTeam( id )
{
static szArgs[ 192 ], szCommand[ 192 ];
read_args( szArgs, sizeof ( szArgs ) -1 );
if( !szArgs[ 0 ] )
return PLUGIN_CONTINUE;
remove_quotes( szArgs );
if( equal( szArgs, "/gtstats", strlen( "/gtstats" ) ) )
{
replace( szArgs, sizeof ( szArgs ) -1, "/gt", "" );
formatex( szCommand, sizeof ( szCommand ) -1, "gt_%s", szArgs );
client_cmd( id, szCommand );
return PLUGIN_HANDLED;
}
return PLUGIN_CONTINUE;
}
public ClCmdGTStats( id )
{
new szFirstArg[ 32 ];
read_argv( 1, szFirstArg, sizeof ( szFirstArg ) -1 );
if( equali( szFirstArg, "" ) )
{
get_user_name( id, g_szName[ id ], sizeof ( g_szName[ ] ) -1 );
ReplaceName( id, true );
BeginShowingStats( id, g_szName[ id ] );
}
else
{
new iPlayer = cmd_target( id, szFirstArg, 8 );
if(!iPlayer )
{
client_print( id, print_chat, "%s Player '%s' is not connected on server.", g_szTag, szFirstArg );
return PLUGIN_HANDLED;
}
else if( iPlayer == id )
{
client_print( id, print_chat, "%s Use only '/gtstats' instead.", g_szTag );
return PLUGIN_HANDLED;
}
get_user_name( iPlayer, g_szName[ id ], sizeof ( g_szName[ ] ) -1 );
ReplaceName( id, true );
BeginShowingStats( id, g_szName[ id ] );
}
return PLUGIN_HANDLED;
}
public ClCmdAmxGTStats( id )
{
if( !( get_user_flags( id ) & ADMIN_RCON ) )
{
client_cmd( id, "echo You have NO acces to this command!" );
return PLUGIN_HANDLED;
}
new szFirstArg[ 32 ];
read_argv( 1, szFirstArg, sizeof( szFirstArg ) -1 );
if( equali( szFirstArg, "" ) )
{
client_cmd( id, "echo USAGE: amx_gtstats <^"Exact Name^"> " );
return PLUGIN_HANDLED;
}
copy( g_szName[ id ], sizeof( g_szName[ ] ) -1, szFirstArg );
ReplaceName( id, true );
BeginShowingStats( id, g_szName[ id ] );
client_cmd( id, "echo Requesting information about: %s!", szFirstArg );
client_cmd( id, "echo Watch your chat for information!" );
return PLUGIN_HANDLED;
}
public BeginShowingStats( id, const szName[ ] )
{
if( g_iSocket[ id ] > 0 )
socket_close( g_iSocket[ id ] );
g_iSocket[ id ] = socket_open( g_szHost, 80, SOCKET_TCP, g_iError[ id ] );
if( g_iError[ id ] == 0 && g_iSocket[ id ] > 0 )
{
formatex( g_szRequest[ id ], sizeof ( g_szRequest[ ] ) -1,"GET %s/%s/%s/ HTTP/1.1^r^nHost: %s^r^n^r^n", g_szPlayer, szName, g_szServerIp, g_szHost );
socket_send( g_iSocket[ id ], g_szRequest[ id ], sizeof( g_szRequest[ ] ) );
g_iPacketNum[ id ] = 0;
set_task( 0.1, "task_Change", id + TASK_CHANGE, _, _, "b", 0 );
set_task( 5.0, "task_Close", id + TASK_CLOSE );
client_print( id, print_chat, "%s Waiting for GameTracker to response..", g_szTag );
}
else
{
switch( g_iError[ id ] )
{
case 1: log_amx("[ERROR] Unable to create socket." );
case 2: log_amx("[ERROR] Unable to connect to hostname." );
case 3: log_amx("[ERROR] Unable to connect to the HTTP port." );
}
client_print( id, print_chat, "%s An error occuered while trying to establish connection!", g_szTag );
return PLUGIN_HANDLED;
}
remove_task( id + TASK_DISPLAY );
set_task( 3.0, "task_Display", id + TASK_DISPLAY );
return PLUGIN_CONTINUE;
}
public task_Change( id )
{
id -= TASK_CHANGE;
if( !is_user_connected( id ) )
return PLUGIN_HANDLED;
if( /*socket_change*/socket_is_readable( g_iSocket[ id ], 0 ) )
{
if( socket_recv( g_iSocket[ id ], g_szData[ id ], sizeof( g_szData[ ] ) ) )
{
if( ++g_iPacketNum[ id ] == 1 )
{
if( containi( g_szData[ id ], g_szServerNotFound ) != -1 )
{
client_print( id, print_chat, "%s Current Server was not found on GameTracker database!", g_szTag );
task_Close( id + TASK_CLOSE );
return PLUGIN_HANDLED;
}
if( containi( g_szData[ id ], g_szPlayerNotFound ) != -1 || containi( g_szData[ id ], g_szPlayerSearchFor ) != -1 )
{
ReplaceName( id, false );
client_print( id, print_chat, "%s Player '%s' was not found on GameTracker database!", g_szTag, g_szName[ id ] );
task_Close( id + TASK_CLOSE );
return PLUGIN_HANDLED;
}
}
static iDataStart;
static iNum, szError[ 128 ];
iNum = 0;new bool:bFixedStats = false;
for( new i = iFirstSeen; i <= iRank; i++ )
{
if( i >= iScore && !bFixedStats )
{
if( contain( g_szData[ id ], "CURRENT STATS" ) != -1 )
{
new iAllStats = containi( g_szData[ id ], "ALL TIME STATS" );
if( iAllStats != -1 )
{
bFixedStats = true;
format( g_szData[ id ], sizeof( g_szData[ ] ) -1, "%s", g_szData[ id ][ iAllStats ] );
}
}
}
iDataStart = containi( g_szData[ id ], g_szDatas[ i ][ szPrefix ] );
if( iDataStart != -1 )
{
new Regex:hRegex = regex_match( g_szData[ id ][ iDataStart ], g_szDatas[ i ][ szPattern ], iNum, szError, sizeof( szError ) -1 );
if( hRegex >= REGEX_OK )
{
regex_substr( hRegex, 0, g_szPlayerDatas[ i ][ id ], sizeof( g_szPlayerDatas[ ][ ] ) -1 );
server_print( "%s %s", g_szDatas[ i ][ szPrefix ], g_szPlayerDatas[ i ][ id ] );
regex_free( hRegex );
}
else
{
client_print( id, print_chat, "%s Regex occured an error, please try again", g_szTag );
task_Close( id + TASK_CLOSE );
}
}
}
}
}
return PLUGIN_CONTINUE;
}
public task_Close( id )
{
id -= TASK_CLOSE;
if( !is_user_connected( id ) )
return;
remove_task( id + TASK_CHANGE );
remove_task( id + TASK_DISPLAY );
socket_close( g_iSocket[ id ] );
g_iSocket[ id ] = 0
g_iPacketNum[ id ] = 0;
g_iError[ id ] = 0;
copy( g_szData[ id ], sizeof( g_szData[ ] ) -1, "" );
}
public task_Display( id )
{
id -= TASK_DISPLAY;
if( !is_user_connected( id ) )
return;
client_print( id, print_chat, "%s GameTracker has successfully responded..", g_szTag );
GTStatsMenu( id );
}
public GTStatsMenu( id )
{
ReplaceName( id, false );
new szMenuName[ 512 ], szHostName[ 64 ];
get_cvar_string( "hostname", szHostName, sizeof( szHostName ) -1 );
replace_all( g_szPlayerDatas[ iRank ][ id ], sizeof( g_szPlayerDatas[ ][ ] ) -1, "#", "" );
replace_all( g_szPlayerDatas[ iRank ][ id ], sizeof( g_szPlayerDatas[ ][ ] ) -1, " out of ", " \wfrom\r " );
formatex( szMenuName, sizeof( szMenuName ) -1, "\y| \r%s \y|^n| \wPlayer Statistics for: \r%s \y|^n^n^n|\w First Seen:\r %s\y |^n|\w Last Seen:\r %s\y |^n|\w Score:\r %i\y |^n|\w Score per Minute:\r %.1f\y |^n|\w Minutes Played:\r %i\w (\r %s\w )\y |^n|\w Rank on Server:\r %s\y |^n^n^n\wPress\r 1\w to see this stats in\y MOTD^n\wStatistics directly token from:^n \r%s",
szHostName, g_szName[ id ] , g_szPlayerDatas[ iFirstSeen ][ id ], g_szPlayerDatas[ iLastSeen ][ id ],
str_to_num( g_szPlayerDatas[ iScore ][ id ] ), floatstr( g_szPlayerDatas[ iScorePerMinute ][ id ] ),
str_to_num( g_szPlayerDatas[ iMinutesPlayed ][ id ] ), GetPlayedTime( str_to_num( g_szPlayerDatas[ iMinutesPlayed ][ id ] ) ),
g_szPlayerDatas[ iRank ][ id ], g_szHost );
show_menu( id, (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9), szMenuName, -1, "GTStatsMenu" );
}
public GTStatsMenuHandler( id, iKey )
{
/* Menu:
* | %s |
* | Player Statistics for: %s |
*
*
*
* | First Seen: %s |
* | Last Seen: %s |
* | Score: %i |
* | Score per Minute: %.1f |
* | Time Played: %i ( %s ) |
* | Rank on Server: %s |
*
*
* Press 1 to see this stats in MOTD
* Statistics directly token from:
* www.gametracker.com
*/
switch( iKey )
{
case 0:
{
ReplaceName( id, true );
static szCustomUrl[ 128 ];
formatex( szCustomUrl, sizeof( szCustomUrl ) -1, "http://%s%s/%s/%s/",
g_szHost, g_szPlayer, g_szName[ id ], g_szServerIp );
show_motd( id, szCustomUrl );
GTStatsMenu( id );
}
case 1..9: return;
}
}
GetPlayedTime( const iTime )
{
new szTime[ 64 ];
new iSeconds = iTime * 60;
new iMinutes = 0;
new iHours = 0;
while( iSeconds >= 60 )
{
iSeconds -= 60;
iMinutes++;
}
while( iMinutes >= 60 )
{
iMinutes -= 60;
iHours++;
}
new bool:UseFormatInsteadFormatex = false;
if( iSeconds )
{
formatex( szTime, sizeof( szTime ) -1, "%i s", iSeconds );
UseFormatInsteadFormatex = true;
}
if( iMinutes )
{
if( UseFormatInsteadFormatex )
{
format( szTime, sizeof( szTime ) -1, "%i m, %s", iMinutes, szTime );
}
else
{
formatex( szTime, sizeof( szTime ) -1, "%i m", iMinutes);
UseFormatInsteadFormatex = true;
}
}
if( iHours )
{
if( UseFormatInsteadFormatex )
{
format( szTime, sizeof( szTime ) -1, "%i h, %s", iHours, szTime );
}
else
{
formatex( szTime, sizeof( szTime ) -1, "%i h", iHours );
UseFormatInsteadFormatex = true;
}
}
if( !UseFormatInsteadFormatex )
{
copy( szTime, sizeof( szTime ) -1, "Unknown" );
}
return szTime;
}
ReplaceName( id, const bool:bSafe = true )
{
if( bSafe )
{
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "#", "%23" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "?", "%3F" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, ":", "%3A" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, ";", "%3B" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "/", "%2F" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, ",", "%2C" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "$", "%24" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "@", "%40" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "+", "%2B" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "=", "%3D" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "®", "®" );
}
else
{
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%23","#" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%3F", "?" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%3A", ":" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%3B", ";" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%2F", "/" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%2C", "," );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%24", "$" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%40", "@" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%2B", "+" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "%3D", "=" );
replace_all( g_szName[ id ], sizeof( g_szName[ ] ) -1, "®", "®" );
}
}