-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_better_antiafk.lua
72 lines (62 loc) · 2.26 KB
/
cl_better_antiafk.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local idle = {
t = 0,
last_message = 0,
stopped = false,
mx = 0,
my = 0,
afk = false
}
local function checkIdle(client, cmd)
if not IsValid(client) then return end
local curtime = os.time()
if idle.t == 0 then
-- init things
idle.t = curtime
idle.mx = gui.MouseX()
idle.my = gui.MouseY()
return
end
if GetRoundState() == ROUND_ACTIVE and client:IsTerror() and client:Alive() then
if idle.stopped then -- prevent messing up after new round
idle.t = curtime
idle.stopped = false
end
local idle_limit = 120
-- networking sucks sometimes
if idle_limit <= 0 then
idle_limit = 300
end
if cmd:GetButtons() ~= 0 or math.abs(cmd:GetMouseX()) > 2 or math.abs(cmd:GetMouseY()) > 2 then
idle.t = curtime
idle.afk = false
elseif system.HasFocus() and (gui.MouseX() ~= idle.mx or gui.MouseY() ~= idle.my) then
-- why the fuck is mousepos given without focus lol?
idle.t = curtime
idle.afk = false
idle.mx = gui.MouseX()
idle.my = gui.MouseY()
elseif not idle.afk and curtime > (idle.t + idle_limit) then
idle.afk = true
RunConsoleCommand("say", "(AUTOMATED MESSAGE) I have been moved to the Spectator team because I was idle/AFK.")
timer.Simple(0.3, function()
RunConsoleCommand("ttt_spectator_mode", 1)
net.Start("TTT_Spectate")
net.WriteBool(true)
net.SendToServer()
RunConsoleCommand("ttt_cl_idlepopup")
end)
elseif curtime - idle.last_message >= 5 and curtime > (idle.t + (idle_limit / 2)) then
-- will repeat
idle.last_message = curtime
LANG.Msg("idle_warning")
end
else
idle.stopped = true
end
end
hook.Add("Initialize", "BetterAntiAFK", function()
if not system.IsOSX() then -- keep legacy check for OSX (see https://wiki.garrysmod.com/page/system/HasFocus)
timer.Remove("idlecheck")
hook.Add("StartCommand", "BetterAntiAFK", checkIdle)
end
end )