-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisafk.lua
183 lines (143 loc) · 4.75 KB
/
isafk.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
--@name isAFK
--@author Ax25 :3
--@shared
if CLIENT then
playersStatus = {}
local status = game.hasFocus()
local lastStatus = status
local focus = game.hasFocus()
local inputAFK = false
local lastInputTime = timer.curtime()
local function sendStatus()
net.start("focusStatus")
net.writeBool(status)
net.send()
end
sendStatus()
local function sendPing()
net.start("ping")
net.send()
end
sendPing()
timer.create("ping", 4, 0, sendPing)
timer.create("afkCheck", 0.1, 0, function()
focus = game.hasFocus()
if timer.curtime() - lastInputTime > 15 then
inputAFK = true
else
inputAFK = false
end
if ( not focus ) or ( inputAFK ) then
status = false
else
status = true
end
if lastStatus ~= status then
lastStatus = status
sendStatus()
end
end)
hook.add("inputPressed", "lastInput", function()
lastInputTime = timer.curtime()
end)
hook.add("mousemoved", "lastInput", function()
lastInputTime = timer.curtime()
end)
net.receive("Status", function()
playersStatus = net.readTable()
end)
if player() == owner() then
enableHud(owner(), true)
end
local fontRoboto60 = render.createFont(
"Roboto",
60,
500,
false,
false,
true,
false,
0,
true,
0
)
hook.add("PostDrawTranslucentRenderables", "", function()
for k,data in pairs(playersStatus) do
local ply = player(k)
if not ply:isValid() then
playersStatus[k] = nil
continue
end
local dist = player():getPos():getDistance(ply:getPos())
if ( not data.status and player() ~= ply ) and dist < 1000 then
local m = ply:getMatrix()
local pos = ply:worldToLocal( ply:getAttachment( ply:lookupAttachment("eyes") ) )
m:translate(pos+Vector(0, 0, 20))
local ang = (render.getEyePos() - m:getTranslation()):getAngle() + Angle(90, 0, 0)
m:setAngles(ang)
m:rotate(Angle(0, 90, 0))
m:setScale(Vector(0.1, -0.1))
render.pushMatrix(m)
render.enableDepth(true)
render.setColor(Color(10, 167, 238))
render.setFont(fontRoboto60)
render.drawSimpleText(0, 0, "AFK", 1, 1)
render.popMatrix()
end
end
end)
else
local playersStatus = {}
local function createPlyTable(ply)
playersStatus[ply:getUserID()] = {}
playersStatus[ply:getUserID()].status = true
playersStatus[ply:getUserID()].lastPing = 0
playersStatus[ply:getUserID()].lastSvInput = timer.curtime()
end
net.receive("focusStatus", function(_,ply)
if not playersStatus[ply:getUserID()] then
createPlyTable(ply)
end
playersStatus[ply:getUserID()].status = net.readBool()
playersStatus[ply:getUserID()].afksince = timer.curtime()
playersStatus[ply:getUserID()].lastPing = timer.curtime()
--print(ply,playersStatus[ply:getUserID()].status)
end)
net.receive("ping", function(_,ply)
if not playersStatus[ply:getUserID()] then
createPlyTable(ply)
end
playersStatus[ply:getUserID()].lastPing = timer.curtime()
end)
timer.create("broadcastStatus", 1, 0, function()
for k,_ in pairs(playersStatus) do
if not player(k):isValid() then
playersStatus[k] = nil
end
end
net.start("Status")
net.writeTable(playersStatus)
net.send()
end)
hook.add("KeyPress", "svlastInput", function(ply)
if not playersStatus[ply:getUserID()] then
createPlyTable(ply)
end
playersStatus[ply:getUserID()].lastSvInput = timer.curtime()
end)
timer.create("checkSvLastInput", 2, 0, function() -- incase client stop sending data
for k,ply in pairs(find.allPlayers()) do
if not playersStatus[ply:getUserID()] then
createPlyTable(ply)
end
if timer.curtime() - playersStatus[ply:getUserID()].lastPing > 6 then
if timer.curtime() - playersStatus[ply:getUserID()].lastSvInput > 15 then
playersStatus[ply:getUserID()].status = false
playersStatus[ply:getUserID()].afksince = timer.curtime()
else
playersStatus[ply:getUserID()].status = true
end
end
end
end)
end