Also-Known-As [MySQL]
#1

So I was trying to create a Also-Known-As system using MySQL, I am not quite sure if I should use a threaded or non-threaded query on this especially I am using them both for commands and OnPlayerConnect.

OnPlayerConnect
PHP Code:
    new Cacheresultcountrows;
    
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `accounts` WHERE `IP` = '%s'"PlayerInfo[playerid][p_newIP]);
    
result mysql_query(mysqlquery);
    
cache_get_row_count(rows);
    for(new 
0rowsi++) {
        new 
aka_ip[16];
        
cache_get_value_name(i"IP"aka_ip);
        if(
IpMatch(aka_ipPlayerInfo[playerid][p_newIP])) {
            
count ++;
        }
    }
    if(
count) {
        
format(querysizeof(query), "[Warning] "white"%s may have connected before to the server with a different name. (/aka %d)"GetName(playerid), playerid);
        foreach(
Playeri) if(PlayerInfo[i][p_Admin] >= 1) {
            
SendClientMessage(iCOLOR_REDquery);
        }
        
printf("[Warning] %s may have connected before to the server with a different name. (/aka %d)"GetName(playerid), playerid);
    }
    
cache_delete(result); 
/aka
PHP Code:
CMD:aka(playeridparams[])
{
    if(!
PlayerInfo[playerid][p_Logged]) return SendClientMessage(playerid0xFFFFFFFF">> "red"You are not logged in.");
    if(
PlayerInfo[playerid][p_Admin] < && !IsPlayerAdmin(playerid)) return SendClientMessage(playerid0xFFFFFFFF">> "red"You are not an administrator to use this command.");
    new 
idCacheresultaka_names[5][MAX_PLAYER_NAME], countrowsquery[128], string[135];
    if(
sscanf(params"u"id)) return SendClientMessage(playeridCOLOR_RED"Usage: "white"/aka [playerid]");
    if(
id == INVALID_PLAYER_ID) return SendClientMessage(playerid0xFFFFFFFF">> "red"Player not connected.");
    if(
PlayerInfo[playerid][p_Admin] < PlayerInfo[id][p_Admin]) return SendClientMessage(playerid0xFFFFFFFF">> "red"You cannot use this command on higher admin.");
    
mysql_format(mysqlquerysizeof(query), "SELECT * FROM `accounts` WHERE `IP` = '%s'"PlayerInfo[id][p_newIP]);
    
result mysql_query(mysqlquery);
    
cache_get_row_count(rows);
    for(new 
0rowsi++) {
        new 
aka_ip[16];
        
cache_get_value_name(i"IP"aka_ip);
        if(
IpMatch(aka_ipPlayerInfo[id][p_newIP])) {
            if(
count sizeof(aka_names)) {
                
cache_get_value_name(i"NAME"aka_names[i]);
            }
            
count ++;
        }
    }
    
    
format(stringsizeof(string), ">> "green"%s is also known as... (IP: %s)"GetName(id), PlayerInfo[id][p_newIP]);
    
SendClientMessage(playeridCOLOR_GREENstring);
    
format(stringsizeof(string), "%s"aka_names[0]);
    for (new 
1= ((count sizeof (aka_names)) ? (sizeof (aka_names)) : (count)); ji++)
    {
        if (
== (1))
        {
            if (
count sizeof (aka_names))
                
format(stringsizeof(string), "%s, %s and %d more."stringaka_names[i], (count j));
            else
                
format(stringsizeof(string), "%s and %s."stringaka_names[i]);
        } else {
              
format(stringsizeof(string), "%s, %s"stringaka_names[i]);
        }
    }
    
    if(!
count) {
        
format(stringsizeof(string), "This player has no other names used in the server.");
    }
    
SendClientMessage(playeridCOLOR_GREYstring);
    
cache_delete(result);
    return 
1;

Based off from Gammix's /aka, I tried doing a version of my own and so far it works great (performance-wise? *shrugs*)
Reply
#2

Comparing the IPs does not make sense as it fetches only records that match the IP to begin with. What you need is COUNT aggregate function which returns 1 row with the counter.

The data are not accessed too frequently so I would get rid of the array and execute the query each time. But you need to select only the name from the database and not everything.

The way it is done, it can only store 1 IP. Create a table to track the activity of the players.
Reply
#3

Use MySQL wildcards and here's how your code should look like:

You can use some wildcard magic to detect similar IPs.

PHP Code:
mysql_format(mysqlquerysizeof query"SELECT * FROM `accounts` WHERE `IP` = '%s'"PlayerInfo[playerid][p_newIP]);
mysql_pquery(mysqlquery"CheckAKA""i"playerid); 
PHP Code:
forward CheckAKA(playerid);
public 
CheckAKA(playerid) {

    new
        
rowsip[18];

    
cache_get_row_count(rows);

    for (new 
irowsi++) {

        
cache_get_value_name(i"IP"ip);

        if (
IpMatch(ipPlayerInfo[playerid][p_newIP]))
            
count ++;
    }

    if (
count) {
        
format(querysizeof(query), "[Warning] "white"%s may have connected before to the server with a different name. (/aka %d)"GetName(playerid), playerid);
        
        foreach (new 
Player) {
            if (!
PlayerInfo[i][p_Admin])
                continue;

            
SendClientMessage(iCOLOR_REDquery);
        }
    }

    return 
1;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)