mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-19 15:23:12 +09:00
Address review: page layout, five more features, framework changes
Framework: - Intervals are gone. Update periods are hardcoded on_nth_tick values again, since nobody has needed to change them. - apply is a single loop over the declared fields and logs a warning when an override is invalid. receive_update also warns about unknown keys. - Nested tables are addressed as "section.key". Lists of strings can be overridden. Features.optional declares a value with no default. - Features.on_apply rebuilds values derived from the config, such as lookup sets. It also runs during on_load. exp_roles is assumed to be present: - AFK Kick picks a role (active_role_id) instead of a custom check function. - Chat Auto Reply, Protection, and Nuke Protection no longer take a permission as a setting. New features, each chosen for an edge case: - Discord Alerts: conditional handlers, and a command map that becomes a list. - Entity Protection: lists turned into sets at load, and bookkeeping that must keep running while disabled. - Nuke Protection: config keyed by defines, flattened to a banned items list. - Popups: one config shared by two control files. - Spawn Area: nested sections, and settings that only apply when the map is created. Page: now laid out like the role page. It has a search across every setting, one long list with a sticky sidebar to jump between sections, and the enabled switch separated at the front of each header with Reset to defaults on the right. Changed rows are highlighted, and a bottom bar offers Revert and Apply. Role settings use a role picker and lists use a tag select. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
69e9f172a3
commit
4bc1773c82
23 files changed
+989
-670
No files matched your search
@@ -16,10 +16,35 @@ function luaFiles(dir, prefix = "") {
|
||||
|
||||
const moduleFiles = luaFiles(path.join(import.meta.dirname, "..", "module"));
|
||||
|
||||
/** Read a literal default from a Features.config call, only covers single line booleans, numbers, and strings. */
|
||||
function luaDefault(source, key) {
|
||||
const match = new RegExp(`^\\s*${key} = (true|false|-?[\\d.]+|"[^"]*"),`, "m").exec(source);
|
||||
return match ? JSON.parse(match[1]) : undefined;
|
||||
/**
|
||||
* Read a default from a Features.config call, a path of "a.b" looks for b after the table a starts.
|
||||
* Only covers booleans, numbers, strings, lists of strings, and Features.optional.
|
||||
*/
|
||||
function luaDefault(source, path) {
|
||||
const keys = path.split(".");
|
||||
let start = 0;
|
||||
for (const section of keys.slice(0, -1)) {
|
||||
const match = new RegExp(`^\\s*${section} = \\{`, "m").exec(source.slice(start));
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
start += match.index + match[0].length;
|
||||
}
|
||||
|
||||
const key = keys.at(-1);
|
||||
const match = new RegExp(`^\\s*${key} = (true|false|-?[\\d.]+|"[^"]*"|Features\\.optional\\("\\w+"\\)|\\{[^{}]*\\})`, "m")
|
||||
.exec(source.slice(start));
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
const literal = match[1];
|
||||
if (literal.startsWith("Features.optional")) {
|
||||
return null;
|
||||
}
|
||||
if (literal.startsWith("{")) {
|
||||
return [...literal.matchAll(/"([^"]*)"/g)].map(item => item[1]);
|
||||
}
|
||||
return JSON.parse(literal);
|
||||
}
|
||||
|
||||
t.test("features", t2 => {
|
||||
@@ -31,7 +56,7 @@ t.test("features", t2 => {
|
||||
t3.equal(declarations.length, 1, "one lua file declares the feature");
|
||||
const source = declarations[0]?.[1] ?? "";
|
||||
for (const field of feature.fields) {
|
||||
t3.equal(luaDefault(source, field.name), field.default, `${field.name} default matches the lua side`);
|
||||
t3.strictSame(luaDefault(source, field.name), field.default, `${field.name} default matches the lua side`);
|
||||
}
|
||||
t3.end();
|
||||
});
|
||||
@@ -62,5 +87,21 @@ t.test("validateFeatureValues()", t2 => {
|
||||
() => validateFeatureValues("afk_kick", { afk_minutes: Infinity }),
|
||||
{ message: "Setting afk_minutes of afk_kick must be a finite number" },
|
||||
);
|
||||
t2.throws(
|
||||
() => validateFeatureValues("protection", { always_protected_types: ["boiler", 1] }),
|
||||
{ message: "Setting always_protected_types of protection must be a list of strings" },
|
||||
);
|
||||
t2.throws(
|
||||
() => validateFeatureValues("afk_kick", { active_role_id: 1.5 }),
|
||||
{ message: "Setting active_role_id of afk_kick must be a role id or null" },
|
||||
);
|
||||
t2.same(
|
||||
validateFeatureValues("protection", { always_protected_types: ["boiler"], always_trigger_repeat_types: ["reactor", "fusion-reactor", "rocket-silo"] }),
|
||||
{ always_protected_types: ["boiler"] },
|
||||
"lists equal to their default are dropped",
|
||||
);
|
||||
t2.same(validateFeatureValues("afk_kick", { active_role_id: null }), {}, "no role is the default");
|
||||
t2.same(validateFeatureValues("afk_kick", { active_role_id: 3 }), { active_role_id: 3 });
|
||||
t2.same(validateFeatureValues("spawn_area", { "turrets.enabled": false }), { "turrets.enabled": false }, "nested settings");
|
||||
t2.end();
|
||||
});
|
||||
@@ -17,10 +17,13 @@ local Framework = assert(loadfile(shared_root .. "/framework.lua"))() --- @type
|
||||
--- @field storage table The table registered with storage, as on_init would store it
|
||||
--- @field on_load fun(tbl: table) Act out on_load with the given storage table
|
||||
--- @field load_config fun(name: string): table Load module/config/<name>.lua
|
||||
--- @field logged string[] Messages passed to log
|
||||
|
||||
return Framework.suite(function(env)
|
||||
--- @cast env ExpScenario.TestEnv
|
||||
local callback --- @type fun(tbl: table)
|
||||
env.logged = {}
|
||||
log = function(message) env.logged[#env.logged + 1] = message end
|
||||
env.extend_requires{
|
||||
["modules/exp_util/storage"] = {
|
||||
register = function(tbl, fn)
|
||||
|
||||
@@ -81,35 +81,57 @@ Suite.test("guard() declares features by name", function(env)
|
||||
Suite.eq(env.Features.is_enabled("test"), false, "and can be looked up")
|
||||
end)
|
||||
|
||||
Suite.test("guard() runs intervals once per period of the config value in seconds", function(env)
|
||||
local config = env.Features.config("test", { every = 5 })
|
||||
local ticks, seconds = {}, {}
|
||||
local lib = env.Features.guard(config, {
|
||||
on_nth_tick = { [60] = function(event) seconds[#seconds + 1] = event.tick end },
|
||||
intervals = { every = function(event) ticks[#ticks + 1] = event.tick end },
|
||||
})
|
||||
Suite.eq(lib.intervals, nil, "intervals are replaced by on_nth_tick")
|
||||
Suite.test("receive_update() logs invalid and unknown overrides", function(env)
|
||||
local config = env.Features.config("test", { flag = true })
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { flag = 1, extra = 2 } } }
|
||||
Suite.eq(config.flag, true, "the default is used")
|
||||
Suite.eq(#env.logged, 2, "both are logged")
|
||||
end)
|
||||
|
||||
local function run_until(last)
|
||||
for tick = 60, last, 60 do lib.on_nth_tick[60]{ tick = tick } end
|
||||
end
|
||||
Suite.test("config() addresses nested values by path", function(env)
|
||||
local offset = { 1, 2 }
|
||||
local config = env.Features.config("test", { section = { flag = true, count = 1 }, offset = offset })
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { ["section.flag"] = false, offset = { "x" } } } }
|
||||
Suite.eq(config.section, { flag = false, count = 1 }, "the nested value changes in place")
|
||||
Suite.eq(config.offset, offset, "lists of other types can not be overridden")
|
||||
|
||||
run_until(1200)
|
||||
Suite.eq(ticks, { 300, 600, 900, 1200 }, "every 5 seconds")
|
||||
Suite.eq(#seconds, 20, "an existing handler every second still runs")
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = {} } }
|
||||
Suite.eq(config.section.flag, true, "and goes back to its default")
|
||||
end)
|
||||
|
||||
ticks = {}
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { every = 7 } } }
|
||||
run_until(1260)
|
||||
Suite.eq(ticks, { 420, 840, 1260 }, "the period can change at runtime")
|
||||
Suite.test("config() allows lists of strings to be overridden", function(env)
|
||||
local config = env.Features.config("test", { names = { "a", "b" }, empty = {} })
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { names = { "c" }, empty = { "d" } } } }
|
||||
Suite.eq(config.names, { "c" }, "a list is replaced")
|
||||
Suite.eq(config.empty, { "d" }, "an empty default is a list")
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { names = { 1 } } } }
|
||||
Suite.eq(config.names, { "a", "b" }, "a list with other types is invalid")
|
||||
end)
|
||||
|
||||
Suite.test("optional() declares a value without a default", function(env)
|
||||
local config = env.Features.config("test", { role_id = env.Features.optional("number") })
|
||||
Suite.eq(config.role_id, nil, "nil until overridden")
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { role_id = 3 } } }
|
||||
Suite.eq(config.role_id, 3, "overridden with the given type")
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = {} } }
|
||||
Suite.eq(config.role_id, nil, "and back to nil")
|
||||
end)
|
||||
|
||||
Suite.test("on_apply() is called now and after every change", function(env)
|
||||
local config = env.Features.config("test", { names = { "a" } })
|
||||
local seen = {}
|
||||
env.Features.on_apply("test", function(applied) seen[#seen + 1] = applied.names[1] end)
|
||||
env.Features.receive_update{ { name = "test", enabled = true, values = { names = { "b" } } } }
|
||||
env.on_load{ test = { enabled = true, names = { "c" } } }
|
||||
Suite.eq(seen, { "a", "b", "c" }, "on register, update, and load")
|
||||
Suite.eq(config.names, { "c" }, "after the config is applied")
|
||||
end)
|
||||
|
||||
Suite.test("config files declare their feature", function(env)
|
||||
env.extend_requires{ ["modules/exp_roles"] = {} }
|
||||
local config = env.load_config("afk_kick")
|
||||
env.Features.receive_update{ { name = "afk_kick", enabled = true, values = { afk_minutes = 3 } } }
|
||||
Suite.eq(config.afk_minutes, 3, "afk_kick can be changed")
|
||||
Suite.eq(config.kick_minutes, 30, "other values keep their default")
|
||||
local config = env.load_config("popups")
|
||||
env.Features.receive_update{ { name = "popups", enabled = true, values = { show_player_damage = false } } }
|
||||
Suite.eq(config.show_player_damage, false, "popups can be changed")
|
||||
Suite.eq(config.show_player_health, true, "other values keep their default")
|
||||
end)
|
||||
|
||||
return Suite.run()
|
||||
Reference in new issue
Block a user