/*
* Anti AFK library by jim7777
* version 1.8
*
* Allows you to retrieve and kick "Away from Keyboard" players.
*
* Requires:
* OrderEvent by Bribe - http://www.hiveworkshop.com/forums/jass-resources-412/snippet-order-event-190871/
* GetPlayerColored by Ammorth - http://www.wc3c.net/showthread.php?t=101692
*
* API:
*
* function RegisterAFKPlayer takes player p returns boolean
* function RemoveAFKPlayer takes player p returns nothing
* function ResetAFKPlayer takes player p returns nothing
* function GetAFKTime takes player p returns real
*/
library AntiAFK requires OrderEvent, GetPlayerColored
globals
private constant real INTERVAL = 1 //how often should we check for afk players? value should be in seconds
private constant string AFK = "-afk" //the chat command to enter for checking afk players
private constant string KICK = "-kickafk" //the chat comment to enter for kicking players
private constant integer MAX_TIME = 10 //the required amount of time (in seconds) to check if a player can be kicked
// value should be greater than 1
private constant integer MIN_TIME = 3 //the required amount of time (in seconds) to check if a player is afk
// value should be greater than 1
endglobals
//! textmacro AntiAFK_OnKickHook
/*
* Add your hook functions here if we have successfully kicked a player
* variable p is the afk player and variable t is the player who kicked that afk player
* Default Value:
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,8,GetPlayerNameColored(p) + " was kicked by " + GetPlayerNameColored(t) + " for being afk!")
call RemovePlayer(p,PLAYER_GAME_RESULT_VICTORY)
//===============================================*/
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,8,GetPlayerNameColored(p) + " was kicked by " + GetPlayerNameColored(t) + " for being afk!")
call RemovePlayer(p,PLAYER_GAME_RESULT_DEFEAT)
//===============================================
//! endtextmacro
//==== END OF CONFIGURABLES =====
globals
private hashtable ht = InitHashtable()
endglobals
private struct Afk
player pl
real dur
static method KickPlayer takes player t, player p, integer id returns nothing
call thistype(LoadInteger(ht,id,0)).destroy()
call FlushChildHashtable(ht,id)
//! runtextmacro AntiAFK_OnKickHook()
endmethod
static method CheckOutput takes nothing returns nothing
local integer i = 0
local integer int
loop
exitwhen i > 11
if HaveSavedInteger(ht,i,0) then
set int = LoadInteger(ht,i,0)
set thistype(int).dur = thistype(int).dur + INTERVAL
if thistype(int).dur >= MAX_TIME and not HaveSavedBoolean(ht,i,0) then
call SaveBoolean(ht,i,0,true)
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,5,GetPlayerNameColored(thistype(int).pl) + " has been afk for " + R2S(thistype(LoadInteger(ht,i,0)).dur/60) + " minute(s). Type |CFF7EBFF1" + KICK + " " + I2S(i+1) + "|r to kick this player." )
endif
endif
set i = i + 1
endloop
endmethod
static method OnChat takes nothing returns boolean
local string str = GetEventPlayerChatString()
local string st = ""
local integer i = 0
local integer int
local player p = GetTriggerPlayer()
if str == AFK then
loop
exitwhen i > 11
if HaveSavedInteger(ht,i,0) then
set int = LoadInteger(ht,i,0)
if thistype(int).dur > MIN_TIME then
set st = st + GetPlayerNameColored(Player(i)) + " has been afk for " + R2S(thistype(int).dur/60) + " minute(s). \n"
endif
endif
set i = i + 1
endloop
if st == "" then
call DisplayTimedTextToPlayer(p,0,0,5,"No one is afk.")
else
call DisplayTimedTextToPlayer(p,0,0,5,st)
endif
call DisplayTimedTextToPlayer(p,0,0,1," ")
elseif SubString(str,0,StringLength(KICK)) == KICK then
if S2I(SubString(str,StringLength(KICK)+1,2)) != 0 then
set int = S2I(SubString(str,StringLength(KICK)+1,2))
if int <= 12 and int > 0 then
set int = int - 1
if HaveSavedBoolean(ht,int,0) and int != GetPlayerId(p) then
call thistype.KickPlayer.execute(p,Player(int),int)
endif
endif
elseif S2I(SubString(str,StringLength(KICK)+1,1)) != 0 then
set int = S2I(SubString(str,StringLength(KICK)+1,1))
if int <= 12 and int > 0 then
set int = int - 1
if HaveSavedBoolean(ht,int,0) and int != GetPlayerId(p) then
call thistype.KickPlayer.execute(p,Player(int),int)
endif
endif
endif
endif
set p = null
return false
endmethod
static method OnOrder takes nothing returns boolean
local integer id = GetPlayerId(GetTriggerPlayer())
if HaveSavedInteger(ht,id,0) and thistype(LoadInteger(ht,id,0)).dur > (MIN_TIME-1) then
set thistype(LoadInteger(ht,id,0)).dur = 0
call RemoveSavedBoolean(ht,id,0)
endif
return false
endmethod
static method register takes player p returns nothing
local thistype dat
local integer i = GetPlayerId(p)
if not HaveSavedInteger(ht,i,0) and GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING then
set dat = thistype.allocate()
call SaveInteger(ht,i,0,dat)
set dat.pl = Player(i)
set dat.dur = 0
debug else
debug call BJDebugMsg("[AntiAfk] Player " + GetPlayerName(Player(i)) + " cannot be registered!")
endif
endmethod
static method onInit takes nothing returns nothing
local integer i = 0
local trigger t
local thistype dat
if MIN_TIME <= 1 or MAX_TIME <= 1 then
debug call BJDebugMsg("[AntiAFK] Crashed! Invalid values for MIN_TIME or MAX_TIME")
return
endif
set t = CreateTrigger()
call RegisterAnyOrderEvent(function thistype.OnOrder)
call TimerStart(CreateTimer(),INTERVAL,true,function thistype.CheckOutput)
loop
exitwhen i > 11
call TriggerRegisterPlayerChatEvent(t,Player(i),"-",false)
if GetPlayerController(Player(i)) == MAP_CONTROL_USER and GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and not HaveSavedInteger(ht,i,0) then
set dat = thistype.allocate()
call SaveInteger(ht,i,0,dat)
set dat.pl = Player(i)
set dat.dur = 0
endif
set i = i + 1
endloop
call TriggerAddCondition(t,Filter(function thistype.OnChat))
set t = null
endmethod
endstruct
function RegisterAFKPlayer takes player p returns nothing
call Afk.register(p)
endfunction
function RemoveAFKPlayer takes player p returns nothing
call Afk(LoadInteger(ht,GetPlayerId(p),0)).destroy()
call FlushChildHashtable(ht,GetPlayerId(p))
endfunction
function ResetAFKPlayer takes player p returns nothing
set Afk(LoadInteger(ht,GetPlayerId(p),0)).dur = 0
call RemoveSavedBoolean(ht,GetPlayerId(p),0)
endfunction
function GetAFKTime takes player p returns real
return Afk(LoadInteger(ht,GetPlayerId(p),0)).dur
endfunction
endlibrary