Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Simple afk checker by Rogue 2018 Sept 2nd.
Functions:
__________
IsPlayerPaused(playerid);
IsPlayerAFK(playerid);
Callbacks:
__________
OnPlayerPause(playerid);
OnPlayerResume(playerid);
OnPlayerAFK(playerid);
OnPlayerReturn(playerid);
Callbacks to hook and add:
_________________________
public OnPlayerText
public OnPlayerClickMap
public OnPlayerCommandText
public OnDialogResponse
public OnPlayerClickTextDraw
public OnPlayerClickPlayerTextDraw
public OnPlayerSelectedMenuRow
*/
#if defined rAFKCHECK_included_
#endinput
#endif
#define rAFKCHECK_included_
#include <a_samp>
#define RAFK_MAX_WAIT_TICKS 60
#define RAFK_MAX_AFK_RANGE 2.0
enum rAFK_PLAYER_DATA
{
bool:rAfkPaused,
bool:rAfkAFK,
rAfkClickTicks,
rAfkPTicks,
rAfkWarnTicks,
Float:rAfkX,
Float:rAfkY,
Float:rAfkZ,
rAfkPTimer
}
new
rAFK_Player[MAX_PLAYERS][rAFK_PLAYER_DATA];
#if defined rAFK_OnPlayerKeyStateChange
forward rAFK_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
#endif
#if defined rAFK_OnPlayerConnect
forward rAFK_OnPlayerConnect(playerid);
#endif
#if defined rAFK_OnPlayerDisconnect
forward rAFK_OnPlayerDisconnect(playerid, reason);
#endif
#if defined rAFK_OnPlayerSpawn
forward rAFK_OnPlayerSpawn(playerid);
#endif
#if defined rAFK_OnPlayerUpdate
forward rAFK_OnPlayerUpdate(playerid);
#endif
public OnPlayerSpawn(playerid)
{
GetPlayerPos(playerid, rAFK_Player[playerid][rAfkX], rAFK_Player[playerid][rAfkY], rAFK_Player[playerid][rAfkZ]);
#if defined rAFK_OnPlayerSpawn
return rAFK_OnPlayerSpawn(playerid);
#else
return 1;
#endif
}
public OnPlayerConnect(playerid)
{
rAFK_Player[playerid][rAfkX] = -9999.0;
rAFK_Player[playerid][rAfkPTimer] = SetTimerEx("rAfk_PChecker", 1000, true, "i", playerid);
#if defined rAFK_OnPlayerConnect
return rAFK_OnPlayerConnect(playerid);
#else
return 1;
#endif
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
rAFK_Player[playerid][rAfkClickTicks] = GetTickCount();
#if defined rAFK_OnPlayerKeyStateChange
return rAFK_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
#else
return 1;
#endif
}
public OnPlayerDisconnect(playerid, reason)
{
KillTimer(rAFK_Player[playerid][rAfkPTimer]);
rAFK_Player[playerid][rAfkPTimer] = 0;
rAFK_Player[playerid][rAfkPTicks] = 0;
rAFK_Player[playerid][rAfkWarnTicks] = 0;
rAFK_Player[playerid][rAfkClickTicks] = 0;
rAFK_Player[playerid][rAfkPaused] = false;
rAFK_Player[playerid][rAfkAFK] = false;
rAFK_Player[playerid][rAfkX] = 0.0;
rAFK_Player[playerid][rAfkY] = 0.0;
rAFK_Player[playerid][rAfkZ] = 0.0;
#if defined rAFK_OnPlayerDisconnect
return rAFK_OnPlayerDisconnect(playerid, reason);
#else
return 1;
#endif
}
public OnPlayerUpdate(playerid)
{
rAFK_Player[playerid][rAfkPTicks] = GetTickCount();
#if defined rAFK_OnPlayerUpdate
return rAFK_OnPlayerUpdate(playerid);
#else
return 1;
#endif
}
forward rAfk_PChecker(playerid);
public rAfk_PChecker(playerid)
{
if(!rAFK_Player[playerid][rAfkPaused] && GetTickCount() > (rAFK_Player[playerid][rAfkPTicks]+1500) && rAFK_Player[playerid][rAfkX] != -9999.0)
{
if(GetPlayerState(playerid) != PLAYER_STATE_WASTED && GetPlayerState(playerid) != PLAYER_STATE_SPECTATING && GetPlayerState(playerid) != PLAYER_STATE_NONE)
{
rAFK_Player[playerid][rAfkPaused] = true;
#if defined OnPlayerPause
OnPlayerPause(playerid);
#endif
}
}
if(IsPlayerInRangeOfPoint(playerid, RAFK_MAX_AFK_RANGE, rAFK_Player[playerid][rAfkX], rAFK_Player[playerid][rAfkY], rAFK_Player[playerid][rAfkZ]))
{
if(rAFK_Player[playerid][rAfkX] != -9999.0 && GetPlayerState(playerid) != PLAYER_STATE_WASTED && GetPlayerState(playerid) != PLAYER_STATE_SPECTATING && GetPlayerState(playerid) != PLAYER_STATE_NONE)
{
if(rAFK_Player[playerid][rAfkWarnTicks] < RAFK_MAX_WAIT_TICKS)
{
rAFK_Player[playerid][rAfkWarnTicks]++;
}
else
{
rAFK_Player[playerid][rAfkAFK] = true;
#if defined OnPlayerAFK
OnPlayerAFK(playerid);
#endif
}
}
}
else
{
GetPlayerPos(playerid, rAFK_Player[playerid][rAfkX], rAFK_Player[playerid][rAfkY], rAFK_Player[playerid][rAfkZ]);
rAFK_Player[playerid][rAfkWarnTicks] = 0;
}
if( (rAFK_Player[playerid][rAfkAFK] && rAFK_Player[playerid][rAfkWarnTicks] != RAFK_MAX_WAIT_TICKS) || (GetTickCount() - rAFK_Player[playerid][rAfkClickTicks]) <= 500)
{
rAFK_Player[playerid][rAfkAFK] = false;
#if defined OnPlayerReturn
OnPlayerReturn(playerid);
#endif
}
if(rAFK_Player[playerid][rAfkPaused] && GetTickCount() < (rAFK_Player[playerid][rAfkPTicks]+1500))
{
rAFK_Player[playerid][rAfkPaused] = false;
#if defined OnPlayerResume
OnPlayerResume(playerid);
#endif
}
return 1;
}
stock bool:IsPlayerPaused(playerid)
return rAFK_Player[playerid][rAfkPaused];
stock bool:IsPlayerAFK(playerid)
return (rAFK_Player[playerid][rAfkWarnTicks] == RAFK_MAX_WAIT_TICKS);
#if defined _ALS_OnPlayerConnect
#undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect rAFK_OnPlayerConnect
#if defined _ALS_OnPlayerDisconnect
#undef OnPlayerDisconnect
#else
#define _ALS_OnPlayerDisconnect
#endif
#define OnPlayerDisconnect rAFK_OnPlayerDisconnect
#if defined _ALS_OnPlayerSpawn
#undef OnPlayerSpawn
#else
#define _ALS_OnPlayerSpawn
#endif
#define OnPlayerSpawn rAFK_OnPlayerSpawn
#if defined _ALS_OnPlayerUpdate
#undef OnPlayerUpdate
#else
#define _ALS_OnPlayerUpdate
#endif
#define OnPlayerUpdate rAFK_OnPlayerUpdate
#if defined _ALS_OnPlayerKeyStateChange
#undef OnPlayerKeyStateChange
#else
#define _ALS_OnPlayerKeyStateChange
#endif
#define OnPlayerKeyStateChange rAFK_OnPlayerKeyStateChange
@AmirSavand

This comment has been minimized.

Copy link

@AmirSavand AmirSavand commented Oct 8, 2018

What's the difference between OnPlayerPause and OnPlayerAFK?

@RogueDrifter

This comment has been minimized.

Copy link
Owner Author

@RogueDrifter RogueDrifter commented Oct 8, 2018

OnPlayerPause is when the player presses the ESC button, OnPlayerAFK is when the player doesn't move for a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment