[Tutorial] Making simple Ping checker
#1

Hello guys!Im currently learning how to script and making some functions and other stuff and i needed simple ping checker , so i checked the forums ,but i haven't found any that should be working fine.But i noticed ****** post about how normal ping checkers should be made.

Quote:

Also, there have been multiple topics on this before, and the general consensus is that kicking when people go over a limit is a bad idea because you can get lag spikes for half a second on otherwise fine players. Check if their ping is high for a while then kick them to not penalise players who don't normally lag.

Okay let's get started!

First of all color define, forward and player variable:

pawn Код:
#define CHATCOLOR_RED 0xFF0000FF // With this we're just only define color that we will use for SendClientMessage() and SendClientMessageToAll().
forward PingChecker(); // With this we register a function called PingChecker.
new bool:PingChecked[MAX_PLAYERS]; //This i a player variable which we will use to check if player ping was checked.Remember that bool type of variable can hold only "true" or "false".No numbers , no strings ,just true or false.

Creating the timer:


Post this under OnGameModeInit():
pawn Код:
/*
    Post this under OnGameModeInit

    SetTimer is the function which sets the timer , "PingChecker" is the fucntion name which we are going to call.
    60000 parameter is the time in miliseconds in this case its 1 minute.
    and the last parameter is set to "true" because we are telling to repeat.

    SetTimer(function[],time,repeat);

    */

    SetTimer("PingChecker", 60000, true);
And last , let's call the function PingChecker every minute (you can change that):


pawn Код:
public PingChecker()
{
    //Here we loop through all the players
    for(new i;i < MAX_PLAYERS;i++)
    {
        //After starting the loop we're checking if player is connected with native samp function IsPlayerConnected().
        if(IsPlayerConnected(i))
        {
            //After that we check the player ping with GetPlayerPing() if player ping is higher than 300 then A statement will be true and if player ping was checked and player variable says
            //false then B statement will be true , too.
            //---A statement---------------B statement-----------
            //------True-------------------True------------------
            if(GetPlayerPing(i) > 300 && PingChecked[i] == false) // first statement
            //----------------True and True = true - so PingCheckerFunction will procceed.
            {
                //Here we just simply send every player (that has higher than 300 ping) a message that he's been noticed
                //SendClientMessage() is function , "i" is playerid in this case it could be anyone (but with higher than 300 ping)
                //CHATCOLOR_RED is a color define of the text
                SendClientMessage(i, CHATCOLOR_RED, "Server noticed that your ping is higher than the limit (300).Please fix it and connect again or you will be kicked after 1 min.");
                //Here we set for every player (that has higher than 300 ping) that his ping was checked and we set it to "true"
                PingChecked[i] = true;
            }
            //After a minute the function will be called again and if one of the first statement cases will fail we will skip first statement and go to the second
            //After that we check the player ping with GetPlayerPing() again and if player ping is higher than 300 ,then A statement will be true and if player ping was checked and player variable says
            //false ,then B statement will be true , too.
            //---A statement---------------B statement-----------
            //------True-------------------True------------------
            if(GetPlayerPing(i) > 300 && PingChecked[i] == true) // second statemet
            //----------------True and True = true - so PingCheckerFunction will procceed.
            {
                //Here we make a local variables which are pName and string and in the brackets [] we set their size.
                //MAX_PLAYER_NAME is equal to 24 as samp native size and it cannot be changed.Unless if you #undef it i guess.
                //And simple maths MAX_PLAYER_NAME*2 = 24*2 = 48
                new pName[MAX_PLAYER_NAME] , string[MAX_PLAYER_NAME*2];
                //Here we get player name with a function GetPlayerName() ,then we store it in a variable pName and we say that the name should not be higher than MAX_PLAYER_NAME = 24.
                GetPlayerName(i, pName, sizeof(pName));
                //After that we format the string with size not higher than 48
                //And in with this %s we set that we want that pName would be typed in a chat
                //%s - for string %d - for integer %f - for float
                format(string, sizeof(string), "Server: %s was kicked for high ping.", pName);
                //Server: playername was kicked for high ping. - compiler will read it like this.I guess.
                //Here we send our formated string to all players
                SendClientMessageToAll(CHATCOLOR_RED, string);
                //Here we send player a goodbye message , but i guess it won't work because...
                SendClientMessage(i, CHATCOLOR_RED, "You were noticed about your ping ,but you didn't take it in a matter.Bye!");
                //Player will be kicked imediately
                Kick(i);
            }
        }
    }
    return 1;
}
RAW code as requested:

pawn Код:
public PingChecker()
{
    for(new i;i < MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i))
        {
            if(GetPlayerPing(i) > 300 && PingChecked[i] == false)
            {
                SendClientMessage(i, CHATCOLOR_RED, "Server: Your ping is higher than the limit.Please fix it or you will be kicked after a minute!");
                PingChecked[i] = true;
            }

            if(GetPlayerPing(i) > 300 && PingChecked[i] == true)
            {
                new pName[MAX_PLAYER_NAME] , string[MAX_PLAYER_NAME*2];
                GetPlayerName(i, pName, sizeof(pName));
                format(string, sizeof(string), "Server: %s was kicked for high ping.", pName);
                SendClientMessageToAll(CHATCOLOR_RED, string);
                Kick(i);
            }
        }
    }
    return 1;
}
Thank you for your attention.It's my first tutorial so comments are heavily appreciated.
Reply


Messages In This Thread
Making simple Ping checker - by Dziugsas - 09.11.2014, 12:43
Re: Making simple Ping checker - by sammp - 09.11.2014, 21:10
Re: Making simple Ping checker - by Luis- - 09.11.2014, 21:16
Re: Making simple Ping checker - by Dziugsas - 09.11.2014, 22:33
Re: Making simple Ping checker - by Dziugsas - 09.11.2014, 22:35
Re: Making simple Ping checker - by fall3n - 10.11.2014, 13:49

Forum Jump:


Users browsing this thread: 1 Guest(s)