Guest User

pause_hook.inc - Dayrion

a guest
Nov 25th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.00 KB | None | 0 0
  1. /*
  2.     -------------------------------------------------------------------------------------
  3.     * Include made by * aka Dayrion.
  4.  
  5.     public OnPlayerUnPause(playerid, time);
  6.         - When a player return in game after being AFK. Return the time which he were AFK in seconds.
  7.     public OnPlayerPause(playerid);
  8.         - When a player back to the desktop or press ESC.
  9.     bool:IsPlayerPaused(playerid);
  10.         - Return if, or not, a player is in pause.
  11.     -------------------------------------------------------------------------------------
  12. */
  13.  
  14. // ===================================================================================================================================================
  15.  
  16. #tryinclude <a_samp>
  17. #tryinclude <YSI\y_hooks>
  18.  
  19. #if defined _included_ji_afkdetect
  20.     #endinput
  21. #endif
  22.  
  23. #if !defined MAX_PLAYERS
  24.     #error "You must have the lastest version of SAMP (0.3.7) or include it (a_samp) before this one."
  25. #endif
  26.  
  27. #if !defined hook
  28.     #error "You must have latest version of y_hooks."
  29. #endif
  30.  
  31. #define _included_ji_afkdetect
  32.  
  33. #if !defined MAX_TIME_ALLOWED
  34.     #define MAX_TIME_ALLOWED 5 // sec
  35. #endif
  36.  
  37. #if !defined INTERVAL_AFK_DETECT
  38.     #define INTERVAL_AFK_DETECT 800 // ms
  39. #endif
  40.  
  41.  
  42. // ===================================================================================================================================================
  43.  
  44. forward OnPlayerUnPause(playerid, time);
  45. forward OnPlayerPause(playerid);
  46. //bool:IsPlayerPaused(playerid);
  47.  
  48. // ===================================================================================================================================================
  49.  
  50. static
  51.     PlayerAFK[MAX_PLAYERS],
  52.     AFKDetect[MAX_PLAYERS],
  53.     v_Ji_TAFK;
  54.  
  55. // ===================================================================================================================================================
  56.  
  57. #if defined FILTERSCRIPT
  58.  
  59.     hook OnFilterScriptInit()
  60.     {
  61.         v_Ji_TAFK = SetTimer("Ji_TAFK", INTERVAL_AFK_DETECT, true);
  62.     }
  63.  
  64.     hook OnFilterScriptExit()
  65.     {
  66.         KillTimer(v_Ji_TAFK);
  67.     }
  68.  
  69. #else
  70.  
  71.     hook OnGameModeInit()
  72.     {
  73.         v_Ji_TAFK = SetTimer("Ji_TAFK", INTERVAL_AFK_DETECT, true);    
  74.     }
  75.  
  76.     hook OnGameModeExit()
  77.     {
  78.         KillTimer(v_Ji_TAFK);
  79.     }
  80.  
  81. #endif
  82.  
  83. // ===================================================================================================================================================
  84.    
  85. hook OnPlayerUpdate(playerid)
  86. {
  87.     AFKDetect[playerid] = gettime();
  88. }
  89.  
  90. stock
  91.     bool:IsPlayerPaused(playerid)
  92.         return PlayerAFK[playerid] > 0 ? true : false;
  93.  
  94. forward Ji_TAFK();
  95. public Ji_TAFK()
  96. {
  97.     for(new j = GetPlayerPoolSize(); j > 0; j--)
  98.     {
  99.         if(!IsPlayerConnected(j))
  100.             continue;
  101.  
  102.         if(gettime() - AFKDetect[j] > MAX_TIME_ALLOWED && PlayerAFK[j] == 0)
  103.         {
  104.             PlayerAFK[j] = AFKDetect[j];
  105.             CallRemoteFunction("OnPlayerPause", "i", j);
  106.         }
  107.         else if(gettime() - AFKDetect[j] < MAX_TIME_ALLOWED && PlayerAFK[j] > 0)
  108.         {
  109.             CallRemoteFunction("OnPlayerUnPause", "ii", j, (gettime() - PlayerAFK[j]) + MAX_TIME_ALLOWED);
  110.             PlayerAFK[j] = 0;
  111.         }
  112.     }
  113.     return 1;
  114. }
Add Comment
Please, Sign In to add comment