SA-MP Forums Archive
Weird issue with a loop - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Weird issue with a loop (/showthread.php?tid=140355)



Weird issue with a loop - biltong - 08.04.2010

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


Re: Weird issue with a loop - Sascha - 08.04.2010

that is the maxplayer number...
if you add

if(IsPlayerConnected(i))

below the loop it should be fixed


Re: Weird issue with a loop - biltong - 08.04.2010

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

EDIT: Who knew, it worked! Thanks!


Re: Weird issue with a loop - dice7 - 08.04.2010

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


Re: Weird issue with a loop - biltong - 08.04.2010

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


Re: Weird issue with a loop - dice7 - 08.04.2010

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


Re: Weird issue with a loop - M4S7ERMIND - 08.04.2010

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


Re: Weird issue with a loop - biltong - 08.04.2010

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


Re: Weird issue with a loop - M4S7ERMIND - 08.04.2010

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!


Re: Weird issue with a loop - biltong - 08.04.2010

Yeah I added IsPlayerConnected, it works fine now

Thanks everwun