General > Developers Corner

Bringing back UAfk.. but not with that name of course! =)

<< < (2/3) > >>

MrPresident:

--- Code: -----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

--- End code ---

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.

JamminR:
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

MrPresident:
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: -----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..

--- End code ---


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]

MrPresident:
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.

Megiddo:
I'd really prefer you not use the name Uafk. Thanks!

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version