mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 15:43:13 +09:00
Convert global_chat, research_sync and statistics_exporter to entrypoint functions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd609c3c54
commit
cfeb4b6a4a
7 files changed
+231
-237
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { CommandTree, Command } from "@clusterio/lib";
|
||||
import { BaseCtlPlugin } from "@clusterio/ctl";
|
||||
import type { CtlPluginContext } from "@clusterio/ctl";
|
||||
import { ChatEvent } from "./messages.js";
|
||||
|
||||
const globalChatCommands = new CommandTree({
|
||||
@@ -14,8 +14,8 @@ globalChatCommands.add(new Command({
|
||||
},
|
||||
}));
|
||||
|
||||
export class CtlPlugin extends BaseCtlPlugin {
|
||||
async addCommands(rootCommand: CommandTree) {
|
||||
export default async function(context: CtlPluginContext) {
|
||||
context.hooks.addCommands.attach(context.plugin.name, async (rootCommand) => {
|
||||
rootCommand.add(globalChatCommands);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { BaseInstancePlugin } from "@clusterio/host";
|
||||
import type { InstancePluginContext } from "@clusterio/host";
|
||||
import { ChatEvent } from "./messages.js";
|
||||
|
||||
/**
|
||||
@@ -12,41 +12,38 @@ function removeTags(content: string): string {
|
||||
return content.replace(/\[(?:gps|special-item|train|train-stop)=\S*?\]/gm, "");
|
||||
}
|
||||
|
||||
export class InstancePlugin extends BaseInstancePlugin {
|
||||
messageQueue: string[] = [];
|
||||
export default async function(context: InstancePluginContext) {
|
||||
const { instance, host, plugin } = context;
|
||||
let messageQueue: string[] = [];
|
||||
|
||||
async init() {
|
||||
this.instance.handle(ChatEvent, this.handleChatEvent.bind(this));
|
||||
function sendChat(message: string) {
|
||||
instance.sendTo("allInstances", new ChatEvent(instance.name, message));
|
||||
}
|
||||
|
||||
onControllerConnectionEvent(event: string) {
|
||||
if (event === "connect") {
|
||||
for (let message of this.messageQueue) {
|
||||
this.sendChat(message);
|
||||
}
|
||||
this.messageQueue = [];
|
||||
}
|
||||
}
|
||||
|
||||
async handleChatEvent(event: ChatEvent) {
|
||||
instance.handle(ChatEvent, async (event: ChatEvent) => {
|
||||
// TODO check if cross server chat is enabled
|
||||
let content = `[${event.instanceName}] ${removeTags(event.content)}`;
|
||||
await this.sendRcon(`/sc game.print('${lib.escapeString(content)}')`, true);
|
||||
}
|
||||
await instance.sendRcon(`/sc game.print('${lib.escapeString(content)}')`, true, plugin.name);
|
||||
});
|
||||
|
||||
sendChat(message: string) {
|
||||
this.instance.sendTo("allInstances", new ChatEvent(this.instance.name, message));
|
||||
}
|
||||
instance.hooks.controllerConnectionEvent.attach(plugin.name, (event) => {
|
||||
if (event === "connect") {
|
||||
for (let message of messageQueue) {
|
||||
sendChat(message);
|
||||
}
|
||||
messageQueue = [];
|
||||
}
|
||||
});
|
||||
|
||||
async onOutput(output: lib.ParsedFactorioOutput) {
|
||||
instance.hooks.output.attach(plugin.name, async (output) => {
|
||||
if (output.type === "action" && output.action === "CHAT") {
|
||||
if (this.host.connector.connected) {
|
||||
this.sendChat(output.message);
|
||||
if (host.connector.connected) {
|
||||
sendChat(output.message);
|
||||
} else {
|
||||
this.messageQueue.push(output.message);
|
||||
messageQueue.push(output.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// TODO implement info command in lua?
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import assert from "node:assert/strict";
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import * as mock from "../../../test/mock.js";
|
||||
import * as lines from "../../../test/lib/factorio/lines.js";
|
||||
@@ -31,40 +32,44 @@ describe("global_chat plugin", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("class InstancePlugin", function() {
|
||||
let instancePlugin;
|
||||
describe("instance entrypoint", function() {
|
||||
let mockInstance;
|
||||
|
||||
before(async function() {
|
||||
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance(), new mock.MockHost());
|
||||
await instancePlugin.init();
|
||||
try {
|
||||
lib.registerPluginMessages([info]);
|
||||
} catch (err) {
|
||||
// Already registered by the full test suite
|
||||
}
|
||||
({ instance: mockInstance } = await mock.loadInstancePlugin(instance.default, info));
|
||||
});
|
||||
|
||||
describe(".handleChatEvent()", function() {
|
||||
describe("ChatEvent handler", function() {
|
||||
it("should send received chat as command", async function() {
|
||||
instancePlugin.instance.server.rconCommands = [];
|
||||
await instancePlugin.handleChatEvent(new ChatEvent("test", "User: message"));
|
||||
mockInstance.server.rconCommands = [];
|
||||
await mock.getHandler(mockInstance, ChatEvent)(new ChatEvent("test", "User: message"));
|
||||
assert.deepEqual(
|
||||
instancePlugin.instance.server.rconCommands,
|
||||
mockInstance.server.rconCommands,
|
||||
["/sc game.print('[test] User: message')"],
|
||||
);
|
||||
});
|
||||
it("should filter server-specific tags from received chat", async function() {
|
||||
instancePlugin.instance.server.rconCommands = [];
|
||||
await instancePlugin.handleChatEvent(new ChatEvent("test", "Train [train=1235]"));
|
||||
mockInstance.server.rconCommands = [];
|
||||
await mock.getHandler(mockInstance, ChatEvent)(new ChatEvent("test", "Train [train=1235]"));
|
||||
assert.deepEqual(
|
||||
instancePlugin.instance.server.rconCommands,
|
||||
mockInstance.server.rconCommands,
|
||||
["/sc game.print('[test] Train ')"],
|
||||
);
|
||||
});
|
||||
});
|
||||
describe(".onOutput()", function() {
|
||||
describe("output hook", function() {
|
||||
it("should forward chat", async function() {
|
||||
let count = 0;
|
||||
for (let [_, output] of lines.testLines) {
|
||||
for (let [line, output] of lines.testLines) {
|
||||
if (output.type === "action" && output.action === "CHAT") {
|
||||
instancePlugin.instance.connector.sentMessages = [];
|
||||
await instancePlugin.onOutput(output);
|
||||
assert(instancePlugin.instance.connector.sentMessages.length, "message was not sent");
|
||||
mockInstance.connector.sentMessages = [];
|
||||
await mockInstance.hooks.output.invoke(output, line);
|
||||
assert(mockInstance.connector.sentMessages.length, "message was not sent");
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
@@ -72,11 +77,11 @@ describe("global_chat plugin", function() {
|
||||
});
|
||||
it("should ignore regular output", async function() {
|
||||
let count = 0;
|
||||
for (let [_, output] of lines.testLines) {
|
||||
for (let [line, output] of lines.testLines) {
|
||||
if (output.type !== "action" || output.action !== "CHAT") {
|
||||
instancePlugin.instance.connector.sentMessages = [];
|
||||
await instancePlugin.onOutput(output);
|
||||
assert(!instancePlugin.instance.connector.sentMessages.length, "message was sent");
|
||||
mockInstance.connector.sentMessages = [];
|
||||
await mockInstance.hooks.output.invoke(output, line);
|
||||
assert(!mockInstance.connector.sentMessages.length, "message was sent");
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user