Disconnect player if inactive (and free a slot)

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

Disconnect player if inactive (and free a slot)

by cacatoès » Mon Oct 13, 2014 16:50

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
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

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

by kaeza » Mon Oct 13, 2014 19:14

Hello and welcome.

Something like this should work:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

User avatar
Gael de Sailly
Member
 
Posts: 475
Joined: Sun Jan 26, 2014 17:01
GitHub: Gael-de-Sailly
IRC: Gael-de-Sailly
In-game: Gael-de-Sailly

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

by Gael de Sailly » Mon Oct 13, 2014 20:15

I support.
+1
Very busy this year too, so do not expect me to be very active on the forum or in game. But I'm not about to drop Minetest forever :)
 

CWz
Member
 
Posts: 185
Joined: Tue Dec 24, 2013 17:01

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

by CWz » Wed Oct 15, 2014 14:21

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

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

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

by kaeza » Thu Oct 16, 2014 08:28

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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

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 » Thu Oct 16, 2014 20:01

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 » Fri Oct 17, 2014 07:15

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
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

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

by kaeza » Fri Oct 17, 2014 22:44

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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

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

by kaeza » Fri Oct 17, 2014 22:46

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 | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

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 » Sat Oct 18, 2014 05:36

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: 185
Joined: Tue Dec 24, 2013 17:01

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

by CWz » Mon Oct 20, 2014 15:55

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 » Sun Oct 26, 2014 20:15

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.
 


Return to Minetest Features

Who is online

Users browsing this forum: No registered users and 6 guests

cron