Is there a way to make this CMD shorter ?
#1

Hey guys, pretty complicated for a damn ban CMD, is there a way to make it shorter/better ?

PHP Code:
CMD:banip(playeridparams[])
{
    if(!
IsAuth(playerid3)) return NoAuth(playerid);
    new 
ip[16];
    if(
sscanf(params"s[16]s[128]"ipparams)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /banip [IP] [reason]");
    if(
RIPID(ip) > -1)
    {
        new 
string[248];
        
format(stringsizeof(string), "This IP is connected to the server using the name %s (ID: %d), use /ban instead."RPN(RIPID(ip)), RIPID(ip));
        
SendClientMessage(playeridCOLOR_WHITEstring);
        return 
1;
    }
    new 
Cache:resultquery[128];
    
mysql_format(mysqlquerysizeof(query), "SELECT `AdminLevel`, `Username` from `Admins` WHERE `IP`='%e'"ip);
    
result mysql_query(mysqlquery);
    if(
cache_get_row_count(mysql))
    {
        new 
adminlevelaname[MAX_PLAYER_NAME], string[400];
        
adminlevel cache_get_row_int(00mysql);
        
cache_get_row(01anamemysqlsizeof(aname));
        if(
aInfo[playerid][aLevel] < 5SendClientMessage(playeridCOLOR_WHITE"This IP is registered to an Administrator, You have to be Elite Admin or above to ban him.");
          else if(
adminlevel aInfo[playerid][aLevel])
        {
            
format(string ,sizeof(string), "This IP is registered to %s, who is a %d level Administrator, you can't ban him."anameadminlevel);
            
SendClientMessage(playeridCOLOR_WHITEstring);
        }
        else
        {
            
format(stringsizeof(string), "%s %s has banned IP Address %s (Registered to %s), Reason: %s"RPARN(playerid), RPN(playerid), ipanameparams);
            
SendAMessage(1string);
            new 
ban[30];
            
format(bansizeof(ban), "banip %s"ip);
            
SendRconCommand(ban);
            
Log("logs/bans.log"string);
        }
        
cache_delete(result);
        return 
1;
    }
    else
    {
        new 
string[248];
        
format(stringsizeof(string), "%s %s has banned IP Address %s, Reason: %s"RPARN(playerid), RPN(playerid), ipparams);
        
SendAMessage(1string);
        
Log("logs/bans.log"string);
          new 
ban[30];
        
format(bansizeof(ban), "banip %s"ip);
        
SendRconCommand(ban);
        
cache_delete(result);
        return 
1;
    }

Thanks in advice.
Reply
#2

Anyone?
Reply
#3

Your code looks a little messy for me. Those huge cells for strings aren't required nor you need to create a string for each messages. Also, you can use a callback with mysql_tquery instead of using cached variables.
Reply
#4

Use strlib (https://sampforum.blast.hk/showthread.php?tid=362764) instead of a dozen of format.
Reply
#5

Shorter code doesn't mean better.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)