Author Topic: Bringing back UAfk.. but not with that name of course! =)  (Read 6622 times)

0 Members and 1 Guest are viewing this topic.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Bringing back UAfk.. but not with that name of course! =)
« on: December 20, 2007, 02:48:20 AM »
right now it sets a players table by uniqueID when they join the server and checks angles every 2.5 minutes. If they havn't changed their angle since the last check, it'll flag them as afk..which at this point only sends them a message and sets a variable.. nothing visual. If they STILL havn't moved by the next check.. they will be kicked from the server.

This addon requires ULib to run as it uses some ULibrary functions. (nothing that couldn't be rewritten easily.. but why bother?!)

I plan on adding in some settings variables.. such as letting the configuring person set the time till kick and probably the option not to kick.

Something visual.. I'll have to look in to the text under the name thing.. I'm sure I could get a name change worked out like in UAfk.


Ideas? Comments?

Oh yeah..here is what I have so far.. it is working.. and I'm actually using it on my server now. I'm sure there might be some bugs.. but nothing I could find in SP.. so now Im guineapigging my Dedi.

Code: [Select]
if SERVER then

pangles = {} --Initialize the Table

function AFKCheck()
local curangle = "0" --Default value for this variable
for i, v in ipairs(player.GetAll()) do
curangle = tostring(v:GetAngles()) --Gets the current angle of the player in the loop
if pangles[v:UniqueID()].angle == curangle then --Does this new angle match the angle stored?
if pangles[v:UniqueID()].afk == "1" then
pangles[v:UniqueID()].afk = "0"
pangles[v:UniqueID()].angle = "0"
game.ConsoleCommand("ulx kick " ..v:GetName().. " AFK - Auto Kick\n") --Kick the player if they have not moved since last check.
elseif pangles[v:UniqueID()].afk == "0" then
pangles[v:UniqueID()].afk = "1"
ULib.tsay(v, "You are flagged as AFK, if you do not move in 2:30 you will be kicked", true)
end
elseif pangles[v:UniqueID()].angle == curangle then
pangles[v:UniqueID()].afk = "0"
end
pangles[v:UniqueID()].angle = tostring(v:GetAngles()) --Store new angle value for the user in the loop
--v:ConCommand("say Test")
end
end
timer.Create( "G4P_AFKTimer", 150, 0, AFKCheck ) --Checks status every 2.5 Minutes.
end

function PlayerSpawn( ply ) -- Sets default data for a player when he joins.
pangles[ply:UniqueID()] = { angle="0", afk="0"}
end
hook.Add( "PlayerInitialSpawn", "afkspawnhook", PlayerSpawn );
**Save this to a lua file and throw it in your LUA/Autorun folder if you want to use it...**
I lightly commented it for my own personal use.. and there are some testing commented out lines in there you might find.. they do nothing for the function.. just things I used to test it.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #1 on: December 20, 2007, 05:28:15 AM »
Nice work Pres. Make it an addon for ULib (Ulib has a modules folder too)
As you add more features, perhaps have it auto adjust the time it waits to do kicks.
2.5 min if server > 85% full, 10 min if less, something like that.

I look forward to seeing it reach the releases section here and on FP.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #2 on: December 20, 2007, 08:48:45 AM »
As far as making this into a module for ULib, is there somethign I have to do to it? or would this be loaded correctly if I placed it into the ULib/Modules folder?

And as for the kick time adjustment, like that idea, perhaps set up a config at the start of the file

--Kick Time Configuration (Time in minutes)--
--Time to wait if server is 75% full or more--
local ktime75 = 5

--Time to wait if server is less than 75% full--
local ktime = 10

--End Configuration

ktime75 = ktime75*60 --Gets the time in seconds (what the script will use)
ktime = ktime*60 --Ditto^^




Then just having one higherlevel if than call performing one function if the connected players / Max Players >= .75 and < .75.

are the ULib function calls to get connected player count and max player count the same now as they were in the last version of ULX?

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #3 on: December 20, 2007, 10:23:32 AM »
Good work! One suggestion would be to store the current angle as an angle object instead of a string, as it will take less memory that way.

To make it a ULib module, just stick it in the module folder. It will execute on both server and client though, so take care.

are the ULib function calls to get connected player count and max player count the same now as they were in the last version of ULX?

ULib doesn't have any such functions?
Experiencing God's grace one day at a time.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #4 on: December 20, 2007, 11:56:13 AM »
Hmm.. I could have sworn I saw a function like that when I was looking at the old code for UAfk.. maybe not.. I'll look again. Also, the code I have is speperated completely by if SERVER and if CLIENT statements.. so running on both shouldn't be an issue.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #5 on: December 20, 2007, 01:31:36 PM »
Code: [Select]
--UAfk - By Zach Petty (MrPresident) for the use on gman4president.com G4P GarrysMod servers.
--Original concept by Megiddo of Team Ulysses
--Completely rewritten for the new lua and ULib
--
--BEGIN CONFIGURATION--
--
--AFK Timer (In Seconds) NOTE: If Kick Flag is turned on below, they will be kicked in twice the time you set for the afk flag--
local uafktime = 10
--
--Should the User be kicked if they staf afk after being flagged as AFK?--
local uafkick = true
--
--Should admins be immune to the afk kick if it is enabled?--
local uafkimmune = true
--
--
--END CONFIGURATION.. DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING--



if SERVER then
--ADDS THE CONSOLE COMMAND TO SET THE AFK TIMER--
function uafktimevar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if tonumber(args[1]) >= 1 then
uafktime = args[1]
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Player: " ..ply:GetName().. " has set the afk timer to " ..uafktime.. " seconds", true)
timer.Create( "G4P_AFKTimer", uafktime, 0, AFKCheck ) --Checks status every period you designate.

end
else
ULib.tsay(ply, "Value must be numerical and higher than 0", true)
end

end
concommand.Add("UAFK_SetTVar",uafktimevar)

--ADDS THE CONSOLE COMMAND TO SET KICK FLAGS--
function uafkkickvar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if args[1] == "0" then
uafkick = false
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Player: " ..ply:GetName().. " has turned AFK Kicking off!", true)
end
elseif args[1] == "1" then
uafkick = true
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Player: " ..ply:GetName().. " has turned AFK Kicking on!", true)
end
else
ULib.tsay(ply, "You must enter 1 for True or 0 for False!", true)
return
end
end
concommand.Add("UAFK_SetKickVar",uafkkickvar)

--ADDS THE CONSOLE COMMAND TO SET ADMIN IMMUNITY--
function uafkadminvar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if args[1] == "0" then
uafkimmune = false
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Player: " ..ply:GetName().. " has turned Admin Immunity off!", true)
end
elseif args[1] == "1" then
uafkimmune = true
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Player: " ..ply:GetName().. " has turned Admin Immunity on!", true)
end
else
ULib.tsay(ply, "You must enter 1 for True or 0 for False!", true)
return
end
end
concommand.Add("UAFK_SetAdminVar",uafkadminvar)


pangles = {} --Initialize the Table

function AFKCheck()
local curangle = "0" --Default value for this variable

for i, v in ipairs(player.GetAll()) do

curangle = v:GetAngles() --Gets the current angle of the player in the loop

if pangles[v:UniqueID()].angle == curangle then --Does this new angle match the angle stored?

if pangles[v:UniqueID()].afk == "1" then
if uafkick == true then
if uafkimmune == true then
if v:IsAdmin() then
ULib.tsay(v, "Still afk, but immune to being kicked", true)
else
game.ConsoleCommand("ulx kick " ..v:GetName().. " UAfk:AutoKick\n") --Kick the player if
end
else
game.ConsoleCommand("ulx kick " ..v:GetName().. " UAfk:AutoKick\n") --Kick the player if
end
else
ULib.tsay(v, "Still Afk... Kick has been turned off though.", true)
end

elseif pangles[v:UniqueID()].afk == "0" then
pangles[v:UniqueID()].afk = "1"
ULib.tsay(v, "You are flagged as AFK, if you do not move in " ..uafktime.. " seconds you will be kicked", true)

end

elseif pangles[v:UniqueID()].angle == curangle then
pangles[v:UniqueID()].afk = "0"

end

pangles[v:UniqueID()].angle = v:GetAngles() --Store new angle value for the user in the loop

v:ConCommand("say Test.. :" ..tostring(pangles[v:UniqueID()].angle)) -- Just my test callback to catch when the timer updates and what the angle is being stored as. Leave this commented out.

end

end

timer.Create( "G4P_AFKTimer", uafktime, 0, AFKCheck ) --Checks status every period you designate.

function PlayerSpawn( ply ) -- Sets default data for a player when he joins.
pangles[ply:UniqueID()] = { angle=Angle( 0, 0, 0 ), afk="0"}
end
hook.Add( "PlayerInitialSpawn", "afkspawnhook", PlayerSpawn );
end

New version?!? =)

In this I added
+Config at the beginning of the file to set the AFK Time (Kick time is still 2x AFK time)
+Config at the beginning of the file to set if Admins should be kicked
+Config at the beginning of the file to set if afk players should be kicked at all.
+Console commands to allow SuperAdmins to change all 3 of these variables from their console
+Changed the way the angle is stored.. no longer a string.


Everything works handydandy.. I think I'll just add the feature JamminR suggested with the different kick times for if the server is more or less populated
I know my coding is kind of sloppy and the new sections are hardly commented at all.. haha.. but it works =)


Also.. I'd like permission to keep the old name, since it is a remake of the original script, unless you would rather me not. And either way, I gave due credit at the top of my lua file to Megiddo and Team Ulysses.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #6 on: December 20, 2007, 04:13:47 PM »
Zakap, I didn't notice this earlier...not only is this reliant upon ULib, it will also require ULX.
I wasn't sure if you meant it to be or not.

If you don't mean to 'require' ULX, use ULib.kick
See its usage comment in the addons\ULib\lua\ULib\server\player.lua file.
If you want usage example, see (duh :P ) the ulx kick in \addons\ULX\lua\ULX\util.lua
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #7 on: December 20, 2007, 06:58:17 PM »
ya.. right after I posted this source.. I replaced the game.consolecommand with ULib.kick.. because the concommand way was giving me trouble with reasons cutting off.. or if the players name was jack smith it was saying the reason was smith.. LOL.. so I just decided to use the ULib way. Thanks anyways. Also.. this code is buggy.. im debugging it now.



Here is a finished working version.. only thing I need to impliment is
-Name changes (afk)PlayerName ?? dunno if we want this? suggestions?
-SubText under name.. is this even possible in GMod10?
-Variable kick times based on server population.

Code: [Select]
--UAfk - By Zach Petty (MrPresident) for the use on gman4president.com G4P GarrysMod servers.
--Original concept by Megiddo of Team Ulysses
--Completely rewritten for the new lua and ULib
--
--BEGIN CONFIGURATION--
--
--UAfk enabled? Should this addon start enabled?--
local uafkenabled = true
--
--AFK Timer (In Seconds) NOTE: If Kick Flag is turned on below, they will be kicked in twice the time you set for the afk flag--
local uafktime = 120
--
--Should the User be kicked if they staf afk after being flagged as AFK?--
local uafkick = true
--
--Should admins be immune to the afk kick if it is enabled?--
local uafkimmune = true
--
--
--END CONFIGURATION.. DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING--



if SERVER then
--ADDS THE CONSOLE COMMAND TO SET THE AFK TIMER--
function uafktimevar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if tonumber(args[1]) >= 1 then
uafktime = args[1]
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has set the afk timer to " ..uafktime.. " seconds", true)
timer.Create( "G4P_AFKTimer", uafktime, 0, AFKCheck ) --Checks status every period you designate.

end
else
ULib.tsay(ply, "Value must be numerical and higher than 0", true)
end

end
concommand.Add("uafk_time",uafktimevar)

--ADDS THE CONSOLE COMMAND TO SET KICK FLAGS--
function uafkkickvar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if args[1] == "0" then
uafkick = false
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has turned AFK Kicking off!", true)
end
elseif args[1] == "1" then
uafkick = true
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has turned AFK Kicking on!", true)
end
else
ULib.tsay(ply, "You must enter 1 for True or 0 for False!", true)
return
end
end
concommand.Add("uafk_kickenabled",uafkkickvar)

--ADDS THE CONSOLE COMMAND TO SET ADMIN IMMUNITY--
function uafkadminvar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if args[1] == "0" then
uafkimmune = false
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has turned Admin Immunity off!", true)
end
elseif args[1] == "1" then
uafkimmune = true
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has turned Admin Immunity on!", true)
end
else
ULib.tsay(ply, "You must enter 1 for True or 0 for False!", true)
return
end
end
concommand.Add("uafk_adminimmune",uafkadminvar)

--ADDS THE CONSOLE COMMAND TO SET IF IS ENABLED--
function uafkenabledvar( ply, command, args )
if !ply:IsSuperAdmin() then
ULib.tsay(ply, "Only Superadmin can change this variable", true)
return
end
if args[1] == "0" then
uafkenabled = false
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has disabled UAFK!", true)
end
elseif args[1] == "1" then
uafkenabled = true
for i, v in ipairs(player.GetAll()) do
ULib.tsay(v, "Admin: " ..ply:GetName().. " has enabled UAFK!", true)
end
else
ULib.tsay(ply, "You must enter 1 for True or 0 for False!", true)
return
end
end
concommand.Add("uafk_enabled",uafkenabledvar)


pangles = {} --Initialize the Table

function AFKCheck()
if uafkenabled == false then return end
local curangle = "0" --Default value for this variable
for i, v in ipairs(player.GetAll()) do
curangle = v:GetAngles() --Gets the current angle of the player in the loop
if pangles[v:UniqueID()].angle == curangle then --Does this new angle match the angle stored?
if pangles[v:UniqueID()].afk == "1" then
if uafkick == true then
if uafkimmune == true then
if v:IsAdmin() then
ULib.tsay(v, "Still afk, but immune to being kicked", true)
else
--game.ConsoleCommand("ulx kick " ..v:GetName().. " UAfk:AutoKick\n") --Kick the player if
ULib.kick(v, "UAFK: AutoKick Timeout")
end
else
--game.ConsoleCommand("ulx kick " ..v:GetName().. " UAfk:AutoKick\n") --Kick the player if
ULib.kick(v, "UAFK: AutoKick Timeout")
end
else
ULib.tsay(v, "Still Afk... Kick has been turned off though.", true)
end
elseif pangles[v:UniqueID()].afk == "0" then
pangles[v:UniqueID()].afk = "1"
ULib.tsay(v, "You are flagged as AFK, if you do not move in " ..uafktime.. " seconds you will be kicked", true)
end
else
pangles[v:UniqueID()].afk = "0"
--ULib.tsay(v, "Current Angle varies from saved angle. Timers Reset.", true)
end
pangles[v:UniqueID()].angle = v:GetAngles() --Store new angle value for the user in the loop
--ULib.tsay(v, "Test: " ..tostring(pangles[v:UniqueID()].angle)) -- Just my test callback to catch when the timer updates and what the angle is being stored as. Leave this commented out.
end
end
timer.Create( "G4P_AFKTimer", uafktime, 0, AFKCheck ) --Checks status every period you designate.

function PlayerSpawn( ply ) -- Sets default data for a player when he joins.
pangles[ply:UniqueID()] = { angle=Angle( 0, 0, 0 ), afk="0"}
end
hook.Add( "PlayerInitialSpawn", "afkspawnhook", PlayerSpawn );
end
[code]


whew.. finished working edition..


The console commands are...

uafk_enabled (0/1) - turns this module off or on
uafk_adminimmune (0/1) - Admins immune to kick?
uafk_kickenabled (0/1) - Should this script kick users.. if it is off.. it'll just constantly remind them they are afk (to be used in a future version when other visual options are enabled to show everyone if a player is afk)
uafk_time (<integer>) - How often (IN SECONDS) should the script check to see if a player has changed angles[/code]
« Last Edit: December 20, 2007, 07:48:05 PM by zakap »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #8 on: December 21, 2007, 12:27:18 PM »
Okay.. I'm finished with it for now.. time to work on other projects.. but here is a finished working version in addon form.. oh yeah... it's a ULib module now too.. but also in addon form for easy installation and so it wont be deleted if you remove/update your ULib folder.

~Features~
Ability to change time with console command uafk_time <integer>
Ability to disable the script with uafk_enabled <1/0>
Ability to allow admins immunity to kick with uafk_adminimmune <1/0>
Ability to allow the kicking function to not happen with uafk_kickenabled <1/0>

New.. it plays a sound when you are flagged as afk on your client to alert you so if you are there you can move and avoid being kicked.
Console commands can now be used from a dedicated server console.
~~~~~~~


The way this script works is:
It takes the time you have set in the config (top lines of the .lua file) or if you used uafk_time to set your own in game... and every amount of seconds as defined it checks the players entity angle.. (this is basically where the mouse is pointed) and stores it into a variable for your specific client.
If it finds that you have not changed angles since the last time it checked, and you are not flagged as afk, it will flag you as afk.
If it finds that you have not moved since it last checked and you are already flagged as afk, it will kick you from the server (assuming you have kick enabled)

The default time is 150 seconds.. this allows for around 5-7 minutes of complete afk before being kicked.. depending on how soon after a check you stop moving.


Suggestions for a future version are welcome.. I will try to implement anything that seems worth implementing.



Megiddo.. is it okay if I use the name UAfk? like I said in previous post.. you are credited as well as Team Ulysses in the first line of the lua code.. and will continue to be so even if you say no. I just need to know before I release this.

Edit: caught a small error in the code that was causing the callback when you did uafk_time to echo the old time and not the new one.
« Last Edit: December 21, 2007, 12:39:32 PM by zakap »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #9 on: December 21, 2007, 12:48:07 PM »
I'd really prefer you not use the name Uafk. Thanks!
Experiencing God's grace one day at a time.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #10 on: December 21, 2007, 01:12:22 PM »
not a problem.. that's why I asked.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #11 on: December 21, 2007, 06:30:22 PM »
Excellent work! I wanted to say that in the release forum, but figured I'd save it space for the questions/comments of other people.
I think I know another admin who would be interested
(WildWill, you watchin this? Go see the A-Afk in the releases forum here!)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Bringing back UAfk.. but not with that name of course! =)
« Reply #12 on: December 29, 2007, 03:20:05 AM »
    So...

Text above the heads of AFK players..
!afk to flag yourself.. only useful if the settings are to NOT kick afk players.. hehe
Tracers to draw text only if no world is blocking the LoS of the target player
Chatting will prevent being flagged.. as well as bring you out of afk if you are already in it.
Will not inform the afk player that they will be kicked if aafk_kickenabled is set to 0

All of these are implemented.. and are being testing on my server now. If no bugs are found/reported I'll probably have an update tomorrow or maybe the next day.

Things Id like to add...
  • Option to only kick a afk player if the server is full.
  • Movement other than viewangle being checked as well as angle to prevent flagging and bring a player out of afk
I've been a busy bee.. now I'm going to bed. New computer tomorrow I hope.. good night!
« Last Edit: December 29, 2007, 03:22:37 AM by zakap »