AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Custom Vote (https://forums.alliedmods.net/showthread.php?t=183281)

EpicMonkey 04-20-2012 04:51

Custom Vote
 
Hi..

So what am trying to do is make a simple map vote , ill show my code first and then start explaining the problem

PHP Code:


#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Test"
#define VERSION "1.0"
#define AUTHOR "Epic"

new g_Map1[33], g_Map2[33];
new 
g_Vote1[33], g_Vote2[33];
new 
bool:g_VoteStarted;
new 
g_maxplayersg_msgSayText;
static const 
Team[] = "^x03"
static const Green[] = "^x04"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_clcmd("say /test""VoteMap")
    
g_maxplayers get_maxplayers()
    
g_msgSayText get_user_msgid("SayText")

}

public 
VoteMap(id)
{
    
g_VoteStarted true
    
new menuitem1[64], menuitem2[64];
    new 
menu menu_create("Choose 2 Maps:""VoteMap_Handler")
    if(
g_Map1[id])
    {
        
format(menuitem1charsmax(menuitem1), "%s"g_Map1)
        
menu_additem(menumenuitem1"1"0)
    }
    else
    {
        
menu_additem(menu"Select Map1""1"0)
    }
    if(
g_Map2[id])
    {
        
format(menuitem2charsmax(menuitem2), "%s"g_Map2)
        
menu_additem(menumenuitem2"2"0)
    }
    else
    {
        
menu_additem(menu"Select Map2""2"0)
    }
    if(
g_Map1[id] && g_Map2[id])
    {
        
menu_addblank(menu3)
        
menu_additem(menu"Start Vote""4"0)
    }
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
VoteMap_Handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        
menu_destroy(menu);
        
g_Map1[id] = 0
        g_Map2
[id] = 0
        g_VoteStarted 
false
        
return PLUGIN_HANDLED;
        
    }
    
    new 
data[6], iName[64];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
key str_to_num(data);
    
    switch(
key)
    {
        case 
1:
        {
            
ShowMenu1(id)
        }
        case 
2:
        {
            
ShowMenu2(id)
        }
        case 
4:
        {
            for(new 
1<= g_maxplayersi++)
            {
                if(
is_user_connected(i))
                {
                    new 
szName[33]
                    
g_Vote1[id] = 0
                    g_Vote2
[id] = 0
                    StartVote
(i)
                    
get_user_name(idszNamecharsmax(szName))
                    
ChatColor(i"%s[AMXX] %s[%s] %shas Started a vote"GreenTeamszNameGreen)
                    
set_task(10.0"EndVote");
                    return 
PLUGIN_CONTINUE
                
}
            }
        }
    }
    
menu_destroy(menu);
    return 
PLUGIN_CONTINUE
}

public 
ShowMenu1(id)
{
    new 
menu menu_create("Choose first Map:""Menu1_Handler")
    
    
menu_additem(menu"de_dust2""1"0);
    
menu_additem(menu"de_dust""2"0);
    
menu_additem(menu"de_inferno""3"0);
    
menu_additem(menu"de_nuke32""4"0)
    
menu_additem(menu"awp_aztecwave""5"0);
    
menu_additem(menu"de_aztec""6"0);
    
menu_additem(menu"de_train32""7"0);
    
menu_additem(menu"35hp_2""8"0)
    
menu_additem(menu"awp_india""9"0);
    
menu_additem(menu"cs_italy32""10"0);
    
menu_additem(menu"scoutzknivez""11"0);
    
menu_additem(menu"cs_militia""12"0)
    
menu_additem(menu"scoutmap""13"0)
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
Menu1_Handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        
VoteMap(id)
    }
    
    new 
data[6], iName[64];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
key str_to_num(data);
    
    switch(
key)
    {
        case 
1format(g_Map1charsmax(g_Map1), "%s"" de_dust2")
        case 
2format(g_Map1charsmax(g_Map1), "%s"" de_dust")
        case 
3format(g_Map1charsmax(g_Map1), "%s"" de_inferno")
        case 
4format(g_Map1charsmax(g_Map1), "%s"" de_nuke32")
        case 
5format(g_Map1charsmax(g_Map1), "%s"" awp_aztecwave")
        case 
6format(g_Map1charsmax(g_Map1), "%s"" de_aztec")
        case 
7format(g_Map1charsmax(g_Map1), "%s"" de_train32")
        case 
8format(g_Map1charsmax(g_Map1), "%s"" 35hp_2")
        case 
9format(g_Map1charsmax(g_Map1), "%s"" awp_india")
        case 
10format(g_Map1charsmax(g_Map1), "%s"" cs_italy32")
        case 
11format(g_Map1charsmax(g_Map1), "%s"" scoutzknivez")
        case 
12format(g_Map1charsmax(g_Map1), "%s"" cs_militia")
        case 
13format(g_Map1charsmax(g_Map1), "%s"" scoutmap")
    }
    
VoteMap(id)
}

public 
ShowMenu2(id)
{
    new 
menu menu_create("Choose a Map:""Menu2_Handler")
    
menu_additem(menu"de_dust2""1"0);
    
menu_additem(menu"de_dust""2"0);
    
menu_additem(menu"de_inferno""3"0);
    
menu_additem(menu"de_nuke32""4"0)
    
menu_additem(menu"awp_aztecwave""5"0);
    
menu_additem(menu"de_aztec""6"0);
    
menu_additem(menu"de_train32""7"0);
    
menu_additem(menu"35hp_2""8"0)
    
menu_additem(menu"awp_india""9"0);
    
menu_additem(menu"cs_italy32""10"0);
    
menu_additem(menu"scoutzknivez""11"0);
    
menu_additem(menu"cs_militia""12"0)
    
menu_additem(menu"scoutmap""13"0)
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
Menu2_Handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        
VoteMap(id)
    }
    
    new 
data[6], iName[64];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
key str_to_num(data);
    
    switch(
key)
    {
        
        case 
1format(g_Map2charsmax(g_Map2), "%s"" de_dust2")
        case 
2format(g_Map2charsmax(g_Map2), "%s"" de_dust")
        case 
3format(g_Map2charsmax(g_Map2), "%s"" de_inferno")
        case 
4format(g_Map2charsmax(g_Map2), "%s"" de_nuke32")
        case 
5format(g_Map2charsmax(g_Map2), "%s"" awp_aztecwave")
        case 
6format(g_Map2charsmax(g_Map2), "%s"" de_aztec")
        case 
7format(g_Map2charsmax(g_Map2), "%s"" de_train32")
        case 
8format(g_Map2charsmax(g_Map2), "%s"" 35hp_2")
        case 
9format(g_Map2charsmax(g_Map2), "%s"" awp_india")
        case 
10format(g_Map2charsmax(g_Map2), "%s"" cs_italy32")
        case 
11format(g_Map2charsmax(g_Map2), "%s"" scoutzknivez")
        case 
12format(g_Map2charsmax(g_Map2), "%s"" cs_militia")
        case 
13format(g_Map2charsmax(g_Map2), "%s"" scoutmap")
    }
    
VoteMap(id)
}

public 
StartVote(id)
{
    new 
menuitem1[64], menuitem2[64];
    new 
menu menu_create("Which map do you want?""Vote_Handler")
    
    
format(menuitem1charsmax(menuitem1), "%s"g_Map1[id])
    
format(menuitem2charsmax(menuitem2), "%s"g_Map2[id])
    
    
menu_additem(menumenuitem1"1"0);
    
menu_additem(menumenuitem2"2"0);
    
menu_addblank(menu3)
    
menu_additem(menu"None""4"0)
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
Vote_Handler(idmenuitem)
{
    if( 
item == MENU_EXIT)
    {
        
menu_destroy(menu);
        return 
PLUGIN_HANDLED;
    }
    new 
data[6], iName[64], szName[33];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
key str_to_num(data);
    
    switch(
key)
    {
        case 
1
        {
            if(
g_VoteStarted)
            {
                
g_Vote1[id] = g_Vote1[id] + 1
                get_user_name
(idszNamecharsmax(szName))
                
ChatColor(0"%s[AMXX] %s[%s] %shas chosen %s[%s]"GreenTeamszNameGreenTeamg_Map1[id])
            }
            else
            {
                
ChatColor(id"%s[AMXX] Voting time is already over"Green)
            }
        }
        case 
2:
        {
            if(
g_VoteStarted)
            {
                
g_Vote2[id] = g_Vote2[id] + 1
                get_user_name
(idszNamecharsmax(szName))
                
ChatColor(0"%s[AMXX] %s[%s] %shas chosen %s[%s]"GreenTeamszNameGreenTeamg_Map2[id])
            }
            else
            {
                
ChatColor(id"%s[AMXX] Voting time is already over"Green)
            }
        }
        case 
4menu_destroy(id)
    }
    return 
PLUGIN_CONTINUE
}

public 
EndVote(id)
{
    if(
g_Vote1[id] > g_Vote2[id])
    {
        
ChatColor(id"%s[AMXX] Map: %s[%s] %sreceived most Votes: %s[%i]"GreenTeamg_Map1[id], GreenTeamg_Vote1[id])
    }
    else if(
g_Vote2[id] > g_Vote1[id])
    {
        
ChatColor(id"%s[AMXX][VIP] Map: %s[%s] %sreceived most Votes: %s[%i]"GreenTeamg_Map2[id], GreenTeamg_Vote2[id])
    }
    else
    {
        
ChatColor(id"%s[AMXX][VIP] The vote tied at %s[%i] %svotes each"GreenTeamg_Vote1[id], Green)
    }
    
g_Vote1[id] = 0
    g_Vote2
[id] = 0
    g_Map1
[id] = 0
    g_Map2
[id] = 0
    g_VoteStarted 
false
}

stock ChatColor(const id, const input[], any:...) 
{
    new 
count 1players[32]
    static 
msg[191]
    
vformat(msg190input3)
    
    
replace_all(msg190"!g""^4")
    
replace_all(msg190"!y""^1")
    
replace_all(msg190"!t""^3")
    
    if (
idplayers[0] = id; else get_players(playerscount"ch")
    {
        for (new 
0counti++)
        {
            if (
is_user_connected(players[i]))
            {
                
message_begin(MSG_ONE_UNRELIABLE,g_msgSayText,_players[i])
                
write_byte(players[i]);
                
write_string(msg);
                
message_end();
            }
        }
    }


I know that my code can be easily optimized , i could even use enums ... but i made it like this for the time being ,

Problem : When i try and select a map , most of the time , the map doesnt get added to the main menu , some times it gets added but when i vote it goes all crazy .... , like "de_dust2" becomes "ust2"
Any help would be appreciated

Exolent[jNr] 04-20-2012 09:37

Re: Custom Vote
 
You should just have a global array of maps instead of harcoding it everywhere.
It will also fix your problem.

PHP Code:


#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Test"
#define VERSION "1.0"
#define AUTHOR "Epic"

new const gMapNames[][] = {
    
"de_dust2",
    
"de_dust",
    
"de_inferno",
    
"de_nuke32",
    
"awp_aztecwave",
    
"de_aztec",
    
"de_train32",
    
"35hp_2",
    
"awp_india",
    
"cs_italy32",
    
"scoutzknivez",
    
"cs_militia",
    
"scoutmap"
};

new 
g_Map1 = -1g_Map2 = -1;
new 
g_Vote1[33], g_Vote2[33];
new 
bool:g_VoteStarted;
new 
g_maxplayersg_msgSayText;
static const 
Team[] = "^x03"
static const Green[] = "^x04"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
register_clcmd("say /test""VoteMap")
    
g_maxplayers get_maxplayers()
    
g_msgSayText get_user_msgid("SayText")

}

public 
VoteMap(id)
{
    
g_VoteStarted true
    
new menu menu_create("Choose 2 Maps:""VoteMap_Handler")
    if(
g_Map1 >= 0)
    {
        
menu_additem(menugMapNames[g_Map1], "1"0)
    }
    else
    {
        
menu_additem(menu"Select Map1""1"0)
    }
    if(
g_Map2 >= 0)
    {
        
menu_additem(menugMapNames[g_Map2], "2"0)
    }
    else
    {
        
menu_additem(menu"Select Map2""2"0)
    }
    if(
g_Map1 >= && g_Map2 >= 0)
    {
        
menu_addblank(menu3)
        
menu_additem(menu"Start Vote""4"0)
    }
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
VoteMap_Handler(idmenuitem)
{
    if( 
item == MENU_EXIT )
    {
        
menu_destroy(menu);
        
g_Map1 = -1
        g_Map2 
= -1
        g_VoteStarted 
false
        
return PLUGIN_HANDLED;
        
    }
    
    new 
data[6], iName[64];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
key str_to_num(data);
    
    switch(
key)
    {
        case 
1:
        {
            
ShowMenu1(id)
        }
        case 
2:
        {
            
ShowMenu2(id)
        }
        case 
4:
        {
            for(new 
1<= g_maxplayersi++)
            {
                if(
is_user_connected(i))
                {
                    new 
szName[33]
                    
g_Vote1[id] = 0
                    g_Vote2
[id] = 0
                    StartVote
(i)
                    
get_user_name(idszNamecharsmax(szName))
                    
ChatColor(i"%s[AMXX] %s[%s] %shas Started a vote"GreenTeamszNameGreen)
                    
set_task(10.0"EndVote");
                    return 
PLUGIN_CONTINUE
                
}
            }
        }
    }
    
menu_destroy(menu);
    return 
PLUGIN_CONTINUE
}

public 
ShowMenu1(id)
{
    new 
menu menu_create("Choose first Map:""Menu1_Handler")
    
    for(new 
0sizeof(gMapNames); i++) {
        
menu_additem(menugMapNames[i], "");
    }
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
Menu1_Handler(idmenuitem)
{
    
menu_destroy(menu);
    
    if( 
item == MENU_EXIT )
    {
        
VoteMap(id)
        return;
    }
    
    
g_Map1 item;
    
    
VoteMap(id)
}

public 
ShowMenu2(id)
{
    new 
menu menu_create("Choose a Map:""Menu2_Handler")
    
    for(new 
0sizeof(gMapNames); i++) {
        
menu_additem(menugMapNames[i], "");
    }
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
Menu2_Handler(idmenuitem)
{
    
menu_destroy(menu);
    
    if( 
item == MENU_EXIT )
    {
        
VoteMap(id)
        return;
    }
    
    
g_Map2 item;
    
    
VoteMap(id)
}

public 
StartVote(id)
{
    new 
menu menu_create("Which map do you want?""Vote_Handler")
    
    
menu_additem(menugMapNames[g_Map1], "1"0);
    
menu_additem(menugMapNames[g_Map2], "2"0);
    
menu_addblank(menu3)
    
menu_additem(menu"None""4"0)
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);
}

public 
Vote_Handler(idmenuitem)
{
    if( 
item == MENU_EXIT)
    {
        
menu_destroy(menu);
        return 
PLUGIN_HANDLED;
    }
    new 
data[6], iName[64], szName[33];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    
    new 
key str_to_num(data);
    
    switch(
key)
    {
        case 
1
        {
            if(
g_VoteStarted)
            {
                
g_Vote1[id] = g_Vote1[id] + 1
                get_user_name
(idszNamecharsmax(szName))
                
ChatColor(0"%s[AMXX] %s[%s] %shas chosen %s[%s]"GreenTeamszNameGreenTeamgMapNames[g_Map1])
            }
            else
            {
                
ChatColor(id"%s[AMXX] Voting time is already over"Green)
            }
        }
        case 
2:
        {
            if(
g_VoteStarted)
            {
                
g_Vote2[id] = g_Vote2[id] + 1
                get_user_name
(idszNamecharsmax(szName))
                
ChatColor(0"%s[AMXX] %s[%s] %shas chosen %s[%s]"GreenTeamszNameGreenTeamgMapNames[g_Map2])
            }
            else
            {
                
ChatColor(id"%s[AMXX] Voting time is already over"Green)
            }
        }
        case 
4menu_destroy(id)
    }
    return 
PLUGIN_CONTINUE
}

public 
EndVote(id)
{
    if(
g_Vote1[id] > g_Vote2[id])
    {
        
ChatColor(id"%s[AMXX] Map: %s[%s] %sreceived most Votes: %s[%i]"GreenTeamgMapNames[g_Map1], GreenTeamg_Vote1[id])
    }
    else if(
g_Vote2[id] > g_Vote1[id])
    {
        
ChatColor(id"%s[AMXX][VIP] Map: %s[%s] %sreceived most Votes: %s[%i]"GreenTeamgMapNames[g_Map2], GreenTeamg_Vote2[id])
    }
    else
    {
        
ChatColor(id"%s[AMXX][VIP] The vote tied at %s[%i] %svotes each"GreenTeamg_Vote1[id], Green)
    }
    
g_Vote1[id] = 0
    g_Vote2
[id] = 0
    g_Map1 
= -1
    g_Map2 
= -1
    g_VoteStarted 
false
}

stock ChatColor(const id, const input[], any:...) 
{
    new 
count 1players[32]
    static 
msg[191]
    
vformat(msg190input3)
    
    
replace_all(msg190"!g""^4")
    
replace_all(msg190"!y""^1")
    
replace_all(msg190"!t""^3")
    
    if (
idplayers[0] = id; else get_players(playerscount"ch")
    {
        for (new 
0counti++)
        {
            if (
is_user_connected(players[i]))
            {
                
message_begin(MSG_ONE_UNRELIABLE,g_msgSayText,_players[i])
                
write_byte(players[i]);
                
write_string(msg);
                
message_end();
            }
        }
    }



EpicMonkey 04-20-2012 16:03

Re: Custom Vote
 
Wanted to do it that way , but then thought i would try it the other way ... Thank you for replying ...
Now that i tested it , When i start the vote it doesnt show , Ive looped through all the players then showed the vote , but it doesnt show up , What seems to be the problem?

fysiks 04-21-2012 00:51

Re: Custom Vote
 
Quote:

Originally Posted by EpicMonkey (Post 1693132)
Now that i tested it , When i start the vote it doesnt show , Ive looped through all the players then showed the vote , but it doesnt show up , What seems to be the problem?

We can't read your mind, show your code.

EpicMonkey 04-21-2012 04:38

Re: Custom Vote
 
Quote:

Originally Posted by fysiks (Post 1693385)
We can't read your mind, show your code.

the code posted by exolent .....

fysiks 04-21-2012 13:22

Re: Custom Vote
 
Quote:

Originally Posted by EpicMonkey (Post 1693132)
Ive looped through all the players then showed the vote

This is not in Exolent's code, you had to add code to do this.

Sorry, I must have skipped over the switch.

EpicMonkey 04-21-2012 13:44

Re: Custom Vote
 
Quote:

Originally Posted by fysiks (Post 1693758)
This is not in Exolent's code, you had to add code to do this.

PHP Code:

case 4
        { 
            for(new 
1<= g_maxplayersi++) 
            { 
                if(
is_user_connected(i)) 
                { 
                    new 
szName[33
                    
g_Vote1[id] = 
                    g_Vote2
[id] = 
                    StartVote
(i
                    
get_user_name(idszNamecharsmax(szName)) 
                    
ChatColor(i"%s[AMXX] %s[%s] %shas Started a vote"GreenTeamszNameGreen
                    
set_task(10.0"EndVote"); 
                    return 
PLUGIN_CONTINUE 
                

            } 
        } 


fysiks 04-21-2012 13:52

Re: Custom Vote
 
Oops, I must have skipped over switch when looking through it.

EpicMonkey 04-21-2012 16:04

Re: Custom Vote
 
Quote:

Originally Posted by fysiks (Post 1693786)
Oops, I must have skipped over switch when looking through it.

yeah ....... anyways , not sure whats wrong , ive even looped through all the players


All times are GMT -4. The time now is 07:42.

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