Raised This Month: $51 Target: $400
 12% 

assign random number


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
666
Senior Member
Join Date: Mar 2015
Old 12-15-2018 , 11:34   assign random number
Reply With Quote #1

good morning, evenings, nights

I'm using this add-on which assigns a number to the player by joining the team until that point everything works fine.

but I want to improve it because I've noticed that when a player disconnects and connects another player assigns him a number that another player already has

example:
if 5 players join the CT team, it assigns a unique number to each player without repeating

PLAYER 1: NUMBER - 4
PLAYER 2: NUMBER - 5
PLAYER 3: NUMBER - 2
PLAYER 4: NUMBER - 3
PLAYER 5: NUMBER - 1

if player 4 is disconnected with the number 3, when another player joins, a random number is assigned, what I want is for him to assign the number of player 4 that was disconnected

PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

new NUM_PLAYER[32]
const 
MaxRandom 5;
new 
AllNumbers[ ( MaxRandom >> ) + ];
new 
bool:g_bJoined[33];

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_event"TeamInfo""join_team""a")
}

PRE_RANDOM_TTiRanMin iRanMax )
{
    for ( new 
iRanMin <= iRanMax i++ )
        
AllNumbers>> ] |= ( << );
}

public 
GET_RANDOM_TTiRanMin iRanMax )
{
    static 
UsedNumbers[ ( MaxRandom >> ) + ];
    new 
iRandom bool:bAllUsedTT=true;
    
    if ( 
iRanMax MaxRandom )
        
set_fail_state"Must increase MaxRandom value: %d > %d" iRanMax MaxRandom );
        
    
//Check if all numbers have already bee nused, if so this will reset the 'used' variable
    
for ( new sizeofUsedNumbers ) ; i++ )
    {
        if ( 
AllNumbers] && ( AllNumbers] != UsedNumbers] ) )
        {
            
bAllUsedTT false;
            break;
        }
    }
    
    
//If all random numbers have already been used, reset variable so they can be re-used
    
if( bAllUsedTT )
        
arraysetUsedNumbers sizeofUsedNumbers ) );
        
    
//Find random number
    
do
    {
        
iRandom random_numiRanMin iRanMax );
    }
    while ( ( 
UsedNumbersiRandom >> ] & ( << iRandom ) ) )
    
    
//Set current random number as used
    
UsedNumbersiRandom >> ] |= ( << iRandom );
    
    
//Return random number
    
return iRandom;
}

PRE_RANDOM_CTiRanMin iRanMax )
{
    for ( new 
iRanMin <= iRanMax i++ )
        
AllNumbers>> ] |= ( << );
}

public 
GET_RANDOM_CTiRanMin iRanMax )
{
    static 
UsedNumbers[ ( MaxRandom >> ) + ];
    new 
iRandom bool:bAllUsedCT=true;
    
    if ( 
iRanMax MaxRandom )
        
set_fail_state"Must increase MaxRandom value: %d > %d" iRanMax MaxRandom );
        
    
//Check if all numbers have already bee nused, if so this will reset the 'used' variable
    
for ( new sizeofUsedNumbers ) ; i++ )
    {
        if ( 
AllNumbers] && ( AllNumbers] != UsedNumbers] ) )
        {
            
bAllUsedCT false;
            break;
        }
    }
    
    
//If all random numbers have already been used, reset variable so they can be re-used
    
if( bAllUsedCT )
        
arraysetUsedNumbers sizeofUsedNumbers ) );
        
    
//Find random number
    
do
    {
        
iRandom random_numiRanMin iRanMax );
    }
    while ( ( 
UsedNumbersiRandom >> ] & ( << iRandom ) ) )
    
    
//Set current random number as used
    
UsedNumbersiRandom >> ] |= ( << iRandom );
    
    
//Return random number
    
return iRandom;
}

public 
client_authorized(id)
{
    
g_bJoined[id] = false;
}

public 
join_team()
{
    new 
id read_data(1);
    
    if(!
is_user_connected(id) || g_bJoined[id]) return PLUGIN_CONTINUE;
    
    static 
user_team[32];
    
read_data(2user_team31);
    new 
szName[32];
    
get_user_name(id,szName,charsmax(szName));
    
    switch(
user_team[0])
    {
        case 
'C':
        {
            
g_bJoined[id] = true;
            
PRE_RANDOM_CT);
            
NUM_PLAYER[id] = GET_RANDOM_CT(15);
            
server_print("CT: %d - player: %s"NUM_PLAYER[id],szName);
            
client_print(id,print_chat,"you are the number player: %d",NUM_PLAYER[id]);
        }
        
        case 
'T':
        {
            
g_bJoined[id] = true;
            
PRE_RANDOM_TT);
            
NUM_PLAYER[id] = GET_RANDOM_TT(15);
            
server_print("TT: %d - player: %s"NUM_PLAYER[id],szName);
            
client_print(id,print_chat,"you are the number player: %d",NUM_PLAYER[id]);
        }
    }
    
    return 
PLUGIN_CONTINUE;

666 is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 12-15-2018 , 12:41   Re: assign random number
Reply With Quote #2

you must store something in client_disconnect...i would do this, i hope it works:
PHP Code:
new bool:bLeftPlayer 
new g_iLeftPlayer

public client_disconnectid )
{
    if( 
g_bJoinedid ] )
    {
        
bLeftPlayer true
        g_iLeftPlayer 
NUM_PLAYERid ]
    }
}
public 
join_team() 

    
//,...
    
    
case 'C':
    {
        
g_bJoined[id] = true
        if( !
bLeftPlayer )
        {
            
PRE_RANDOM_CT); 
            
NUM_PLAYER[id] = GET_RANDOM_CT(15); 
        }
        else
        {
            
NUM_PLAYERid ] = g_iLeftPlayer
            bLeftPlayer 
false
        
}
        
server_print("CT: %d - player: %s"NUM_PLAYER[id],szName); 
        
client_print(id,print_chat,"you are the number player: %d",NUM_PLAYER[id]); 
    }
    case 
'T':
    {
        
//same sa above
    
}

__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 12-15-2018 at 15:18. Reason: typos
JocAnis is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-15-2018 , 14:46   Re: assign random number
Reply With Quote #3

I think the gap/missed requirement is you want no vacant numbers before generating a new higher number?

So in this data set

PLAYER 1: NUMBER - 4
PLAYER 2: NUMBER - 5
PLAYER 3: NUMBER - 2
PLAYER 4: NUMBER - 3
PLAYER 5: NUMBER - 1

Player 4 disconnects, making 3 vacant. When player 6 joins, he should get 3 instead of 6?
__________________
Bugsy is offline
666
Senior Member
Join Date: Mar 2015
Old 12-15-2018 , 14:51   Re: assign random number
Reply With Quote #4

Quote:
Originally Posted by JocAnis View Post
you must store something in client_disconnect...i would do this, i hope it works:
PHP Code:
new bool:bLeftPlayer 
new g_iLeftPlayer

public client_disconnecdid )
{
    if( 
g_bJoinedid ] )
    {
        
bLeftPlayer true
        g_iLeftPlayer 
NUM_PLAYERid ]
    }
}
public 
join_team() 

    
//,...
    
    
case 'C':
    {
        
g_bJoined[id] = true
        if( !
bLeftPlayer )
        {
            
PRE_RANDOM_CT); 
            
NUM_PLAYER[id] = GET_RANDOM_CT(15); 
        }
        else( 
bLeftPlayer )
        {
            
NUM_PLAYERid ] = g_iLeftPlayer
            bLeftPlayer 
false
        
}
        
server_print("CT: %d - player: %s"NUM_PLAYER[id],szName); 
        
client_print(id,print_chat,"you are the number player: %d",NUM_PLAYER[id]); 
    }
    case 
'T':
    {
        
//same sa above
    
}

It seems that it does not work because it does not recognize the bool: bLeftPlayer when it disconnects or something happens there, but I do not know what to do
666 is offline
666
Senior Member
Join Date: Mar 2015
Old 12-15-2018 , 14:59   Re: assign random number
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
I think the gap/missed requirement is you want no vacant numbers before generating a new higher number?

So in this data set

PLAYER 1: NUMBER - 4
PLAYER 2: NUMBER - 5
PLAYER 3: NUMBER - 2
PLAYER 4: NUMBER - 3
PLAYER 5: NUMBER - 1

Player 4 disconnects, making 3 vacant. When player 6 joins, he should get 3 instead of 6?
player 6 who joined the server must get the number 3 of player 4 that was disconnected and not another repeated number
666 is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 12-15-2018 , 15:21   Re: assign random number
Reply With Quote #6

weird...i had little typo mistakes in code, i guess you fixed it while addint to your code...
PHP Code:
public client_disconnectid )
{
        
bLeftPlayer true
        g_iLeftPlayer 
NUM_PLAYERid ]
        
client_print0print_chat"debug - playerID %d bool: %d, random_num: %d"idbLeftPlayerg_iLeftPlayer )

can you add just this to your code and then see what is working what not..
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)
JocAnis is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-15-2018 , 16:10   Re: assign random number
Reply With Quote #7

Just use the player entity index, it behaves the same way that you describe and it's nearly random.
__________________
fysiks 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 13:57.


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