ULX

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

0 Members and 1 Guest are viewing this topic.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
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
  1. assert( _file.Exists( "lua/ULib/init.lua" ), "UAfk needs ULib to run!" )
  2. _OpenScript( "ULib/init.lua" )
  3. assert( ULib.VERSION >= 1.2, "UAfk requires ULib version 1.2 or higher to run!" )
  4.  
  5. local maxAfk = 300 -- Time from AFK to kick, 0 to disable kicking.
  6. local minAfk = 60 -- Time required to be considered AFK.
  7. local minPlayers = 4 -- The minimum number of players required before this script will start kicking (disabled if maxAfk is 0).
  8. local checkTime = 1 -- How often, in seconds, it checks if you're afk.
  9. local access = ACCESS_RESERVATION -- Access needed not to be kicked (if maxAfk is enabled).
  10. local prefixAfk = "[AFK] " -- What to prefix the user's name with when they go AFK (requires the ULib server plugin).
  11. local afkText = "This player is AFK!" -- What to show over player's head. Set to "" to disable.
  12. local ucl = ULib.mainUcl
  13.  
  14. local rectKey = 678
  15. local textKey = 679
  16.  
  17. -- Note: we're checking by angles, since that changes more often than position. We're also taking the tostring() so we get an approximation
  18.  
  19. -- Let's set up our information
  20. local playerInfo = {}
  21. for i=1, _MaxPlayers() do
  22.         playerInfo[ i ] = { ang=tostring(vector3( 0, 0, 0 )), time=0, afk=false }
  23. end
  24.  
  25.  
  26. local function checkPlayers()
  27.         local curTime = _CurTime()
  28.         local players = ULib.totalPlayers()
  29.         for i=1, _MaxPlayers() do
  30.                 if _PlayerInfo( i, "connected" ) then
  31.                         local curang = tostring( _EntGetAng( i ) )
  32.                         if playerInfo[ i ].ang ~= curang then -- They are not AFK
  33.                                 if playerInfo[ i ].afk then -- If they just came back from AFK.
  34.                                         if ULib.pluginEnabled() and prefixAfk ~= "" then
  35.                                                 ULib.cexec( i, "name \"" .. playerInfo[ i ].name .. "\"" )
  36.                                         end
  37.                                         _GModText_Hide( i, textKey )
  38.                                         _GModText_Hide( i, textKey + 1 )
  39.                                         _GModText_Hide( 0, textKey + 1 + i )
  40.                                         _GModRect_Hide( i, rectKey )
  41.                                         _PlayerGod( i, false ) -- Give them god so they can't be killed or moved
  42.                                 end
  43.                                 playerInfo[ i ] = { ang=curang, time=curTime, afk=false }
  44.  
  45.                         else -- They are AFK
  46.                                 local afkTime = curTime - playerInfo[ i ].time
  47.                                 if afkTime >= minAfk then -- They've hit AFK time
  48.                                         if not playerInfo[ i ].afk then -- They're just now going afk.
  49.                                                 playerInfo[ i ].afk = true
  50.                                                 if ULib.pluginEnabled() and prefixAfk ~= "" then
  51.                                                         local name = _PlayerInfo( i, "name" )
  52.                                                         if string.sub( name, 1, string.len( prefixAfk ) ) == prefixAfk then -- They already have the prefix for some reason
  53.                                                                 playerInfo[ i ].name = string.sub( name, string.len( prefixAfk ) + 1 )
  54.                                                         else
  55.                                                                 playerInfo[ i ].name = _PlayerInfo( i, "name" )
  56.                                                                 ULib.cexec( i, "name \"" .. prefixAfk .. playerInfo[ i ].name .. "\"" )
  57.                                                         end
  58.                                                 end
  59.                                                 ULib.sendRect( i, rectKey, 0, 0, 1, 1, 9999, 70, 70, 70, 200 )
  60.                                                 ULib.sendText( i, textKey, 0.1, 0.4, "You are currently flagged as AFK", 9999, 0, 255, 0, 255, "ImpactMassive" )
  61.                                                 if afkText ~= "" then
  62.                                                         _GModText_Start( "Default" )
  63.                                                                 _GModText_SetColor( 255, 0, 0, 255 )
  64.                                                                 _GModText_SetTime( 9999, 0, 0 )
  65.                                                                 _GModText_SetEntityOffset( vector3( 0, 0, 60 ) )
  66.                                                                 _GModText_SetEntity( i )
  67.                                                                 _GModText_SetText( afkText )
  68.                                                         _GModText_Send( 0, textKey + 1 + i )
  69.                                                 end
  70.                                                 _PlayerGod( i, true ) -- Give them god so they can't be killed or moved
  71.                                         end
  72.  
  73.                                         if maxAfk > 0 and players >= minPlayers and not ucl:query( i, access ) then
  74.                                                 if afkTime >= maxAfk and not ucl:query( i, access ) then -- Past kick time
  75.                                                         if ULib.pluginEnabled() and prefixAfk ~= "" then -- Set their name back before kicking.
  76.                                                                 ULib.cexec( i, "name \"" .. playerInfo[ i ].name .. "\"" )
  77.                                                         end
  78.  
  79.                                                         ULib.addTimer( 0.5, 1, ULib.kick, i, "AFK Timeout" ) -- This ensures the cexec will get to them
  80.                                                 end
  81.  
  82.                                                 local timeLeft = maxAfk - afkTime
  83.                                                 local h, m, s = ULib.sToHMS( timeLeft )
  84.                                                 local timeText
  85.                                                 if m > 0 then
  86.                                                         timeText = m .. ":" .. string.format( "%02i", s ) .. " minutes"
  87.                                                 else
  88.                                                         timeText = s .. " seconds"
  89.                                                 end
  90.                                                 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 )
  91.                                         end
  92.                                 end
  93.                         end
  94.                 end
  95.         end
  96. end
  97. ULib.addTimer( checkTime, 0, checkPlayers )
  98.  
  99. local function disconnect( name, userid ) -- Reset their information
  100.         playerInfo[ userid ] = { ang=tostring(vector3( 0, 0, 0 )), time=0, afk=false }
  101.         _GModText_Hide( 0, textKey + 1 + userid )
  102. end
  103. HookEvent( "eventPlayerDisconnect", disconnect )
Experiencing God's grace one day at a time.

Offline Suicidal.Banana

  • Newbie
  • *
  • Posts: 30
  • Karma: 3
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
Oh my, almost 7 years

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8098
  • Karma: 390
  • Sertafide Ulysses Jenius
    • 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
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Terminal58

  • Newbie
  • *
  • Posts: 41
  • Karma: 1
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
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: 6214
  • Karma: 394
  • Project Lead
Re: UAfk -- Another AFK Manager (Uses client execute!)
« Reply #5 on: November 19, 2007, 06:53:24 AM »
Not compatible.
Experiencing God's grace one day at a time.