#include <amxmodx>
#include <amxmisc>
#include <cstrike>

#define xCS_TEAM_CT 2
#define xCS_TEAM_T 1
new g_transfer[33]

public plugin_init()
{
   register_plugin("Advanced Team Transfer","1.62","Genesis")

   register_concmd("amx_transfer","transfer_player",ADMIN_SLAY,"<transfer player to opp team or use @ to swap teams>")
   register_concmd("amx_mtransfer","massive_transfer",ADMIN_LEVEL_B,"<swap teams>")
   register_event("DeathMsg","ACS_eventDeath","a")

   register_event("SendAudio", "endr", "a", "2&%!MRAD_terwin")
   register_event("SendAudio", "endr", "a", "2&%!MRAD_ctwin")
}

public plugin_precache()
{
	//precache_sound("rad/transfer2.wav")
}

public client_dissconnect(id)
{
	g_transfer[id] = 0
}

public endr()
{
	new y32[32], x
	get_players(y32,x,"c") 
	for(new a = 0; a < x; a++)
	{
		if(g_transfer[y32[a]] == 1)
		{
	
			if (get_user_team (y32[a])==xCS_TEAM_CT)
			{
				cs_set_user_team (y32[a], CS_TEAM_T)
			}
			else if(get_user_team (y32[a])==xCS_TEAM_T)
			{
				cs_set_user_team (y32[a], CS_TEAM_CT)
			}
			//client_cmd(y32[a], "spk rad/transfer2.wav")
			g_transfer[y32[a]] = 0
			new name2[32]
			get_user_name(y32[a], name2, 31)

			client_print(0,print_chat,"%s is now being transferred", name2)
		}
	}
	return PLUGIN_CONTINUE
}

public transfer_player(id,level,cid)
{
	if (!cmd_access(id,level,cid,1))
    		return PLUGIN_HANDLED
	
	
	new target, target2, target3
	new arg[32], arg2[32], arg3[32]
	read_argv(1,arg,31)
	read_argv(2,arg2,31)
	read_argv(3,arg3,31)

	target = cmd_target(id,arg,3)
	target2 = cmd_target(id,arg2,3)
	target3 = cmd_target(id,arg3,3)

	
	if(target != 0)
	{
		if(is_user_alive(target))
		{
			g_transfer[target] = 1
			new name[32], name2[32]
			get_user_name(id, name2, 31)
			get_user_name(target, name, 31)
			client_print(0,print_chat,"ADMIN %s: has enqueued %s for transfer", name2, name)
		}
		else
		{
			if (get_user_team(target)==xCS_TEAM_CT)
			{
				cs_set_user_team(target, CS_TEAM_T)
			}
			else if(get_user_team(target)==xCS_TEAM_T)
			{
				cs_set_user_team(target,CS_TEAM_CT)
			}
			//client_cmd(target, "spk rad/transfer2.wav")
			new name[32], name2[32], authid[32], authid2[32]
			get_user_authid(id,authid,31)
  			get_user_name(id,name,31)
  			get_user_authid(target,authid2,31)
  			get_user_name(target,name2,31)
  			log_amx("Cmd: ^"%s<%d><%s><>^" transferred ^"%s<%d><%s><>^"",name,get_user_userid(id),authid,name2,get_user_userid(target),authid2 )
    
  			switch(get_cvar_num("amx_show_activity")) 
			{
  				case 2: client_print(0,print_chat,"ADMIN %s: transferred %s",name,name2)
  				case 1: client_print(0,print_chat,"ADMIN: transferred %s",name2)
  			}       
      
  			console_print(id,"[AMXX] Client ^"%s^" transferred",name2)  
		}
	}

	target = target2

	if(target != 0)
	{
		if(is_user_alive(target))
		{
			g_transfer[target] = 1
		}
		else
		{
			if (get_user_team(target)==xCS_TEAM_CT)
			{
				cs_set_user_team(target, CS_TEAM_T)
			}
			else if(get_user_team(target)==xCS_TEAM_T)
			{
				cs_set_user_team(target,CS_TEAM_CT)
			}
			//client_cmd(target, "spk rad/transfer2.wav")
			new name[32], name2[32], authid[32], authid2[32]
			get_user_authid(id,authid,31)
  			get_user_name(id,name,31)
  			get_user_authid(target,authid2,31)
  			get_user_name(target,name2,31)
  			log_amx("Cmd: ^"%s<%d><%s><>^" transferred ^"%s<%d><%s><>^"",name,get_user_userid(id),authid,name2,get_user_userid(target),authid2 )
    
  			switch(get_cvar_num("amx_show_activity")) 
			{
  				case 2: client_print(0,print_chat,"ADMIN %s: transferred %s",name,name2)
  				case 1: client_print(0,print_chat,"ADMIN: transferred %s",name2)
  			}       
      
  			console_print(id,"[AMXX] Client ^"%s^" transferred",name2)  
		}
	}
	target = target3

	
	if(target != 0)
	{
		if(is_user_alive(target))
		{
			g_transfer[target] = 1
		}
		else
		{
			if (get_user_team(target)==xCS_TEAM_CT)
			{
				cs_set_user_team(target, CS_TEAM_T)
			}
			else if(get_user_team(target)==xCS_TEAM_T)
			{
				cs_set_user_team(target,CS_TEAM_CT)
			}
			//client_cmd(target, "spk rad/transfer2.wav")
			new name[32], name2[32], authid[32], authid2[32]
			get_user_authid(id,authid,31)
  			get_user_name(id,name,31)
  			get_user_authid(target,authid2,31)
  			get_user_name(target,name2,31)
  			log_amx("Cmd: ^"%s<%d><%s><>^" transferred ^"%s<%d><%s><>^"",name,get_user_userid(id),authid,name2,get_user_userid(target),authid2 )
    
  			switch(get_cvar_num("amx_show_activity")) 
			{
  				case 2: client_print(0,print_chat,"ADMIN %s: transferred %s",name,name2)
  				case 1: client_print(0,print_chat,"ADMIN: transferred %s",name2)
  			}       
      
  			console_print(id,"[AMXX] Client ^"%s^" transferred",name2)  
		}
	}
	return PLUGIN_CONTINUE
}

public massive_transfer(id,level,cid)
{
		if (!cmd_access(id,level,cid,0))
    		return PLUGIN_HANDLED
		
		new y32[32], x
		get_players(y32,x,"c") 
		if (x==0){ 
			//console_print(id,"No clients in such team") 
			return PLUGIN_HANDLED 
		} 
		for(new a=0;a<x;++a) {
			if (get_user_team (y32[a])==xCS_TEAM_CT)
			{
				cs_set_user_team (y32[a], CS_TEAM_T)
			}
			else if(get_user_team (y32[a])==xCS_TEAM_T)
			{
				cs_set_user_team (y32[a], CS_TEAM_CT)
			}
		}
		//client_cmd(0, "spk rad/transfer2.wav")
		new name[32], authid[32]
		get_user_authid(id,authid,31)
  		get_user_name(id,name,31)
  		
		log_amx("Cmd: ^"%s<%d><%s><>^" used massive transfer ^"<>^"",name,get_user_userid(id),authid)
    
  		switch(get_cvar_num("amx_show_activity")) 
		{
  			case 2: client_print(0,print_chat,"ADMIN %s: transferred all players to opposing teams", name)
  			case 1: client_print(0,print_chat,"ADMIN: transferred all players to opposing teams")
  		}      
		return PLUGIN_HANDLED
}

public ACS_eventDeath()
{
	new theVictim = read_data(2)

	if(g_transfer[theVictim] == 1)
	{
		g_transfer[theVictim] = 0
		if(get_user_team(theVictim)==xCS_TEAM_CT)
		{
			cs_set_user_team (theVictim, CS_TEAM_T)
		}
		else if(get_user_team (theVictim)==xCS_TEAM_T)
		{
			cs_set_user_team (theVictim, CS_TEAM_CT)
		}
		//client_cmd(theVictim, "spk rad/transfer2.wav")
	}
}