Skip to content

Commit

Permalink
Merge pull request #1 from SwissalpS/minimal
Browse files Browse the repository at this point in the history
Minimal solution that should solve the main issue we had
  • Loading branch information
SwissalpS committed Feb 10, 2020
2 parents 48f23b5 + af6cb60 commit 3490f50
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

globals = {
"afkkick"
}

read_globals = {
Expand All @@ -12,5 +13,5 @@ read_globals = {
"dump",

-- deps
"default", "minetest"
"default", "minetest", "xp_redo"
}
105 changes: 58 additions & 47 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,74 +1,85 @@
--[[
Afk Kick mod for Minetest by GunshipPenguin
re-written for Pandorabox.io by SwissalpS
To the extent possible under law, the author(s)
have dedicated all copyright and related and neighboring rights
to this software to the public domain worldwide. This software is
distributed without any warranty.
]]
--]]

local MAX_INACTIVE_TIME = tonumber(minetest.settings:get("afkkick.max_inactive_time") or "1800")
local CHECK_INTERVAL = 1
local WARN_TIME = 20
afkkick = {}

local players = {}
local checkTimer = 0
afkkick.maxInactiveTime = tonumber(minetest.settings:get("afkkick.max_inactive_time") or "1800")
afkkick.checkInterval = 120
afkkick.radius = 13
afkkick.kickMessage = "for loitering at spawn"

-- one time calculation of kickarea
local spawnPos = minetest.string_to_pos(minetest.settings:get("static_spawnpoint") or "(0, 0, 0)")
local radiusVector = { x = afkkick.radius, y = afkkick.radius, z = afkkick.radius }
afkkick.posA = vector.add(spawnPos, radiusVector)
afkkick.posB = vector.subtract(spawnPos, radiusVector)

-- table tracking players times
afkkick.players = {}

minetest.register_on_joinplayer(function(player)
local playerName = player:get_player_name()
players[playerName] = {
lastAction = minetest.get_gametime()
afkkick.players[playerName] = {
kickAt = 0
}
end)

minetest.register_on_leaveplayer(function(player)
local playerName = player:get_player_name()
players[playerName] = nil
afkkick.players[playerName] = nil
end)

minetest.register_on_chat_message(function(playerName)
players[playerName]["lastAction"] = minetest.get_gametime()
end)
-- track time of last call
local iTimeNext = 0

minetest.register_globalstep(function(dtime)
local currGameTime = minetest.get_gametime()
minetest.register_globalstep(function()

--Check for inactivity once every CHECK_INTERVAL seconds
checkTimer = checkTimer + dtime
local currentTime = minetest.get_gametime()

local checkNow = checkTimer >= CHECK_INTERVAL
if checkNow then
checkTimer = checkTimer - CHECK_INTERVAL
-- Check for inactivity once every CHECK_INTERVAL seconds
if iTimeNext > currentTime then
-- not yet
return
end

--Loop through each player in players
for playerName, info in pairs(players) do
local player = minetest.get_player_by_name(playerName)
if player then
--Check if this player is doing an action
for _, keyPressed in pairs(player:get_player_control()) do
if keyPressed then
info["lastAction"] = currGameTime
end
end
iTimeNext = currentTime + afkkick.checkInterval

if checkNow then
--Kick player if he/she has been inactive for longer than MAX_INACTIVE_TIME seconds
if info["lastAction"] + MAX_INACTIVE_TIME < currGameTime then
minetest.kick_player(playerName, "Kicked for inactivity")
end
local playerPos, playerName
local isInX, isInY, isInZ, isAtSpawn
local kickTime

--Warn player if he/she has less than WARN_TIME seconds to move or be kicked
if info["lastAction"] + MAX_INACTIVE_TIME - WARN_TIME < currGameTime then
minetest.chat_send_player(playerName,
minetest.colorize("#FF8C00", "Warning, you have " ..
tostring(info["lastAction"] + MAX_INACTIVE_TIME - currGameTime + 1) ..
" seconds to move or be kicked"))
-- Loop through each player that is online
for _, player in ipairs(minetest.get_connected_players()) do
playerName = player:get_player_name()
-- only bother if is a real player
if 0 < #playerName then
--Check if this player is near spawn
playerPos = player:get_pos()
isInX = (playerPos.x > afkkick.posB.x) and (playerPos.x < afkkick.posA.x)
isInY = (playerPos.y > afkkick.posB.y) and (playerPos.y < afkkick.posA.y)
isInZ = (playerPos.z > afkkick.posB.z) and (playerPos.z < afkkick.posA.z)
isAtSpawn = isInX and isInY and isInZ
if isAtSpawn then
kickTime = afkkick.players[playerName].kickAt
if 0 == kickTime then
-- first time we check and player is at spawn
afkkick.players[playerName].kickAt = currentTime + afkkick.maxInactiveTime
elseif kickTime < currentTime then
-- player has been here long enough, kick
minetest.kick_player(playerName, afkkick.kickMessage)
end
end
else
-- Clean up garbage
players[playerName] = nil
end
end
end)
else
-- player is out of kick location
afkkick.players[playerName].kickAt = 0
end -- if at spawn or not
end -- if real player
end -- for loop
end -- function
)
4 changes: 4 additions & 0 deletions mod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = afkkick
description = kicks players from spawn if they stay there too long.
depends = default

0 comments on commit 3490f50

Please sign in to comment.