Files
factorio-clusterio/plugins/global_chat/instance.js
T
Hornwitser bf5a4a8a9c Split up into multiple packages with Lerna
Split up codebase into the packages @clsterio/lib, @clusterio/master,
@clusterio/slave, and @clusterio/ctl using lerna to manage them.  This
will allow deployment to use only the parts needed reducing install
size, and better separate the code into shared library, master, slave
and control.
2020-09-21 18:00:07 +02:00

65 lines
1.4 KiB
JavaScript

/**
* @module
*/
"use strict";
const plugin = require("@clusterio/lib/plugin");
const luaTools = require("@clusterio/lib/luaTools");
/**
* Removes gps and train tags from messags
*
* @param {string} content - string to strip tags from.
* @returns {string} stripped string.
*/
function removeTags(content) {
return content.replace(/(\[gps=-?\d+,-?\d+\]|\[train=\d+\])/g, "");
}
class InstancePlugin extends plugin.BaseInstancePlugin {
async init() {
this.messageQueue = [];
}
onMasterConnectionEvent(event) {
if (event === "connect") {
for (let message of this.messageQueue) {
this.sendChat(message);
}
this.messageQueue = [];
}
}
async chatEventHandler(message) {
// TODO check if cross server chat is enabled
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
await this.instance.server.sendRcon(`/sc game.print('${luaTools.escapeString(content)}')`, true);
}
sendChat(message) {
this.info.messages.chat.send(this.instance, {
instance_name: this.instance.name,
content: message,
});
}
async onOutput(output) {
if (output.type === "action" && output.action === "CHAT") {
if (this.slave.connector.connected) {
this.sendChat(output.message);
} else {
this.messageQueue.push(output.message);
}
}
}
// TODO implement info command in lua?
}
module.exports = {
InstancePlugin,
// For testing only
_removeTags: removeTags,
};