#include <amxmodx>
#include <amxmisc>
 
#define PLUGIN 		"STEAM Players"
#define VERSION 	"0.0b"
#define AUTHOR 		"Quit edit"
 
#define ACCESS_LEVEL ADMIN_CVAR
#define ADMIN_LISTEN	ADMIN_RCON
 
new message[192]
new sayText
new teamInfo
new maxPlayers
 
new show_ip,show_steamid,show_time,show_ping,show_loss;
 
new g_MessageColor
new g_NameColor
new g_AdminListen
 
new strName[191]
new strText[191]
new alive[11]
 
public plugin_init()
{
	register_plugin (PLUGIN , VERSION , AUTHOR)
 
	g_MessageColor = register_cvar ("amx_color", "1")
	g_NameColor = register_cvar ("amx_namecolor", "6")
	g_AdminListen = register_cvar ("amx_listen", "1")
	show_ip = register_cvar("sp_showip","0")
	show_steamid = register_cvar("sp_showsteamid","1")
	show_time = register_cvar("sp_showtime","0")
	show_ping = register_cvar("sp_showping","0")
	show_loss = register_cvar("sp_showloss","0")
 
 
	sayText = get_user_msgid ("SayText")
	teamInfo = get_user_msgid ("TeamInfo")
	maxPlayers = get_maxplayers()
 
 
	register_message (sayText, "det_duplicated")
 
	register_clcmd ("amx_color", "set_color", ACCESS_LEVEL, "<color>")
	register_clcmd ("amx_namecolor", "set_name_color", ACCESS_LEVEL, "<color>")
	register_clcmd ("amx_listen", "set_listen", ACCESS_LEVEL, "<1 | 0>")
 
	register_clcmd ("say /steamosok", "who_steam")
	register_clcmd ("say /sp", "who_steam")
	register_clcmd ("say /steamosjatekosok", "who_steam")
	register_clcmd ("say", "hook_say")
	register_clcmd ("say_team", "hook_teamsay")
}
 
 
public det_duplicated (msgId, msgDest, receiver)
{
	return PLUGIN_HANDLED
}
public who_steam(id)
{
	new bool:ip_showed = false
	new bool:steamid_showed = false
	new bool:time_showed = false
	new bool:ping_showed = false
	new bool:loss_showed = false
	if (get_pcvar_num(show_ip)>0)
	{
		ip_showed = true
	}
	if (get_pcvar_num(show_steamid)>0)
	{
		steamid_showed = true
	}
	if (get_pcvar_num(show_time)>0)
	{
		time_showed = true
	}
	if (get_pcvar_num(show_ping)>0)
	{
		ping_showed = true
	}
	if (get_pcvar_num(show_loss)>0)
	{
		loss_showed = true
	}
 
	new players[32], playersFound
	get_players(players,playersFound)
	new pos, message[2048]
	new name[40], ip[30],steamid[32], ping, loss;
	new str_pt[6],str_pi[6],str_lo[6]
	pos += format(message[pos],2048 - pos,"<STYLE>body{background:#232323;color:#cfcbc2;margin:20px}table{width:100%%;line-height:160%%;font-size:12px}th{background:#2f3030;color:#c4b550;text-align:left}.q{border:2px solid #4a4945}tr{background:#4a4945}.b{background:#3b3b37}</STYLE>")
	for (new i = 0;i < playersFound;i++) 
	{
		new bool:steam = false
		if (is_user_steam(id))
			steam = true
		new bool:bot = false
		if (is_user_bot(id))
			bot = true
		get_user_name(players[i],name,39)
		get_user_ip(players[i],ip,29,1)
		get_user_authid(players[i], steamid, 31)
		num_to_str(get_user_time(players[i]),str_pt,5)
		get_user_ping(players[i],ping,loss)
		num_to_str(ping,str_pi,5)
		num_to_str(loss,str_lo,5)
 
		pos += format(message[pos],2048 - pos,"%d.<font color=^"red^">%s</font> <font color=^"green^">%s</font>%s %s%s",i+1,name,steam?"[STEAM]":"",bot?"[BOT]":"",ip_showed?"IP:":"",ip_showed?ip:"")
		pos += format(message[pos],2048 - pos," %s%s %s%s",steamid_showed?"STEAMID:":"",steamid_showed?steamid:"",time_showed?"TIME:":"",time_showed?str_pt:"")
		pos += format(message[pos],2048 - pos," %s%s %s%s^n",ping_showed?"PING":"",ping_showed?str_pi:"",loss_showed?"LOSS:":"",loss_showed?str_lo:"")
	}
	show_motd(id,message,"STEAMOS Jatekosok")
	return PLUGIN_HANDLED
}
public hook_say(id)
{
	read_args (message, 191)
	remove_quotes (message)
 
	if (message[0] == '@' || message[0] == '/' || message[0] == '!' || equal (message, "")) 
 
		return PLUGIN_CONTINUE
 
 
	new name[32]
	get_user_name (id, name, 31)
 
	new bool:steam = false
 
	if (is_user_steam(id))
		steam = true
 
 
	new isAlive
 
	if (is_user_alive (id))
		{
			isAlive = 1
			alive = "^x01"
		}
	else
		{
			isAlive = 0
			alive = "^x01"
		}
 
	static color[10]
 
 
 
	if (steam)
		{
			switch (get_pcvar_num (g_NameColor))
				{
					case 1:
						format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
 
					case 2:
						format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
 
					case 3:
						{
							color = "SPECTATOR"
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
 
					case 4:
						{
							color = "CT"
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
 
					case 5:
						{
							color = "TERRORIST"
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
 
					case 6:
						{
							get_user_team (id, color, 9)
 
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
				}
 
			switch (get_pcvar_num (g_MessageColor))
				{
					case 1:	// Yellow
						format (strText, 191, "%s", message)
 
					case 2:	// Green
						format (strText, 191, "^x04%s", message)
 
					case 3:	// White
						{
							copy (color, 9, "SPECTATOR")
							format (strText, 191, "^x03%s", message)
						}
 
					case 4:	// Blue
						{
							copy (color, 9, "CT")
							format (strText, 191, "^x03%s", message)
						}
 
					case 5:	// Red
						{
							copy (color, 9, "TERRORIST")
							format (strText, 191, "^x03%s", message)
						}
				}
		}
 
	else
		{
			get_user_team (id, color, 9)
 
			format (strName, 191, "%s^x03%s", alive, name)
 
			format (strText, 191, "%s", message)
		}
 
	format (message, 191, "%s^x01: %s", strName, strText)
 
	sendMessage (color, isAlive)
 
	return PLUGIN_CONTINUE
}
 
 
public hook_teamsay(id)
{
	new playerTeam = get_user_team(id)
	new playerTeamName[19]
 
	switch (playerTeam)
		{
			case 1:
				copy (playerTeamName, 11, "Terrorists")
 
			case 2:
				copy (playerTeamName, 18, "Counter-Terrorists")
 
			default:
				copy (playerTeamName, 9, "Spectator")
		}
 
	read_args (message, 191)
	remove_quotes (message)
 
	if (message[0] == '@' || message[0] == '/' || message[0] == '!' || equal (message, ""))  
 
		return PLUGIN_CONTINUE
 
 
	new name[32]
	get_user_name (id, name, 31)
 
	new bool:steam = false
 
	if (is_user_steam(id))
		steam = true
 
 
	new isAlive
 
	if (is_user_alive (id))
		{
			isAlive = 1
			alive = "^x01"
		}
	else
		{
			isAlive = 0
			alive = "^x01"
		}
 
	static color[10]
 
 
 
	if (steam)
		{
			switch (get_pcvar_num (g_NameColor))
				{
					case 1:
						format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
 
					case 2:
						format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
 
					case 3:
						{
							color = "SPECTATOR"
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
 
					case 4:
						{
							color = "CT"
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
 
					case 5:
						{
							color = "TERRORIST"
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
 
					case 6:
						{
							get_user_team (id, color, 9)
 
							format (strName, 191, "%s^x03%s ^x04[STEAM]", alive, name)
						}
				}
 
			switch (get_pcvar_num (g_MessageColor))
				{
					case 1:	// Yellow
						format (strText, 191, "%s", message)
 
					case 2:	// Green
						format (strText, 191, "^x04%s", message)
 
					case 3:	// White
						{
							copy (color, 9, "SPECTATOR")
							format (strText, 191, "^x03%s", message)
						}
 
					case 4:	// Blue
						{
							copy (color, 9, "CT")
							format (strText, 191, "^x03%s", message)
						}
 
					case 5:	// Red
						{
							copy (color, 9, "TERRORIST")
							format (strText, 191, "^x03%s", message)
						}
				}
		}
 
	else
		{
			get_user_team (id, color, 9)
 
			format (strName, 191, "%s^x03%s", alive, name)
 
			format (strText, 191, "%s", message)
		}
 
	format (message, 191, "%s^x01: %s", strName, strText)
 
	sendTeamMessage (color, isAlive, playerTeam)
 
	return PLUGIN_CONTINUE	
}
 
 
public set_color (id, level, cid)
{
	if (!cmd_access(id, level, cid, 2))
		return PLUGIN_HANDLED
 
	new arg[1], newColor
	read_argv (1, arg, 1)
 
	newColor = str_to_num (arg)
 
	if (newColor >= 1 && newColor <= 5)
		{
			set_cvar_num ("amx_color", newColor)
			set_pcvar_num (g_MessageColor, newColor)
 
			if (get_pcvar_num (g_NameColor) != 1 &&
			       ((newColor == 3 &&  get_pcvar_num (g_NameColor) != 3)
			     || (newColor == 4 &&  get_pcvar_num (g_NameColor) != 4)
			     || (newColor == 5 &&  get_pcvar_num (g_NameColor) != 5)))
				{
					set_cvar_num ("amx_namecolor", 2)
					set_pcvar_num (g_NameColor, 2)
				}
		}
 
	return PLUGIN_HANDLED
}
 
 
public set_name_color (id, level, cid)
{
	if (!cmd_access(id, level, cid, 2))
		return PLUGIN_HANDLED
 
	new arg[1], newColor
	read_argv (1, arg, 1)
 
	newColor = str_to_num (arg)
 
	if (newColor >= 1 && newColor <= 6)
		{
			set_cvar_num ("amx_namecolor", newColor)
			set_pcvar_num (g_NameColor, newColor)
 
			if ((get_pcvar_num (g_MessageColor) != 1
			    && ((newColor == 3 &&  get_pcvar_num (g_MessageColor) != 3)
			     || (newColor == 4 &&  get_pcvar_num (g_MessageColor) != 4)
			     || (newColor == 5 &&  get_pcvar_num (g_MessageColor) != 5)))
			     || get_pcvar_num (g_NameColor) == 6)
				{
					set_cvar_num ("amx_color", 2)
					set_pcvar_num (g_MessageColor, 2)
				}
		}
 
	return PLUGIN_HANDLED
}
 
 
public set_listen (id, level, cid)
{
	if (!cmd_access(id, level, cid, 2))
		return PLUGIN_HANDLED
 
	new arg[1], newListen
	read_argv(1, arg, 1)
 
	newListen = str_to_num (arg)
 
	set_cvar_num ("amx_listen", newListen)
	set_pcvar_num (g_AdminListen, newListen)
 
	return PLUGIN_HANDLED
}
 
 
public sendMessage (color[], alive)
{
	new teamName[10]
 
	for (new player = 1; player < maxPlayers; player++)
		{
			if (!is_user_connected(player))
				continue
 
			if (alive && is_user_alive(player) || !alive && !is_user_alive(player) || get_pcvar_num(g_AdminListen) && get_user_flags(player) & ADMIN_LISTEN)
				{
					get_user_team (player, teamName, 9)
 
					changeTeamInfo (player, color)
 
					writeMessage (player, message)
 
					changeTeamInfo (player, teamName)
				}
		}
}
 
 
public sendTeamMessage (color[], alive, playerTeam)
{
	new teamName[10]
 
	for (new player = 1; player < maxPlayers; player++)
		{
			if (!is_user_connected(player))
				continue
 
			if (get_user_team(player) == playerTeam || get_pcvar_num(g_AdminListen) && get_user_flags(player) & ADMIN_LISTEN)
				{
					if (alive && is_user_alive(player) || !alive && !is_user_alive(player) || get_pcvar_num(g_AdminListen) && get_user_flags(player) & ADMIN_LISTEN)
						{
							get_user_team (player, teamName, 9)
 
							changeTeamInfo (player, color)
 
							writeMessage (player, message)
 
							changeTeamInfo (player, teamName)
						}
				}
		}
}
 
 
public changeTeamInfo (player, team[])
{
	message_begin (MSG_ONE, teamInfo, _, player)
	write_byte (player)
	write_string (team)
	message_end()
}
 
 
public writeMessage (player, message[])
{
	message_begin (MSG_ONE, sayText, {0, 0, 0}, player)
	write_byte (player)
	write_string (message)
	message_end ()
}
stock bool:is_user_steam(id)
{
	new authid[32]
	get_user_authid(id, authid, 31)
	new hossz = strlen(authid)
 
	if(!(equali(authid, "STEAM_ID_PENDING") ||
	equali(authid, "STEAM_ID_LAN") ||
	equali(authid, "HLTV") ||
	equali(authid, "4294967295") ||
	equali(authid, "VALVE_ID_LAN") ||
	equali(authid, "VALVE_ID_PENDING")) && hossz <= 18)
	{
		return true
	}
	else
	{
		return false
	}
	return false
}