mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-17 08:50:42 +09:00
Write plugins without state shared elsewhere as functions (#492)
The instance plugins of exp_groups, exp_reports, exp_roles, exp_scenario and exp_server_ups, and the controller plugins of exp_reports and exp_scenario, were classes only to hold their handlers. They are now init functions with closures, like clusterio's own plugins. The exp_roles and exp_groups controller plugins stay classes because exp_scenario reaches their datastores through get(controller). AutoRestart stays a class because its tests step check() with a fake clock. Tests spy on handle() and the hooks to call the handlers. The reports webhook tests stub fetch instead of replacing postWebhook, and the feature reconcile test writes the stored records to disk before start. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
26a801da5e
commit
68a964cf57
12 files changed
+709
-820
No files matched your search
+47
-69
@@ -1,91 +1,69 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import type { Instance, InstancePluginContext } from "@clusterio/host";
|
||||
import type { InstancePluginContext } from "@clusterio/host";
|
||||
|
||||
export class InstancePlugin {
|
||||
private updateInterval?: ReturnType<typeof setInterval>;
|
||||
private gameTimes: number[] = [];
|
||||
export default async function (context: InstancePluginContext) {
|
||||
const { instance, logger, plugin } = context;
|
||||
let updateInterval: ReturnType<typeof setInterval> | undefined;
|
||||
const gameTimes: number[] = [];
|
||||
|
||||
private constructor(
|
||||
public instance: Instance,
|
||||
public logger: lib.Logger,
|
||||
public name: string,
|
||||
) {}
|
||||
|
||||
static async fromContext(context: InstancePluginContext) {
|
||||
const instance = context.instance;
|
||||
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
|
||||
|
||||
const hooks = instance.hooks;
|
||||
hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
|
||||
hooks.exit.attach(plugin.name, plugin.onExit.bind(plugin));
|
||||
hooks.instanceConfigFieldChanged.attach(plugin.name, plugin.onInstanceConfigFieldChanged.bind(plugin));
|
||||
hooks.playerEvent.attach(plugin.name, plugin.onPlayerEvent.bind(plugin));
|
||||
return plugin;
|
||||
}
|
||||
|
||||
async onStart() {
|
||||
if (!this.instance.config.get("factorio.settings")["auto_pause"]) {
|
||||
this.setInterval();
|
||||
function startUpdates() {
|
||||
if (!updateInterval) {
|
||||
updateInterval = setInterval(updateUps, instance.config.get("exp_server_ups.update_interval"));
|
||||
}
|
||||
}
|
||||
|
||||
onExit() {
|
||||
this.clearInterval();
|
||||
}
|
||||
|
||||
async onInstanceConfigFieldChanged(field: string, curr: unknown): Promise<void> {
|
||||
if (field === "exp_server_ups.update_interval") {
|
||||
this.clearInterval();
|
||||
this.setInterval();
|
||||
} else if (field === "exp_server_ups.average_interval") {
|
||||
this.gameTimes.splice(curr as number);
|
||||
function stopUpdates() {
|
||||
if (updateInterval) {
|
||||
clearInterval(updateInterval);
|
||||
updateInterval = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async onPlayerEvent(event: lib.PlayerEvent): Promise<void> {
|
||||
if (event.type === "join") {
|
||||
this.setInterval();
|
||||
} else if (event.type === "leave" && this.instance.playersOnline.size == 0 && this.instance.config.get("factorio.settings")["auto_pause"]) {
|
||||
this.clearInterval();
|
||||
}
|
||||
}
|
||||
|
||||
setInterval() {
|
||||
if (!this.updateInterval) {
|
||||
this.updateInterval = setInterval(this.updateUps.bind(this), this.instance.config.get("exp_server_ups.update_interval"));
|
||||
}
|
||||
}
|
||||
|
||||
clearInterval() {
|
||||
if (this.updateInterval) {
|
||||
clearInterval(this.updateInterval);
|
||||
this.updateInterval = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async updateUps() {
|
||||
async function updateUps() {
|
||||
let ups = 0;
|
||||
const collected = this.gameTimes.length - 1;
|
||||
const collected = gameTimes.length - 1;
|
||||
if (collected > 0) {
|
||||
const minTick = this.gameTimes[0];
|
||||
const maxTick = this.gameTimes[collected];
|
||||
const interval = this.instance.config.get("exp_server_ups.update_interval") / 1000;
|
||||
const minTick = gameTimes[0];
|
||||
const maxTick = gameTimes[collected];
|
||||
const interval = instance.config.get("exp_server_ups.update_interval") / 1000;
|
||||
ups = (maxTick - minTick) / (collected * interval);
|
||||
}
|
||||
|
||||
try {
|
||||
const newGameTime = await this.instance.sendRcon(`/_rcon return exp_server_ups.refresh(${ups})`, false, this.name);
|
||||
this.gameTimes.push(Number(newGameTime));
|
||||
const newGameTime = await instance.sendRcon(`/_rcon return exp_server_ups.refresh(${ups})`, false, plugin.name);
|
||||
gameTimes.push(Number(newGameTime));
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Failed to receive new game time: ${error}`);
|
||||
logger.error(`Failed to receive new game time: ${error}`);
|
||||
}
|
||||
|
||||
if (collected > this.instance.config.get("exp_server_ups.average_interval")) {
|
||||
this.gameTimes.shift();
|
||||
if (collected > instance.config.get("exp_server_ups.average_interval")) {
|
||||
gameTimes.shift();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (context: InstancePluginContext) {
|
||||
await InstancePlugin.fromContext(context);
|
||||
const hooks = instance.hooks;
|
||||
hooks.start.attach(plugin.name, async () => {
|
||||
if (!instance.config.get("factorio.settings")["auto_pause"]) {
|
||||
startUpdates();
|
||||
}
|
||||
});
|
||||
|
||||
hooks.exit.attach(plugin.name, stopUpdates);
|
||||
|
||||
hooks.instanceConfigFieldChanged.attach(plugin.name, async (field, curr) => {
|
||||
if (field === "exp_server_ups.update_interval") {
|
||||
stopUpdates();
|
||||
startUpdates();
|
||||
} else if (field === "exp_server_ups.average_interval") {
|
||||
gameTimes.splice(curr as number);
|
||||
}
|
||||
});
|
||||
|
||||
hooks.playerEvent.attach(plugin.name, async (event: lib.PlayerEvent) => {
|
||||
if (event.type === "join") {
|
||||
startUpdates();
|
||||
} else if (event.type === "leave" && instance.playersOnline.size == 0 && instance.config.get("factorio.settings")["auto_pause"]) {
|
||||
stopUpdates();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user