Is there a smarter way to do this ?
#1

Hey guys, this is a mysql CMD to retrieve all registered accounts on an IP address, do you have any suggestions to make it faster & more efficient ?
PHP код:
CMD:checkalts(playeridparams[])
{
    if(!
IsAllowed(playerid3)) return NoAuth(playerid);
    new 
playerb;
    if(
sscanf(params"u"playerb)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /checkalts [playerid]");
    if(!
IsPlayerConnected(playerb)) return SendClientMessage(playeridCOLOR_WHITE"Invalid ID.");
    if(
IsImmune(playeridplayerb)) return NotImmune(playerid);
    new 
query[128], Cache:resultrowstempname[24], tempdate[12];
    
mysql_format(mysqlquerysizeof(query), "SELECT `Username`, `Score`, `Registration` FROM `Users` WHERE `IP`='%s' ORDER BY `UserID`"RPIP(playerb));
    
result mysql_query(mysqlquery);
    
rows cache_get_row_count(mysql);
    
format(querysizeof(query), "Active & Registered accounts on %s."RPIP(playerb));
    
SendClientMessage(playeridCOLOR_REPORTquery);
    if(
rows)
    {
        for(new 
0rowsi++)
        {
            
cache_get_field_content(i"Username"tempnamemysqlsizeof(tempname));
            
cache_get_field_content(i"Registration"tempdatemysqlsizeof(tempdate));
            
format(querysizeof(query), "Username: %s || Registration Date: %s || Score: %d"tempnametempdatecache_get_field_content_int(i"Score"));
            
SendClientMessage(playeridCOLOR_REDquery);
        }
    }
    
cache_delete(result);
    return 
1;

Reply
#2

Check if the player is INVALID_PLAYER_ID instead of checking if the player is not connected.
Don't use SendClientMessage inside the loop.
You don't need to use sizeof in the cache_get.., It basically get the sizeof the destination. ( max_len = sizeof(destination)).

PHP код:
CMD:checkalts(playeridparams[])
{
    if(!
IsAllowed(playerid3)) return NoAuth(playerid);
    new 
playerb;
    if(
sscanf(params"u"playerb)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /checkalts [playerid]");
    if(
playerb == INVALID_PLAYER_ID) return SendClientMessage(playeridCOLOR_WHITE"Invalid ID.");
    if(
IsImmune(playeridplayerb)) return NotImmune(playerid);
    new 
query[128], Cache:resulttempname[24], tempdate[12];
    
mysql_format(mysqlquerysizeof(query), "SELECT `Username`, `Score`, `Registration` FROM `Users` WHERE `IP` = '%s' ORDER BY `UserID`"RPIP(playerb));
    
result mysql_query(mysqlquery);
    new 
rows cache_num_rows();
    
format(querysizeof(query), "Active & Registered accounts on %s."RPIP(playerb));
    
SendClientMessage(playeridCOLOR_REPORTquery);
    if(
rows)
    {
        for(new 
irowsi++)
        {
            
cache_get_field_content(i"Username"tempname); // use the connection handle if you have option DUPLICATE_CONNECTION
            
cache_get_field_content(i"Registration"tempdate);  // use the connection handle if you have option DUPLICATE_CONNECTION
            
format(querysizeof(query), "Username: %s || Registration Date: %s || Score: %d"tempnametempdatecache_get_field_content_int(i"Score"));
        }
        
SendClientMessage(playeridCOLOR_REDquery);
    }
    
cache_delete(result);
    return 
1;

I think it's good now.
Reply
#3

Quote:
Originally Posted by oMa37
Посмотреть сообщение
Check if the player is INVALID_PLAYER_ID instead of checking if the player is not connected.
Don't use SendClientMessage inside the loop.
You don't need to use sizeof in the cache_get.., It basically get the sizeof the destination. ( max_len = sizeof(destination)).

PHP код:
CMD:checkalts(playeridparams[])
{
    if(!
IsAllowed(playerid3)) return NoAuth(playerid);
    new 
playerb;
    if(
sscanf(params"u"playerb)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /checkalts [playerid]");
    if(
playerb == INVALID_PLAYER_ID) return SendClientMessage(playeridCOLOR_WHITE"Invalid ID.");
    if(
IsImmune(playeridplayerb)) return NotImmune(playerid);
    new 
query[128], Cache:resulttempname[24], tempdate[12];
    
mysql_format(mysqlquerysizeof(query), "SELECT `Username`, `Score`, `Registration` FROM `Users` WHERE `IP` = '%s' ORDER BY `UserID`"RPIP(playerb));
    
result mysql_query(mysqlquery);
    new 
rows cache_num_rows();
    
format(querysizeof(query), "Active & Registered accounts on %s."RPIP(playerb));
    
SendClientMessage(playeridCOLOR_REPORTquery);
    if(
rows)
    {
        for(new 
irowsi++)
        {
            
cache_get_field_content(i"Username"tempname); // use the connection handle if you have option DUPLICATE_CONNECTION
            
cache_get_field_content(i"Registration"tempdate);  // use the connection handle if you have option DUPLICATE_CONNECTION
            
format(querysizeof(query), "Username: %s || Registration Date: %s || Score: %d"tempnametempdatecache_get_field_content_int(i"Score"));
        }
        
SendClientMessage(playeridCOLOR_REDquery);
    }
    
cache_delete(result);
    return 
1;

I think it's good now.
Actually I have to, since I want to present ALL the accounts on the same IP.
Thanks for the INVALID PLAYER ID tip, are you sure it should work and not fuck up the system ?
Reply
#4

Quote:
Originally Posted by NeXoR
Посмотреть сообщение
Thanks for the INVALID PLAYER ID tip, are you sure it should work and not fuck up the system ?
INVALID_PLAYER_ID means the ID is not exists, which means the player is not connected, so yeah it wont screw up anything.
Reply
#5

Then why not just use !IsPlayerConnected? What if somehow the player ID is 10 and player ID 10 isn't connected? Then your logic fails. IsPlayerConnected works for existing AND invalid IDs, I think the original method was fine.

EDIT: I thought you said IsPlayerConnected wouldn't work in this situation. Either you edited it or I completely misread it.
Reply
#6

Checking if target ID is INVALID_PLAYER_ID is much better than calling IsPlayerConnected function.
Reply
#7

SSCANF searches for the player with the specified ID/name (only with 'u', 'q' and 'r'). So, checking it against a constant value IS more efficient. Moreover, doing the search again with IsPlayerConnected is redundant and basically means repetitive code which should be avoided at all times.
Reply
#8

Yes, I'm aware of that. Lol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)