/* AMX Mod script.
*
* (c) Copyright 2002, OLO
* This file is provided as is (no warranties).
*
*/

#include <amxmod> /* natives for AMX */

/* cvars:
*  max. time on which map can be extended (there will be always min. one vote)
*  amx_extendmap_max  < value >
*
*  when voting is successful set how much time we want play
*  amx_extendmap_step  < value >
*/

new gShowVote = 1
new gVotes
new gExtendVote

public check_votes()
{
	new players[32], inum = 0
	get_players(players,inum,"c")
	new Float:step_time = get_cvar_float("amx_extendmap_step")
	new Float:mp_time = get_cvar_float("mp_timelimit")
	if (inum && ((float(gVotes) / float(inum)) >= get_cvar_float("amx_extendmap_ratio"))) {
		client_print(0,print_chat,"[AMX] Voting successful. Map will be extended to next %.0f minutes.",step_time)
		new Float:new_time = mp_time + step_time
		set_cvar_float("mp_timelimit",new_time)
		if (new_time < get_cvar_float("amx_extendmap_max") )
			gShowVote = 1
		else
			remove_task(7777) /* task is no more needed */
	}
	else {
		remove_task(7777) /* task is no more needed */
		client_print(0,print_chat,"[AMX] Voting failed. Map won't be extended.")
	}
	return PLUGIN_HANDLED
}

public vote_count(id,key)
{
	if (get_cvar_float("amx_vote_answers")) {
		new name[32]
		get_user_name(id,name,32)
		client_print(id,print_chat,"[AMX] %s voted for extending",name)
	}
	gVotes++
	return PLUGIN_HANDLED
}

public vote_extend()
{
	if (gShowVote&&get_timeleft()<120){ /* 2 min. before end of map */
		gShowVote = 0			
		gVotes = 0
		new mapname[64]
		get_mapname(mapname,64) /* get name of map */
		new menu[256]
		format(menu,256,"\yExtend map %s?\w^n^n1.  Yes^n0.  No",mapname)
		show_menu(0,(1<<0)|(1<<9),menu,10)
		set_task(10.0,"check_votes")
		client_print(0,print_chat,"[AMX] Voting for map extending started...")
	}
	return PLUGIN_HANDLED
}

public plugin_init()
{
	register_plugin("Map extend","0.5","default")
	gExtendVote = register_menuid("Extend map ") 
	register_menucmd(gExtendVote ,(1<<0),"vote_count")
	register_cvar("amx_extendmap_ratio","0.4")
	register_cvar("amx_extendmap_max","90")
	register_cvar("amx_extendmap_step","15")
	set_task(10.0,"vote_extend",7777,"",0,"b") /* set timer which will check if it is time to vote */
	return PLUGIN_CONTINUE
}
