Files
factorio-expcluster/exp_scenario/module/control/afk_kick.lua
T
bbassieandClaude Opus 5.5 88358b23b9 Add a web page for configuring scenario features
Features can now be enabled, disabled, and configured from the web UI
while the game is running, without editing Lua files. The first five
features covered are AFK Kick, Chat Auto Reply, Death Markers, Mine
Depletion, and Station Auto Name.

Lua side: module/features.lua holds each feature's defaults, keeps the
overrides from the controller in storage, and patches them onto the
config table in place. Features.guard skips a feature's handlers while
it is disabled. The five config files move from exp_legacy into
exp_scenario/module/config and their control files now read config at
runtime instead of deciding at load which handlers to register. AFK
Kick times are now in minutes.

Controller side: features.ts lists the editable settings and their
defaults. Overrides that differ from the default are stored in
exp_scenario/features.json and guarded by the existing
exp_scenario.config.view/edit permissions. The instance plugin sends
them over rcon on start, on every change, and again after the
controller restarts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 17:56:14 +00:00

88 lines
2.9 KiB
Lua

--[[-- Control -- AFK Kick
Kicks players when all players on the server are afk
]]
local Async = require("modules/exp_util/async")
local Features = require("modules/exp_scenario/features")
local Storage = require("modules/exp_util/storage")
local config = require("modules/exp_scenario/config/afk_kick")
local ticks_per_minute = 3600
--- @type { last_active: number }
local script_data = { last_active = 0 }
Storage.register(script_data, function(tbl)
script_data = tbl
end)
--- Kicks an afk player, used to add a delay so the gui has time to appear
local afk_kick_player_async =
Async.register(function(player)
if game.tick - script_data.last_active < config.kick_minutes * ticks_per_minute then return end
game.kick_player(player, "AFK while no active players on the server")
end)
--- Check if there is an active player
local function has_active_player()
for _, player in ipairs(game.connected_players) do
if player.afk_time < config.afk_minutes * ticks_per_minute
or config.admin_as_active and player.admin
or config.trust_as_active and player.online_time > config.trust_minutes * ticks_per_minute
or config.custom_active_check and config.custom_active_check(player) then
script_data.last_active = game.tick
return true
end
end
return false
end
--- Check for an active player every update_time number of ticks
local function check_afk_players()
-- Check for active players
if has_active_player() then return end
-- Check if players should be kicked
if game.tick - script_data.last_active < config.kick_minutes * ticks_per_minute 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 frame = player.gui.screen.add{
type = "frame",
name = "afk-kick",
caption = { "exp_afk-kick.kick-message" },
}
local uis = player.display_scale
local res = player.display_resolution
frame.location = {
x = res.width * (0.5 - 0.11 * uis),
y = res.height * (0.5 - 0.14 * uis),
}
-- Kick the player, some delay needed allow the gui to show
afk_kick_player_async:start_after(60, player)
end
end
--- Remove the screen gui if it is present
--- @param event EventData.on_player_joined_game
local function on_player_joined_game(event)
local player = assert(game.get_player(event.player_index))
local frame = player.gui.screen["afk-kick"]
if frame and frame.valid then frame.destroy() end
end
local e = defines.events
return Features.guard(config, {
events = {
[e.on_player_joined_game] = on_player_joined_game,
},
on_nth_tick = {
[config.update_time] = check_afk_players,
},
has_active_player = has_active_player,
})