mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-17 22:33:13 +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
+28
-47
@@ -1,5 +1,4 @@
|
||||
import type { Instance, InstancePluginContext } from "@clusterio/host";
|
||||
import type * as lib from "@clusterio/lib";
|
||||
import type { InstancePluginContext } from "@clusterio/host";
|
||||
import * as messages from "./messages.js";
|
||||
|
||||
/** Sent by the lua side when a player reports another. */
|
||||
@@ -29,82 +28,64 @@ export type IpcReportDelete = {
|
||||
* the answer is handed back to lua once it arrives. The instance may have
|
||||
* stopped by then, in which case the answer is dropped.
|
||||
*/
|
||||
export class InstancePlugin {
|
||||
private constructor(
|
||||
public instance: Instance,
|
||||
public logger: lib.Logger,
|
||||
public name: string,
|
||||
) {}
|
||||
export default async function (context: InstancePluginContext) {
|
||||
const { instance } = context;
|
||||
|
||||
static async fromContext(context: InstancePluginContext) {
|
||||
const instance = context.instance;
|
||||
const plugin = new InstancePlugin(instance, context.logger, context.plugin.name);
|
||||
|
||||
instance.server.handle("exp_reports:create", plugin.handleCreateIPC.bind(plugin));
|
||||
instance.server.handle("exp_reports:list", plugin.handleListIPC.bind(plugin));
|
||||
instance.server.handle("exp_reports:delete", plugin.handleDeleteIPC.bind(plugin));
|
||||
return plugin;
|
||||
/** Hand an answer to lua, unless the instance stopped while it was being fetched. */
|
||||
async function luaSend(receiver: string, json: any) {
|
||||
if (instance.status !== "running") {
|
||||
return;
|
||||
}
|
||||
await instance.sendRcon(
|
||||
`/sc exp_reports.${receiver}(helpers.json_to_table[=[${JSON.stringify(json)}]=])`, true
|
||||
);
|
||||
}
|
||||
|
||||
async handleCreateIPC(event: IpcReportCreate) {
|
||||
instance.server.handle("exp_reports:create", async (event: IpcReportCreate) => {
|
||||
try {
|
||||
const report = await this.instance.sendTo("controller", new messages.ReportCreateRequest(
|
||||
const report = await instance.sendTo("controller", new messages.ReportCreateRequest(
|
||||
event.player_name, event.reason, event.by_player_name,
|
||||
));
|
||||
const reports = await this.instance.sendTo("controller", new messages.ReportListRequest(event.player_name));
|
||||
await this.luaSend("receive_created", {
|
||||
const reports = await instance.sendTo("controller", new messages.ReportListRequest(event.player_name));
|
||||
await luaSend("receive_created", {
|
||||
report: report.toJSON(),
|
||||
reports: reports.map(other => other.toJSON()),
|
||||
});
|
||||
} catch (err: any) {
|
||||
await this.luaSend("receive_error", { caller: event.by_player_name, message: err.message });
|
||||
await luaSend("receive_error", { caller: event.by_player_name, message: err.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async handleListIPC(event: IpcReportList) {
|
||||
instance.server.handle("exp_reports:list", async (event: IpcReportList) => {
|
||||
try {
|
||||
const reports = await this.instance.sendTo("controller", new messages.ReportListRequest(event.player_name));
|
||||
await this.luaSend("receive_list", {
|
||||
const reports = await instance.sendTo("controller", new messages.ReportListRequest(event.player_name));
|
||||
await luaSend("receive_list", {
|
||||
caller: event.caller,
|
||||
player_name: event.player_name,
|
||||
reports: reports.map(report => report.toJSON()),
|
||||
});
|
||||
} catch (err: any) {
|
||||
await this.luaSend("receive_error", { caller: event.caller, message: err.message });
|
||||
await luaSend("receive_error", { caller: event.caller, message: err.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async handleDeleteIPC(event: IpcReportDelete) {
|
||||
instance.server.handle("exp_reports:delete", async (event: IpcReportDelete) => {
|
||||
try {
|
||||
let reports = await this.instance.sendTo("controller", new messages.ReportListRequest(event.player_name));
|
||||
let reports = await instance.sendTo("controller", new messages.ReportListRequest(event.player_name));
|
||||
if (event.by_player_name !== undefined) {
|
||||
reports = reports.filter(report => report.byPlayerName === event.by_player_name);
|
||||
}
|
||||
for (const report of reports) {
|
||||
await this.instance.sendTo("controller", new messages.ReportDeleteRequest(report.id));
|
||||
await instance.sendTo("controller", new messages.ReportDeleteRequest(report.id));
|
||||
}
|
||||
await this.luaSend("receive_deleted", {
|
||||
await luaSend("receive_deleted", {
|
||||
caller: event.caller,
|
||||
player_name: event.player_name,
|
||||
by_player_name: event.by_player_name,
|
||||
count: reports.length,
|
||||
});
|
||||
} catch (err: any) {
|
||||
await this.luaSend("receive_error", { caller: event.caller, message: err.message });
|
||||
await luaSend("receive_error", { caller: event.caller, message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
/** Hand an answer to lua, unless the instance stopped while it was being fetched. */
|
||||
async luaSend(receiver: string, json: any) {
|
||||
if (this.instance.status !== "running") {
|
||||
return;
|
||||
}
|
||||
await this.instance.sendRcon(
|
||||
`/sc exp_reports.${receiver}(helpers.json_to_table[=[${JSON.stringify(json)}]=])`, true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default async function (context: InstancePluginContext) {
|
||||
await InstancePlugin.fromContext(context);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user