AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Menu Handler (https://forums.alliedmods.net/showthread.php?t=154612)

nikhilgupta345 04-10-2011 21:28

Menu Handler
 
Code:

public Show_DayMenu()
{
    new menuTitle[256];
    format( menuTitle, charsmax( menuTitle ), "Choose A Day: [%i]", gTimeLeft );
    new menu = menu_create( menuTitle, "DayMenuHandler" );
   
    new holder[6], itemHolder[256];
    for( new i = 0; i < gTotalOptions; i++ )
    {
        num_to_str( i + 1, holder, 5 );
        format( itemHolder, charsmax( itemHolder ), "%s [Votes: %i]", gDaymenuOptions[i], gMainDayVotes[i] );
        menu_additem( menu, itemHolder, holder );
    }
   
    gTimeLeft--;
   
    new players[32], num, player;
    get_players( players, num, "ae", "CT" );
   
   
    if( gTimeLeft >= 0 )
    {
        for( new i = 0; i < num; i++ )
        {
            player = players[i];
            showDayMenu( player, menu );
        }
    }
   
    else
    {
        menu_destroy( menu );
       
        for( new i = 0; i < num; i++ )
        {
            player = players[i];
           
            show_menu( player, 0, "^n", 1 );
        }
    }
   
}

public showDayMenu( id, menu )
{
    menu_display( id, menu, 0 );
   
    menu_destroy( menu );
   
    return PLUGIN_HANDLED;
}

public DayMenuHandler( id, menu, item )
{
    if( cs_get_user_team( id ) != CS_TEAM_CT )
    {
        ColorChat( id, TEAM_COLOR, "^01[Jailbreak] You must be a ^03Counter-Terrorist ^01to vote." );
       
        menu_destroy( menu );
       
        return PLUGIN_HANDLED;
    }
   
    else if( gHasVoted[id] )
    {
        ColorChat( id, TEAM_COLOR, "^01[Jailbreak] You have already voted." );
        menu_destroy( menu );
        return PLUGIN_HANDLED;
    }
   
    else if( !is_user_alive( id ) )
    {
        ColorChat( id, NORMAL, "[Jailbreak] You must be alive to vote." );
        menu_destroy( menu );
        return PLUGIN_HANDLED;
    }
   
   
    new access, callback;
    new data[6], szName[64];
    menu_item_getinfo( menu, item, access, data, 5, szName, 63, callback );
   
    new key = str_to_num( data );
   
    gMainDayVotes[key]++;
   
    gHasVoted[id] = true;
   
    menu_destroy( menu );
   
    showDayMenu( id, menu );
   
    return PLUGIN_HANDLED;
}

It shows "You have already voted" like 100 times for some reason. Any ideas?

fysiks 04-10-2011 22:19

Re: Menu Handler
 
Not quite sure what you are trying to say is actually happening but there are some major logical errors in that code.

What exactly are you trying to do?

nikhilgupta345 04-11-2011 20:59

Re: Menu Handler
 
Sorry, I'm trying to make a votemenu, where there are about 10 different options and then there's a 10 second countdown when the vote ends. Only CT's can choose options, and they can only choose once.

fysiks 04-12-2011 00:06

Re: Menu Handler
 
Replace that whole code block that you posted with this:

PHP Code:

new g_pMenug_iCount

public Show_DayMenu() { start_vote(); }

public 
start_vote() // This function starts the whole process.
{
    
g_iCount 10
    show_vote_menu
()
    
set_task(1.0"taskCount", .flags="a", .repeat=10)
}

end_vote()
{
    if(
g_pMenu)
        
menu_destroy(g_pMenu)
    
    
show_menu(00"^n"1)
    
    
// process votes
}

public 
taskCount()
{
    
g_iCount--
    if(!
g_iCount)
        
end_vote()
}

show_vote_menu()
{
    if(
g_pMenu)
        
menu_destroy(g_pMenu)
    
    if( !(
g_iCount 0) )
        return
    
    new 
szBuffer[32]
    
formatex(szBuffercharsmax(szBuffer), "Choose A Day: [%i]"g_iCount)
    
g_pMenu menu_create(szBuffer"menuHandler")

    
// Build Menu
    
new szNum[3]
    for( new 
0gTotalOptionsi++ )
    {
        
num_to_str(iszNumcharsmax(szNum))
        
formatszBuffercharsmax(szBuffer), "%s [Votes: %i]"gDaymenuOptions[i], gMainDayVotes[i])
        
menu_additemg_pMenuszBufferszNum)
    }

    
// Show Menu
    
new iPlayers[32], iPlayersNum
    get_players
(iPlayersiPlayersNum)
    for( new 
0iPlayersNumi++ )
    {
        
menu_display(iPlayers[i], g_pMenu)
    }
}

public 
menuHandler(idmenuitem)
{
    if( !
gHasVoted[id] )
    {
        new 
_accesscallback;
        new 
data[6];
        
menu_item_getinfomenuitem_accessdata5""0callback )
        
gMainDayVotes[str_to_numdata )]++
        
        
gHasVoted[id] = true
    
}
    else
    {
        
client_print(idprint_chat"You already voted")
    }
    
    
show_vote_menu()


Tell me if the main functionality works (ignore that it doesn't check if they are alive or if they are on the correct team).

nikhilgupta345 04-12-2011 17:44

Re: Menu Handler
 
The voting works fine, but the counting down doesn't work correctly. It updates the time remaining only after you choose after the menu options, so it would stay on 5 or 6 until you press a menu option and it updates.

fysiks 04-12-2011 18:50

Re: Menu Handler
 
Quote:

Originally Posted by nikhilgupta345 (Post 1448581)
The voting works fine, but the counting down doesn't work correctly. It updates the time remaining only after you choose after the menu options, so it would stay on 5 or 6 until you press a menu option and it updates.

Oh, yeah. I have it update when the votes change.

You should be able to do this:

PHP Code:

public taskCount() 

    
g_iCount-- 
    if(
g_iCount 0)
    {
        
show_vote_menu()
    }
    else
    {
        
end_vote()
    }


With this you may be able to remove the show_vote_menu() in the menu handler (try it with and without; without will be more efficient).

nikhilgupta345 04-12-2011 20:16

Re: Menu Handler
 
K, so now the countdown and vote works fine, but after I have voted, and whenever the countdown goes down by 1, it says "You have already voted.". So basically if I vote when the countdown is at 7, it shows "You have already voted" 6 times. (7 - 1)

I'm pretty sure it's calling the menuhandler function whenever the countdown goes down, but I'm not sure how to fix that :/

fysiks 04-12-2011 20:39

Re: Menu Handler
 
Quote:

Originally Posted by nikhilgupta345 (Post 1448636)
K, so now the countdown and vote works fine, but after I have voted, and whenever the countdown goes down by 1, it says "You have already voted.". So basically if I vote when the countdown is at 7, it shows "You have already voted" 6 times. (7 - 1)

I'm pretty sure it's calling the menuhandler function whenever the countdown goes down, but I'm not sure how to fix that :/

Oh! You have to add the

PHP Code:

    if( item == MENU_EXIT )
    {
        
menu_destroy(menu);
        return 
PLUGIN_HANDLED;
    } 

in the handler. When you destroy the menu it cancels the menu. When you cancel the menu it forces people to effectively select the "Exit" key regardless of it existing or not. :)

i think

nikhilgupta345 04-12-2011 21:52

Re: Menu Handler
 
Thanks, works perfect now.

<3 (NO HOMO)

Emp` 04-13-2011 00:38

Re: Menu Handler
 
Quote:

Originally Posted by fysiks (Post 1448641)
When you destroy the menu it cancels the menu. When you cancel the menu it forces people to effectively select the "Exit" key regardless of it existing or not. :)

i think

Yup.


All times are GMT -4. The time now is 20:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.