mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-21 23:33:12 +09:00
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>
This commit is contained in:
1 parent
46bb15e4a6
commit
88358b23b9
30 files changed
+1226
-89
No files matched your search
@@ -0,0 +1,18 @@
|
||||
--- Settings for kicking players when everyone online is afk
|
||||
-- @config AFK-Kick
|
||||
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
local Roles = require("modules/exp_roles")
|
||||
|
||||
return Features.register("afk_kick", {
|
||||
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
|
||||
afk_minutes = 10, --- @setting afk_minutes The time in minutes that must pass for a player to be considered afk
|
||||
kick_minutes = 30, --- @setting kick_minutes The time in minutes that must pass without any active players for all players to be kicked
|
||||
trust_minutes = 600, --- @setting trust_minutes The time in minutes 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, needs a restart to change
|
||||
custom_active_check = function(player)
|
||||
local veteran = Roles.get_role_by_name("Veteran")
|
||||
return veteran ~= nil and not Roles.get_player_highest_role(player):is_lower_than(veteran)
|
||||
end,
|
||||
})
|
||||
@@ -0,0 +1,138 @@
|
||||
--- This file defines the different triggers for the chat bot
|
||||
-- @config Chat-Auto-Reply
|
||||
|
||||
local ExpUtil = require("modules/exp_util")
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
local Async = require("modules/exp_util/async")
|
||||
local config_server_detail = require("modules.exp_legacy.config.server_detail") --- @dep config.server_detail
|
||||
|
||||
local floor = math.floor
|
||||
local random = math.random
|
||||
local format_string = string.format
|
||||
local locale_reply = "exp_chat-auto-reply.chat-reply"
|
||||
|
||||
local send_message_async =
|
||||
Async.register(function(player, message)
|
||||
if player == nil then
|
||||
game.print(message)
|
||||
else
|
||||
player.print(message)
|
||||
end
|
||||
end)
|
||||
|
||||
local afk_time_units = {
|
||||
minutes = true,
|
||||
seconds = true,
|
||||
}
|
||||
|
||||
return Features.register("chat_auto_reply", {
|
||||
--- @type table<string, LocalisedString | fun(player: LuaPlayer, is_command: boolean): LocalisedString>
|
||||
messages = { --- @setting messages will trigger when ever the word is said
|
||||
["discord"] = { "info.discord", config_server_detail["discord"] },
|
||||
["expgaming"] = { "info.website", config_server_detail["website"] },
|
||||
["website"] = { "info.website", config_server_detail["website"] },
|
||||
["status"] = { "info.status", config_server_detail["status"] },
|
||||
["github"] = { "info.github", config_server_detail["github"] },
|
||||
["patreon"] = { "info.patreon", config_server_detail["patreon"] },
|
||||
["donate"] = { "info.patreon", config_server_detail["patreon"] },
|
||||
["command"] = { "info.custom-commands" },
|
||||
["commands"] = { "info.custom-commands" },
|
||||
["softmod"] = { "info.softmod" },
|
||||
["plugin"] = { "info.softmod" },
|
||||
["script"] = { "info.softmod" },
|
||||
["redmew"] = { "info.redmew" },
|
||||
["comfy"] = { "info.redmew" },
|
||||
["rhd"] = { "info.lhd" },
|
||||
["lhd"] = { "info.lhd" },
|
||||
["loop"] = { "exp_chat-auto-reply.reply-loops" },
|
||||
["roundabout"] = { "exp_chat-auto-reply.reply-loops" },
|
||||
["roundabouts"] = { "exp_chat-auto-reply.reply-loops" },
|
||||
["clusterio"] = { "exp_chat-auto-reply.reply-clusterio" },
|
||||
["players"] = function()
|
||||
return { "exp_chat-auto-reply.reply-players", #game.players }
|
||||
end,
|
||||
["online"] = function()
|
||||
return { "exp_chat-auto-reply.reply-online", #game.connected_players }
|
||||
end,
|
||||
["afk"] = function(player)
|
||||
local max = player
|
||||
for _, next_player in pairs(game.connected_players) do
|
||||
if max.afk_time < next_player.afk_time then
|
||||
max = next_player
|
||||
end
|
||||
end
|
||||
|
||||
return { "exp_chat-auto-reply.reply-afk", max.name, ExpUtil.format_time_locale(max.afk_time, "long", afk_time_units) }
|
||||
end,
|
||||
},
|
||||
allow_command_prefix_for_messages = true, --- @setting allow_command_prefix_for_messages when true any message trigger will print to all player when prefixed
|
||||
command_admin_only = false, --- @setting command_admin_only when true will only allow chat commands for admins
|
||||
command_permission = "exp_scenario.chat.commands", --- @setting command_permission the permission used to allow command prefixes
|
||||
command_prefix = "!", --- @setting command_prefix prefix used for commands below and to print to all players (if enabled above)
|
||||
--- @type table<string, LocalisedString | fun(player: LuaPlayer, is_command: boolean): LocalisedString>
|
||||
commands = { --- @setting commands will trigger only when command prefix is given
|
||||
["dev"] = { "exp_chat-auto-reply.reply-dev" },
|
||||
["magic"] = { "exp_chat-auto-reply.reply-magic" },
|
||||
["aids"] = { "exp_chat-auto-reply.reply-aids" },
|
||||
["riot"] = { "exp_chat-auto-reply.reply-riot" },
|
||||
["lenny"] = { "exp_chat-auto-reply.reply-lenny" },
|
||||
["blame"] = function(player)
|
||||
local names = { "Cooldude2606", "arty714", "badgamernl", "mark9064", "aldldl", "Drahc_pro", player.name }
|
||||
for _, next_player in pairs(game.connected_players) do
|
||||
names[#names + 1] = next_player.name
|
||||
end
|
||||
|
||||
return { "exp_chat-auto-reply.reply-blame", table.get_random(names) }
|
||||
end,
|
||||
["hodor"] = function()
|
||||
local options = { "?", ".", "!", "!!!" }
|
||||
return { "exp_chat-auto-reply.reply-hodor", table.get_random(options) }
|
||||
end,
|
||||
["evolution"] = function(player)
|
||||
return { "exp_chat-auto-reply.reply-evolution", format_string("%.2f", game.forces["enemy"].get_evolution_factor(player.surface)) }
|
||||
end,
|
||||
["makepopcorn"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-popcorn-2", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-popcorn-1" } }
|
||||
end,
|
||||
["passsomesnaps"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-snaps-2", player.name } })
|
||||
send_message_async:start_after(timeout * (random() + 0.5), nil, { locale_reply, { "exp_chat-auto-reply.reply-snaps-3", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-snaps-1" } }
|
||||
end,
|
||||
["makecocktail"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-cocktail-2", player.name } })
|
||||
send_message_async:start_after(timeout * (random() + 0.5), nil, { locale_reply, { "exp_chat-auto-reply.reply-cocktail-3", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-cocktail-1" } }
|
||||
end,
|
||||
["makecoffee"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-coffee-2", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-coffee-1" } }
|
||||
end,
|
||||
["orderpizza"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-pizza-2", player.name } })
|
||||
send_message_async:start_after(timeout * (random() + 0.5), nil, { locale_reply, { "exp_chat-auto-reply.reply-pizza-3", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-pizza-1" } }
|
||||
end,
|
||||
["maketea"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-tea-2", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-tea-1" } }
|
||||
end,
|
||||
["meadplease"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-mead-2", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-mead-1" } }
|
||||
end,
|
||||
["passabeer"] = function(player)
|
||||
local timeout = floor(180 * (random() + 0.5))
|
||||
send_message_async:start_after(timeout, nil, { locale_reply, { "exp_chat-auto-reply.reply-beer-2", player.name } })
|
||||
return { locale_reply, { "exp_chat-auto-reply.reply-beer-1" } }
|
||||
end,
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,15 @@
|
||||
--- This config controls what happens when a player dies mostly about map markers and item collection
|
||||
-- @config Death-Markers
|
||||
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
|
||||
return Features.register("death_markers", {
|
||||
collect_corpses = true, --- @setting collect_corpses enables items being returned to the spawn point in chests upon corpse expiring
|
||||
show_map_markers = true, --- @setting show_map_markers shows markers on the map where bodies are
|
||||
clean_map_markers = false, --- @setting clean_map_markers removes the map marker once the body is gone
|
||||
include_time_of_death = true, --- @setting include_time_of_death weather to include the time of death on the map marker
|
||||
map_icon = nil, --- @setting map_icon the icon that the map marker shows; nil means no icon; format as a SingleID
|
||||
show_light_at_corpse = true, --- @setting show_light_at_corpse if a light should be rendered at the corpse
|
||||
show_line_to_corpse = true, --- @setting show_line_to_corpse if a line should be rendered from you to your corpse
|
||||
period_check_map_tags = 60 * 60 * 5, --- @setting period_check_map_tags ticks between checks for missing map markers, needs a restart to change
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
--- Settings for mine depletion
|
||||
-- @config Mine-Depletion
|
||||
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
|
||||
return Features.register("mine_depletion", {
|
||||
fluid = true, --- @setting fluid When true, checks for for fluid pipes when removing miners
|
||||
chest = true, --- @setting chest When true, checks for for chest when removing miners
|
||||
beacon = true, --- @setting beacon When true, checks for for beacon when removing miners
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
--- Settings for naming train stations when they are built
|
||||
-- @config Station-Auto-Name
|
||||
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
|
||||
return Features.register("station_auto_name", {
|
||||
--[[
|
||||
__icon__
|
||||
__item_name__
|
||||
__backer_name__
|
||||
__direction__
|
||||
__x__
|
||||
__y__
|
||||
]]
|
||||
station_name = "[L] __icon__", --- @setting station_name the name given to new stations, uses the placeholders above
|
||||
})
|
||||
@@ -3,8 +3,11 @@ 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_legacy.config.afk_kick")
|
||||
local config = require("modules/exp_scenario/config/afk_kick")
|
||||
|
||||
local ticks_per_minute = 3600
|
||||
|
||||
--- @type { last_active: number }
|
||||
local script_data = { last_active = 0 }
|
||||
@@ -15,16 +18,16 @@ 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_time then return end
|
||||
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_time
|
||||
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_time
|
||||
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
|
||||
@@ -40,7 +43,7 @@ local function check_afk_players()
|
||||
if has_active_player() then return end
|
||||
|
||||
-- Check if players should be kicked
|
||||
if game.tick - script_data.last_active < config.kick_time then return end
|
||||
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
|
||||
@@ -73,7 +76,7 @@ end
|
||||
|
||||
local e = defines.events
|
||||
|
||||
return {
|
||||
return Features.guard(config, {
|
||||
events = {
|
||||
[e.on_player_joined_game] = on_player_joined_game,
|
||||
},
|
||||
@@ -81,4 +84,4 @@ return {
|
||||
[config.update_time] = check_afk_players,
|
||||
},
|
||||
has_active_player = has_active_player,
|
||||
}
|
||||
})
|
||||
@@ -2,10 +2,9 @@
|
||||
Adds auto replies to chat messages, as well as chat commands
|
||||
]]
|
||||
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
local Roles = require("modules/exp_roles")
|
||||
local config = require("modules.exp_legacy.config.chat_reply")
|
||||
local prefix = config.command_prefix
|
||||
local prefix_len = string.len(prefix)
|
||||
local config = require("modules/exp_scenario/config/chat_auto_reply")
|
||||
|
||||
local find = string.find
|
||||
local sub = string.sub
|
||||
@@ -16,6 +15,8 @@ local function on_console_chat(event)
|
||||
if not event.player_index then return end
|
||||
local player = assert(game.get_player(event.player_index))
|
||||
local message = event.message:lower():gsub("%s+", "")
|
||||
local prefix = config.command_prefix
|
||||
local prefix_len = string.len(prefix)
|
||||
|
||||
-- Check if the player can chat commands
|
||||
local commands_allowed = true
|
||||
@@ -56,8 +57,8 @@ end
|
||||
|
||||
local e = defines.events
|
||||
|
||||
return {
|
||||
return Features.guard(config, {
|
||||
events = {
|
||||
[e.on_console_chat] = on_console_chat,
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -3,8 +3,9 @@ Makes markers on the map where places have died and reclaims items if not recove
|
||||
]]
|
||||
|
||||
local ExpUtil = require("modules/exp_util")
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
local Storage = require("modules/exp_util/storage")
|
||||
local config = require("modules.exp_legacy.config.death_logger")
|
||||
local config = require("modules/exp_scenario/config/death_markers")
|
||||
|
||||
local map_tag_time_format = ExpUtil.format_time_factory{ format = "short", hours = true, minutes = true }
|
||||
|
||||
@@ -40,6 +41,7 @@ end
|
||||
|
||||
--- Checks that all map tags are present and valid, creating any that are missing
|
||||
local function check_map_tags()
|
||||
if not config.show_map_markers then return end
|
||||
for _, corpse_data in pairs(character_corpses) do
|
||||
if not corpse_data.tag or not corpse_data.tag.valid then
|
||||
create_map_tag(corpse_data)
|
||||
@@ -98,6 +100,7 @@ end
|
||||
--- Draw lines to the player corpse
|
||||
--- @param event EventData.on_player_respawned
|
||||
local function on_player_respawned(event)
|
||||
if not config.show_line_to_corpse then return end
|
||||
local index = event.player_index
|
||||
local player = assert(game.get_player(index))
|
||||
for _, corpse_data in pairs(character_corpses) do
|
||||
@@ -122,6 +125,7 @@ end
|
||||
--- Collect all items from expired character corpses
|
||||
--- @param event EventData.on_character_corpse_expired
|
||||
local function on_character_corpse_expired(event)
|
||||
if not config.collect_corpses then return end
|
||||
local corpse = event.corpse
|
||||
local inventory = assert(corpse.get_inventory(defines.inventory.character_corpse))
|
||||
ExpUtil.transfer_inventory_to_surface{
|
||||
@@ -132,26 +136,16 @@ local function on_character_corpse_expired(event)
|
||||
}
|
||||
end
|
||||
|
||||
local on_nth_tick = {}
|
||||
if config.show_map_markers then
|
||||
on_nth_tick[config.period_check_map_tags] = check_map_tags
|
||||
end
|
||||
|
||||
local e = defines.events
|
||||
local events = {
|
||||
[e.on_player_died] = on_player_died,
|
||||
[e.on_object_destroyed] = on_object_destroyed,
|
||||
}
|
||||
|
||||
if config.show_line_to_corpse then
|
||||
events[e.on_player_respawned] = on_player_respawned
|
||||
end
|
||||
|
||||
if config.collect_corpses then
|
||||
events[e.on_character_corpse_expired] = on_character_corpse_expired
|
||||
end
|
||||
|
||||
return {
|
||||
on_nth_tick = on_nth_tick,
|
||||
events = events,
|
||||
}
|
||||
return Features.guard(config, {
|
||||
on_nth_tick = {
|
||||
[config.period_check_map_tags] = check_map_tags,
|
||||
},
|
||||
events = {
|
||||
[e.on_player_died] = on_player_died,
|
||||
[e.on_object_destroyed] = on_object_destroyed,
|
||||
[e.on_player_respawned] = on_player_respawned,
|
||||
[e.on_character_corpse_expired] = on_character_corpse_expired,
|
||||
},
|
||||
})
|
||||
@@ -3,7 +3,8 @@ Marks mining drills for deconstruction when resources deplete
|
||||
]]
|
||||
|
||||
local Async = require("modules/exp_util/async")
|
||||
local config = require("modules.exp_legacy.config.miner")
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
local config = require("modules/exp_scenario/config/mine_depletion")
|
||||
|
||||
local floor = math.floor
|
||||
|
||||
@@ -270,8 +271,8 @@ end
|
||||
|
||||
local e = defines.events
|
||||
|
||||
return {
|
||||
return Features.guard(config, {
|
||||
events = {
|
||||
[e.on_resource_depleted] = on_resource_depleted,
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -2,7 +2,8 @@
|
||||
Automatically name stations when they are placed based on closest resource and direction from spawn
|
||||
]]
|
||||
|
||||
local config = require("modules.exp_legacy.config.station_auto_name")
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
local config = require("modules/exp_scenario/config/station_auto_name")
|
||||
|
||||
local get_direction do
|
||||
local directions = {
|
||||
@@ -98,9 +99,9 @@ end
|
||||
|
||||
local e = defines.events
|
||||
|
||||
return {
|
||||
return Features.guard(config, {
|
||||
events = {
|
||||
[e.on_built_entity] = rename_station,
|
||||
[e.on_robot_built_entity] = rename_station,
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,135 @@
|
||||
--[[-- ExpScenario - Features
|
||||
Feature config which the controller can change while the game is running
|
||||
|
||||
Each feature declares its defaults in config/<name>.lua by returning the table
|
||||
from Features.register. The controller sends overrides over rcon, these are kept
|
||||
in storage and copied onto that same table, so read values when they are used
|
||||
rather than copying them into locals when the file loads.
|
||||
|
||||
Only boolean, number, and string defaults can be overridden. Values used while
|
||||
the file loads, such as on_nth_tick periods, can not change without a restart
|
||||
and should not be listed in features.ts.
|
||||
|
||||
--- Declaring a feature:
|
||||
local Features = require("modules/exp_scenario/features")
|
||||
return Features.register("my_feature", {
|
||||
show_message = true,
|
||||
})
|
||||
|
||||
--- Using it from a control file, handlers do nothing while it is disabled:
|
||||
local config = require("modules/exp_scenario/config/my_feature")
|
||||
return Features.guard(config, {
|
||||
events = { [defines.events.on_player_joined_game] = on_player_joined_game },
|
||||
})
|
||||
]]
|
||||
|
||||
local Storage = require("modules/exp_util/storage")
|
||||
|
||||
--- @class ExpScenario.Features
|
||||
local Features = {}
|
||||
|
||||
local overridable = {
|
||||
boolean = true,
|
||||
number = true,
|
||||
string = true,
|
||||
}
|
||||
|
||||
--- @class ExpScenario.FeatureConfig
|
||||
--- @field enabled boolean
|
||||
|
||||
--- The live config tables by feature name
|
||||
local features = {} --- @type table<string, ExpScenario.FeatureConfig>
|
||||
|
||||
--- Copies of the defaults by feature name, used to undo overrides
|
||||
local defaults = {} --- @type table<string, table<string, any>>
|
||||
|
||||
--- Overrides received from the controller by feature name
|
||||
local overrides = {} --- @type table<string, table<string, boolean | number | string>>
|
||||
|
||||
--- Reset a feature to its defaults and apply its overrides
|
||||
--- @param name string
|
||||
local function apply(name)
|
||||
local config = features[name]
|
||||
local feature_defaults = defaults[name]
|
||||
for key, value in pairs(feature_defaults) do
|
||||
config[key] = value
|
||||
end
|
||||
|
||||
for key, value in pairs(overrides[name] or {}) do
|
||||
local default = feature_defaults[key]
|
||||
if default ~= nil and type(value) == type(default) and overridable[type(value)] then
|
||||
config[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Storage.register(overrides, function(tbl)
|
||||
overrides = tbl
|
||||
for name in pairs(features) do
|
||||
apply(name)
|
||||
end
|
||||
end)
|
||||
|
||||
--- Register a feature and its defaults, features are enabled unless the defaults say otherwise
|
||||
--- @generic T : table
|
||||
--- @param name string
|
||||
--- @param config T
|
||||
--- @return T
|
||||
function Features.register(name, config)
|
||||
assert(features[name] == nil, "Feature already registered: " .. name)
|
||||
if config.enabled == nil then
|
||||
config.enabled = true
|
||||
end
|
||||
|
||||
local feature_defaults = {}
|
||||
for key, value in pairs(config) do
|
||||
feature_defaults[key] = value
|
||||
end
|
||||
|
||||
features[name] = config
|
||||
defaults[name] = feature_defaults
|
||||
return config
|
||||
end
|
||||
|
||||
--- Wrap the event and on_nth_tick handlers of an event handler lib so they do nothing while the feature is disabled
|
||||
--- @generic T : table
|
||||
--- @param config ExpScenario.FeatureConfig
|
||||
--- @param lib T
|
||||
--- @return T
|
||||
function Features.guard(config, lib)
|
||||
for _, handlers in pairs{ lib.events or {}, lib.on_nth_tick or {} } do
|
||||
for key, handler in pairs(handlers) do
|
||||
handlers[key] = function(event)
|
||||
if config.enabled then
|
||||
return handler(event)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return lib
|
||||
end
|
||||
|
||||
--- @class ExpScenario.FeatureUpdate
|
||||
--- @field name string
|
||||
--- @field enabled boolean
|
||||
--- @field values table<string, boolean | number | string>
|
||||
|
||||
--- Called over rcon by the instance plugin with the features which changed, unknown features are ignored
|
||||
--- @param updates ExpScenario.FeatureUpdate[]
|
||||
function Features.receive_update(updates)
|
||||
for _, update in ipairs(updates) do
|
||||
local name = update.name
|
||||
if features[name] then
|
||||
local override = { enabled = update.enabled }
|
||||
for key, value in pairs(update.values) do
|
||||
override[key] = value
|
||||
end
|
||||
|
||||
overrides[name] = override
|
||||
apply(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Features
|
||||
@@ -0,0 +1,6 @@
|
||||
--- @diagnostic disable: global-in-non-module
|
||||
|
||||
-- Access using `/sc exp_scenario.features.foo()`
|
||||
exp_scenario = {
|
||||
features = require("modules/exp_scenario/features"),
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
"load": [
|
||||
],
|
||||
"require": [
|
||||
"globals.lua",
|
||||
"control.lua"
|
||||
],
|
||||
"dependencies": {
|
||||
|
||||
Reference in new issue
Block a user