/* TopX v0.1

* Details : With this plugin on server you can add a new type of "Stats"

* Commands : /topX 
 X = value 
  For example : /top656 and will display players who placed on rank "656" and players
around him

! Remember : for use /top656 you must have "656" players on rank


* Credits:
- KRoT@L for codes in AMX System
- XAD for him Stats Example

* That's all

* Have a nice day now

*/


#include <amxmodx>
#include <amxmisc>
#include <csstats>


#define PLUGIN "TopX"
#define VERSION "0.1"
#define AUTHOR "SAMURAI"

#define MAX_BUFFER_LENGTH       2047
#define MAX_NAME_LENGTH         31

#define STATS_KILLS             0
#define STATS_DEATHS            1
#define STATS_HS                2
#define STATS_TKS               3
#define STATS_SHOTS             4
#define STATS_HITS              5
#define STATS_DAMAGE            6

#define STATS_NUM 10

new g_sBuffer[MAX_BUFFER_LENGTH+1] = "";
new t_sName[MAX_NAME_LENGTH+1] = "";

public plugin_init() 
{
	register_plugin(PLUGIN,VERSION,AUTHOR)
	
	register_clcmd("say", "hookSay")
}



Float:effec( izStats[8] ) 
{
	if( !izStats[STATS_KILLS] ) return ( 0.0 );
	new Float:result
	result = 100.0 * float( izStats[STATS_KILLS] ) / float( izStats[STATS_KILLS] + izStats[STATS_DEATHS] );
	return (result > 100.0) ? 100.0 : result
}

format_top15(sBuffer[MAX_BUFFER_LENGTH+1], statsFrom)
{
	statsFrom--;
	new iStatsMax = statsFrom + STATS_NUM;
	new iMax = get_statsnum();
	new izStats[8], izBody[8];
	new iLen = 0, iOldLen = 0;
	
	if(iStatsMax > iMax)
		iStatsMax = iMax;
	
	iLen = format( sBuffer, MAX_BUFFER_LENGTH,
	"<body bgcolor=#000000><font color=#FFB000><pre>" );
	iLen += format( sBuffer[iLen], MAX_BUFFER_LENGTH - iLen,
	"%2s %-22.22s %9s %9s %9s %9s %4s %4s %4s^n",
	"#",
	"Nick",
	"Kills",
	"Deaths",
	"Hits",
	"Shots",
	"HS",
	"Eff.",
	"Acc.");
	for(new i = statsFrom; i < iStatsMax && MAX_BUFFER_LENGTH - iLen > 0; i++) {
		get_stats(i, izStats, izBody, t_sName, MAX_NAME_LENGTH);
		iOldLen = iLen
		iLen += format( sBuffer[iLen], MAX_BUFFER_LENGTH - iLen,
		"%2d %-22.22s %9d %9d %9d %9d %4d %3.0f%% %3.0f%%^n", i+1, t_sName,
		izStats[STATS_KILLS], izStats[STATS_DEATHS], izStats[STATS_HITS],
		izStats[STATS_SHOTS], izStats[STATS_HS],
		effec( izStats ), accuracy( izStats ) );
		while(MAX_BUFFER_LENGTH-iLen > 2 && replace(sBuffer[iOldLen], MAX_BUFFER_LENGTH-iOldLen, "<", "&lt;")) {
			iLen += 3;
		}
	}
}

Float:accuracy( izStats[8] ) 
{
	if( !izStats[STATS_SHOTS] ) return ( 0.0 );
	new Float:result
	result = 100.0 * float( izStats[STATS_HITS] ) / float( izStats[STATS_SHOTS] );
	return (result > 100.0) ? 100.0 : result
}

public hookSay(id) 
{
	new args[128]
	read_args(args, 127)
	remove_quotes(args)
	if(equal(args, "/top", 4)) {
		new szStatsFrom[16]
		copy(szStatsFrom, 15, args[4])
		new statsFrom = str_to_num(szStatsFrom)
		new iMax = get_statsnum();
		if ( 0 < statsFrom < iMax ) {
			format_top15(g_sBuffer, statsFrom)
			show_motd(id, g_sBuffer, "Top")
		}
	}
	return PLUGIN_CONTINUE
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1033\\ f0\\ fs16 \n\\ par }
*/
