Disconnect player if inactive (and free a slot)
Disconnect player if inactive (and free a slot)
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,
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,
- 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)
Hello and welcome.
Something like this should work:
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
Check out my stuff! | Donations greatly appreciated! PayPal
- 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)
I support.
+1
+1
Just realize how bored we would be if the world was perfect.
-
- 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)
i don't think the timeout time resets if the player starts moving
- 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)
Thanks for spotting that. I corrected the snippet.CWz wrote:i don't think the timeout time resets if the player starts moving
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
Check out my stuff! | Donations greatly appreciated! PayPal
Re: Disconnect player if inactive (and free a slot)
Thanks for the code.
Are there chances for this to be merged into minetest ?
Are there chances for this to be merged into minetest ?
Re: Disconnect player if inactive (and free a slot)
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
- 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 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.Sol wrote:keza, is there any particular advantage of using .after instead of globalstep?
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
Check out my stuff! | Donations greatly appreciated! PayPal
- 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)
I wouldn't bet on that, but feel free to submit a pull/feature request on Github.cacatoès wrote:Thanks for the code.
Are there chances for this to be merged into minetest ?
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
Check out my stuff! | Donations greatly appreciated! PayPal
Re: Disconnect player if inactive (and free a slot)
Thanks for explanation!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.
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
-
- 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)
Now time for me to figure out to how add a "may afk" priv to that script
Re: Disconnect player if inactive (and free a slot)
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.
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.
Who is online
Users browsing this forum: Blockhead and 12 guests