#1

Hello Samp,
I had been trying to make a automatic afk detector .
it should be like when player is not moving from a place for 5 mins he should be set to afk and if the player moves within this time he should be non afk

How can i make it ?
Reply
#2

Quote;

Код:
new
	bool: IsPlayerPausedx[MAX_PLAYERS],
	PlayerPausedCount[MAX_PLAYERS],
	Paused_Timer,
    AFkMi[MAX_PLAYERS]
;

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

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

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

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

forward OnPlayerPaused(playerid);
public OnPlayerPaused(playerid)
{
	AFkMi[playerid] = 1;
	// If the player is AFK.
	// AFK operations.
}

forward OnPlayerUnpaused(playerid);
public OnPlayerUnpaused(playerid)
{
	AFkMi[playerid] = 0;
	// Afk player leaves.
	// AFK operations.
}

forward OnPlayersPaused();
public OnPlayersPaused()
{
	foreach(new playerid : Player)
	{
		if(PlayerPausedCount[playerid] < 2 ) PlayerPausedCount[playerid]++;
		else
		{
			CallLocalFunction("OnPlayerPaused", "i", playerid);
			IsPlayerPausedx[playerid] = true;
		}
	}
}
The rest is yours.
Reply
#3

...u can stand there without ESC..

So this is not..what he wanna do
Reply
#4

OnPlayerUpdate doesnt get called when they pause their game

pawn Код:
#define     PAUSE_TIME  60 //we are setting pause time to 60 seconds. It means, if they are AFK for more than 60 seconds = kick. Change this to how many SECONDS you want. (5 minutes = 300 seconds)

new lastCheck[MAX_PLAYERS];

public OnPlayerConnect(playerid)
{
    lastCheck[playerid] = gettime()+PAUSE_TIME; //We will just save their first check on connect
    return 1;
}

public OnPlayerUpdate(playerid)
{
    if(lastCheck[playerid] < gettime()) {
        //Lets say they were afk for 5 seconds (PAUSE_TIME default)? This will not be called for 5 or more seconds (until they come back)

        /* Shortly, how this works?

            1. Player A is playing, we are saving last time he Updated (moves, wrote something etc...)
            2. Player A is not doing anything for 5 seconds? (PAUSE_TIME) then OnPlayerUpdate will not be called.
            3. They are back? OnPlayerUpdate gets called and checks if they were AFK for more than PAUSE_TIME seconds

        */

        Kick(playerid); //we are kicking them or w/e you want to do here
       
    }
    lastCheck[playerid] = gettime()+PAUSE_TIME; //saving the time when he last responded
    return 1;
}

This will work with ANY time of AFK

Problem with the system?
  • Player is AFK but still in game (like not alttabbed or anything as such) he might get shot/hit by a car, receives SendClientMessage etc, the system will call OnPlayerUpdate
  • Player closes the game while alt-tabbed. The OnPlayerUpdate wont be called since he wasnt in the game when he exited (for example if he kills the process thru Task Manager etc)
But besides that, should work with most of the AFK checks (alt tabbing, pressing escape to tab etc)

Quote:
Originally Posted by sampkinq
Посмотреть сообщение
Quote;
Dont use both OnPlayerUpdate & 1 second timer for this.

Basically what can happen is that you have that looping timer + OnPlayerUpdate with CallLocalFunction that call the same callback, its like an infinite loop when it detects that the player is AFK
Reply
#5

You don't need to make one.

Add this to an .inc file and include it in your script:

Код:
/*______________________________________________________________________________
						Auto AFK detector!
							By Lordz.
							 2013!
______________________________________________________________________________*/



#if defined _included_autoafk
 #endinput
#endif

#define _included_autoafk

#tryinclude <a_samp>

/*
native bool:IsPlayerPaused(playerid);
*/

new L_pTicks[MAX_PLAYERS],
	L_pAFK[MAX_PLAYERS],
	L_pisSpawned[MAX_PLAYERS];


stock ExecuteLAFK()
{
 SetTimer("L_AFKDETECT", 500, true);
 return 1;
}
forward L_AFKDETECT();

#if defined FILTERSCRIPT
public OnFilterScriptInit() { ExecuteLAFK(); CallLocalFunction("L_AFK_OnFS", ""); return 1; }
forward L_AFK_OnFS();
 #if defined _ALS_OnFilterScriptInit
  #undef OnFilterScriptInit
 #else
  #define _ALS_OnFilterScriptInit
 #endif

#define OnFilterScriptInit L_AFK_OnFS

#else
public OnGameModeInit() { ExecuteLAFK(); CallLocalFunction("L_AFK_OnGM", ""); return 1; }
forward L_AFK_OnGM();
 #if defined _ALS_OnGameModeInit
  #undef OnGameModeInit
 #else
  #define _ALS_OnGameModeInit
 #endif

#define OnGameModeInit L_AFK_OnGM
#endif

public OnPlayerUpdate(playerid)
{
 L_pTicks[playerid] = GetTickCount();
 if(L_pAFK[playerid] == 1)
 {
  L_pAFK[playerid] = 0;
  CallLocalFunction("OnPlayerResume", "i", playerid);
 }
 CallLocalFunction("L_AFK_OPU", "i", playerid);
 return 1;
}

forward L_AFK_OPU(playerid);

#if defined _ALS_OnPlayerUpdate
 #undef OnPlayerUpdate
#else
 #define _ALS_OnPlayerUpdate
#endif

#define OnPlayerUpdate L_AFK_OPU

public OnPlayerConnect(playerid)
{
 L_pAFK[playerid] = 0;
 L_pTicks[playerid] = 0;
 L_pisSpawned[playerid] = 0;
 CallLocalFunction("L_AFK_OPC", "i", playerid);
 return 1;
}

forward L_AFK_OPC(playerid);

#if defined _ALS_OnPlayerConnect
 #undef OnPlayerConnect
#else
 #define _ALS_OnPlayerConnect
#endif

#define OnPlayerConnect L_AFK_OPC

public OnPlayerSpawn(playerid)
{
 L_pisSpawned[playerid] = 1;
 CallLocalFunction("L_AFK_OPS", "i", playerid);
 return 1;
}

forward L_AFK_OPS(playerid);

#if defined _ALS_OnPlayerSpawn
 #undef OnPlayerSpawn
#else
 #define _ALS_OnPlayerSpawn
#endif

#define OnPlayerSpawn L_AFK_OPS

public OnPlayerDeath(playerid, killerid, reason)
{
 L_pisSpawned[playerid] = 0;
 CallLocalFunction("L_AFK_OPD", "iii", playerid, killerid, reason);
 return 1;
}

forward L_AFK_OPD(playerid, killerid, reason);

#if defined _ALS_OnPlayerDeath
 #undef OnPlayerDeath
#else
 #define _ALS_OnPlayerDeath
#endif

#define OnPlayerDeath L_AFK_OPD

public L_AFKDETECT()
{
 new tick = GetTickCount();
 for(new i; i< GetMaxPlayers(); i++)
 {
  if(!IsPlayerConnected(i)) continue;
  if(L_pAFK[i] != 0) continue;
  if(L_pisSpawned[i] != 1) continue;
  if((tick - L_pTicks[i]) >= 4500)
  {
   if(GetPlayerState(i) >= 1 && GetPlayerState(i) <= 3)
   {
	L_pAFK[i] = 1;
	CallLocalFunction("OnPlayerPause", "i", i);
   }
  }
 }
 return 1;
}

forward OnPlayerPause(playerid);
forward OnPlayerResume(playerid);

stock bool:IsPlayerPaused(playerid)
{
 if(L_pAFK[playerid] == 1) return true;
 else return false;
}
Credits go to Lordz.
Now after you included this, just use IsPlayerPaused(playerid).
I hope I helped!
Reply
#6

PHP код:
new IdleAfkTime[MAX_PLAYERS];
new 
Float:pLastpos[MAX_PLAYERS][2];
new 
afktimer[MAX_PLAYERS];
new 
playername[MAX_PLAYER_NAME];
public 
OnPlayerConnect(playerid)
{
    
IdleAfkTime[playerid] = 300;//Afk after 5 minutes
    
afktimer[playerid] = SetTimerEx("IdleAfk"10001"i"playerid);
    return 
1;
}
forward IdleAfk(playerid);
public 
IdleAfk(playerid)
{
        new 
Float:xFloat:yFloat:zstringsdata[20];
        if (
IsPlayerConnected(playerid))
        {
            
GetPlayerPos(playeridxyz);
            if ((
== pLastpos[playerid][0] && == pLastpos[playerid][1]) || ((!= pLastpos[playerid][0] || != pLastpos[playerid][1]) && GetPlayerState(playerid) != PLAYER_STATE_PASSENGER))
            {
                
IdleAfkTime[playerid] --;
                if (
IdleAfkTime[playerid] == 0)
                {
                            
GetPlayerName(playeridplayernameMAX_PLAYER_NAME);
                            
format(stringsdata,sizeof(stringsdata), "* %s is AFK."playername);
                            
SendClientMessageToAll(COLOR_YELLOWstringsdata);
                            
TogglePlayerControllable(playerid0);
                }
            }
        }
}
CMD:back(playeridparams[])
{
    
    
GetPlayerName(playeridplayernameMAX_PLAYER_NAME);
    
format(stringsdata,sizeof(stringsdata), "* %s is Back."playername);
    
SendClientMessageToAll(COLOR_YELLOWstringsdata);
    
TogglePlayerControllable(playerid1);
    return ;

Reply
#7

i had checked these systems. but i dont get a suited one for me.
let me explain more
when a player is standing at a same point for more than 3 mins he should be set to afk and when he moves he should be removed from afk thats what i want.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)