/* Ce plugin permet de lancer un vote pour allumer le brouillard sur le serveur */
 
/* 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>
 
 
/* 40% des personnes doivent répondre oui pour l'allumer. */
#define MX_VOTE_RATIO 40
 
#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 Fog?", "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("map_fog")==1) {
				centersay("Vote over. Fog will stay.",18,249,244,0);
			} else {
				centersay("Vote successful.",18,249,244,0);
				exec("map_fog 1");
			}
		} else {
			if(getvar("map_fog")==0) {
				centersay("Vote over. Fog is on.",18,63,187,239);
				exec("map_fog 1");
			} else {
				centersay("Vote over.  No fog remain.",18,63,187,239);
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if(getvar("map_fog")==1) {
			snprintf(Text, MAX_TEXT_LENGTH, "Fog vote succeeded, but not enough votes for change (needed %s)^nFog will stay.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Fog vote succeeded, but not enough votes for change (needed %s)^nNo fog 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_Fog")!=-1) { 
		MXVote(); 
		return PLUGIN_CONTINUE;
	}
	if (strcasestr(Data, "Fog")!=-1) {
		Match = TRUE;
		}
	if (Match==TRUE) {
		if (getvar("map_fog") == 1){ 
			centersay ("Fog  is on!^n To change say vote_Fog!",7,0,255,0); 
		} else {
			centersay ("Fog is off. ^n To change say vote_Fog. ",7,0,255,0);
		}
	}
	return PLUGIN_CONTINUE;
}
public admin_Fog(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,"map_fog","1");
		centersay ("Fog is on!^n To change say vote_Fog!",7,0,255,0); 
	} else {
		execute_command(User,Command,"map_fog","0");
		centersay ("Fog is off. ^n To change say vote_Fog. ",7,0,255,0);
	}
	MXVote();
	return PLUGIN_HANDLED;
}
 
public plugin_init() {
	plugin_registerinfo("Fog Plugin","Enables Fog vote on/off and auto-responses.",STRING_VERSION);
	plugin_registercmd("admin_vote_Fog","admin_vote_Fog",ACCESS_VOTE_MX,"admin_vote_gravity : Starts a vote to enable fog.");
	plugin_registercmd("admin_Fog","admin_Fog",ACCESS_FUN,"admin_Fog <#> 1 turns Fog on, 0 turns Fog off.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say Fog: Will give Fog status, Clients say vote_Fog will start vote.");
	plugin_registerhelp("say",ACCESS_ALL,"say vote_Fog: Will start Fog vote.");
 
#if AUTO_MX_VOTE==1	
	set_timer("MXVote",30,1);
#endif
 
	return PLUGIN_CONTINUE;
}