/* 

Clan Try Outs Version 1.2 (c) Zenith77 2005

-=-=-=-=-=

This plugin will allow you to try out people.

Just simply type amx_tryout and a menu will appear asking them if they want to 
try out. If they choose yes a ' |TryOut| ' tag will be added to there name.
and Scores will be logged to the sever.

Note: Admins will not be able to see the menu or people already trying out or
people who have the clan tag on!

-=-=-=Commands=-=-=-

amx_tryout <target> -- begins a try out for a target!
amx_end <target> -- ends a try out for target!
amx_tryout_all -- begins a try out for everybody
amx_end_all -- ends a try out for everybody
amx_tryout_time <target> <time (minutes) > -- sets a timed tryout!

amx_tryout_clear -- clears server of people that are not trying out, people who have the clan 
tag on,or admins will not be kicked!

amx_accept <target> -- automaticaly adds your clan tag to the target.
The clan tag is adjustable by the cvar amx_clan_tag.

-=-=-=-=-=-=-

-=-=-=CVARS=-=-

amx_clan_tag "[YourTagHere]" -- the tag will be automatically added when you
" accept " somebody into the clan with amx_accept <target>

-=-=-=-=-=-

-=-=-Notes=-=-

Requested by angel killer

-=-=-=-=-
-=-=-=-=Versions=-=-=-=-

V1.0 -- Realese
V1.1 -- made it to where people with the clan are not shown try out menu
V1.2 -- added timed tryout & abitly to clear the serve of all players that are not trying out.. admins and peopl
 with the clan tag on are excluded
*/

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Clan Try Outs"
#define VERSION "1.2"
#define AUTHOR "Zenith77"
#define TRYOUT_TYPE_NULL 0
#define TRYOUT_TYPE_NOTIME 1
#define TRYOUT_TYPE_TIME 2

#define TASK_TRYOUT 5457
#define KeysTryOutMenu MENU_KEY_1|MENU_KEY_2

/*
#define ACTION_TRYOUT 1
#define ACTION_ACCEPT 2

#define MENU_SIZE    256
#define MENU_PLAYERS 7
*/
new bool:isTryingOut[33]
new Name[33][32] // frist dimension is index second dimension stores the name
new tryoutType[33]
new tryoutTime[33]

/*
new Action[33]
new g_iMenuPosition
new g_iMenuPlayers[32] 
*/
public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	
	register_cvar("amx_clan_tag", "[YourTagHere]") // clan tag
	//register_cvar("amx_tryout_pw", "MyPW")  //password to the server when
	//register_cvar("amx_tryout_kick", "1" )
	
	register_concmd("amx_tryout", "beginTryOut1", ADMIN_RCON, "<target> begins a try out for target!" )
	register_concmd("amx_end_tryout", "endTryOut1", ADMIN_RCON, "<target> ends try out for target!" )
	register_concmd("amx_tryout_all", "beginTryOut",ADMIN_RCON ,"begins a try out for everybody!" )
	register_concmd("amx_end_tryout_all", "endTryout", ADMIN_RCON, "ends a try out for everybody!")
	register_concmd("amx_tryout_time", "beginTimedTryOut", ADMIN_RCON, "<target> <time (minutes) > begins a timed try out for target!" )
	register_concmd("amx_tryout_clear", "clearServer", ADMIN_RCON, "clears server of people who are not tryout!" )
	register_concmd("amx_accept", "accept", ADMIN_RCON, "<target> accepts user into clan!")
	
	/*
	register_clcmd("amx_tryout_menu", "tryoutMenu", ADMIN_RCON, "shows a list of players that you can select for a try out!" )
	*/
	register_menucmd(register_menuid("tryout"), 1023, "PressedTryOutMenu")
	register_menucmd( register_menuid("\rPlayer Menu:"), 1023, "PressedtryoutSelMenu" ) 
	
	register_logevent("Round_Start", 2, "0=World triggered", "1=Round_Start")  
	
}

public client_connect(id) {
	
	isTryingOut[id] = false
	
	tryoutType[id] = TRYOUT_TYPE_NULL
	
	tryoutTime[id] = 0
	
	get_user_name(id, Name[id], 31)
	
	
}

public client_disconnect(id) {
	
	isTryingOut[id] = false
	
	if( tryoutType[id] == TRYOUT_TYPE_TIME ) {
		
		remove_task(TASK_TRYOUT+id)
		
	}
	
	
}

public ShowTryOutMenu(id) {
	
	new menutext[512]
	new clanName[32]
	
	get_cvar_string("amx_clan_tag", clanName, 31)
	format(menutext, 511, "\rWould you like to try out for %s ?^n \y1.Yes^n \w2.No", clanName)
	show_menu(id,KeysTryOutMenu, menutext, 15, "tryout") // Display menu
	
}

public PressedTryOutMenu(id, key) {
	/* Menu:
	* Would you like to try out for the clan?
	* 
	* 1. Yes
	* 2. No
	*/
	
	switch(key) {
		case 0: {
			
			new clanName[32]
			
			get_cvar_string("amx_clan_tag", clanName, 31)
			
			get_user_name(id, Name[id],31)
			
			if( tryoutType[id] == TRYOUT_TYPE_TIME ) {
				
				set_task(float(tryoutTime[id]), "stopTryout", TASK_TRYOUT+id)
				
				tryoutTime[id] /= 60 // seconds to minutes
				
				client_print(id, print_chat, "[AMXX] You are now in a timed tyrout for %s! [ TIME: %d minute(s) ]", clanName, tryoutTime[id]  )
			
				client_print(0, print_chat, "[AMXX] %s is now trying out for %s!", Name[id], clanName )
				

				
			}
			
			else { 
				client_print(id, print_chat, "[AMXX] You are now trying out for %s!", clanName )
			
				client_print(0, print_chat, "[AMXX] %s is now trying out for %s!", Name[id], clanName )
			}
			
			client_cmd(id, "name ^"|TryOut| %s^"", Name[id])
			
			isTryingOut[id] = true
			
			
			
		}
		
		case 1: {
			
			client_print(id, print_chat, "[AMXX] You have declined Try Out Status, thank you anyway !" )
			
			tryoutType[id] = TRYOUT_TYPE_NULL
		}
	}
} 
public beginTryOut1(id, level, cid) {
	
	if( !cmd_access(id, level, cid, 2) ) return PLUGIN_HANDLED
	
	new arg[32]
	new target
	new name[32]
	
	read_argv(id, arg, 31)
	
	target = cmd_target(id, arg, 2)
	
	get_user_name(target, name, 31)
	
	if( isTryingOut[target] ) {
		
		client_print(id, print_console, "[AMXX] %s is already trying out!", name)
		
		return PLUGIN_HANDLED
	}
	
	client_print(id, print_console, "[AMXX] You have requested %s to try out!", name )
	
	ShowTryOutMenu(target)
	
	return PLUGIN_HANDLED
}

public endTryOut1(id, level ,cid) {
	
	if( !cmd_access(id, level, cid, 2) ) return PLUGIN_HANDLED
	
	new arg[32]
	new target
	
	read_argv(id, arg, 31)
	
	target = cmd_target(id, arg, 2)
	
	if( !isTryingOut[target] ) {
		
		client_print(id, print_console, "[AMXX] %s is not trying out at this moment!", Name[target] )
		
		return PLUGIN_HANDLED
	}
	
	client_cmd(id, "name ^"%s^" ", Name[target] )
	
	isTryingOut[target] = false
	
	set_hudmessage(255, 255, 0, -1.0, -1.0, 2, 6.0, 12.0)
	show_hudmessage(0, "[AMXX] Your Try Out is now over!")
	
	return PLUGIN_HANDLED
}


public beginTryOut(id, level, cid) {
	
	if( !cmd_access(id, level, cid, 1) ) return PLUGIN_HANDLED
	
	set_hudmessage(255, 255, 0, -1.0, -1.0, 2, 6.0, 12.0)
	show_hudmessage(0, "[AMXX] Tryouts will now begin !")
	
	client_print(0, print_chat, "[AMXX] Tryouts will now begin !")
	
	new i
	
	new clanName[20]
	
	get_cvar_string("amx_clan_tag", clanName, 19)
	
	for( i = 1; i <get_maxplayers(); i++ ) {
		
		if( !is_user_connected(i) ) continue
		
		if( is_user_admin(i) ) continue
		
		if( isTryingOut[i] ) continue
		
		if( containi(Name[i], clanName) != -1 ) continue
		
		ShowTryOutMenu(i)
	}
	
	return PLUGIN_HANDLED
}

public endTryout(id, level, cid) {
	
	if( !cmd_access(id, level, cid, 1) ) return PLUGIN_HANDLED
	
	set_hudmessage(255, 255, 0, -1.0, -1.0, 2, 6.0, 12.0)
	show_hudmessage(0, "[AMXX] TryOuts have ended!")
	
	client_print(0, print_console, "[AMXX] Try Outs have ended!" )
	
	new i
	
	for( i = 1; i <get_maxplayers(); i++ ) {
		
		if( !is_user_connected(i) ) continue
		
		isTryingOut[i] = false
		
		client_cmd(i, "name ^"%s^"",Name[i])
	}
	
	return PLUGIN_HANDLED
}

public beginTimedTryOut(id, level, cid) {
	
	if( !cmd_access(id, level, cid, 3)  ) return PLUGIN_HANDLED
	
	new arg1[32], arg2[32] /* target, time */
	new target
	new szTargetName[32]
	
	
	read_argv(1, arg1, 31)
	read_argv(2, arg2, 31)
	
	target = cmd_target(id, arg1, 2)
	
	get_user_name(target, szTargetName, 31)
	
	if( isTryingOut[target] ) {
		
		client_print(id, print_console, "[AMXX] %s is already trying out!", szTargetName)
		
		return PLUGIN_HANDLED
	}
	
	tryoutTime[target] = str_to_num(arg2)
	
	client_print(id, print_console, "[AMXX] You have requested %s to a timed tryout! [ TIME: %d minute(s) ]", szTargetName, tryoutTime[target] )
	
	tryoutTime[target] *= 60 // convert minutes to seconds ...
	
	
	tryoutType[target] = TRYOUT_TYPE_TIME
	
	ShowTryOutMenu(target)
	
	return PLUGIN_HANDLED
	
}

public clearServer(id, level, cid) {
	
	if( !cmd_access(id, level, cid, 1) ) return PLUGIN_HANDLED
	
	new i
	
	new clanName[20]
	
	get_cvar_string("amx_clan_tag", clanName, 19)
	
	for( i = 1; i < get_maxplayers(); i++ ) {
		
		if( is_user_admin(i) ) continue
		
		if( isTryingOut[id] ) continue // fail-safe
		
		if( containi(Name[i], clanName) != -1 ) continue
		
		new iName[32]
		
		get_user_name(i, iName, 31)
		
		server_cmd("kick ^"%s^" ^"[AMXX] A tryout is currently in progress!^" ", iName)
		
	}
	
	
	return PLUGIN_HANDLED
	
}
		

public accept(id, level, cid) {
	
	if( !cmd_access(id, level, cid, 2) ) return PLUGIN_HANDLED
	
	new arg[32]
	new authid[32]
	new target
	new clanName[32]
	
	read_argv(id, arg, 31)
	
	target = cmd_target(id, arg, 2)
	
	isTryingOut[target] = false
	
	get_cvar_string("amx_clan_tag", clanName, 31)
	
	client_cmd(target, "name ^"%s %s^" ", clanName, Name[target])
	
	client_print(target, print_chat, "[AMXX] %s has allowed you into the clan!", Name[id] )
	
	get_user_authid(id, authid, 31)
	
	log_amx("<%s><><%s><> allowed %s to come into the clan !", Name[id], authid, Name[target])
	
	return PLUGIN_HANDLED
}
/*
public AcceptTarget(id, target) {
	
	new authid[32]
	new clanName[32]
	
	isTryingOut[target] = false
	
	get_cvar_string("amx_clan_tag", clanName, 31)
	
	client_cmd(target, "name ^"%s %s^" ", clanName, Name[target])
	
	client_print(target, print_chat, "[AMXX] %s has allowed you into the clan!", Name[id] )
	
	get_user_authid(id, authid, 31)
	
	log_amx("<%s><><%s><> allowed %s to come into the clan !", Name[id], authid, Name[target])
	
	return PLUGIN_HANDLED
}
*/
public stopTryout(id) {
	
	id -= TASK_TRYOUT
	
	set_hudmessage(255, 255, 0, -1.0, -1.0, 0, 6.0, 12.0)
	show_hudmessage(id, "[AMXX] Your tryout is now over!")
	
	client_cmd(id, "name ^"%s^" ", Name[id] )
	
	new i 
	
	for( i = 1; i < get_maxplayers(); i++ ) {
		
		if( is_user_admin(i) ) {
			
			client_print(i, print_chat, "[AMXX] %s's tryout is over!", Name[id] )
			
		}
	}
	
	tryoutType[id] = TRYOUT_TYPE_NULL
	
	isTryingOut[id] = false
	
}
	
	
	

public Round_Start() {
	
	server_print("======Tryout Stats=======")
	server_print("")
	
	new i
	
	for( i = 1; i <get_maxplayers(); i++) {
		
		if( !is_user_connected(i) ) continue
		
		//if( is_user_admin(i) ) showStats(i)
		
		if( isTryingOut[i] ) {
			
			new name[32]
			new authid[32]
			new frags
			new deaths
			
			get_user_name(i, name, 31)
			get_user_authid(i, authid, 31)
			
			frags = get_user_frags(i)
			deaths = get_user_deaths(i)
			
			log_amx("<%s><><%s><> -- FRAGS: %d DEATHS: %d", name, authid, frags, deaths)
		}
	}
	server_print("")
	server_print("======END======")
}
/*
public showStats(id) {
	
	client_print(id, print_console, "=======TRYOUT STATS========" )
	client_print(id, print_console, " " )
	
	
	for( i = 1; i <get_maxplayers(); i++) {
		
		if( !is_user_connected(i) ) continue
		
		if( isTryingOut[i] ) {
			
			new name[32]
			new authid[32]
			new frags
			new deaths
			
			get_user_name(i, name, 31)
			get_user_authid(i, authid, 31)
			
			frags = get_user_frags(i)
			deaths = get_user_deaths(i)
			
			("<%s><><%s><> -- FRAGS: %d DEATHS: %d", name, authid, frags, deaths)
		}
	}
	
	client_print(id, print_console, " " )
	client_print(id, print_console, "=======END========" )
	
}
*/

/*
public tryoutMenu(id, level ,cid) {
	
	if( !cmd_access(id, level, cid, 1) ) return PLUGIN_HANDLED
	
	
	ShowtryoutSelMenu(id, g_iMenuPosition = 0)
	
	return PLUGIN_HANDLED
}

public ShowtryoutSelMenu( id, pos ) {
	
	if( pos < 0 ) return
	
	new i, j
	new szMenuBody[MENU_SIZE]
	new iCurrKey = 0
	new szUserName[32]
	new iStart = pos * MENU_PLAYERS
	new iNum
	
	get_players( g_iMenuPlayers, iNum )
	
	if( iStart >= iNum )
		iStart = pos = g_iMenuPosition = 0
	
	new iLen = format( szMenuBody, MENU_SIZE-1, "\rTryout | Accept Menu \R%d/%d^n\w^n", pos+1, (iNum / MENU_PLAYERS + ((iNum % MENU_PLAYERS) ? 1 : 0 )) )
	new iEnd = iStart + MENU_PLAYERS
	new iKeys = (1<<9|1<<7)
	
	if( iEnd > iNum )
		iEnd = iNum
	
	for( i = iStart; i < iEnd; i++ )
		{
		j = g_iMenuPlayers[i]
		get_user_name( j, szUserName, 31 )
		
		if( is_user_admin(j) )
			{
			iCurrKey++
			iLen += format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "\d%d. %s^n\w", iCurrKey, szUserName )
		}else
		{
			iKeys |= (1<<iCurrKey++)
			iLen += format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "%d. %s^n", iCurrKey, szUserName )
		}
	}
	
	switch(Action[id]) {
		
		case 1: {
			iKeys |= (1<<iCurrKey++)
			
			iLen += format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "^n8. Tryout")
		}
		case 2: {
			
			iKeys |= (1<<iCurrKey++)
		
			format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "^n8. Accept")
		}
		
		default: {
			
			Action[id] = ACTION_TRYOUT
			
			iKeys |= (1<<iCurrKey++)
			
			iLen += format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "^n8. Tryout")
		}
	}
	
	if( iEnd != iNum )
		{
		format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "^n9. More...^n0. %s", pos ? "Back" : "Exit" )
		iKeys |= (1<<8)
	}
	else
	format( szMenuBody[iLen], (MENU_SIZE-1-iLen), "^n0. %s", pos ? "Back" : "Exit" )
	
	show_menu( id, iKeys, szMenuBody, -1 )
	
	return
}

public PressedtryoutSelMenu( id, key ) {
	
	switch( key ) {
		
		
		case 8: ShowtryoutSelMenu( id, ++g_iMenuPosition ) // More Option
		case 9: ShowtryoutSelMenu( id, --g_iMenuPosition ) // Back Option
		
		case 7: {
			client_print(id, print_chat, "***CASE 7 ACTIVATED! ********" )
			switch(Action[id]) {
				
				case 1: {
					
					Action[id] = ACTION_ACCEPT
					
					ShowtryoutSelMenu(id, g_iMenuPosition)
				}
				
				case 2: {
					
					Action[id] = ACTION_TRYOUT
					
					ShowtryoutSelMenu(id, g_iMenuPosition)
				}
			}
		}
		
		// Chose a Player
		default:
		{
			new iPlayerID = g_iMenuPlayers[g_iMenuPosition * MENU_PLAYERS + key]
			
			
			switch(Action[id]) {
				
				case 1: ShowTryOutMenu(iPlayerID)
				case 2: AcceptTarget(id, iPlayerID)
			}
			
		}
	}
	return PLUGIN_HANDLED
}  
*/
