/*

ICQ: 979699

*/

#include <amxmodx>
#include <fakemeta>

new const PLUGIN[] = "Percent Bug Fix"
new const VERSION[] = "2.1"
new const AUTHOR[] = "F4RR3LL"

#define OFFSET_INTERNALMODEL	126
#define OFFSET_TEAM		114

enum CS_Internal_Models
{
	CS_DONTCHANGE = 0,
	CS_CT_URBAN = 1,
	CS_T_TERROR = 2,
	CS_T_LEET = 3,
	CS_T_ARCTIC = 4,
	CS_CT_GSG9 = 5,
	CS_CT_GIGN = 6,
	CS_CT_SAS = 7,
	CS_T_GUERILLA = 8,
	CS_CT_VIP = 9,
	CZ_T_MILITIA = 10,
	CZ_CT_SPETSNAZ = 11
}

enum CsTeams
{
	CS_TEAM_UNASSIGNED = 0,
	CS_TEAM_T = 1,
	CS_TEAM_CT = 2,
	CS_TEAM_SPECTATOR = 3
}

enum ChatColor
{
	CHATCOLOR_NORMAL = 1, 	// Normal
	CHATCOLOR_GREEN, 	// Green Color
	CHATCOLOR_TEAM_COLOR, 	// Red, grey, blue
	CHATCOLOR_GREY, 	// grey
	CHATCOLOR_RED, 		// Red
	CHATCOLOR_BLUE, 	// Blue
}

new g_TeamName[][] = 
{
	"",
	"TERRORIST",
	"CT",
	"SPECTATOR"
}

new g_msgSayText
new g_msgTeamInfo

new g_say_team[] = "say_team"

new g_name_change[] = "#Cstrike_Name_Change"
new g_name[] = "name"

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_clcmd("say", "cmdCheckSay", -1)
	register_clcmd(g_say_team, "cmdCheckSay", -1)
	
	register_forward(FM_ClientUserInfoChanged, "forward_client_userinfochanged")
	
	g_msgSayText = get_user_msgid("SayText")
	g_msgTeamInfo = get_user_msgid("TeamInfo")
}

public cmdCheckSay(id)
{
	static message[192]
	read_args(message, 191)
	
	if(contain(message, "%") != -1) 
	{
		remove_quotes(message)
		
		static cmd[9]
		read_argv(0, cmd, sizeof cmd -1)
		
		static name[32]
		get_user_name(id, name, sizeof name -1)
		
		static CsTeams:team
		team = fm_get_user_team(id)
		
		static new_message[191] // -1 :o
		static players[32], totalplayers, pid
		
		switch (team)
		{
			case CS_TEAM_SPECTATOR:
			{
				if(equal(cmd, g_say_team))
				{
					get_players(players, totalplayers, "c")
					format(new_message, sizeof new_message -1, "^x01(Spectator)^x03 %s^x01 :  %s", name, message)
					
					for (new i = 0; i < totalplayers; i++)
					{
						pid = players[i]
						
						if(fm_get_user_team(pid) == CS_TEAM_SPECTATOR)
						{
							colorChat(pid, CHATCOLOR_GREY, "%s", new_message)
						}
					}
				} 
				else 
				{
					get_players(players, totalplayers, "bc")
					format(new_message, sizeof new_message -1, "^x01*SPEC*^x03 %s^x01 :  %s", name, message)
					
					colorChat(pid, CHATCOLOR_GREY, "%s", new_message)
				}
			}
			case CS_TEAM_T:
			{
				if(is_user_alive(id))
				{
					if(equal(cmd, g_say_team))
					{
						get_players(players, totalplayers, "ace", "TERRORIST")
						format(new_message, sizeof new_message -1, "^x01(Terrorist)^x03 %s^x01 :  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_RED, "%s", new_message)
						}
					} 
					else 
					{
						get_players(players, totalplayers, "ac")
						format(new_message, sizeof new_message -1, "^x03%s^x01 :  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_RED, "%s", new_message)
						}
					}
				} 
				else 
				{
					if(equal(cmd, g_say_team))
					{
						get_players(players, totalplayers, "bce", "TERRORIST")
						format(new_message, sizeof new_message -1, "^x01*DEAD*(Terrorist)^x03 %s^x01 :  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_RED, "%s", new_message)
						}
					} 
					else 
					{
						get_players(players, totalplayers, "bc")
						format(new_message, sizeof new_message -1, "^x01*DEAD*^x03 %s^x01 :  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_RED, "%s", new_message)
						}
					}
				}
			}
			case CS_TEAM_CT:
			{
				if(is_user_alive(id))
				{
					if(equal(cmd, g_say_team))
					{
						get_players(players, totalplayers, "ace", "CT")
						format(new_message, sizeof new_message -1, "^x01(Counter-Terrorist) ^x03%s ^x01:  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_BLUE, "%s", new_message)
						}
					} 
					else 
					{
						get_players(players, totalplayers, "ac")
						format(new_message, sizeof new_message -1, "^x03%s ^x01:  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_BLUE, "%s", new_message)
						}
					}
				}
				else 
				{
					if(equal(cmd, g_say_team))
					{
						get_players(players, totalplayers, "bce", "CT")
						format(new_message, sizeof new_message -1, "^x01*DEAD*(Counter-Terrorist)^x03 %s^x01 :  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_BLUE, "%s", new_message)
						}
					} 
					else 
					{
						get_players(players, totalplayers, "bc")
						format(new_message, sizeof new_message -1, "^x01*DEAD*^x03 %s^x01 :  %s", name, message)
						
						for (new i = 0; i < totalplayers; i++)
						{
							pid = players[i]
							
							colorChat(pid, CHATCOLOR_BLUE, "%s", new_message)
						}
					}
				}
			}
		}
		
		return PLUGIN_HANDLED
	}
	
	return PLUGIN_CONTINUE
}

public forward_client_userinfochanged(id, buffer)
{
	if (!is_user_connected(id))
		return FMRES_IGNORED

	static oldname[32], newname[32]
	pev(id, pev_netname, oldname, sizeof oldname -1)
	
	engfunc(EngFunc_InfoKeyValue, buffer, g_name, newname, sizeof newname - 1)
	
	if(equal(newname, oldname))
		return FMRES_IGNORED
	
	message_begin(MSG_BROADCAST, g_msgSayText)
	write_byte(id)
	write_string(g_name_change)
	write_string(oldname)
	write_string(newname)
	message_end()

	return FMRES_SUPERCEDE
}

stock CsTeams:fm_get_user_team(id, &{ CS_Internal_Models, _ }:model = CS_DONTCHANGE)
{
	model = CS_Internal_Models:get_pdata_int(id, OFFSET_INTERNALMODEL)
	
	return CsTeams:get_pdata_int(id, OFFSET_TEAM)
}

colorChat(id, ChatColor:color, const msg[], {Float,Sql,Result,_}:...)
{
	new team, index, MSG_Type
	new bool:teamChanged = false
	static message[192]
	
	switch(color)
	{
		case CHATCOLOR_NORMAL: // Normal
		{
			message[0] = 0x01;
		}
		case CHATCOLOR_GREEN: // Green
		{
			message[0] = 0x04;
		}
		default: // Grey, Red, Blue
		{
			message[0] = 0x03;
		}
	}
	
	vformat(message[1], 190, msg, 4);
	
	if(id == 0)
	{
		index = findAnyPlayer();
		MSG_Type = MSG_ALL;
	}
	else
	{
		index = id;
		MSG_Type = MSG_ONE;
	}
	if(index != 0)
	{
		team = get_user_team(index);	
		if(color == CHATCOLOR_RED && team != 1)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[1])
			teamChanged = true
		}
		else if(color == CHATCOLOR_BLUE && team != 2)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[2])
			teamChanged = true
		}
		else if(color == CHATCOLOR_GREY && team != 0)
		{
			messageTeamInfo(index, MSG_Type, g_TeamName[0])
			teamChanged = true
		}
		messageSayText(index, MSG_Type, message);
		if(teamChanged)
		{
			if(is_user_connected(id)) messageTeamInfo(index, MSG_Type, g_TeamName[team])
		}
	}
}

messageSayText(id, type, message[])
{
	message_begin(type, g_msgSayText, _, id)
	write_byte(id)		
	write_string(message)
	message_end()
}
	
messageTeamInfo(id, type, team[])
{
	message_begin(type, g_msgTeamInfo, _, id)
	write_byte(id)
	write_string(team)
	message_end()
}
	
findAnyPlayer()
{
	static players[32], inum, pid
	
	get_players(players, inum)
	
	for (new a = 0; a < inum; a++)
	{
		pid = players[a]
		if(is_user_connected(pid))
			return pid
	}
	
	return 0
}