#include <amxmodx>
#include <colorchat>
#include <hamsandwich>

#define PLUGIN "BestPlayer"
#define VERSION "1.0"
#define AUTHOR "xZANDARMx"

new g_iKills[33]
new g_iDeaths[33]
new g_hs[33]
new Damage[33];

new bestplayer = 0, g_iMaxPlayers;

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_event("DeathMsg", "death_event", "a", "1>0");
	register_event("HLTV", "eHLTV", "a", "1=0", "2=0");
	register_logevent("wiadomosc",2,"1=Round_End")
	RegisterHam(Ham_TakeDamage, "player", "ForwardPlayerDmg", 1);
	g_iMaxPlayers = get_maxplayers();
}

public client_connect(id) 
{
	g_iKills[id] = 0
	g_iDeaths[id] = 0
}

public ForwardPlayerDmg(iVictim, iInflictor, iAttacker, Float:fDamage, iDamageBits)
 {
	if(is_user_connected(iAttacker) && iAttacker != iVictim)
		Damage[iAttacker] += floatround(fDamage);

}

public death_event()
{
	new iKiller = read_data(1), iVictim = read_data(2), iHitplace = read_data(3);

	if (iKiller == iVictim)
	{
		g_iDeaths[iKiller]++;
		return;
	}

	g_iKills[iKiller]++;
	g_iDeaths[iVictim]++;

	if(iHitplace)
	{
		g_hs[iKiller]++;
	}
}

public wiadomosc() 
{ 
	if(get_playersnum() < 2)
		return
	
	for(new i=1; i <= g_iMaxPlayers; i++)
	{
		if (is_user_connected(i) && g_iKills[i] > g_iKills[bestplayer] || g_iKills[i] == g_iKills[bestplayer] && g_iDeaths[i] < g_iDeaths[bestplayer] || g_iKills[i] == g_iKills[bestplayer] && g_hs[i] > g_hs[bestplayer])
		{
			bestplayer = i;
		}
	}
	
	new name[32];
	get_user_name(bestplayer, name, 31);
	
	new iKills = g_iKills[bestplayer]
	new iHS = g_hs[bestplayer]
	
	ColorChat(0, GREEN,"[NazwaForum.pl] ^x01| ^x01Gracz rundy: ^x03%s", name)
	if(iKills >= 5 || iKills == 0)
	{
		ColorChat(0, GREEN, "[NazwaForum.pl] ^x01| ^x01Zdobyl ^x03%d^x04 Fragow", iKills)
	}
	else if(iKills > 1 && iKills < 5)
	{
		ColorChat(0, GREEN, "[NazwaForum.pl] ^x01| ^x01Zdobyl ^x03%d^x04 Fragi", iKills)
	}
	else if(iKills == 1)
	{
		ColorChat(0, GREEN, "[NazwaForum.pl] ^x01| ^x01Zdobyl Fraga")
	}
	ColorChat(0, GREEN, "[NazwaForum.pl] ^x01| ^x01Ustrzelil ^x03%d ^x04 HeadShotow", iHS)
	
	new bestdmg = 0, bestdmgid = 0;
	
	for(new i=1; i<= g_iMaxPlayers; i++)
	{
		if(is_user_connected(i) && Damage[i] > bestdmg)
		{
			bestdmg = Damage[i];
			bestdmgid = i;
		}
	}
	
	if(!bestdmgid)
		return;
	
	get_user_name(bestdmgid, name, 31);
	ColorChat(0, GREEN, "[NazwaForum.pl] ^x01| ^x01Najwiecej obrazen zadal ^x03%s ^x01(^x01%d ^x04obrazen!).", name, bestdmg);
}


public eHLTV()
{
	for( new i = 1 ; i <= g_iMaxPlayers ; i++ )
	{
		g_iKills[ i ] = 0;
		g_iDeaths[ i ] = 0;
		g_hs[ i ] = 0;
		Damage[i] = 0;
	}
}