Raised This Month: $178 Target: $400
 44% 

Solved Random Player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Suntox
Member
Join Date: Nov 2016
Old 06-10-2018 , 20:04   Random Player
Reply With Quote #1

hi, i want to get a random player who doesn't have freeday.

freeday native : is_user_freeday(id)

PHP Code:
RandomPlayer()
{
    new 
players[32], pnum;
    
get_players(playerspnum);
   
    if( !
pnum ) return -1;
    
    return 
players[random(pnum)];


Last edited by Suntox; 06-11-2018 at 14:24.
Suntox is offline
Relaxing
Veteran Member
Join Date: Jun 2016
Location: Catlands
Old 06-11-2018 , 02:46   Re: Random Player
Reply With Quote #2

Code:
RandomPlayer() {     static id;     new players[32], pnum;     do     {         get_players(players, pnum);             if( !pnum ) return -1;         id = players[random(pnum)];     }       while (is_user_freeday(id));     return id; }
Somelike half working this?

I had to repeat the loop if the random player had freeday so it proceeds again and choses a new one.
__________________
Have A Relaxing Day | Wiki
aim_relaxing | not online



Last edited by Relaxing; 06-11-2018 at 02:48.
Relaxing is offline
Suntox
Member
Join Date: Nov 2016
Old 06-11-2018 , 14:23   Re: Random Player
Reply With Quote #3

Thanks, it's works.
Suntox is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-12-2018 , 20:11   Re: Random Player
Reply With Quote #4

Beware that Relaxing's method is non-deterministic and can freeze your server for an amount of time. It will also be an infinite loop if all players have free day (which will look like your server freezes permanently).

Here is a version that has a maximum execution time (32 iterations) and therefore will never cause an infinite loop. If no player no player is found, it will return zero.

PHP Code:
public random_nonfreeday_player()
{
    new 
iPlayers[32], iPlayersNumindex
    get_players
(iPlayersiPlayersNum)
    
    new 
iRandomIndex random(iPlayersNum)
    
    for( new 
0iPlayersNumi++ )
    {
        
index = (iRandomIndex i) % iPlayersNum
        
if( !is_user_freeday(iPlayers[index]) )
        {
            return 
iPlayers[index]
        }
    }
    
    return 
0

__________________

Last edited by fysiks; 06-12-2018 at 20:14.
fysiks is offline
Reply


Thread Tools
Display Modes

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 04:29.


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