/* Plugin generated by AMXX-Studio */
/*
*
* v. 1.1 fixed exploit. you can no longer extend the map by a negative number.
* v. 1.0 initial release
*
*
*/
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Map Extender"
#define VERSION "1.1"
#define AUTHOR "jsauce"
//#define DEBUG		// For debugging only.
#define MAX_MAPS 32	// Change this if you have more than 32 maps in mapcycle.
#define MAPCYCLE	// Check mapcycle to prevent non mapcycle files from being extended.
#define EXTENDMAX 3	// Maximum number of times a map may be extended by anyone.
#define EXTENDTIME 15	// Maximum amount of time any map can be extended at once.

new user_limit = 0	
new bool:cyclerfile
new gNum = 0
new gMap[64] 

public plugin_init()
{
	register_plugin(PLUGIN,VERSION,AUTHOR)
	register_concmd("amx_extend","admin_extend",ADMIN_LEVEL_A,"<added time to extend> : ex. 5, if you want to extend it five more minutes.")
	#if defined DEBUG
	register_concmd("amx_user_limit","extend_user_limit",ADMIN_RCON,"Extend user_limit for debugging only.")
	#endif
	#if defined MAPCYCLE
	new map[64], i, line = 0
	get_mapname(map,63)
	#if defined DEBUG
	new newtext[256]
	format(newtext,255,"DEBUG: mapname %s",map)
	write_file("debugger.txt",newtext,-1)
	#endif
  	while ( gNum < MAX_MAPS && read_file("mapcycle.txt",line++,gMap,63,i) )
	{
		#if defined DEBUG
		new newtext[256]
		format(newtext,255,"DEBUG: gNum is %i line is %i gMap is %s",gNum,line,gMap)
		write_file("debugger.txt",newtext,-1)
		#endif
		if ( gMap[0] == ';' ) continue

		if (equali(gMap,map))
		{
			cyclerfile = true
			break
		}
		++gNum
	}
	#endif
	return PLUGIN_CONTINUE
} 

public admin_extend(id,level,cid)
{
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED
	
	new arg[32],name[32]
	read_argv(1,arg,31)
	get_user_name(id,name,31)
	#if defined MAPCYCLE
	#if defined DEBUG
	client_print(id,print_chat,"DEBUG: CyclerFile is %s",cyclerfile)
	#endif
	if (!cyclerfile)
	{
		client_print(id,print_chat,"[AMXX] Maps that are not in the mapcycle cannot be extended.")
		return PLUGIN_HANDLED
	}
	#endif
	if (arg[0])
	{
		if(containi(arg,"-") != -1)
		{
			#if defined DEBUG
			client_print(id,print_chat,"DEBUG: tlimit is neg at %i",arg)
			#endif
			client_print(id,print_chat,"[AMXX] You cannot extend the maptime by a negative number.")
			return PLUGIN_HANDLED
		}
		new tlimit = str_to_num(arg)
		#if defined DEBUG
		client_print(id,print_chat,"DEBUG: tlimit is %i user_limit is %i",tlimit,user_limit)
		#endif
		if (user_limit >= EXTENDMAX)
		{
			#if defined DEBUG
			client_print(id,print_chat,"DEBUG: Script has reached user_limit max of %i",user_limit)
			#endif
			client_print(id,print_chat,"[AMXX] No user may extend any map more than %i times.",EXTENDMAX)
			return PLUGIN_HANDLED
		}
		if (tlimit > EXTENDTIME)
		{
			#if defined DEBUG
			client_print(id,print_chat,"DEBUG: Script has reached tlimit max of %i",tlimit)
			#endif
			client_print(id,print_chat,"[AMXX] No map may be extended longer than %i minutes at a time.",EXTENDTIME)
			tlimit = EXTENDTIME
		}
		#if defined DEBUG
		client_print(id,print_chat,"DEBUG: Script has reached timelimit change of %i",tlimit)
		#endif
		set_cvar_float("mp_timelimit",get_cvar_float("mp_timelimit") + tlimit)
		client_print(0,print_chat,"[AMXX] %s has extended the map time by %i minutes",name,tlimit)
		++user_limit
		return PLUGIN_HANDLED
	}
	
	return PLUGIN_HANDLED
}
#if defined DEBUG
public extend_user_limit(id,level,cid)
{
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED
	
	new arg[32]
	read_argv(1,arg,31)
	
	if (arg[0])
	{
		
		user_limit = str_to_num(arg) + user_limit
		client_print(id,print_chat,"DEBUG: Added %d to user_limit to have %i total",arg[0],user_limit)
	}
	
	return PLUGIN_HANDLED
}
#endif		