AlliedModders

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

pupdebox 05-08-2016 15:54

Help vote
 
Code:

public vote()
{
       
        new ad[32],sznum[6]
        new menu = menu_create("\rVote For CT","vote_handler")
        for(new i = 1;i<=get_maxplayers();i++)
        if(is_user_connected(i) && get_user_team(i) == 2)
       
       
       
        {
                num_to_str(i,sznum,5)
                get_user_name(i,ad,31)
                menu_additem(menu,ad,sznum)
               
               
               
        }
        menu_display(id,menu, 0)
        return PLUGIN_HANDLED
}

I can show all cts in menu but i dont know how to select the most voted player how to do it? im newbie help please

OciXCrom 05-08-2016 18:45

Re: Help vote
 
The first step is to post the menu's handler.

pupdebox 05-08-2016 19:32

Re: Help vote
 
may be like that ? i dont know somebody helpppp
Code:

public vote_handler(id,menu,item)
{
        if(item == MENU_EXIT)
       
       
       
        {
                menu_destroy(menu)
                return PLUGIN_HANDLED
               
               
               
        }

        new ad[32],callback,access,data[6]
        menu_item_getinfo(menu,item,access,data,5,ad,31,callback)
        new tid = str_to_num(data)
        g_votes[tid]++

        }


siriusmd99 05-11-2016 09:38

Re: Help vote
 
Try :


PHP Code:

//Add these 2 defines at the top of your plugin
#define TIME 10.0  //This is time after results will be checked, you can change it's value
#define TASK_CHECK 6459 //This is task id used to check if another task is not in process

public vote()
{
    if(
task_exists(TASK_CHECK)){
        return 
PLUGIN_HANDLED
        
//Blocking vote if another one is started or is in process

    
new ad[32],sznum[6]
    new 
menu menu_create("\rVote For CT","vote_handler")
    for(new 
1;i<=get_maxplayers();i++)
    if(
is_user_connected(i) && get_user_team(i) == 2)
    
    
    
    {
        
num_to_str(i,sznum,5)
        
get_user_name(i,ad,31)
        
menu_additem(menu,ad,sznum)
        
        
        
    }
    
arrayset(g_votes33)//Cleaning the array before vote
    
menu_display(id,menu0)
    
set_task(TIMEcheck_resultTASK_CHECK//Setting a task to check our results
    
return PLUGIN_HANDLED
}

public 
vote_handler(id,menu,item)
{
    if(
item == MENU_EXIT)
    
    
    
    {
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED
        
        
        
    
}

        new 
ad[32],callback,access,data[6]
        
menu_item_getinfo(menu,item,access,data,5,ad,31,callback)
        new 
tid str_to_num(data)
        
g_votes[tid]++

}

public 
check_results(){

  new 
leader;
  
  for(new 
i33i++){
  
  for(new 
j33j++){
  
  if(
g_votes[i] < g_votes[j])
  break;
  
  if(
j==32)
  
leader=g_votes[i]
  }
  }
  if(
is_user_connected(leader))
  {
  
name[32]
  
get_user_name(leadername31)
  
client_print(0print_chat"[VOTE RESULT] The most voted player is ^"%s^""name)
  }else{
  
client_print(0print_chat"[VOTE RESULT] Vote has failed. Player is not connected")
  }



Black Rose 05-18-2016 15:41

Re: Help vote
 
By saving the current highest current value, you don't have to loop everything in two dimensions. Just compare every entry to the previously stored value.

Easy version:
Code:
    new best = 0     for ( new i = 1 ; i < sizeof votes ; i++ ) {         if ( votes[i] > votes[best] ) {             best = i         }     }     // best will contain the player index that won. And you can use votes[best] for the number of votes.
The problem is the player index doesn't change until you rejoin the server and that may get unfair if two players always get the same amount of votes. So then you will have to determine if there are mutliple winners and perhaps randomly select one of them.

Complicated version:
Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");         new votes[33];     new best;     new number;     for ( new i ; i < sizeof votes ; i++ ) {         votes[i] = random(10);         server_print("%d: %d", i, votes[i]);     }     for ( new i = 1 ; i < sizeof votes ; i++ ) {         switch ( clamp(votes[i] - votes[best], -1, 1) ) { // This compares how many more votes the person "i" got than person "best(so far)".             case 0 : number++; // "i" has the same number of votes as the current leader.             case 1 : { // "i" is the new leader.                 best = i;                 number = 1;             }             // default : { } Nobody cares about a loser.         }     }     server_print("Highest value is %d, which %d people got.", votes[best], number); }

Another, probably better option is creating an array storing both the player index and the value, using bubble sort on them and looping from the top until you reach a lower value. That is of course if you want multiple results.
Code:
#include <amxmodx> enum {     PlayerIndex,     Score } new ArrayWithPlayers[33][2]; public plugin_init() {     register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");     server_print("Before sorting:^n");     for ( new i = 1 ; i < sizeof ArrayWithPlayers ; i++ ) {         ArrayWithPlayers[i][PlayerIndex] = i;         ArrayWithPlayers[i][Score] = random(10);         server_print("%2d: PID: %2d, Score: %d", i, ArrayWithPlayers[i][PlayerIndex], ArrayWithPlayers[i][Score]);     }     SortCustom2D(ArrayWithPlayers, sizeof ArrayWithPlayers, "MySortFunc");     server_print("^nAfter sorting:^n");     for ( new i = 0 ; i < sizeof ArrayWithPlayers ; i++ ) {         server_print("%2d: PID: %2d, Score: %d", i, ArrayWithPlayers[i][PlayerIndex], ArrayWithPlayers[i][Score]);     } } public MySortFunc(const elem1[], const elem2[])     return clamp(elem2[Score] - elem1[Score], -1, 1); // Clamp just in case. It seems to work without it but I'm not taking that risk.
Code:

Before sorting:

 1: PID:  1, Score: 9
 2: PID:  2, Score: 5
 3: PID:  3, Score: 3
 4: PID:  4, Score: 7
 5: PID:  5, Score: 4
 6: PID:  6, Score: 1
 7: PID:  7, Score: 8
 8: PID:  8, Score: 6
 9: PID:  9, Score: 4
10: PID: 10, Score: 0
11: PID: 11, Score: 4
12: PID: 12, Score: 0
13: PID: 13, Score: 9
14: PID: 14, Score: 2
15: PID: 15, Score: 6
16: PID: 16, Score: 1
17: PID: 17, Score: 4
18: PID: 18, Score: 1
19: PID: 19, Score: 1
20: PID: 20, Score: 2
21: PID: 21, Score: 4
22: PID: 22, Score: 1
23: PID: 23, Score: 0
24: PID: 24, Score: 0
25: PID: 25, Score: 1
26: PID: 26, Score: 1
27: PID: 27, Score: 3
28: PID: 28, Score: 9
29: PID: 29, Score: 5
30: PID: 30, Score: 2
31: PID: 31, Score: 6
32: PID: 32, Score: 0

After sorting:

 0: PID:  1, Score: 9
 1: PID: 28, Score: 9
 2: PID: 13, Score: 9
 3: PID:  7, Score: 8
 4: PID:  4, Score: 7
 5: PID: 15, Score: 6
 6: PID: 31, Score: 6
 7: PID:  8, Score: 6
 8: PID:  2, Score: 5
 9: PID: 29, Score: 5
10: PID: 11, Score: 4
11: PID:  9, Score: 4
12: PID:  5, Score: 4
13: PID: 21, Score: 4
14: PID: 17, Score: 4
15: PID:  3, Score: 3
16: PID: 27, Score: 3
17: PID: 14, Score: 2
18: PID: 20, Score: 2
19: PID: 30, Score: 2
20: PID: 19, Score: 1
21: PID: 22, Score: 1
22: PID:  6, Score: 1
23: PID: 18, Score: 1
24: PID: 16, Score: 1
25: PID: 25, Score: 1
26: PID: 26, Score: 1
27: PID: 23, Score: 0
28: PID: 12, Score: 0
29: PID: 10, Score: 0
30: PID:  0, Score: 0
31: PID: 24, Score: 0
32: PID: 32, Score: 0



All times are GMT -4. The time now is 18:35.

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