Author Topic: UAfk -- Another AFK Manager (Uses client execute!)  (Read 2257 times)

0 Members and 1 Guest are viewing this topic.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3488
  • Karma: 188
  • Project Lead
    • View Profile
UAfk -- Another AFK Manager (Uses client execute!)
« on: November 16, 2006, 07:54:26 AM »
Alright, here's another AFK Manager, written for ULib. Unlike other AFK managers (Sorry Terminal58), this script goes very easy as far as CPU time required; it also tracks AFK status by player angle, which is the most likely variable to change.

We're debating whether or not to put this in ULX 2.25. If you have an opinion either way, please let us know!

Features:
  • Does not require an exorbitant amount of CPU time. (And you can change how much it requires using checkTime).
  • Tracks by player's angle.
  • Offers a kick feature, immunity to kick for admins, and an auto-disable for the kick when the server has under minPlayers currently on the server.
  • Tells players how long they have until they're kicked.
  • Changes player's names to reflect their AFK status (Requires the ULib plugin).
  • Puts text above the player informing others of their AFK status
  • Makes the AFK player invincible while AFK


Code: (lua) [Select]
assert( _file.Exists( "lua/ULib/init.lua" ), "UAfk needs ULib to run!" )
_OpenScript( "ULib/init.lua" )
assert( ULib.VERSION >= 1.2, "UAfk requires ULib version 1.2 or higher to run!" )

local maxAfk = 300 -- Time from AFK to kick, 0 to disable kicking.
local minAfk = 60 -- Time required to be considered AFK.
local minPlayers = 4 -- The minimum number of players required before this script will start kicking (disabled if maxAfk is 0).
local checkTime = 1 -- How often, in seconds, it checks if you're afk.
local access = ACCESS_RESERVATION -- Access needed not to be kicked (if maxAfk is enabled).
local prefixAfk = "[AFK] " -- What to prefix the user's name with when they go AFK (requires the ULib server plugin).
local afkText = "This player is AFK!" -- What to show over player's head. Set to "" to disable.
local ucl = ULib.mainUcl

local rectKey = 678
local textKey = 679

-- Note: we're checking by angles, since that changes more often than position. We're also taking the tostring() so we get an approximation

-- Let's set up our information
local playerInfo = {}
for i=1, _MaxPlayers() do
playerInfo[ i ] = { ang=tostring(vector3( 0, 0, 0 )), time=0, afk=false }
end


local function checkPlayers()
local curTime = _CurTime()
local players = ULib.totalPlayers()
for i=1, _MaxPlayers() do
if _PlayerInfo( i, "connected" ) then
local curang = tostring( _EntGetAng( i ) )
if playerInfo[ i ].ang ~= curang then -- They are not AFK
if playerInfo[ i ].afk then -- If they just came back from AFK.
if ULib.pluginEnabled() and prefixAfk ~= "" then
ULib.cexec( i, "name \"" .. playerInfo[ i ].name .. "\"" )
end
_GModText_Hide( i, textKey )
_GModText_Hide( i, textKey + 1 )
_GModText_Hide( 0, textKey + 1 + i )
_GModRect_Hide( i, rectKey )
_PlayerGod( i, false ) -- Give them god so they can't be killed or moved
end
playerInfo[ i ] = { ang=curang, time=curTime, afk=false }

else -- They are AFK
local afkTime = curTime - playerInfo[ i ].time
if afkTime >= minAfk then -- They've hit AFK time
if not playerInfo[ i ].afk then -- They're just now going afk.
playerInfo[ i ].afk = true
if ULib.pluginEnabled() and prefixAfk ~= "" then
local name = _PlayerInfo( i, "name" )
if string.sub( name, 1, string.len( prefixAfk ) ) == prefixAfk then -- They already have the prefix for some reason
playerInfo[ i ].name = string.sub( name, string.len( prefixAfk ) + 1 )
else
playerInfo[ i ].name = _PlayerInfo( i, "name" )
ULib.cexec( i, "name \"" .. prefixAfk .. playerInfo[ i ].name .. "\"" )
end
end
ULib.sendRect( i, rectKey, 0, 0, 1, 1, 9999, 70, 70, 70, 200 )
ULib.sendText( i, textKey, 0.1, 0.4, "You are currently flagged as AFK", 9999, 0, 255, 0, 255, "ImpactMassive" )
if afkText ~= "" then
_GModText_Start( "Default" )
_GModText_SetColor( 255, 0, 0, 255 )
_GModText_SetTime( 9999, 0, 0 )
_GModText_SetEntityOffset( vector3( 0, 0, 60 ) )
_GModText_SetEntity( i )
_GModText_SetText( afkText )
_GModText_Send( 0, textKey + 1 + i )
end
_PlayerGod( i, true ) -- Give them god so they can't be killed or moved
end

if maxAfk > 0 and players >= minPlayers and not ucl:query( i, access ) then
if afkTime >= maxAfk and not ucl:query( i, access ) then -- Past kick time
if ULib.pluginEnabled() and prefixAfk ~= "" then -- Set their name back before kicking.
ULib.cexec( i, "name \"" .. playerInfo[ i ].name .. "\"" )
end

ULib.addTimer( 0.5, 1, ULib.kick, i, "AFK Timeout" ) -- This ensures the cexec will get to them
end

local timeLeft = maxAfk - afkTime
local h, m, s = ULib.sToHMS( timeLeft )
local timeText
if m > 0 then
timeText = m .. ":" .. string.format( "%02i", s ) .. " minutes"
else
timeText = s .. " seconds"
end
ULib.sendText( i, textKey + 1, 0.4, 0.25, "You have " .. timeText .. " left until you're kicked.", checkTime + 1, 255, 0, 0, 255, "ChatFont", 0, 0 )
end
end
end
end
end
end
ULib.addTimer( checkTime, 0, checkPlayers )

local function disconnect( name, userid ) -- Reset their information
playerInfo[ userid ] = { ang=tostring(vector3( 0, 0, 0 )), time=0, afk=false }
_GModText_Hide( 0, textKey + 1 + userid )
end
HookEvent( "eventPlayerDisconnect", disconnect )
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” -Brian W. Kernighan
"I love working on my crappy code. it is crappy, but it is mine. I guess I could love other people's code, but it's like loving other people's children -- not quite the same as your own" -- Jeff Atwood
"Their tree came and found my tree!" -- Stickly Man!

Offline Suicidal.Banana

  • Newbie
  • *
  • Posts: 28
  • Karma: 2
    • View Profile
Re: UAfk -- Another AFK Manager (Uses client execute!)
« Reply #1 on: March 05, 2007, 08:59:08 AM »
Very nice, include it with ULX 2.25

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3262
  • Karma: 106
  • Project Specialist
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
Re: UAfk -- Another AFK Manager (Uses client execute!)
« Reply #2 on: March 05, 2007, 06:22:55 PM »
Suicidal Banana,
Gmod 10's release, and the ULib 2 release along shortly after made this go the wayside for ULX 3.
Far beyond 2.25 of Gmod 9 days.

Its funny you reply though. I had thought about remaking this as an addon for ULib 2+ here in the past few days.
Megiddo gave me permission a long time ago.
I just never sank my teeth into it.

If you or someone else would like to do it before I do, by all means please feel free.

Wire mod seems to have grabbed my attention as of late.

So many toys, so little time.
:P
Software Upgrade Paradox - If you improve a piece of software enough times, you eventually ruin it - David Pogue

Offline Terminal58

  • Newbie
  • *
  • Posts: 41
  • Karma: 1
    • View Profile
Re: UAfk -- Another AFK Manager (Uses client execute!)
« Reply #3 on: March 05, 2007, 08:37:55 PM »
Alright, here's another AFK Manager, written for ULib. Unlike other AFK managers (Sorry Terminal58), this script goes very easy as far as CPU time required; it also tracks AFK status by player angle, which is the most likely variable to change.
*cry*

Offline Manny102

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: UAfk -- Another AFK Manager (Uses client execute!)
« Reply #4 on: November 19, 2007, 04:37:13 AM »
Hello!

Is this script compatible with the GMOD10 anymore?
How would i use this script in my server?

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3488
  • Karma: 188
  • Project Lead
    • View Profile
Re: UAfk -- Another AFK Manager (Uses client execute!)
« Reply #5 on: November 19, 2007, 06:53:24 AM »
Not compatible.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” -Brian W. Kernighan
"I love working on my crappy code. it is crappy, but it is mine. I guess I could love other people's code, but it's like loving other people's children -- not quite the same as your own" -- Jeff Atwood
"Their tree came and found my tree!" -- Stickly Man!