Code:
#include <amxmodx>
// type of votes for vote_type
#define VOTE_BOTADD 1
#define VOTE_BOTKICK 2
// value of votes for vote_data
#define VOTE_YES 1
#define VOTE_NO 0
#define VOTE_NONE -1
// time variables
#define VOTE_TIME 15.0 // max time of vote
#define VOTE_FLOOD 120.0 // time allowed between votes
new vote_type;
new vote_data[33];
new Float:last_vote;
public plugin_init() {
register_menucmd(register_menuid("vote"),1023,"count_vote");
}
// call this from wherever
public start_vote(id,type) {
if(vote_type > 0) {
client_print(id,print_chat,"* Sorry, a vote is already in progress.");
return PLUGIN_HANDLED;
}
if(get_gametime() - VOTE_FLOOD <= last_vote) {
client_print(id,print_chat,"* Sorry, a vote has occurred recently.");
return PLUGIN_HANDLED;
}
new i;
vote_type = type;
// clear votes
for(i=0;i<33;i++)
vote_data[i] = VOTE_NONE;
new menu[192];
new keys = MENU_KEY_1|MENU_KEY_2;
format(menu,191,"%s^n^n1. Yes^n2. No",(vote_type == VOTE_BOTADD) ? "Add more bots?" : "Kick all bots?");
show_menu(0,keys,menu,floatround(VOTE_TIME),"vote"); // not sure if you can use 0 for this command
set_task(VOTE_TIME,"end_vote",89);
return PLUGIN_HANDLED;
}
public count_vote(id,key) {
// make sure vote is still going
if(vote_type == 0)
return;
// add votes
if(key == 0)
vote_data[id] = VOTE_YES;
else
vote_data[id] = VOTE_NO;
// see if all voting has finished
new i, bool:done = true;
for(i=0;i<33;i++) {
if(vote_data[i] == VOTE_NONE && is_user_connected(i)) {
done = false;
break;
}
}
if(done) {
remove_task(89);
end_vote();
}
client_print(id,print_chat,"* You voted %s",(key == 0) ? "Yes" : "No");
}
public end_vote() {
// make sure vote is still going
if(vote_type == 0)
return;
vote_type = 0;
last_vote = get_gametime();
// tally the results!!
new i, yes, no;
for(i=0;i<33;i++) {
if(vote_data[i] == VOTE_YES)
yes++;
else if(vote_data[i] == VOTE_NO)
no++;
}
// YES
if(yes > no) {
if(vote_type == VOTE_BOTADD) {
// add bots
}
else {
// remove bots
}
client_print(0,print_chat,"* %s won with %i/%i votes",(vote_type == VOTE_BOTADD) ? "Add more bots" : "Kick all bots",yes,no);
}
else {
client_print(0,print_chat,"* %s won with %i/%i votes",(vote_type == VOTE_BOTADD) ? "Don't add more bots" : "Don't kick all bots",no,yes);
}
}