I customized the advanced vote menu from menus tutorial. For some reason it doesn't show up. I guess the problem is in the way menu items are added (with loop). Any ideas ?
Code:
#include <amxmodx>
#include <amxmisc>
//How many different votes there will be in the menu
#define MAXVOTE 5
new gVoteMenu;
new gVotes[MAXVOTE];
new gVoting;
new Winner
new const CHOICE[MAXVOTE][] =
{
"Item #1",
"Item #2",
"Item #3",
"Item #4",
"Item #5"
}
new const CONDITION[MAXVOTE][] =
{
"Condition #1",
"Condition #2",
"Condition #3",
"Condition #4",
"Condition #5"
}
public plugin_init()
{
register_plugin("Advanced Vote", "0.1", "n0XX`")
register_clcmd( "say /vote","TheVote");
register_clcmd( "say /winner","WhoWon")
}
public WhoWon(id)
{
if(Winner)
client_print(id, print_chat, "The Winner is %s", CHOICE[Winner-1])
else
client_print(id, print_chat, "Nothing")
return PLUGIN_HANDLED
}
public TheVote()
{
new players[32], pnum, tempid;
get_players(players, pnum, "ac");
for( new i; i<pnum; i++ )
{
tempid = players[i];
{
StartVote(tempid)
client_print(tempid, print_chat, "Menu sent.") //Only to make sure.
}
}
}
public StartVote(id)
{
if( gVoting )
{
client_print(id, print_chat, "There is already a vote going.");
return PLUGIN_HANDLED;
}
gVoteMenu = menu_create("\rMake a selection!", "menu_handler");
//Here you can do whatever you want to add your voteids.
//We are going to use players for the example
gVoting = 1;
new iNum = 1
for(new i = 0; i < MAXVOTE; i++)
{
new menuitem[21]
formatex(menuitem, 20, "\w%s", CHOICE[0])
menu_additem(gVoteMenu, menuitem, "iNum", 0);
iNum++
}
set_task(10.0, "EndVote");
return PLUGIN_HANDLED;
}
public menu_handler(id, menu, item)
{
//If the menu was exited or if there is not a vote
if( item == MENU_EXIT || !gVoting )
{
return PLUGIN_HANDLED;
}
new data[6], iName[64];
new access, callback;
menu_item_getinfo(menu, item, access, data,5, iName, 63, callback);
//Get the voteid number that was selected
new voteid_num = str_to_num(data);
gVotes[voteid_num]++
return PLUGIN_HANDLED;
}
public EndVote()
{
//This will hold how many different votes were selected
new votes_select;
//This will hold the top 3 votes
new votes[3];
//This will hold the top 3 selected voteids
new voteid[3];
new i, j;
//Loop through all the voteids
for( i=0; i<MAXVOTE; i++ )
{
//If the voteid recieved any votes
if( gVotes[i] )
{
//If we are still trying to get the top 3
if( votes_select < 3 )
{
//Save the data for the current voteid selected
votes[votes_select] = gVotes[i];
voteid[votes_select] = i;
//Go to the next voteid that might have been selected
votes_select++;
}
else
{
//Loop through all the top votes, replace any that are lower than the selected voteid
for( j=0; j<3; j++ )
{
//If this one recieved less votes
if( votes[j] < gVotes[i] )
{
//Change the data to the voteid with more votes
votes[j] = gVotes[i];
voteid[j] = i;
//Don't need to bother looking for more
break;
}
}
}
}
}
//If noone voted
if( !votes_select )
{
client_print(0, print_chat, "CRICKEY! No one voted!");
}
//Else if one voteid recieved all the votes
else if( votes_select == 1 )
{
//Give it to the voteid
SelectWin( voteid[0] );
}
//Else if two different voteids recieved all the votes
else if( votes_select == 2 )
{
//If they recieved even votes
if( votes[0] == votes[1] )
{
//Give it to a random one
client_print(0, print_chat, "Vote has tied. Choosing random from tied votes." );
SelectWin( voteid[ random(2) ] );
}
//Else if the first recieved the most
else if( votes[0] > votes[1] )
//Give it to the first
SelectWin( voteid[0] );
//Else the second recieved the most
else
//Give it to the second
SelectWin( voteid[1] );
}
//Else there were at least 3 different votes
else
{
//Here you might want to do run-off voting, but well just select a random one
client_print(0, print_chat, "Could not determine a winner. Selecting random." );
SelectWin( voteid[ random(3) ] );
}
menu_destroy(gVoteMenu);
gVoting = 0;
}
stock SelectWin(iWin)
{
Winner = iWin
client_print(0, print_chat, "The winner is %s", CHOICE[iWin - 1])
set_task(5.0, "Tell", 0)
}
public Tell()
{
switch(Winner)
{
case 1-MAXVOTE:
{
client_print(0, print_chat, "Conditions: %s", CONDITION[Winner-1])
}
}
}
I guess everything else should be fine as in other parts of code were made only minor changes.