Detecting a player tabbed
#1

I've seen a lot of topics about this and most people suggest getting the tick count. Based on reference - GetTickCount, it would cause problems on servers with uptime of over 24 days. So I'm wondering on what would be the best and simple way to check if a player is tabbed out? Aside from getting the tick count.
Reply
#2

That's the best solution, it does not matter about the uptime. Usually every owner restarts the server once per week so that wouldn't be a problem.

The rest of the methods I've seen about detecting is the position if it has not changed but it is not accurate in my opinion.
Reply
#3

I see. Well, with that being said, could you post an example of the simplest way to detect if a player is tabbed using tick counts? I'd appreciate it.

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
The rest of the methods I've seen about detecting is the position if it has not changed but it is not accurate in my opinion.
If a player doesn't move at all, it wouldn't mean that they're tabbed already. That simply makes me think that it would be inaccurate unless a specific limit is set before a player is determined to be tabbed. Is that how it works?
Reply
#4

This really easy actually here is what I use never had a problem with it. I got this from somewhere but can't remember where however it's been modified to use y_hooks. All you would really need to do it is include it in your gamemode (need YSI too) then you can use the function IsPlayerPaused(playerid);

pawn Код:
#include <YSI\y_hooks>

#define TIME_DIF 2000 //Decreasing will make it more accurate, leave it like that, it's fine.
// #define USECALLBACKS

forward C_Paused(playerid);

#if defined USECALLBACKS
forward OnPlayerPause(playerid);
forward OnPlayerUnPause(playerid);
#endif

new g_Paused[MAX_PLAYERS];
new bool:g_Requesting[MAX_PLAYERS];
new bool:g_IsPaused[MAX_PLAYERS];

hook OnPlayerConnect(playerid)
{
    g_IsPaused[playerid] = false;
    g_Requesting[playerid] = false;
    return 1;
}

hook OnPlayerUpdate(playerid)
{
    g_Paused[playerid] = GetTickCount();
    return 1;
}

hook OnPlayerRequestClass(playerid)
{
    g_Requesting[playerid] = true;
    g_IsPaused[playerid] = false;
    return 1;
}

hook OnPlayerDisconnect(playerid)
{
    g_Requesting[playerid] = false;
    g_IsPaused[playerid] = false;
    return 1;
}

hook OnPlayerSpawn(playerid)
{
    g_Requesting[playerid] = false;
    g_IsPaused[playerid] = false;
    return 1;
}

public C_Paused(playerid)
{
    if(GetTickCount()-g_Paused[playerid] > TIME_DIF && g_Requesting[playerid] != true && g_IsPaused[playerid] != true && InvalidStates(playerid) != 1)
    {
        #if defined USECALLBACKS
        OnPlayerPause(playerid);
        #endif
        g_IsPaused[playerid] = true;
    }
    else if(GetTickCount()-g_Paused[playerid] < TIME_DIF && g_Requesting[playerid] != true && g_IsPaused[playerid] != false && InvalidStates(playerid) != 1)
    {
        #if defined USECALLBACKS
        OnPlayerUnPause(playerid);
        #endif
        g_IsPaused[playerid] = false;
    }
    return 1;
}

stock IsPlayerPaused(playerid) { return g_IsPaused[playerid]; }

stock InvalidStates(playerid)
{
    new pState = GetPlayerState(playerid);
    if(pState == 0 || pState == 7) return 1;
    else return 0;
}

task PauseUpdate[1000]() { foreach(new i : Player) { C_Paused(i); } }

#if defined USECALLBACKS
public OnPlayerPause(playerid) { SendClientMessage(playerid, STEALTH_GREEN, "Paused"); }
public OnPlayerUnPause(playerid) { SendClientMessage(playerid, STEALTH_GREEN, "UnPaused"); }
#endif
Reply
#5

This should work. Add it to your gamemode.
pawn Код:
#include < a_samp >
#include < foreach >

#undef MAX_PLAYERS
#define MAX_PLAYERS (50)
// Change to your amount of max players you want.

new
    bool: IsPlayerPaused[ MAX_PLAYERS ],
    PlayerPausedCount[ MAX_PLAYERS ],
   
    Paused_Timer
;

main( ) { }

public OnGameModeInit( )
{
    Paused_Timer = SetTimer( "OnPlayersPaused", 1000, true );
    return 1;
}

public OnGameModeExit( )
{
    KillTimer( Paused_Timer );
    return 1;
}

public OnPlayerConnect( playerid )
{
    IsPlayerPaused[ playerid ] = false;
    PlayerPausedCount[ playerid ] = 0;
    return 1;
}

public OnPlayerUpdate( playerid )
{
    PlayerPausedCount[ playerid ] = 0;
    if( IsPlayerPaused[ playerid ] )
    {
        CallLocalFunction( "OnPlayerUnpaused", "i", playerid );
        IsPlayerPaused[ playerid ] = false;
    }
    return 1;
}

forward OnPlayerPaused( playerid );
public OnPlayerPaused( playerid )
{
}

forward OnPlayerUnpaused( playerid );
public OnPlayerUnpaused( playerid )
{
}

forward OnPlayersPaused( );
public OnPlayersPaused( )
{
    foreach(new playerid : Player)
    {
        if( IsPlayerSpawned( playerid ) )
        {
            if( PlayerPausedCount[ playerid ] < 2 ) PlayerPausedCount[ playerid ]++;
            else
            {
                CallLocalFunction( "OnPlayerPaused", "i", playerid );
                IsPlayerPaused[ playerid ] = true;
            }
        }
    }
}

stock IsPlayerSpawned( playerid )
{
    switch( GetPlayerState( playerid ) )
    {
        case 0, 7, 9: return 0;
    }
    return 1;
}
Reply
#6

Thanks to both! But I'm having problems using YSI, I've already posted something about this and there were no replies for resolution. I already have YSI and included it onto the game-mode without errors nor warnings.

Each time I try to use timers(y_timers), it doesn't work at all.

First, I had this:
pawn Код:
Timer:OneSecondInterval[1000]() { }
since that didn't work, someone suggested using this:
pawn Код:
timer OneSecondInterval[1000]() { }
but the same thing, nothing happens.
Reply
#7

Quote:
Originally Posted by Skribblez
Посмотреть сообщение
Thanks to both! But I'm having problems using YSI, I've already posted something about this and there were no replies for resolution. I already have YSI and included it onto the game-mode without errors nor warnings.

Each time I try to use timers(y_timers), it doesn't work at all.

First, I had this:
pawn Код:
Timer:OneSecondInterval[1000]() { }
since that didn't work, someone suggested using this:
pawn Код:
timer OneSecondInterval[1000]() { }
but the same thing, nothing happens.
Did you not use defer ?

pawn Код:
#include <YSI\y_timers>

task RepeatingTimer[1000]()
{
    printf("Called every 1 seconds.");
}

timer DelayedTimer[500](playerid)
{
    printf("May be called after 0.5 seconds");
}

main()
{
    defer DelayedTimer(42);
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)