Raised This Month: $ Target: $400
 0% 

Iterating over players to get info


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
supergreg
Senior Member
Join Date: Jul 2007
Location: Oslo, Norway
Old 06-22-2009 , 18:22   Iterating over players to get info
Reply With Quote #1

I am having some problems iterating over the players to get their rates.
First I was just using this:
PHP Code:
for ( 1<= 32i++ )
{
    if ( !
is_user_bot(i) && is_user_connected(i) )
    {
        
// work my magic
    
}

Then I read in a thread here that looping over players this way may lead to unreliable results, like if a client disconnects and leaves an index declared but pointing to "null"/nonexisting player.
Question 1. Doesn't the user_connected() check eliminate events where that could be a real problem?

So I copied some code (thanks) from that thread which I cannot find again, and had:
PHP Code:
new players[32], numi;
get_players(playersnum);
for ( 
1<= numi++ )
{
    new 
player players[i];
    if ( !
is_user_bot(player) && is_user_connected(player) )
    {
        
// work my magic
    
}
    else
    {
        
// woops.
        // dumped values: i = 1. player = 0.
    
}

This was pretty much copy+paste, but gave me problems. It triggered the else-clause. I dumped my variables as you can see in the comments. I tried this while I was alone on the server, I had client id 1, and it tried to query client 0.
Question 2. Am I just being a moron here, did I implement the code snippet incorrectly, or was that iterating-players snippet I copied really for other use cases? What would be different?

Current code:
PHP Code:
new lineOut[BIG_BUFF 1];
new 
players[MAX_PLAYERS], numi;
get_players(playersnum);
for ( 
1<= numi++ )
{
    if ( !
is_user_bot(i) && is_user_connected(i) )
    {
        new 
tempNick[MAX_NICKLENGTH 1];
        
copy(tempNickMAX_NICKLENGTHg_szNicks[i]);
        
replace_all(tempNickMAX_NICKLENGTH"<""");
        
replace_all(tempNickMAX_NICKLENGTH">""");

        
get_user_info(i"rate"g_iRates[i], MAX_RATE);
        
get_user_info(i"cl_updaterate"g_iClUpdateRates[i], MAX_RATE);
        
get_user_info(i"cl_dlmax"g_iDlMax[i], MAX_RATE);

        
query_client_cvar(i"cl_cmdrate""cvarCheck");
        
query_client_cvar(i"ex_interp""cvarCheck");
        
//query_client_cvar(player, "cl_rate", "cvarCheck");

        
formatex(lineOutBIG_BUFF"rate: %s^tcl_updaterate: %s^tcl_cmdrate: %s^tex_interp: %s^tcl_dlmax: %s^t^t| %s"g_iRates[i], g_iClUpdateRates[i], g_iClCmdRates[i], g_iInterp[i], g_iDlMax[i], tempNick);
        
client_print(idprint_consolelineOut);          
    }
    else
    {
        
server_print("reported as bot/not connected. Stored nick %s. i=%i. player=%i."g_szNicks[i], iplayer);
    }

I am currently using this, it seems to work fine in my tests (1-3 players). I am wondering though if this code falls victim of Question 1. The only problem I currently have with this code is that sometimes the values which needs to be ran through query_client.. (like interp) is not reported on the first run of this function, but on all consecutive runs.
Question 3. Why are interp+cmdrate values blank after the first run, what can I do to avoid that?
Question 4. Why does querying for "cl_rate" always yield errors or bad response?

Sample output for the last code:
Code:
] amx_listrates 
~~~~~~~~~~~~~~~~~~~~~~~ Listing player rates ~~~~~~~~~~~~~~~~~~~~~~~
rate: 25000    cl_updaterate: 80    cl_cmdrate:     ex_interp:     cl_dlmax: 100    cc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
] amx_listrates  
~~~~~~~~~~~~~~~~~~~~~~~ Listing player rates ~~~~~~~~~~~~~~~~~~~~~~~
rate: 25000    cl_updaterate: 80    cl_cmdrate: 76    ex_interp: 0.013    cl_dlmax: 100    cc
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sorry for such a lengthy post on this, hopefully it is generic enough to be of use to many others.

Last edited by supergreg; 06-22-2009 at 18:25. Reason: add sample output
supergreg is offline
 



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 15:44.


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