/* This plugin enables The One Mode votes and auto-responses.
 * Idea for The One Mode vote from lazyjoe21.
 */
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
/* Change this to whatever ratio of players need to vote for a setting to change it. */
#define TOM_VOTE_RATIO 50
 
/* Change to 1 to have The One Mode vote start 160 seconds in to a map */
#define AUTO_TOM_VOTE 0
 
#define ACCESS_VOTE_TOM 1
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
public TOMVote() {
	new strDummy[10];
	vote("Turn on The One Mode on the next map?", "Yes", "No", "HandleTOMVote",strDummy);
}
 
public HandleTOMVote(WinningOption,HLData,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new Ratio = TOM_VOTE_RATIO;
	new strData[MAX_DATA_LENGTH];
	convert_string(HLData, strData,MAX_DATA_LENGTH);
 
	if (VoteCount >= Ratio*UserCount/100) {
		if (WinningOption==1) {
			if(getvar("mp_theonemode")==1) {
				centersay("Vote over.  The One Mode will remain enabled.",18,249,244,0);
			} else {
				centersay("Vote successful. The One Mode will be enabled on the next map.",18,249,244,0);
				exec("mp_theonemode 1");
			}
		} else {
			if(getvar("mp_theonemode")==1) {
				centersay("The One Mode will be disabled on the next map.",18,63,187,239);
				exec("mp_theonemode 0");
			} else {
				centersay("Vote over.  The One Mode will remain disabled.",18,63,187,239);
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if(getvar("mp_theonemode")==1) {
			snprintf(Text, MAX_TEXT_LENGTH, "Vote succeeded, but not enough votes for change (needed %s)^nThe One Mode will remain enabled.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Vote succeeded, but not enough votes for change (needed %s)^nThe One Mode will remain disabled.", strNumber);
		}
		centersay(Text,18,63,187,239);
	}
}
 
public admin_vote_tom(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	say_command(User,Command,Data);
	TOMVote();
	return PLUGIN_HANDLED;
}
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strstripquotes(Data);
	new Match = FALSE;
	if (strcasestr(Data, "ff on")!=-1) {
		Match = TRUE;
	} else if (strcasestr(Data, "tom is on")!=-1) {
		Match = TRUE;
	} else if (strcasestr(Data, "The One Mode on")!=-1) {
		Match = TRUE;
	}
	if (Match==TRUE) {
		if (getvar("mp_theonemode") == 1){
			centersay ("The One Mode will be ON!",7,0,255,0);
		} else {
			centersay ("The One Mode will be OFF!",7,0,255,0);
		}
	}
	if (strcasestr(Data, "vote_tom")!=-1) {
		TOMVote();
	}
	return PLUGIN_CONTINUE;
}
 
 
public plugin_init() {
	plugin_registerinfo("The One Mode Plugin","Enables The One Mode votes and auto-responses.",STRING_VERSION);
	plugin_registercmd("admin_vote_tom","admin_vote_tom",ACCESS_VOTE_TOM,"admin_vote_tom : Starts a vote to enable/disable The One Mode.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say tom on, tom is on, The One Mode on: Will give The One Mode status.");
 
#if AUTO_TOM_VOTE==1
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
	set_timer("TOMVote",160,1);
#endif
 
	return PLUGIN_CONTINUE;
}