Author Topic: Detecting player AFK time  (Read 2784 times)

Merkel

  • Jr. Member
  • **
  • Posts: 86
  • Karma: +4/-10
    • View Profile
Detecting player AFK time
« on: July 30, 2014, 02:26:33 pm »
Code: [Select]
Time <- array ( GetMaxPlayers ( ), 0 ); // Set by default all the array to 0 with size of the MaxPlrs
Name <- array ( GetMaxPlayers ( ), null ); // Store the names

function onScriptLoad ( ) {

NewTimer ( "IncreaseAllAFK", 1000, 0 );
}

function IncreaseAllAFK ( ) {

local Players = GetPlayers ( ) + 5; // If one player left with ID 1 and exists another with ID 2, increase +5 the player count

for ( local i = 0; i < Players; i ++ ) {

if ( FindPlayer ( i ) ) {

Time [ FindPlayer ( i ).ID ] ++; // Increase AFK time

if ( Time [ FindPlayer ( i ).ID ] >= 30 ) {

local Colname = FindPlayer ( i ).ColouredName;

if ( Colname.find ( "[AFK]" ) == null ) {

Name [ FindPlayer ( i ).ID ] = FindPlayer ( i ).ColouredName;

FindPlayer ( i ).ColouredName =  Colname + " [#ffffff][AFK]";
}
}

// Kicks after 7 minutes

if ( Time [ FindPlayer ( i ).ID ] >= 420 ) {

MessagePlayer ( "You was kicked by AFK limit time. [ 7 minutes ]", FindPlayer ( i ) );

KickPlayer ( FindPlayer ( i ) );
}
}
}
}

// Check if a key was pressed:

function onPlayerKeyStateChange ( pPlayer, Key, Pressed ) {
 
  if ( Time [ pPlayer.ID ] >= 30 ) {

MessagePlayer ( "You were AFK for " + Time [ pPlayer.ID ] + " seconds. ", pPlayer ); // Show time

Time [ pPlayer.ID ] = 0;

pPlayer.ColouredName = Name [ pPlayer.ID ];

return;
}

Time [ pPlayer.ID ] = 0;
}

P.S. : I forgot some things.
« Last Edit: July 31, 2014, 12:39:44 am by Merkel »
Westwood Studios

The best studios company in strategy videogames.

Vortrex

  • Full Member
  • ***
  • Posts: 264
  • Karma: +54/-74
    • View Profile
Re: Detecting player AFK time
« Reply #1 on: July 31, 2014, 07:11:19 am »
Here's another way:

Note: The timer can be replaced with a different method if needed, like the "onTimeChange" event or something.

Code: [Select]
function AFKCheck()
{
foreach( ii , iv in Players )
{
if( iv.Spawned )
{
if( !IsTabbed[ iv.ID ] )
{
if( ( time( ) - LastTick[ iv.ID ] ) >= MAX_PAUSE_TIME )
{
IsTabbed[ iv.ID ] = true;
}
}

}
}
}

function onPlayerUpdate( player )
{
if( IsTabbed[ player.ID ] )
{
local amount = time( ) - LastTick[ player.ID ];
IsTabbed[ player.ID ] = false;
}

LastTick[ player.ID ] = time( );
}

function onScriptLoad( )
{
Players <- { };
LastTick <- array( 128 , 0 );
IsTabbed <- array( 128 , 0 );

// Time before each player is considered "paused" in seconds
// NOT in milliseconds
MAX_PAUSE_TIME <- 2;

AFKTimer <- NewTimer( "AFKCheck" , 500 , 0 );
}

function onPlayerConnect( player )
{
Players[ player.ID ] <- player;
}

function onPlayerPart( player , reason )
{
Players[ player.ID ] <- null;
}

EDIT: Some details I forgot to mention ...
The MAX_PAUSE_TIME can be any number, but 2 seems decent enough, so I chose it. It is used for how much time needs to pass before a player is considered "paused" and NOT how often the check happens.

Also, you can add other things to happen when a player pauses or unpauses. Turn the player into a ghost or something cool if you want.
« Last Edit: July 31, 2014, 07:14:17 am by Vortrex »

Merkel

  • Jr. Member
  • **
  • Posts: 86
  • Karma: +4/-10
    • View Profile
Re: Detecting player AFK time
« Reply #2 on: July 31, 2014, 11:19:57 am »
Here's another way:

Note: The timer can be replaced with a different method if needed, like the "onTimeChange" event or something.

Code: [Select]
function AFKCheck()
{
foreach( ii , iv in Players )
{
if( iv.Spawned )
{
if( !IsTabbed[ iv.ID ] )
{
if( ( time( ) - LastTick[ iv.ID ] ) >= MAX_PAUSE_TIME )
{
IsTabbed[ iv.ID ] = true;
}
}

}
}
}

function onPlayerUpdate( player )
{
if( IsTabbed[ player.ID ] )
{
local amount = time( ) - LastTick[ player.ID ];
IsTabbed[ player.ID ] = false;
}

LastTick[ player.ID ] = time( );
}

function onScriptLoad( )
{
Players <- { };
LastTick <- array( 128 , 0 );
IsTabbed <- array( 128 , 0 );

// Time before each player is considered "paused" in seconds
// NOT in milliseconds
MAX_PAUSE_TIME <- 2;

AFKTimer <- NewTimer( "AFKCheck" , 500 , 0 );
}

function onPlayerConnect( player )
{
Players[ player.ID ] <- player;
}

function onPlayerPart( player , reason )
{
Players[ player.ID ] <- null;
}

EDIT: Some details I forgot to mention ...
The MAX_PAUSE_TIME can be any number, but 2 seems decent enough, so I chose it. It is used for how much time needs to pass before a player is considered "paused" and NOT how often the check happens.

Also, you can add other things to happen when a player pauses or unpauses. Turn the player into a ghost or something cool if you want.

And there's another way in Client
Westwood Studios

The best studios company in strategy videogames.

 

© Liberty Unleashed Team.