Guest User

AFK Auto-Detector

a guest
Jan 29th, 2015
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.84 KB | None | 0 0
  1. //AFK Auto-Detect v2.0
  2. //Credits to: Miihai (Mihaitza. - BBGaming.ro)
  3.  
  4. new bool:PlayerAFK[MAX_PLAYERS]; // Seteaza variabila [0-NOT-AFK | 1-AFK] | Set player variable [0-NOT-AFK | 1-AFK]
  5. new AFKDetect[MAX_PLAYERS]; // Seteaza timpul curent din OPU | Set current date/time to player from OnPlayerUpdate(OPU)
  6. new bool: FunctionCall[2][MAX_PLAYERS]; // Ajuta sa nu se repete functiile OnPlayerPause si/sau OnPlayerUnpause | Helps not to repeat OnPlayerPause or/and OnPlayerUnpause
  7. new playerAName[50]; //Player name
  8. new AFKTime[MAX_PLAYERS];
  9.  
  10. #include <a_samp>
  11. #include <foreach>
  12.  
  13. #if defined _AFKDETECT_INC
  14.         #endinput
  15. #endif
  16. #define _AFKDETECT_INC
  17.  
  18. forward OnPlayerPause(playerid);
  19. forward OnPlayerUnpause(playerid);
  20. forward AFKChecker();
  21. forward IsPlayerPause(playerid);
  22. forward TimeSincePaused(playerid);
  23.  
  24. public OnPlayerUpdate(playerid)
  25. {
  26.         AFKDetect[playerid] = gettime();
  27.     #if defined VFUNC_OnPlayerUpdate
  28.         return VFUNC_OnPlayerUpdate(playerid);
  29.     #else
  30.         return 1;
  31.     #endif
  32. }
  33. #if defined VFUNC_OnPlayerUpdate
  34.     forward VFUNC_OnPlayerUpdate(playerid);
  35. #endif
  36. #if defined _ALS_OnPlayerUpdate
  37.     #undef OnPlayerUpdate
  38. #else
  39.     #define _ALS_OnPlayerUpdate
  40. #endif
  41. #define OnPlayerUpdate VFUNC_OnPlayerUpdate
  42.  
  43. public OnPlayerDisconnect(playerid, reason)
  44. {
  45.         if(PlayerAFK[playerid] == true && FunctionCall[1][playerid] == false)
  46.         {
  47.                 GetPlayerName(playerid, playerAName,sizeof(playerAName)),
  48.                 PlayerAFK[playerid] = false;
  49.                 printf("[resume] %s is no longer AFK because he's left the game. (%d seconds)",playerAName,gettime()-AFKTime[playerid]);
  50.                 FunctionCall[1][playerid] = true;
  51.                 FunctionCall[0][playerid] = false;
  52.                 AFKTime[playerid] = 0;
  53.                 CallLocalFunction("OnPlayerUnpause", "i", playerid);
  54.         }
  55.     #if defined VFUNC_OnPlayerDisconnect
  56.         return VFUNC_OnPlayerDisconnect(playerid, reason);
  57.     #else
  58.         return 1;
  59.     #endif
  60. }
  61. #if defined VFUNC_OnPlayerDisconnect
  62.     forward VFUNC_OnPlayerDisconnect(playerid, reason);
  63. #endif
  64. #if defined _ALS_OnPlayerDisconnect
  65.     #undef OnPlayerDisconnect
  66. #else
  67.     #define _ALS_OnPlayerDisconnect
  68. #endif
  69. #define OnPlayerDisconnect VFUNC_OnPlayerDisconnect
  70.  
  71. public OnGameModeInit()
  72. {
  73.         SetTimer("AFKChecker",1000,true);
  74.     #if defined VFUNC_OnGameModeInit
  75.         return VFUNC_OnGameModeInit();
  76.     #else
  77.         return 1;
  78.     #endif
  79. }
  80. #if defined VFUNC_OnGameModeInit
  81.     forward VFUNC_OnGameModeInit();
  82. #endif
  83. #if defined _ALS_OnGameModeInit
  84.     #undef OnGameModeInit
  85. #else
  86.     #define _ALS_OnGameModeInit
  87. #endif
  88. #define OnGameModeInit VFUNC_OnGameModeInit
  89. public AFKChecker()
  90. {
  91.         foreach(Player, i)
  92.         {
  93.                         if(gettime()-AFKDetect[i] > 5)
  94.                         {
  95.                                 if(PlayerAFK[i] == false && FunctionCall[0][i] == false)
  96.                                         GetPlayerName(i, playerAName,sizeof(playerAName)),
  97.                                                 PlayerAFK[i] = true,
  98.                                                         CallLocalFunction("OnPlayerPause", "i", i),
  99.                                                                 printf("[pause] %s is now AFK.",playerAName),
  100.                                                                         FunctionCall[0][i] = true,
  101.                                                                                 FunctionCall[1][i] = false,
  102.                                                                                         AFKTime[i] = gettime();
  103.                         }
  104.                         else
  105.                         {
  106.                                 if(PlayerAFK[i] == true && FunctionCall[1][i] == false)
  107.                                         GetPlayerName(i, playerAName,sizeof(playerAName)),
  108.                                                 PlayerAFK[i] = false,
  109.                                                         CallLocalFunction("OnPlayerUnpause", "i", i),
  110.                                                                 printf("[resume] %s is no longer AFK. (%d seconds)",playerAName,gettime()-AFKTime[i]),
  111.                                                                         FunctionCall[1][i] = true,
  112.                                                                                 FunctionCall[0][i] = false,
  113.                                                                                         AFKTime[i] = gettime()-AFKTime[i];
  114.                         }
  115.         }
  116.         return 1;
  117. }
  118. public IsPlayerPause(playerid)
  119.         if(PlayerAFK[playerid] == true) return 1;
  120.                 else return 0;
  121.  
  122. public TimeSincePaused(playerid) {
  123.         if(PlayerAFK[playerid] == true) return gettime()-AFKTime[playerid];
  124.         else return AFKTime[playerid];
  125. }
Advertisement
Add Comment
Please, Sign In to add comment