Merge pull request #205 from Cooldude2606/feature/afk-kick

Smart AFK Kick
This commit is contained in:
Cooldude2606
2021-04-24 21:25:47 +01:00
committed by GitHub
4 changed files with 80 additions and 1 deletions

View File

@@ -43,6 +43,7 @@ return {
'modules.addons.discord-alerts',
'modules.addons.chat-reply',
'modules.addons.tree-decon',
'modules.addons.afk-kick',
'modules.addons.report-jail',
--- Data

9
config/afk_kick.lua Normal file
View File

@@ -0,0 +1,9 @@
return {
admin_as_active = true, --- @setting admin_as_active When true admins will be treated as active regardless of afk time
trust_as_active = true, --- @setting trust_as_active When true trusted players (by playtime) will be treated as active regardless of afk time
active_role = 'Veteran', --- @setting active_role When not nil a player with this role will be treated as active regardless of afk time
afk_time = 3600*10, --- @setting afk_time The time in ticks that must pass for a player to be considered afk
kick_time = 3600*30, --- @setting kick_time The time in ticks that must pass without any active players for all players to be kicked
trust_time = 3600*60*10, --- @setting trust_time The time in ticks that a player must be online for to count as trusted
update_time = 3600*30, --- @setting update_time How often in ticks the script checks for active players
}

View File

@@ -77,5 +77,8 @@ get-beer-1= 🍺 Pouring A Glass 🍺
get-beer-2= 🍻 Chears Mate 🍻
verify=Please return to our discord and type r!verify __1__
[afk-kick]
message=All players were kicked because everyone was AFK.
[report-jail]
jail=__1__ was jailed because they were reported too many times.
jail=__1__ was jailed because they were reported too many times.

View File

@@ -0,0 +1,66 @@
--- Kicks players when all players on the server are afk
-- @addon afk-kick
local Event = require 'utils.event' --- @dep utils.event
local Global = require 'utils.global' --- @dep utils.global
local config = require 'config.afk_kick' --- @dep config.afk_kick
local Async = require 'expcore.async' --- @dep expcore.async
--- Optional roles require
local Roles
if config.active_role then
Roles = require 'expcore.roles'
end
--- Globals
local primitives = { last_active = 0 }
Global.register(primitives, function(tbl)
primitives = tbl
end)
--- Kicks an afk player, used to add a delay so the gui has time to appear
local kick_player =
Async.register(function(player)
if game.tick - primitives.last_active < config.kick_time then return end -- Safety Catch
game.kick_player(player, 'AFK while no active players on the server')
end)
--- Check for an active player every update_time number of ticks
Event.on_nth_tick(config.update_time, function()
-- Check for active players
for _, player in ipairs(game.connected_players) do
if player.afk_time < config.afk_time
or config.admin_as_active and config.player.admin
or config.trust_as_active and player.online_time > config.trust_time
or config.active_role and Roles.player_has_role(player, config.active_role) then
-- Active player was found
primitives.last_active = game.tick
return
end
end
-- No active player was found, check if players should be kicked
if game.tick - primitives.last_active < config.kick_time then return end
-- Kick time exceeded, kick all players
for _, player in ipairs(game.connected_players) do
-- Add a frame to say why the player was kicked
local res = player.display_resolution
local uis = player.display_scale
player.gui.screen.add{
type = 'frame',
name = 'afk-kick',
caption = {'afk-kick.message'},
}.location = { x=res.width*(0.5 - 0.11*uis), y=res.height*(0.5 - 0.14*uis) }
-- Kick the player, some delay needed because network delay
Async.wait(10, kick_player, player)
end
end)
--- Remove the screen gui if it is present
Event.add(defines.events.on_player_joined_game, function(event)
local player = game.get_player(event.player_index)
local frame = player.gui.screen["afk-kick"]
if frame and frame.valid then frame.destroy() end
end)