/*********************************************************
 * A extendmap-vote plugin - Version 2.50.51             *
 *********************************************************
 *                                                       *
 * Name: plugin_stetze_nextmap_new                       *
 * Author: Stetze (Updated by Rinde)                     *
 * Released: 04/02/03                                    *
 *                                                       *
 *                                                       *
 * Commands:                                             *
 *                                                       *
 * admin_vote_nextmap                                    *
 * admin_vote_maxextend                                  *
 * admin_vote_autostart                                  *
 *                                                       *
 * CVARS:                                                *
 *                                                       *
 * admin_vote_maxextend                                  *
 * admin_vote_autostart                                  *
 *                                                       *
 *********************************************************/
 
/* Includes */
#include <plugin>
#include <adminlib>
 
/* Constants */
#define ACCESS_NEXTMAP_VOTE 1
#define ACCESS_CONTROL_VOTE 2
 
#define MAX_MAP_LENGTH 50
 
/* Global Variables */
new g_Version[] = "2.50.51";
new g_Extendcount;
new g_Extendtime;
 
/* Function Declarations */
forward StartNextMapVote(Timer,Repeat,HLUser,HLParam);
forward HandleNextMapVote(WinningOption,HLParam,VoteCount,UserCount);
forward VoteMaxextend(HLCommand,HLData,HLUserName,UserIndex);
forward VoteAutostart(HLCommand,HLData,HLUserName,UserIndex);
forward VoteNextmap(HLCommand,HLData,HLUserName,UserIndex);
 
/* Event Handlers */
public plugin_init() {
    plugin_registerinfo("NextMap voting plugin by stetze","Enables Voting for Nextmap or extend - visit www.admin-mod.de 4 more!",g_Version);
    plugin_registercmd("admin_vote_nextmap","VoteNextmap",ACCESS_NEXTMAP_VOTE,"admin_vote_nextmap: Starts Voting for next map or extend the current map.");
    plugin_registercmd("admin_vote_maxextend","VoteMaxextend",ACCESS_CONTROL_VOTE,"admin_vote_maxextend <#>: Sets how often a map can be extended.");
    plugin_registercmd("admin_vote_autostart","VoteAutostart",ACCESS_CONTROL_VOTE,"admin_vote_autostart <1|0>: Enables auto-starting of extend map vote.");
 
    if(getvar("admin_vote_autostart") == 1) {
        set_timer("StartNextMapVote",timeleft(0) - 120,0);
    }
    g_Extendtime = getvar("mp_timelimit");
    return PLUGIN_CONTINUE;
}
 
 
 
public StartNextMapVote(Timer,Repeat,HLUser,HLParam) {
    new Text[MAX_TEXT_LENGTH];
    new sNextMap[MAX_MAP_LENGTH];
    if(vote_allowed() == 0) {
        set_timer("StartNextMapVote",10,0);
    } else if(g_Extendcount < getvar("admin_vote_maxextend") || getvar("admin_vote_maxextend") == 0) {
        nextmap(sNextMap,MAX_MAP_LENGTH);
        if(valid_map(sNextMap) == 1) {
            snprintf(Text,MAX_TEXT_LENGTH,"Change to %s or extend current map?",sNextMap);
            vote(Text,"Change","Extend","HandleNextMapVote",sNextMap);
        } else {
            say("Oops, Nextmap-Voting failed!");
        }
    }
    return PLUGIN_HANDLED;
}
 
public HandleNextMapVote(WinningOption,HLParam,VoteCount,UserCount) {
    new Text[MAX_TEXT_LENGTH];
    new VoteMap[MAX_MAP_LENGTH];
    new strTime[MAX_NUMBER_LENGTH];
    new ratio;
    convert_string(HLParam,VoteMap,MAX_MAP_LENGTH);
    if(WinningOption == 1) {
        ratio = getvar("map_ratio")*UserCount/100;
        if(VoteCount >= ratio) {
            snprintf(Text,MAX_TEXT_LENGTH,"Changing map to %s due to vote.",VoteMap);
            say(Text);
            setstrvar("mp_timelimit","0.1");
        } else {
            snprintf(Text,MAX_TEXT_LENGTH,"Next map vote succeeded, but not enough votes for change (needed %i)",ratio);
            say(Text);
        }
    } else if(WinningOption == 2) {
        snprintf(Text,MAX_TEXT_LENGTH,"Nextmap vote over. Current map will be extended for %i minutes.",g_Extendtime);
        say(Text);
        g_Extendcount++;
        numtostr(getvar("mp_timelimit") + g_Extendtime,strTime);
        setstrvar("mp_timelimit",strTime);
        if(getvar("admin_vote_autostart") == 1) {
            set_timer("StartNextMapVote",timeleft(0) + g_Extendtime * 60 - getvar("amv_vote_duration"),0);
        }
    } else {
        say("Next Map vote failed.");
    }
}
 
/* Command Handlers */
public VoteMaxextend(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);
    setstrvar("admin_vote_maxextend",Data);
    say_command(User,Command,Data);
    return PLUGIN_HANDLED;
}
 
public VoteAutostart(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);
    setstrvar("admin_vote_autostart",Data);
    say_command(User,Command,Data);
    return PLUGIN_HANDLED;
}
 
public VoteNextmap(HLCommand,HLData,HLUserName,UserIndex) {
    new Command[MAX_COMMAND_LENGTH];
    new User[MAX_NAME_LENGTH];
    new Text[MAX_TEXT_LENGTH];
    new sNextMap[MAX_MAP_LENGTH];
    if(vote_allowed() == 0) {
        selfmessage("Vote not allowed at this time.");
    } else {
        nextmap(sNextMap,30);
        if(valid_map(sNextMap) == 1) {
            convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
            convert_string(HLUserName,User,MAX_NAME_LENGTH);
            say_command(User,Command,sNextMap);
            snprintf(Text,MAX_TEXT_LENGTH,"Change to %s or extend current map?",sNextMap);
            vote(Text,"Change","Extend","HandleNextMapVote",sNextMap);
        } else {
            say("Oops, Nextmap-Voting failed!");
        }
    }
    return PLUGIN_HANDLED;
}