Play time randomly goes up when player goes afk
#1

pawn Код:
forward TimeOnServer(playerid);
public TimeOnServer(playerid)
{
    PlayerInfo[playerid][MinutesPlayed]++;
    if(PlayerInfo[playerid][MinutesPlayed] >= 60)
    {
        PlayerInfo[playerid][MinutesPlayed] = 0;
        PlayerInfo[playerid][HoursPlayed]++;
        if (PlayerInfo[playerid][pTeamKills] > 0)
        {
            PlayerInfo[playerid][pTeamKills]--;
            new Level;
            new Kills = PlayerInfo[playerid][pKills];
            new Deaths = PlayerInfo[playerid][pDeaths];
            new Penalty = 2 * PlayerInfo[playerid][pTeamKills];
            Level = (Kills - Deaths) - Penalty;
            if (Level < 0)
            {
                SetPlayerScore(playerid, 0);
            }
            else
            {
                SetPlayerScore(playerid, Level);
            }
        }
    }
    else if(PlayerInfo[playerid][MinutesPlayed] == 30 )
    {
        if (PlayerInfo[playerid][pTeamKills] > 0)
        {
            PlayerInfo[playerid][pTeamKills]--;
            new Level;
            new Kills = PlayerInfo[playerid][pKills];
            new Deaths = PlayerInfo[playerid][pDeaths];
            new Penalty = 2 * PlayerInfo[playerid][pTeamKills];
            Level = (Kills - Deaths) - Penalty;
            if (Level < 0)
            {
                SetPlayerScore(playerid, 0);
            }
            else
            {
                SetPlayerScore(playerid, Level);
            }
        }
    }
}
Anyone know what I did wrong / how to fix this?
Reply
#2

Note: TimeOnServer starts at OnPlayerConnect
Reply
#3

Don't increase the variable if they're AFK.

You can check if they're paused by setting a variable to 5 under OnPlayerUpdate and have the value decrease every 1 second. If the variable is ever three or below, they're AFK. That way you can stop increasing the time online variable.
Reply
#4

Quote:
Originally Posted by KyleSmith
Посмотреть сообщение
Don't increase the variable if they're AFK.

You can check if they're paused by setting a variable to 5 under OnPlayerUpdate and have the value decrease every 1 second. If the variable is ever three or below, they're AFK. That way you can stop increasing the time online variable.
Is that just afk, or tabbed out?
Reply
#5

Both work.
Reply
#6

You can detect it using OnPlayerUpdate because it don't get called when player goes paused. Here's a small include which I've wrote before, using it, the code would be:
pawn Код:
forward TimeOnServer(playerid);
public TimeOnServer(playerid)
{
    if(IsPlayerPaused(playerid)) return 0; //Callback doesn't continue if player is paused.
    PlayerInfo[playerid][MinutesPlayed]++;
    if(PlayerInfo[playerid][MinutesPlayed] >= 60)
    {
        PlayerInfo[playerid][MinutesPlayed] = 0;
        PlayerInfo[playerid][HoursPlayed]++;
        if (PlayerInfo[playerid][pTeamKills] > 0)
        {
            PlayerInfo[playerid][pTeamKills]--;
            new Level;
            new Kills = PlayerInfo[playerid][pKills];
            new Deaths = PlayerInfo[playerid][pDeaths];
            new Penalty = 2 * PlayerInfo[playerid][pTeamKills];
            Level = (Kills - Deaths) - Penalty;
            if (Level < 0)
            {
                SetPlayerScore(playerid, 0);
            }
            else
            {
                SetPlayerScore(playerid, Level);
            }
        }
    }
    else if(PlayerInfo[playerid][MinutesPlayed] == 30 )
    {
        if (PlayerInfo[playerid][pTeamKills] > 0)
        {
            PlayerInfo[playerid][pTeamKills]--;
            new Level;
            new Kills = PlayerInfo[playerid][pKills];
            new Deaths = PlayerInfo[playerid][pDeaths];
            new Penalty = 2 * PlayerInfo[playerid][pTeamKills];
            Level = (Kills - Deaths) - Penalty;
            if (Level < 0)
            {
                SetPlayerScore(playerid, 0);
            }
            else
            {
                SetPlayerScore(playerid, Level);
            }
        }
    }
}
Include : https://github.com/Lordzy/AutoAFK

And if you're using a server sided AFK system, make sure that you determine the variable under "TimeOnServer" and return 0 in case if player is AFK (server sided)
Reply
#7

The thing is.. The timer gets bugged or something.. If you tab out for 5 minutes it adds like 1-2 hours to pHours

pawn Код:
public OnPlayerUpdate(playerid)
{
    AFKCheck[playerid] = 5;
    SetTimerEx("AfkTimer", 1000, 1, "i", playerid);
    return 1;
}

forward AfkTimer(playerid);
public AfkTimer(playerid)
{
    AFKCheck[playerid]--;
    if (AFKCheck[playerid] < 3)
    {
        AFK[playerid] = 1;
        SendClientMessage(playerid, COLOR_ORANGE, "Debug - Now afk");
    }
    else
    {
        AFK[playerid] = 0;
    }
}
I tried this, but it just spams the message of afk, even when not afk?

Using the include now, when does it classify the player as afk?
Reply
#8

It seems to work.. Lets test for a longer period of time now..
Ill post here if the time bugs out again.

Thanks for the help to both of you.
Reply
#9

I wouldn't do the timer in OPU.

I would set 1 global timer for all players OnGameModeInit and have that repeating every 1 second.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)