Weird issue with a loop
#1

pawn Код:
public OnRconLoginAttempt(ip[], password[], success)
{
    #if defined LOG_ADMINS
    new logstring[64];
    new IP[17];
    format(IP, sizeof(IP), ip);
    if(success)
    {
      for(new i=0; i < MAX_PLAYERS; i++)
      {
        new plrip[17];
        GetPlayerIp(i, plrip, sizeof(plrip));
        if(!strcmp(IP, plrip, false, 17))
        {
          new name[MAX_PLAYER_NAME];
          GetPlayerName(i, name, sizeof(name));
          format(logstring, sizeof(logstring), "RCON: Player %s(ID %d, IP: %s) logged in.", name, i, IP);
            }
            else format(logstring, sizeof(logstring), "RCON: IP %s logged in.", IP);
        }
        WriteToLogFile(logstring);
    }
    if(!success)
    {
      for(new i=0; i < MAX_PLAYERS; i++)
      {
        new plrip[17];
        GetPlayerIp(i, plrip, sizeof(plrip));
        if(!strcmp(IP, plrip, false, 17))
        {
          new name[MAX_PLAYER_NAME];
          GetPlayerName(i, name, sizeof(name));
          format(logstring, sizeof(logstring), "RCON: Player %s(ID %d, IP: %s) tried to login as RCON, but failed.", name, i, IP);
            }
            else format(logstring, sizeof(logstring), "RCON: IP %s tried to login as RCON, but failed.", IP);
        }
        WriteToLogFile(logstring);
    }
    #endif
    return 1;
}
It works but when I open my log file it shows "RCON: Player (ID:499, IP: 127.0.0.1) has logged in."

Why does it start at 499? o.0
Reply
#2

that is the maxplayer number...
if you add

if(IsPlayerConnected(i))

below the loop it should be fixed
Reply
#3

I'll try that, but I thought GetPlayerIP already does that internally.

EDIT: Who knew, it worked! Thanks!
Reply
#4

It would in this case

pawn Код:
if (GetPlayerIp(i, plrip, sizeof(plrip)))
{
    if(!strcmp(IP, plrip, false, 17))
    {
        new name[MAX_PLAYER_NAME];
        GetPlayerName(i, name, sizeof(name));
        format(logstring, sizeof(logstring), "RCON: Player %s(ID %d, IP: %s) tried to login as RCON, but failed.", name, i, IP);
    }
}
else format(logstring, sizeof(logstring), "RCON: IP %s tried to login as RCON, but failed.", IP);
And since strcmp has the nasty issue of saying that 2 strings are the same if one is empty, you could also do length checks
Reply
#5

No point in doing length checks if the player is connected, he has to have an IP if he's connected.
Reply
#6

Better safe then sorry. Assumptions can be wrong, hence your initial problem in this topic
Reply
#7

format(IP, sizeof(IP), ip); This makes an empty string. You would need to replace it with format(IP, sizeof(IP), "%s", ip);
but I dont think its necessary to make a string for IP.. just if(!strcmp(ip, plrip, false))

EDIT: I was wrong about that
Reply
#8

Nope, got an array must be indexed error when I tried that, though for some reason the way I do it works perfectly.
Reply
#9

Alrite, I made few tests and figured out that if player isnt connected, GetPlayerIp returns 127.0.0.1, which means plrip matches
500 times with IP and finally writes id 499 in you log file, so I say you should use IsPlayerConnected or even better, switch to foreach!
Reply
#10

Yeah I added IsPlayerConnected, it works fine now

Thanks everwun
Reply


Forum Jump:


Users browsing this thread: