mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Port global_chat plugin to typescript (#497)
* .js to .ts * translate * update paths to dist/plugin * add "tsc --build" to prepare * use field main in package.json de find info * tsc --build need to append before webpack * update test and use for plugins package.main
This commit is contained in:
1 parent
477b600b1b
commit
50f12c719b
26 files changed
+104
-102
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import * as lib from "@clusterio/lib";
|
||||
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 lib.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;
|
||||
Reference in new issue
Block a user