Advertisement
Guest User

Ellis

a guest
Jul 12th, 2009
1,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /*
  2.  *  AFK system
  3.  *  Written by Ellis
  4.  */
  5.  
  6. // Including a_samp
  7. #include <a_samp>
  8.  
  9. // Filterscript settings
  10. #define AFK_TIME 5 // minutes
  11.  
  12. // Defining global variables
  13. new
  14.     AFKCheck[ MAX_PLAYERS ],
  15.     AFKTimer;
  16.    
  17. forward CheckIfAFKing( );
  18.  
  19.  
  20. /*******************************************************************************
  21.  *** SA:MP callbacks                                                       *****
  22.  ******************************************************************************/
  23.  
  24. public OnFilterScriptInit( )
  25. {
  26.     // Callback: OnFilterScriptInit( )
  27.    
  28.     for ( new playerid = 0; playerid < MAX_PLAYERS; playerid++ )
  29.         AFKCheck[ playerid ] = 0;
  30.        
  31.     AFKTimer = SetTimer( "CheckIfAFKing", 60000, true );
  32.    
  33.     return true;
  34. }
  35.  
  36. public OnFilterScriptExit( )
  37. {
  38.     // Callback: OnFilterScriptExit( )
  39.    
  40.     KillTimer( AFKTimer );
  41.    
  42.     return true;
  43. }
  44.  
  45. public OnPlayerConnect( playerid )
  46. {
  47.     // Callback: OnPlayerConnect( playerid )
  48.     // Clearing player variable
  49.    
  50.     AFKCheck[ playerid ] = 0;
  51.    
  52.     return true;
  53. }
  54.  
  55. public OnPlayerText( playerid, text[ ] )
  56. {
  57.     // Callback: OnPlayerText( playerid, text[ ] )
  58.     // Clearing player variable
  59.    
  60.     if ( AFKCheck[ playerid ] >= AFK_TIME )
  61.         OnPlayerBackOfAFK( playerid );
  62.    
  63.     AFKCheck[ playerid ] = 0;
  64.    
  65.     return true;
  66. }
  67.  
  68. public OnPlayerPrivmsg( playerid, recieverid, text[ ] )
  69. {
  70.     // Callback: OnPlayerPrivmsg( playerid, recieverid, text[ ] )
  71.     // Clearing player variable
  72.    
  73.     if ( AFKCheck[ playerid ] >= AFK_TIME )
  74.         OnPlayerBackOfAFK( playerid );
  75.    
  76.     AFKCheck[ playerid ] = 0;
  77.    
  78.     return true;
  79. }
  80.  
  81. public OnPlayerCommandText( playerid, cmdtext[ ] )
  82. {
  83.     // Callback: OnPlayerCommandText( playerid, cmdtext[ ] )
  84.     // Clearing player variable
  85.    
  86.     if ( AFKCheck[ playerid ] >= AFK_TIME )
  87.         OnPlayerBackOfAFK( playerid );
  88.        
  89.     AFKCheck[ playerid ] = 0;
  90.    
  91.     return false;
  92. }
  93.  
  94. /*******************************************************************************
  95.  *** Custom functions and callbacks                                        *****
  96.  ******************************************************************************/
  97.  
  98. public CheckIfAFKing( )
  99. {
  100.     // Function: CheckIfAFKing( )
  101.     // Checks if player is AFK. It's called every minute
  102.    
  103.     static
  104.         Float:OldPosX[ MAX_PLAYERS ],
  105.         Float:OldPosY[ MAX_PLAYERS ],
  106.         Float:OldPosZ[ MAX_PLAYERS ];
  107.    
  108.     for ( new playerid = 0; playerid < MAX_PLAYERS; playerid++ )
  109.     {
  110.         if ( !IsPlayerConnected( playerid ) )   continue;
  111.        
  112.         new
  113.             Float:NewPosX,
  114.             Float:NewPosY,
  115.             Float:NewPosZ;
  116.            
  117.         GetPlayerPos( playerid, NewPosX, NewPosY, NewPosZ );
  118.        
  119.         if ( NewPosX == OldPosX[ playerid ] && NewPosY == OldPosY[ playerid ] &&
  120.              NewPosZ == OldPosZ[ playerid ] )
  121.         {
  122.             if ( AFKCheck[ playerid ] < AFK_TIME )
  123.             {
  124.                 AFKCheck[ playerid ]++;
  125.                
  126.                 if ( AFKCheck[ playerid ] == AFK_TIME )
  127.                     OnPlayerCaughtAFK( playerid );
  128.             }
  129.         }
  130.         else
  131.         {
  132.             AFKCheck[ playerid ] = 0;
  133.             OnPlayerBackOfAFK( playerid );
  134.         }
  135.        
  136.         OldPosX[ playerid ] = NewPosX;
  137.         OldPosY[ playerid ] = NewPosY;
  138.         OldPosZ[ playerid ] = NewPosZ;
  139.     }
  140. }
  141.  
  142. stock OnPlayerCaughtAFK( playerid )
  143. {
  144.     // Callback: OnPlayerCaughtAFK( playerid )
  145.     // Adding [AFK] tag to player name
  146.    
  147.     new
  148.         name[ MAX_PLAYER_NAME ];
  149.        
  150.     GetPlayerName( playerid, name, MAX_PLAYER_NAME );
  151.     strins       ( name, "[AFK]", 0, MAX_PLAYER_NAME );
  152.     SetPlayerName( playerid, name );
  153. }
  154.  
  155. stock OnPlayerBackOfAFK( playerid )
  156. {
  157.     // Callback: OnPlayerBackOfAFK( playerid )
  158.     // Removing [AFK] tag from player name
  159.    
  160.     new
  161.         name[ MAX_PLAYER_NAME ];
  162.  
  163.     GetPlayerName( playerid, name, MAX_PLAYER_NAME );
  164.        
  165.     if ( !strcmp( name, "[AFK]", true, 5 ) )
  166.     {
  167.         strdel       ( name, 0, 5 );
  168.         SetPlayerName( playerid, name );
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement