/***********************************************
 AllTalk voting Plugin
This pugin was originally the democracy2 plugin.
I have since modifed the snot out of it and 
turned it into a vote/admin alltalk plugin 
*************************************************/
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
#define ACCESS_RESTRICT 8192
#define ACCESS_VOTE_AT 8192
#define ACCESS_AT 8192
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.26";
 
/* Change to 1 to have a Alltalk vote start 150 seconds in to a map */
#define AUTO_AT_VOTE 0
 
#define ENABLE_AT 1
 
 
 
 
/* Change to 0 to disable Alltalk voting */
#define ENABLE_AT_VOTE 1
 
/* Change to 0 to disable friendly fire status autoresponse */
#define ENABLE_AT_STATUS 1
 
/* Change this to whatever ratio of players need to vote for a Alltalk setting to change it. */
#define AT_VOTE_RATIO 40
 
 
/* Change this to the number secconds a NON-admin will have to wait to Re-Vote somthing. Like to */
new RVfreq=180;
 
 
#if ENABLE_AT_VOTE==1
 
/* Change this to 1 to disable public use of vote_at by default */
new ATVoteOff=0;
 
#endif
 
 
#if ENABLE_AT_VOTE==1
new vote_ATAllowed=1;
@ATVoteTimer() {vote_ATAllowed=1;}
#endif
 
 
 
 
public ATVote() {
	new strDummy[10];
	vote("AllTalk on?", "Yes", "No", "HandleATVote",strDummy);
}
 
public HandleATVote(WinningOption,HLData,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new Ratio = AT_VOTE_RATIO;
	new strData[MAX_DATA_LENGTH];
	new i;
	new maxplayers = maxplayercount();
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Team;
	new WONID;
	convert_string(HLData, strData,MAX_DATA_LENGTH);
 
	if (VoteCount >= Ratio*UserCount/100) {	
		if (WinningOption==1) {
			if(getvar("sv_alltalk")==1) {
				snprintf(Text, MAX_TEXT_LENGTH, "Vote over.  Alltalk will remain enabled.", strNumber);
			} else {
				snprintf(Text, MAX_TEXT_LENGTH, "Vote successful. Alltalk changed to enabled.", strNumber);
				setstrvar("sv_alltalk", "1");
			}
		} else {
			if(getvar("sv_alltalk")==1) {
				snprintf(Text, MAX_TEXT_LENGTH, "ALLtalk is on!!", strNumber);
				setstrvar("sv_alltalk", "0");
			} else {
				snprintf(Text, MAX_TEXT_LENGTH, "Vote over.  Alltalk will remain disabled.", strNumber);
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if(getvar("sv_alltalk")==1) {
			snprintf(Text, MAX_TEXT_LENGTH, "Alltalk vote succeeded, but not enough votes for change (needed %s)^nFriendly fire will remain enabled.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Alltalk vote succeeded, but not enough votes for change (needed %s)^nFriendly fire will remain disabled.", strNumber);
		}
	}
	centersay(Text,18,63,187,239);
	for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
			messageex(Target,Text,print_console);
		}
	}
}
#if ENABLE_AT_VOTE==1
public admin_vote_at(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);
	say_command(User,Command,Data);
 
 
	if (streq(Data, "off")==1) {
		selfmessage( "Public Alltalk voting has been disabled");
		ATVoteOff=1;
 
		return PLUGIN_HANDLED;
	}
	if (streq(Data, "on")==1) {
		selfmessage( "Public Alltalk voting has been enabled");
		ATVoteOff=0;
		return PLUGIN_HANDLED;
	}
 
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
 
 
	vote_ATAllowed=0;
	set_timer("@ATVoteTimer",RVfreq,1);
	ATVote();
 
	return PLUGIN_HANDLED;
}
#endif
 
#if ENABLE_AT==1
public admin_at(HLCommand,HLData,HLUserName,UserIndex) {
	new Text[MAX_TEXT_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new strData[MAX_DATA_LENGTH];
	new i;
	new maxplayers = maxplayercount();
	new Target[MAX_NAME_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	convert_string(HLData, strData,MAX_DATA_LENGTH);
	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);
 
	if (streq(Data, "talk")==1) {
		selfmessage("Alltalk is now on");
		setstrvar("sv_alltalk", "1");
		snprintf(Text, MAX_TEXT_LENGTH, "An Admin has just enabled alltalk^n Your enemies can hear you!");
 
		centersay(Text,18,63,187,239);
		for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH)==1) {
			messageex(Target,Text,print_console);
		}
	}
		return PLUGIN_HANDLED;
}
 
	if (streq(Data, "notalk")==1) {
		selfmessage("Alltalk is now off");
		setstrvar("sv_alltalk", "0");
		snprintf(Text, MAX_TEXT_LENGTH, "An Admin has just disabled alltalk^n Your enemies can't hear you!");
		centersay(Text,18,63,187,239);
		for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH)==1) {
		messageex(Target,Text,print_console);
		}
		return PLUGIN_HANDLED;
	}
}
 
	vote_ATAllowed=0;
	set_timer("@ATVoteTimer",RVfreq,1);
	ATVote();
 
 
	return PLUGIN_HANDLED;	
 
}
 
#endif
 
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);
 
#if ENABLE_AT_STATUS==1
	new ATMatch = FALSE;
	if (strcasestr(Data, "at on")!=-1) {
		ATMatch = TRUE;
	}
	if (strcasestr(Data, "alltalk")!=-1) {
		ATMatch = TRUE;
	}
#endif
 
 
#if ENABLE_AT_STATUS==1
	if (ATMatch==TRUE) {
		if (getvar("sv_alltalk") == 1){ 
			centersay ("Alltalk is ON! The enemy can hear you now! ^n To change say vote_at",9,0,255,0); 
		} else {
			centersay ("Alltalk OFF the enemy can't hear you. ^n Go ahead, talk to your team. To change say vote_at",9,0,255,0);
		}
	}
#endif
 
 
#if ENABLE_AT_VOTE==1
	if (strcasestr(Data, "vote_at")!=-1) {
		if (ATVoteOff==1) {
			say("Admin has disable public use of vote_at.");
		} else {
			if (vote_ATAllowed != 1){
				say("A vote has just been completed, sorry if you didn't like the results.");
			} else {
				if (vote_allowed()!=1) {
					say("Vote not allowed at this time.");
				} else {
					vote_ATAllowed=0;
					set_timer("@ATVoteTimer",RVfreq,1);
					ATVote();
				}
			}
		}
	}
#endif
	return PLUGIN_CONTINUE;
}
 
@ATVote() {
	if (vote_allowed()==1) {
		ATVote();
		say("[AutoVoteAT] Vote Started");
	} else {
		set_timer("@ATVote",30,1);
		say("[AutoVoteAT] Vote not allowed at this time");
		say("Server will retry in 30 secconds");
	}
}
 
public plugin_init() {
	plugin_registerinfo("Democracy Plugin","vote for Alltalk with autorespond status",STRING_VERSION);
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
 
#if ENABLE_AT==1
	plugin_registercmd("admin_at","admin_at",ACCESS_AT,"admin_at talk/notalk: admin ablity to turn alltalk on/off");
	plugin_registerhelp("admin_at",ACCESS_AT,"admin_at talk/notalk : Will turn alltalk on/off.");
#endif
#if ENABLE_AT_VOTE==1
	plugin_registercmd("admin_vote_at","admin_vote_at",ACCESS_VOTE_AT,"admin_vote_at : Starts a vote to enable alltalk.");
	plugin_registerhelp("say",ACCESS_ALL,"say vote_at: Will start at vote.");
#endif
 
#if ENABLE_AT_STATUS==1
	plugin_registerhelp("say",ACCESS_ALL,"say at on, Alltalk: Will give at status, Clients say vote_at will start vote.");
#endif
 
#if AUTO_AT_VOTE==1	
	set_timer("@ATVote",150,1);
#endif
 
	return PLUGIN_CONTINUE;
}