mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-13 08:04:53 +09:00
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>
121 lines
4.4 KiB
JavaScript
121 lines
4.4 KiB
JavaScript
import t from "tap";
|
|
import * as lib from "@clusterio/lib";
|
|
import { Instance } from "@clusterio/host";
|
|
import { InstancePlugin } from "../dist/node/instance.js";
|
|
import { plugin as pluginDeclaration } from "../dist/node/index.js";
|
|
import * as messages from "../dist/node/messages.js";
|
|
|
|
// The instance validates message classes against the link registry
|
|
for (const Message of pluginDeclaration.messages) {
|
|
lib.Link.register(Message);
|
|
}
|
|
|
|
const logger = { child: () => logger, info: () => {}, warn: () => {}, error: () => {}, verbose: () => {} };
|
|
|
|
class TestConnector extends lib.BaseConnector {
|
|
constructor() {
|
|
super(lib.Address.fromShorthand({ instanceId: 1 }), lib.Address.fromShorthand({ hostId: 1 }));
|
|
this.valid = true;
|
|
this.connected = true;
|
|
this.hasSession = true;
|
|
}
|
|
|
|
send() {}
|
|
}
|
|
|
|
/** Build a plugin around a real instance with spies on what leaves it. */
|
|
async function startPlugin(t2, features = [new messages.FeatureRecord("death_markers", false, { show_map_markers: false })]) {
|
|
const instanceConfig = new lib.InstanceConfig("host");
|
|
instanceConfig.set("instance.id", 1);
|
|
instanceConfig.set("instance.name", "test");
|
|
|
|
const instance = new Instance(
|
|
{ assignGamePort: () => 1 }, new TestConnector(), t2.testdir(), "factorioDir", instanceConfig
|
|
);
|
|
|
|
const state = { sent: [], rcons: [] };
|
|
instance.server = {
|
|
handle: () => {},
|
|
sendRcon: async command => {
|
|
state.rcons.push(command);
|
|
return "";
|
|
},
|
|
};
|
|
instance.sendTo = async (dst, request) => {
|
|
state.sent.push(request);
|
|
return request instanceof messages.FeatureListRequest ? features : undefined;
|
|
};
|
|
|
|
const plugin = new InstancePlugin({ plugin: { name: "exp_scenario" }, instance, host: {}, logger });
|
|
await plugin.init();
|
|
return { plugin, instance, state };
|
|
}
|
|
|
|
/** The payload of a recorded rcon command. */
|
|
function decodeRcon(command) {
|
|
const match = command.match(/^\/sc exp_scenario\.features\.receive_update\(helpers\.json_to_table\[(=+)\[(.*)\]\1\]\)$/s);
|
|
return JSON.parse(match[2]);
|
|
}
|
|
|
|
t.test("class InstancePlugin", t2 => {
|
|
t2.test(".onStart() subscribes and sends every feature to lua", async t3 => {
|
|
const { plugin, state } = await startPlugin(t3);
|
|
await plugin.onStart();
|
|
|
|
t3.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes to updates first");
|
|
t3.ok(state.sent[1] instanceof messages.FeatureListRequest, "then lists the features");
|
|
t3.strictSame(
|
|
decodeRcon(state.rcons[0]),
|
|
[{ name: "death_markers", enabled: false, values: { show_map_markers: false } }],
|
|
);
|
|
});
|
|
|
|
t2.test(".handleFeatureUpdatedEvent() only sends while the game is up", async t3 => {
|
|
const { plugin, state } = await startPlugin(t3);
|
|
const event = new messages.FeatureUpdatedEvent([
|
|
new messages.FeatureRecord("afk_kick", true, { afk_minutes: 5 }),
|
|
new messages.FeatureRecord("removed", true, {}, 0, true),
|
|
]);
|
|
|
|
await plugin.handleFeatureUpdatedEvent(event);
|
|
t3.strictSame(state.rcons, [], "nothing is sent before start");
|
|
|
|
await plugin.onStart();
|
|
state.rcons.length = 0;
|
|
await plugin.handleFeatureUpdatedEvent(event);
|
|
t3.strictSame(decodeRcon(state.rcons[0]), [{ name: "afk_kick", enabled: true, values: { afk_minutes: 5 } }],
|
|
"deleted features are left out");
|
|
|
|
await plugin.onExit();
|
|
state.rcons.length = 0;
|
|
await plugin.handleFeatureUpdatedEvent(event);
|
|
t3.strictSame(state.rcons, [], "nothing is sent after exit");
|
|
});
|
|
|
|
t2.test(".onControllerConnectionEvent() syncs again after the controller restarts", async t3 => {
|
|
const { plugin, state } = await startPlugin(t3);
|
|
await plugin.onControllerConnectionEvent("connect");
|
|
t3.strictSame(state.sent, [], "nothing happens before start");
|
|
|
|
await plugin.onStart();
|
|
state.sent.length = 0;
|
|
state.rcons.length = 0;
|
|
await plugin.onControllerConnectionEvent("resume");
|
|
t3.strictSame(state.sent, [], "a resumed session keeps its subscription");
|
|
|
|
await plugin.onControllerConnectionEvent("connect");
|
|
t3.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes again");
|
|
t3.equal(state.rcons.length, 1, "and sends every feature");
|
|
});
|
|
|
|
t2.test(".luaSendFeatures() picks a long bracket the values do not close", async t3 => {
|
|
const { plugin, state } = await startPlugin(t3);
|
|
const name = "a]=]b]==]c";
|
|
await plugin.luaSendFeatures([new messages.FeatureRecord("station_auto_name", true, { station_name: name })]);
|
|
t3.match(state.rcons[0], "json_to_table[===[", "uses a level not found in the json");
|
|
t3.strictSame(decodeRcon(state.rcons[0])[0].values.station_name, name);
|
|
});
|
|
|
|
t2.end();
|
|
});
|