Maybe for someone this code will be usefull. I share this AFK Timer, because most of timers that already exist and "detects" - AFK - works by "PLAYER_FLAGS_CHANGED" EVENT. But it's not correct, because game detect any mouse move and keypress and then it reset afk timer.
For example, if you will move your mouse every 29 minutes or press any key (i mean ANY, for example CTRL), you will never be kicked from game World, for AFK.
As well, most timers uses "OnUpdate" event that triggers every frame, and it's not CPU efficient.
This is very simple and very CPU friendly code, that detects logout (AFK) time with perfect precision.
This code uses only ticker instead of "OnUpdate", that's why it's very CPU friendly.
Even that C_Timer.NewTicker i think a bit more expensive then "OnUpdate", it triggers a lot more rarely.
Only thing it's not tracking - combat or out of combat. But i think it's easy to implement in future.
Lua Code:
local function InitializeAdvancedGameTimer()
local step = 1
local x, y
local timer = 0
-- NEED TO ADD FEATURE (Disable timer in Combat)
local f = Frame or CreateFrame("Frame", "Frame", UIParent)
local function PropagateKeyboardInput(self, key)
print("KEY PRESSED: "..RED_FONT_COLOR_CODE..key..FONT_COLOR_CODE_CLOSE)
timer = 0
end
f:SetScript("OnKeyDown", PropagateKeyboardInput)
f:SetPropagateKeyboardInput(true)
local function GetMousePosition()
x, y = GetCursorPosition();
end
local function CheckMousePosition()
local nx, ny = GetCursorPosition();
if x ~= nx and y ~= ny then
x, y = GetCursorPosition();
print("CURSOR POSITION CHANGED: "..RED_FONT_COLOR_CODE..x.." "..y..FONT_COLOR_CODE_CLOSE)
timer = 0
else
timer = timer + step
if timer > 10 then
print("LOGOUT IN "..YELLOW_FONT_COLOR_CODE..1800 - timer..FONT_COLOR_CODE_CLOSE.." SEC.")
end
end
end
GetMousePosition()
C_Timer.NewTicker(step, CheckMousePosition)
end
InitializeAdvancedGameTimer()
To make this code more CPU efficient, you can change step from 1 second to 10 for example.
P.S. Thank you
MunkDev for helping with code that detects keypress -
http://www.wowinterface.com/forums/s...ad.php?t=52723