mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
Move BaseControllerPlugin to controller/src/BaseControllerPlugin.ts Move BaseInstancePlugin to host/src/BaseInstancePlugin.ts Move BaseControlPlugin to ctl/src/BaseCtlPlugin.ts Move BaseWebPlugin to web_ui/src/BaseWebPlugin.ts This solves the circular package dependency problem of the base plugins needing types that are available only in the dependencies to lib.
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
import { BaseInstancePlugin } from "@clusterio/host";
|
|
import { ChatEvent } from "./messages";
|
|
|
|
/**
|
|
* Removes gps and train tags from messags
|
|
*
|
|
* @param content - string to strip tags from.
|
|
* @returns stripped string.
|
|
*/
|
|
function removeTags(content: string): string {
|
|
return content.replace(/(\[gps=-?\d+,-?\d+\]|\[train=\d+\])/g, "");
|
|
}
|
|
|
|
export class InstancePlugin extends BaseInstancePlugin {
|
|
messageQueue: string[] = [];
|
|
|
|
async init() {
|
|
this.instance.handle(ChatEvent, this.handleChatEvent.bind(this));
|
|
}
|
|
|
|
onControllerConnectionEvent(event: string) {
|
|
if (event === "connect") {
|
|
for (let message of this.messageQueue) {
|
|
this.sendChat(message);
|
|
}
|
|
this.messageQueue = [];
|
|
}
|
|
}
|
|
|
|
async handleChatEvent(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);
|
|
}
|
|
|
|
sendChat(message: string) {
|
|
this.instance.sendTo("allInstances", new ChatEvent(this.instance.name, message));
|
|
}
|
|
|
|
async onOutput(output: lib.ParsedFactorioOutput) {
|
|
if (output.type === "action" && output.action === "CHAT") {
|
|
if (this.host.connector.connected) {
|
|
this.sendChat(output.message);
|
|
} else {
|
|
this.messageQueue.push(output.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO implement info command in lua?
|
|
}
|
|
|
|
// For testing only
|
|
export const _removeTags = removeTags;
|