mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-16 08:24:52 +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
+76
-124
@@ -1,4 +1,4 @@
|
||||
import type { Instance, InstancePluginContext } from "@clusterio/host";
|
||||
import type { InstancePluginContext } from "@clusterio/host";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as messages from "./messages.js";
|
||||
|
||||
@@ -17,174 +17,126 @@ export type IpcPlayerAssignments = {
|
||||
assignments: Record<string, number>,
|
||||
};
|
||||
|
||||
export class InstancePlugin {
|
||||
export default async function (context: InstancePluginContext) {
|
||||
const { instance, plugin } = context;
|
||||
const syncMode = () => instance.config.get("exp_groups.sync_mode");
|
||||
// Once only, don't send permissions for these groups
|
||||
// This is used for groups created on this instance that only need the controller generated id
|
||||
skipSendingPermissions = new Set<string>();
|
||||
const skipSendingPermissions = new Set<string>();
|
||||
// This is used for groups updated / deleted on this instance to stop cycles
|
||||
skipSendingUpdate = new Set<number>();
|
||||
const skipSendingUpdate = new Set<number>();
|
||||
// Track known online players so that we only apply assignment updates for them
|
||||
onlinePlayers = new Set<string>();
|
||||
const onlinePlayers = new Set<string>();
|
||||
|
||||
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);
|
||||
|
||||
instance.handle(messages.GroupUpdatedEvent, plugin.handleGroupUpdatedEvent.bind(plugin));
|
||||
instance.handle(messages.ResolvedAssignmentUpdatedEvent, plugin.handleResolvedAssignmentUpdatedEvent.bind(plugin));
|
||||
instance.server.handle(`exp_group:group_updated`, plugin.handleGroupUpdatedIPC.bind(plugin))
|
||||
instance.server.handle(`exp_group:group_deleted`, plugin.handleGroupDeletedIPC.bind(plugin))
|
||||
instance.server.handle(`exp_group:player_assignments`, plugin.handlePlayerAssignmentsIPC.bind(plugin))
|
||||
|
||||
const hooks = instance.hooks;
|
||||
hooks.instanceConfigFieldChanged.attach(plugin.name, plugin.onInstanceConfigFieldChanged.bind(plugin));
|
||||
hooks.start.attach(plugin.name, plugin.onStart.bind(plugin));
|
||||
hooks.playerEvent.attach(plugin.name, plugin.onPlayerEvent.bind(plugin));
|
||||
return plugin;
|
||||
async function luaSend(receiver: string, json: any) {
|
||||
await instance.sendRcon(`/sc exp_groups.${receiver}(helpers.json_to_table[=[${JSON.stringify(json)}]=])`, true)
|
||||
}
|
||||
|
||||
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
|
||||
switch(field) {
|
||||
case "exp_groups.sync_mode":
|
||||
await this.luaSetEmitEvents(curr == "bidirectional")
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
async onStart() {
|
||||
// We use Date.now() because we need to manually initialise the groups on the lua side
|
||||
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_groups:${messages.GroupUpdatedEvent.name}`, "subscribe", Date.now()
|
||||
async function updatePlayerAssignmentSubscription(playerName: string, action: "subscribe" | "unsubscribe") {
|
||||
await instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_groups:${messages.ResolvedAssignmentUpdatedEvent.name}`, action, 0, lib.SubscriptionFilters.fromShorthand(playerName)
|
||||
));
|
||||
const groups = await this.instance.sendTo("controller", new messages.GroupListRequest())
|
||||
await this.luaSendInitialGroups(groups);
|
||||
await this.luaSetEmitEvents(this.instance.config.get("exp_groups.sync_mode") == "bidirectional")
|
||||
}
|
||||
|
||||
async onPlayerEvent(event: lib.PlayerEvent) {
|
||||
switch(event.type) {
|
||||
case "join":
|
||||
this.onlinePlayers.add(event.name);
|
||||
await this.subscribePlayerAssignment(event.name);
|
||||
break;
|
||||
case "leave":
|
||||
this.onlinePlayers.delete(event.name);
|
||||
await this.unsubscribePlayerAssignment(event.name);
|
||||
break;
|
||||
instance.handle(messages.GroupUpdatedEvent, async event => {
|
||||
if (syncMode() === "disabled") {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async handleGroupUpdatedEvent(event: messages.GroupUpdatedEvent) {
|
||||
for (const group of event.updates) {
|
||||
await this.luaSendGroupUpdate(group);
|
||||
if (skipSendingUpdate.has(group.id)) {
|
||||
skipSendingUpdate.delete(group.id);
|
||||
continue;
|
||||
}
|
||||
|
||||
const json = group.toJSON();
|
||||
if (skipSendingPermissions.has(group.name)) {
|
||||
skipSendingPermissions.delete(group.name);
|
||||
delete (json as any).permissions;
|
||||
}
|
||||
|
||||
await luaSend("receive_group_update", json);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
instance.handle(messages.ResolvedAssignmentUpdatedEvent, async event => {
|
||||
if (syncMode() === "disabled") {
|
||||
return;
|
||||
}
|
||||
|
||||
async handleResolvedAssignmentUpdatedEvent(event: messages.ManualAssignmentUpdatedEvent) {
|
||||
for (const assignment of event.updates) {
|
||||
if (this.onlinePlayers.has(assignment.name)) {
|
||||
await this.luaSendAssignmentUpdate(assignment);
|
||||
if (onlinePlayers.has(assignment.name)) {
|
||||
await luaSend("receive_assignment_update", assignment);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async handleGroupUpdatedIPC(event: IpcGroupUpdated) {
|
||||
instance.server.handle("exp_group:group_updated", async (event: IpcGroupUpdated) => {
|
||||
const permissions = new messages.GroupPermissions(
|
||||
event.permissions.is_blacklist,
|
||||
event.permissions.permissions ?? [],
|
||||
)
|
||||
|
||||
if (event.group_id === undefined) {
|
||||
this.skipSendingPermissions.add(event.group_name);
|
||||
await this.instance.sendTo("controller",
|
||||
skipSendingPermissions.add(event.group_name);
|
||||
await instance.sendTo("controller",
|
||||
new messages.GroupCreateRequest(event.group_name, permissions),
|
||||
);
|
||||
} else {
|
||||
this.skipSendingUpdate.add(event.group_id);
|
||||
await this.instance.sendTo("controller", new messages.GroupUpdateRequest(
|
||||
skipSendingUpdate.add(event.group_id);
|
||||
await instance.sendTo("controller", new messages.GroupUpdateRequest(
|
||||
new messages.GroupRecord(event.group_id, event.group_name, permissions),
|
||||
));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async handleGroupDeletedIPC(event: IpcGroupDeleted) {
|
||||
instance.server.handle("exp_group:group_deleted", async (event: IpcGroupDeleted) => {
|
||||
if (event.group_id === undefined) {
|
||||
return;
|
||||
}
|
||||
this.skipSendingUpdate.add(event.group_id);
|
||||
await this.instance.sendTo("controller", new messages.GroupDeleteRequest(event.group_id));
|
||||
}
|
||||
skipSendingUpdate.add(event.group_id);
|
||||
await instance.sendTo("controller", new messages.GroupDeleteRequest(event.group_id));
|
||||
});
|
||||
|
||||
async handlePlayerAssignmentsIPC(event: IpcPlayerAssignments) {
|
||||
instance.server.handle("exp_group:player_assignments", async (event: IpcPlayerAssignments) => {
|
||||
await Promise.all(
|
||||
Object.entries(event.assignments).map(([playerName, groupId]) =>
|
||||
this.instance.sendTo("controller",
|
||||
instance.sendTo("controller",
|
||||
new messages.AssignmentUpdateRequest(new messages.AssignmentRecord(playerName, groupId)),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
async subscribePlayerAssignment(playerName: string) {
|
||||
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_groups:${messages.ResolvedAssignmentUpdatedEvent.name}`, "subscribe", 0, lib.SubscriptionFilters.fromShorthand(playerName)
|
||||
const hooks = instance.hooks;
|
||||
hooks.instanceConfigFieldChanged.attach(plugin.name, async (field, curr) => {
|
||||
if (field === "exp_groups.sync_mode") {
|
||||
await luaSend("set_emit_events", curr == "bidirectional")
|
||||
}
|
||||
});
|
||||
|
||||
hooks.start.attach(plugin.name, async () => {
|
||||
// We use Date.now() because we need to manually initialise the groups on the lua side
|
||||
await instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_groups:${messages.GroupUpdatedEvent.name}`, "subscribe", Date.now()
|
||||
));
|
||||
}
|
||||
|
||||
async unsubscribePlayerAssignment(playerName: string) {
|
||||
await this.instance.sendTo("controller", new lib.SubscriptionRequest(
|
||||
`exp_groups:${messages.ResolvedAssignmentUpdatedEvent.name}`, "unsubscribe", 0, lib.SubscriptionFilters.fromShorthand(playerName)
|
||||
));
|
||||
}
|
||||
|
||||
async luaSendInitialGroups(groups: messages.GroupRecord[]) {
|
||||
if (this.instance.config.get("exp_groups.sync_mode") === "disabled") {
|
||||
return;
|
||||
const groups = await instance.sendTo("controller", new messages.GroupListRequest())
|
||||
if (syncMode() !== "disabled") {
|
||||
await luaSend("initialise_groups", groups);
|
||||
}
|
||||
await this.luaSend("initialise_groups", groups);
|
||||
}
|
||||
await luaSend("set_emit_events", syncMode() == "bidirectional")
|
||||
});
|
||||
|
||||
async luaSendGroupUpdate(group: messages.GroupRecord) {
|
||||
if (this.instance.config.get("exp_groups.sync_mode") === "disabled") {
|
||||
return;
|
||||
hooks.playerEvent.attach(plugin.name, async (event: lib.PlayerEvent) => {
|
||||
switch(event.type) {
|
||||
case "join":
|
||||
onlinePlayers.add(event.name);
|
||||
await updatePlayerAssignmentSubscription(event.name, "subscribe");
|
||||
break;
|
||||
case "leave":
|
||||
onlinePlayers.delete(event.name);
|
||||
await updatePlayerAssignmentSubscription(event.name, "unsubscribe");
|
||||
break;
|
||||
}
|
||||
|
||||
if (this.skipSendingUpdate.has(group.id)) {
|
||||
this.skipSendingUpdate.delete(group.id);
|
||||
return;
|
||||
}
|
||||
|
||||
const json = group.toJSON();
|
||||
if (this.skipSendingPermissions.has(group.name)) {
|
||||
this.skipSendingPermissions.delete(group.name);
|
||||
delete (json as any).permissions;
|
||||
}
|
||||
|
||||
await this.luaSend("receive_group_update", json);
|
||||
}
|
||||
|
||||
async luaSendAssignmentUpdate(assignment: messages.AssignmentRecord) {
|
||||
if (this.instance.config.get("exp_groups.sync_mode") === "disabled") {
|
||||
return;
|
||||
}
|
||||
await this.luaSend("receive_assignment_update", assignment);
|
||||
}
|
||||
|
||||
async luaSetEmitEvents(emitEvents: boolean) {
|
||||
await this.luaSend("set_emit_events", emitEvents);
|
||||
}
|
||||
|
||||
async luaSend(receiver: string, json: any) {
|
||||
await this.instance.sendRcon(`/sc exp_groups.${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