#include <amxmodx>
#include <fakemeta>
#include <fun>
#include <hamsandwich>
#include <cstrike>

#define PLUGIN_NAME "DM Lider Parlama"
#define PLUGIN_VERSION "1.9"
#define PLUGIN_AUTHOR "osmanbnm"

#define TASK_UPDATE 1337
#define TASK_REAPPLY 2000
#define MAX_PLAYERS 32

new bool:hasGlow[MAX_PLAYERS + 1];

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_logevent("event_round_start", 2, "1=Round_Start");
	register_event("DeathMsg", "event_death", "a");
	RegisterHam(Ham_Spawn, "player", "ham_player_spawn_post", 1);
}

public event_round_start()
{
	if(task_exists(TASK_UPDATE))
		remove_task(TASK_UPDATE);
	
	set_task(1.0, "update_glow_system", TASK_UPDATE);
}

public event_death()
{
	if(task_exists(TASK_UPDATE))
		remove_task(TASK_UPDATE);
	
	set_task(0.2, "update_glow_system", TASK_UPDATE);
}

public ham_player_spawn_post(id)
{
	if(!is_user_alive(id))
		return HAM_IGNORED;
	
	if(hasGlow[id])
	{
		new taskid = TASK_REAPPLY + id;
		if(task_exists(taskid))
			remove_task(taskid);
		
		set_task(0.3, "reapply_glow", taskid);
		set_task(0.5, "reapply_glow", taskid + 100);
	}
	
	return HAM_IGNORED;
}

public reapply_glow(taskid)
{
	new id = taskid - TASK_REAPPLY;
	
	if(id > 100)
		id -= 100;
	
	if(id < 1 || id > MAX_PLAYERS)
		return;
	
	if(!is_user_connected(id) || !is_user_alive(id))
		return;
	
	if(hasGlow[id])
	{
		set_user_rendering(id, kRenderFxGlowShell, 255, 255, 255, kRenderNormal, 25);
	}
}

public update_glow_system()
{
	new topPlayersT[3];
	new topPlayersCT[3];
	new countT = 0;
	new countCT = 0;
	
	get_team_top_players(CS_TEAM_T, topPlayersT, countT);
	get_team_top_players(CS_TEAM_CT, topPlayersCT, countCT);
	
	new bool:currentLeaders[MAX_PLAYERS + 1];
	
	for(new i = 0; i < countT; i++)
	{
		if(topPlayersT[i] > 0 && topPlayersT[i] <= MAX_PLAYERS)
			currentLeaders[topPlayersT[i]] = true;
	}
	
	for(new i = 0; i < countCT; i++)
	{
		if(topPlayersCT[i] > 0 && topPlayersCT[i] <= MAX_PLAYERS)
			currentLeaders[topPlayersCT[i]] = true;
	}
	
	for(new id = 1; id <= MAX_PLAYERS; id++)
	{
		if(!is_user_connected(id))
			continue;
		
		if(currentLeaders[id] && !hasGlow[id])
		{
			if(!is_user_alive(id))
				continue;
			
			set_user_rendering(id, kRenderFxGlowShell, 255, 255, 255, kRenderNormal, 25);
			hasGlow[id] = true;
		}
		else if(!currentLeaders[id] && hasGlow[id])
		{
			set_user_rendering(id, kRenderFxNone, 0, 0, 0, kRenderNormal, 0);
			hasGlow[id] = false;
		}
		else if(currentLeaders[id] && hasGlow[id] && is_user_alive(id))
		{
			set_user_rendering(id, kRenderFxGlowShell, 255, 255, 255, kRenderNormal, 25);
		}
	}
}

stock get_team_top_players(CsTeams:team, topPlayers[3], &count)
{
	new playerIds[MAX_PLAYERS];
	new frags[MAX_PLAYERS];
	new playerCount = 0;
	
	for(new id = 1; id <= MAX_PLAYERS; id++)
	{
		if(!is_user_connected(id))
			continue;
		
		if(cs_get_user_team(id) != team)
			continue;
		
		new currentFrags = get_user_frags(id);
		
		if(currentFrags > 0)
		{
			playerIds[playerCount] = id;
			frags[playerCount] = currentFrags;
			playerCount++;
		}
	}
	
	if(playerCount == 0)
	{
		count = 0;
		return;
	}
	
	for(new i = 0; i < playerCount - 1; i++)
	{
		for(new j = 0; j < playerCount - i - 1; j++)
		{
			if(frags[j] < frags[j + 1])
			{
				new tempFrag = frags[j];
				frags[j] = frags[j + 1];
				frags[j + 1] = tempFrag;
				
				new tempId = playerIds[j];
				playerIds[j] = playerIds[j + 1];
				playerIds[j + 1] = tempId;
			}
		}
	}
	
	count = (playerCount > 3) ? 3 : playerCount;
	
	for(new i = 0; i < count; i++)
		topPlayers[i] = playerIds[i];
}

public client_disconnected(id, bool:drop, message[], maxlen)
{
	if(id < 1 || id > MAX_PLAYERS)
		return;
	
	hasGlow[id] = false;
	
	new taskid = TASK_REAPPLY + id;
	if(task_exists(taskid))
		remove_task(taskid);
	
	new taskid2 = taskid + 100;
	if(task_exists(taskid2))
		remove_task(taskid2);
	
	if(task_exists(TASK_UPDATE))
		remove_task(TASK_UPDATE);
	
	set_task(0.2, "update_glow_system", TASK_UPDATE);
}

public plugin_end()
{
	if(task_exists(TASK_UPDATE))
		remove_task(TASK_UPDATE);
	
	for(new id = 1; id <= MAX_PLAYERS; id++)
	{
		new taskid = TASK_REAPPLY + id;
		if(task_exists(taskid))
			remove_task(taskid);
		
		new taskid2 = taskid + 100;
		if(task_exists(taskid2))
			remove_task(taskid2);
		
		if(hasGlow[id] && is_user_connected(id))
			set_user_rendering(id, kRenderFxNone, 0, 0, 0, kRenderNormal, 0);
		
		hasGlow[id] = false;
	}
}
