/* Ce plugin permet de lancer un vote pour couper les machines gun */
 
/* Basé sur le plugin  matrix*/
 
/* $Id: plugin_dod_fog.sma,v 1.0a 18/02/2002 by Noplay $ */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
 
/* 60% des personnes doivent répondre oui pour l'allumer. */
#define MX_VOTE_RATIO 60
 
#define ACCESS_FUN 8192
 
/* Mettre a 1 pour que le vote se déclenche automatiquement */
#define AUTO_MX_VOTE 0
 
/* Il faut avoir un niveau 1 pour lancer le vote */
#define ACCESS_VOTE_MX 1
 
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
public MXVote() {
	new strDummy[10];
	if(vote_allowed()!=1){
		selfmessage("Voting not allowed at this time.");
		return PLUGIN_HANDLED;
	}
	vote("Do you want turn off Machine Gunner?", "Yes", "No thanks", "HandleMXVote",strDummy);
	return PLUGIN_HANDLED;
}
 
public HandleMXVote(WinningOption,HLData,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new Ratio = MX_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_limitalliesmg")==0) {
				centersay("Vote over. Mg is off.",18,249,244,0);
			} else {
				centersay("Vote successful.",18,249,244,0);
				exec("mp_limitalliesmg 0");
				exec("mp_limitaxismg 0");
			}
		} else {
			if(getvar("mp_limitalliesmg")==-1) {
				centersay("Vote over. Mg is on.",18,63,187,239);
				exec("mp_limitalliesmg -1");
				exec("mp_limitaxismg -1");
			} else {
				centersay("Vote over.  Mg is off.",18,63,187,239);
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if(getvar("mp_limitalliesmg")==1) {
			snprintf(Text, MAX_TEXT_LENGTH, "MG vote succeeded, but not enough votes for change (needed %s)^nMg is on.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "MG vote succeeded, but not enough votes for change (needed %s)^nNo MG will remain.", strNumber);
		}
		centersay(Text,18,63,187,239);
	}
}
 
public admin_vote_Fog(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);		
	MXVote();
	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, "vote_mg")!=-1) { 
		MXVote(); 
		return PLUGIN_CONTINUE;
	}
	if (strcasestr(Data, "Mg")!=-1) {
		Match = TRUE;
		}
	if (Match==TRUE) {
		if (getvar("mp_limitalliesmg") == -1){ 
			centersay ("Machine gunner  is on!^n To change say vote_mg!",7,0,255,0); 
		} else {
			centersay ("Machine gunner is off. ^n To change say vote_mg. ",7,0,255,0);
		}
	}
	return PLUGIN_CONTINUE;
}
public admin_Mg(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);
	if(check_param(Data)==1) {
		execute_command(User,Command,"mp_limitalliesmg","-1");
		execute_command(User,Command,"mp_limitaxismg","-1");
		centersay ("Machine gunner is on!^n To change say vote_mg!",7,0,255,0); 
	} else {
		execute_command(User,Command,"mp_limitalliesmg","0");
		execute_command(User,Command,"mp_limitaxismg","0");
		centersay ("Fog is off. ^n To change say vote_Fog. ",7,0,255,0);
	}
	MXVote();
	return PLUGIN_HANDLED;
}
 
public plugin_init() {
	plugin_registerinfo("Machine gunner Plugin","Enables Fog vote on/off and auto-responses.",STRING_VERSION);
	plugin_registercmd("admin_vote_mg","admin_vote_mg",ACCESS_VOTE_MX,"admin_vote_gravity : Starts a vote to enable Machine gunner.");
	plugin_registercmd("admin_mg","admin_mg",ACCESS_FUN,"admin_mg <#> 1 turns mg on, 0 turns mg off.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say mg: Will give Machine gunner status, Clients say vote_mg will start vote.");
	plugin_registerhelp("say",ACCESS_ALL,"say vote_mg: Will start Machine gunner vote.");
 
#if AUTO_MX_VOTE==1	
	set_timer("MXVote",30,1);
#endif
 
	return PLUGIN_CONTINUE;
}