#include <amxmodx>

new pings[33], max_ping, maxPlayers, flags;
new check_times, check_delay, ban_time, immunitet_flags;

public plugin_init(){
	register_plugin("High Ping Kicker", "1.2", "benio101");
	
	max_ping=register_cvar("hpk_max_ping", "120", FCVAR_SERVER);
	check_times=register_cvar("hpk_check_times", "6", FCVAR_SERVER);
	check_delay=register_cvar("hpk_check_delay", "5.0", FCVAR_SERVER, 5.0);
	ban_time=register_cvar("hpk_ban_time", "1", FCVAR_SERVER);
	immunitet_flags=register_cvar("hpk_immunitet_flags", "a", FCVAR_SERVER);
}

public plugin_cfg(){
	maxPlayers=get_maxplayers();
	
	new flagi[27];
	get_pcvar_string(immunitet_flags, flagi, 26);
	flags=read_flags(flagi);
	
	set_task(get_pcvar_float(check_delay), "checkPing", .flags="b");
}

public client_disconnect(id)
{
	pings[id]=0;
}

public client_putinserver(id)
{
	pings[id]=0;
}

public checkPing(){
	new ping, loss,
		v_max_ping=get_pcvar_num(max_ping),
		v_ban_time=get_pcvar_num(ban_time),
		v_check_times=get_pcvar_num(check_times);
	
	for(new id=1; id<=maxPlayers; ++id)
	{
		if(is_user_connected(id) && get_user_team(id)%3 && !(get_user_flags(id) & flags)){
			get_user_ping(id, ping, loss);
			if(ping>v_max_ping)
			{
				if(++pings[id]>v_check_times)
				{
					server_cmd("amx_ban %d #%d ^"Zbyt wysoki ping. Limit: %dms^"", v_ban_time, get_user_userid(id), v_max_ping);
				}
			} else {
				pings[id]=0;
			}
		}
	}
}