Disconnect player if inactive (and free a slot)

Post Reply
cacatoès
New member
Posts: 7
Joined: Wed Oct 08, 2014 19:32

Disconnect player if inactive (and free a slot)

by cacatoès » Post

Hiya,

I haven't found any thread about this issue,

Each server has a defined number of slots for players. It happens they are all used up, sometimes by players who leave their computers running but who are not in front.

It would make sense to be able to set an inactivity period (like 1 hour), after which if no activity from the player it would gently disconnect (and free the slot). This period could be set by server admins and eventually disabled.

I'm unsure what would be the best way to track (in)activity since I don't know well the game mechanisms, but I guess you have ideas on this ;)

Regards,

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: Disconnect player if inactive (and free a slot)

by kaeza » Post

Hello and welcome.

Something like this should work:

Code: Select all

-- Interval between movement checks (in seconds).
local INTERVAL = 5

-- Minimum distance to move to register as not AFK (in blocks).
local MINDIST = 0.2

-- If player does not move within this time, kick player (in seconds).
local TIMEOUT = 300 -- 5 minutes

local time_afk = { }
local last_pos = { }

local function check_moved()
	for _, p in ipairs(minetest.get_connected_players()) do
		local plname = p:get_player_name()
		local pos = p:getpos()
		local kicked
		if last_pos[plname] then
			local d = vector.distance(last_pos[plname], pos)
			print("Player: "..plname..", Dist: "..d)
			if d < MINDIST then
				time_afk[plname] = (time_afk[plname] or 0) + INTERVAL
				if time_afk[plname] >= TIMEOUT then
					minetest.kick_player(plname,
							"Inactive for "..TIMEOUT.." seconds.")
					kicked = true
				end
			else
				time_afk[plname] = 0
			end
		end
		if not kicked then
			last_pos[plname] = pos
		end
	end
	minetest.after(INTERVAL, check_moved)
end
minetest.after(INTERVAL, check_moved)

minetest.register_on_leaveplayer(function(player)
	local plname = player:get_player_name()
	time_afk[plname] = nil
	last_pos[plname] = nil
end)
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

User avatar
Gael de Sailly
Member
Posts: 845
Joined: Sun Jan 26, 2014 17:01
GitHub: gaelysam
IRC: Gael-de-Sailly
In-game: Gael-de-Sailly gaelysam
Location: Voiron, France

Re: Disconnect player if inactive (and free a slot)

by Gael de Sailly » Post

I support.
+1
Just realize how bored we would be if the world was perfect.

CWz
Member
Posts: 197
Joined: Tue Dec 24, 2013 17:01
GitHub: chaoswormz
IRC: CWz
In-game: CWz
Location: Banana Land

Re: Disconnect player if inactive (and free a slot)

by CWz » Post

i don't think the timeout time resets if the player starts moving

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: Disconnect player if inactive (and free a slot)

by kaeza » Post

CWz wrote:i don't think the timeout time resets if the player starts moving
Thanks for spotting that. I corrected the snippet.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

cacatoès
New member
Posts: 7
Joined: Wed Oct 08, 2014 19:32

Re: Disconnect player if inactive (and free a slot)

by cacatoès » Post

Thanks for the code.

Are there chances for this to be merged into minetest ?

Sol
Member
Posts: 73
Joined: Thu Jul 31, 2014 05:21
In-game: sol

Re: Disconnect player if inactive (and free a slot)

by Sol » Post

keza, is there any particular advantage of using .after instead of globalstep?
There is no such thing as duty. If you know that a thing is right, you want to do it. If you don't want to do it—it isn't right. If it's right and you don't want to do it—you don't know what right is and you're not a man. -- Ayn Rand

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: Disconnect player if inactive (and free a slot)

by kaeza » Post

Sol wrote:keza, is there any particular advantage of using .after instead of globalstep?
By using `after`, I avoid having to implement a timer to do it at some intervals. The `on_globalstep` callbacks run on every server step, which would be potentially slower and not really needed in this case.

EDIT: clarification.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: Disconnect player if inactive (and free a slot)

by kaeza » Post

cacatoès wrote:Thanks for the code.

Are there chances for this to be merged into minetest ?
I wouldn't bet on that, but feel free to submit a pull/feature request on Github.

EDIT: In case anyone has any problems, I release the code I posted as CC-0 (effectively "public domain").
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal

Sol
Member
Posts: 73
Joined: Thu Jul 31, 2014 05:21
In-game: sol

Re: Disconnect player if inactive (and free a slot)

by Sol » Post

kaeza wrote:By using `after`, I avoid having to implement a timer to do it at some intervals. The `on_globalstep` callbacks run on every server step, which would be potentially slower and not really needed in this case.
Thanks for explanation!
There is no such thing as duty. If you know that a thing is right, you want to do it. If you don't want to do it—it isn't right. If it's right and you don't want to do it—you don't know what right is and you're not a man. -- Ayn Rand

CWz
Member
Posts: 197
Joined: Tue Dec 24, 2013 17:01
GitHub: chaoswormz
IRC: CWz
In-game: CWz
Location: Banana Land

Re: Disconnect player if inactive (and free a slot)

by CWz » Post

Now time for me to figure out to how add a "may afk" priv to that script

cacatoès
New member
Posts: 7
Joined: Wed Oct 08, 2014 19:32

Re: Disconnect player if inactive (and free a slot)

by cacatoès » Post

Moreover I believe the code snippet above assumes "activity" is "to walk", while some other actions may as well be considered as "activity".
I would consider someone to be active if:
* Standing up without moving and have a chat with others, or playing with his/her inventory/crafting.

I am unsure a "may afk" priv would be useful. If this is a mean to allow admins of a server to stay as long as they want, I believe some private slots would be more appropriate to answer this need.
The logic with this auto-kick is: if you're automatically kicked, you still can reconnect as soon as you're back on your computer.

So if I sum up,
* Better define what "activity" is (i.e: not only walking)
* I also guess the inactivity period should be a variable in server config (minetest.conf), unless set to a sane default (30 minutes ?)

Unfortunately I can't code, so I may formalize a feature request if this topic makes sense.

Post Reply

Who is online

Users browsing this forum: Blockhead and 12 guests