Raised This Month: $ Target: $400
 0% 

Voting system.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 05-21-2014 , 05:14   Voting system.
Reply With Quote #1

Hi,

Could someone show me how to do a voting system for lets say restarting the server? (server_cmd quit).
Im trying to get a voting system to work last time I did it crashed the server.
is there something I should know about outdated methods? or am I just doing it wrong?
vamppa is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 05-21-2014 , 05:56   Re: Voting system.
Reply With Quote #2

Code:
new g_vote[33] // 1=yes 2 = no public client_disconnect(id) { g_vote[id]=0 } public makevote() { //make the menu or whatever } public votehandler(id) { //cache player's votes in g_vote } public task_checkresults()//create a task when the vote is made for when it's over to call this { new count1, count2 new iPlayers[ 32 ], iNum;     get_players( iPlayers, iNum);         new iPlayer;         for( new i = 0; i < iNum; i++ )     {         iPlayer = iPlayers[ i ]; if(g_vote[iPlayer]==1) count1++ else if(g_vote[iPlayer]==2) count2++         } //this part depends on how you want to count the votes, and requires math //here's a basic one if(count1>count2) { yes } else if(count1==count2) { tie } else { no } }
aron9forever is offline
DavidJr
Senior Member
Join Date: Apr 2012
Old 05-21-2014 , 09:06   Re: Voting system.
Reply With Quote #3

vampa, show us the code.
__________________
What are you looking for here?
DavidJr is offline
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 05-23-2014 , 08:57   Re: Voting system.
Reply With Quote #4

Can someone give me a working sample which I can work with?
It is easier for me to learn that way.

Heres the previous code.

PHP Code:
#include <amxmodx> 
#include <amxmisc>

new gVotes[2];

public 
Voteeh(id)
{
    
gVoteMenu menu_create("vote: Server restart""menu_handler");
    
menu_additem(gVoteMenu"Yes""0"0);
    
menu_additem(gVoteMenu"No""1"0);
    
    
menu_display(idgVoteMenu0);
    
    
set_task(10.0"EndVote",id);
    
    return 
PLUGIN_HANDLED;
}
public 
menu_handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        return 
PLUGIN_HANDLED;
    }
    
    new 
data[6], iName[64];
    new 
accesscallback;
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
voteid str_to_num(data);
    
    
gVotes[voteid]++;
    
    return 
PLUGIN_HANDLED;
}
public 
client_disconnect(id

      
g_gVotes[id] = 
}
public 
EndVote(id)
{
    if( 
gVotes[0] > gVotes[1] )
    {
    
server_cmd("quit")
    
client_print(0,print_chat"Vote succeeded server restart");
    }
    
    
menu_destroy(gVoteMenu);

    return 
PLUGIN_HANDLED


Last edited by vamppa; 05-23-2014 at 09:00.
vamppa is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 05-23-2014 , 09:20   Re: Voting system.
Reply With Quote #5

Quote:
Originally Posted by vamppa View Post
Can someone give me a working sample which I can work with?
It is easier for me to learn that way.

Heres the previous code.

PHP Code:
#include <amxmodx> 
#include <amxmisc>

new gVotes[2];

public 
Voteeh(id)
{
    
gVoteMenu menu_create("vote: Server restart""menu_handler");
    
menu_additem(gVoteMenu"Yes""0"0);
    
menu_additem(gVoteMenu"No""1"0);
    
    
menu_display(idgVoteMenu0);
    
    
set_task(10.0"EndVote",id);
    
    return 
PLUGIN_HANDLED;
}
public 
menu_handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        return 
PLUGIN_HANDLED;
    }
    
    new 
data[6], iName[64];
    new 
accesscallback;
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
voteid str_to_num(data);
    
    
gVotes[voteid]++;
    
    return 
PLUGIN_HANDLED;
}
public 
client_disconnect(id

      
g_gVotes[id] = 
}
public 
EndVote(id)
{
    if( 
gVotes[0] > gVotes[1] )
    {
    
server_cmd("quit")
    
client_print(0,print_chat"Vote succeeded server restart");
    }
    
    
menu_destroy(gVoteMenu);

    return 
PLUGIN_HANDLED


wait, you said your initial code crashes the server
are you aware that it's what it's intended to do?
server_cmd("quit") this closes the HLDS process entirely, just like you would type quit in your console, it doesn't restart the server


are you sure it's crashing and it's not a misunderstanding?
aron9forever is offline
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 05-23-2014 , 09:30   Re: Voting system.
Reply With Quote #6

yeah infact initial code does something different im using shut down aka quit as an easy example.
Im just looking to get the voting system to work seeing im not familiar with it.
Likely initial function was causing a crash yet the voting didnt work as suppose too neither, in another example the menu did not destroy itself after voting.
found this going to see what I can do with it, am getting brain fucks.

PHP Code:
#include <amxmodx>

//This will hold the VoteMenu
new gVoteMenu;
//This will hold the votes for each option
new gVotes[2];
 
//This determines if a vote is already happening
new gVoting;
 
public 
plugin_init()
{
    
//Register a way to get to your vote...
    
register_clcmd"start_vote","StartVote" );
}
public 
StartVoteid )
{
    
//If there is already a vote, don't start another
    
if ( gVoting )
    {
        
client_printidprint_chat"There is already a vote going." );
        
//We return PLUGIN_HANDLED so the person does not get Unknown Command in console
        
return PLUGIN_HANDLED;
    }
    
//Reset vote counts from any previous votes
    
gVotes[0] = gVotes[1] = 0;
    
//Note that if you have more than 2 options, it would be better to use the line below:
    //arrayset( gVotes, 0, sizeof gVotes );

    //Store the menu in the global
    
gVoteMenu menu_create"\rLook at this Vote Menu!:""menu_handler" );

    
//Add some vote options
    
menu_additemgVoteMenu"Vote Option 1""");
    
menu_additemgVoteMenu"Vote Option 2""");

    
//We will need to create some variables so we can loop through all the players
    
new players[32], pnumtempid;

    
//Fill players with available players
    
get_playersplayerspnum );

    
//Start looping through all players to show the vote to
    
for ( new ipnumi++ )
    {
        
//Save a tempid so we do not re-index
        
tempid players[i];

        
//Show the vote to this player
        
menu_displaytempidgVoteMenu);

        
//Increase how many players are voting
        
gVoting++;
    }

    
//End the vote in 10 seconds
    
set_task(10.0"EndVote" );

    return 
PLUGIN_HANDLED;
}
public 
menu_handleridmenuitem )
 {
    
//If the menu was exited or if there is not a vote
    
if ( item == MENU_EXIT || !gVoting )
    {
        
//Note were not destroying the menu
        
return PLUGIN_HANDLED;
    }

    
//Increase the votes for what they selected
    
gVotesitem ]++;

    
//Note were not destroying the menu
    
return PLUGIN_HANDLED;
}
public 
EndVote()
{
    
//If the first option recieved the most votes
    
if ( gVotes[0] > gVotes[1] )
        
client_print(0print_chat"First option recieved most votes (%d )"gVotes[0] );

    
//Else if the second option recieved the most votes
    
else if ( gVotes[0] < gVotes[1] )
        
client_print(0print_chat"Second option recieved most votes (%d )"gVotes[1] );

    
//Otherwise the vote tied
    
else
        
client_print(0print_chat"The vote tied at %d votes each."gVotes[0] );

    
//Don't forget to destroy the menu now that we are completely done with it
    
menu_destroygVoteMenu );

    
//Reset that no players are voting
    
gVoting 0;

vamppa is offline
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 05-23-2014 , 16:59   Re: Voting system.
Reply With Quote #7

Ok got it working.
Is it possible to change the color of the menu? or use hud message instead?
vamppa is offline
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 05-23-2014 , 17:23   Re: Voting system.
Reply With Quote #8

also
PHP Code:
menu_destroygVoteMenu ); 
does not work. pressing slot1 or slot2 does close the menu.
Is there an alternative way to close it?

vamppa is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 05-23-2014 , 17:31   Re: Voting system.
Reply With Quote #9

menu_destroy is not meant to close the menu, just invalidate the handle. If you want to forcibly close the menu (ie, make it disappear from their screen), display another blank menu over it
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
vamppa
Senior Member
Join Date: Apr 2010
Location: The Netherlands
Old 05-24-2014 , 04:25   Re: Voting system.
Reply With Quote #10

Ok sweet thanks.

How about changing its color is that possible?
Another question if im going to make lets say 3 different votes, would I have to duplicate
PHP Code:
#include <amxmodx>

//This will hold the VoteMenu
new gVoteMenu;
//This will hold the votes for each option
new gVotes[2];
 
//This determines if a vote is already happening
new gVoting;
 
public 
plugin_init()
{
    
//Register a way to get to your vote...
    
register_clcmd"start_vote","StartVote" );
}
public 
StartVoteid )
{
    
//If there is already a vote, don't start another
    
if ( gVoting )
    {
        
client_printidprint_chat"There is already a vote going." );
        
//We return PLUGIN_HANDLED so the person does not get Unknown Command in console
        
return PLUGIN_HANDLED;
    }
    
//Reset vote counts from any previous votes
    
gVotes[0] = gVotes[1] = 0;
    
//Note that if you have more than 2 options, it would be better to use the line below:
    //arrayset( gVotes, 0, sizeof gVotes );

    //Store the menu in the global
    
gVoteMenu menu_create"\rLook at this Vote Menu!:""menu_handler" );

    
//Add some vote options
    
menu_additemgVoteMenu"Vote Option 1""");
    
menu_additemgVoteMenu"Vote Option 2""");

    
//We will need to create some variables so we can loop through all the players
    
new players[32], pnumtempid;

    
//Fill players with available players
    
get_playersplayerspnum );

    
//Start looping through all players to show the vote to
    
for ( new ipnumi++ )
    {
        
//Save a tempid so we do not re-index
        
tempid players[i];

        
//Show the vote to this player
        
menu_displaytempidgVoteMenu);

        
//Increase how many players are voting
        
gVoting++;
    }

    
//End the vote in 10 seconds
    
set_task(10.0"EndVote" );

    return 
PLUGIN_HANDLED;
}
public 
menu_handleridmenuitem )
 {
    
//If the menu was exited or if there is not a vote
    
if ( item == MENU_EXIT || !gVoting )
    {
        
//Note were not destroying the menu
        
return PLUGIN_HANDLED;
    }

    
//Increase the votes for what they selected
    
gVotesitem ]++;

    
//Note were not destroying the menu
    
return PLUGIN_HANDLED;
}
public 
EndVote()
{
    
//If the first option recieved the most votes
    
if ( gVotes[0] > gVotes[1] )
        
client_print(0print_chat"First option recieved most votes (%d )"gVotes[0] );

    
//Else if the second option recieved the most votes
    
else if ( gVotes[0] < gVotes[1] )
        
client_print(0print_chat"Second option recieved most votes (%d )"gVotes[1] );

    
//Otherwise the vote tied
    
else
        
client_print(0print_chat"The vote tied at %d votes each."gVotes[0] );

    
//Don't forget to destroy the menu now that we are completely done with it
    
menu_destroygVoteMenu );

    
//Reset that no players are voting
    
gVoting 0;

most of that or is there an more efficient way?
vamppa is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:23.


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