@clusterio/plugin-global_chat
Version:
Clusterio plugin forwarding between Factorio servers
87 lines • 3.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports._removeTags = exports.InstancePlugin = void 0;
const lib = __importStar(require("@clusterio/lib"));
const host_1 = require("@clusterio/host");
const messages_1 = require("./messages");
/**
* Removes gps and train tags from messags
*
* @param content - string to strip tags from.
* @returns stripped string.
*/
function removeTags(content) {
return content.replace(/(\[gps=-?\d+,-?\d+\]|\[train=\d+\])/g, "");
}
class InstancePlugin extends host_1.BaseInstancePlugin {
messageQueue = [];
async init() {
if (!this.instance.config.get("factorio.enable_script_commands")) {
throw new Error("global_chat plugin requires script commands.");
}
this.instance.handle(messages_1.ChatEvent, this.handleChatEvent.bind(this));
}
onControllerConnectionEvent(event) {
if (event === "connect") {
for (let message of this.messageQueue) {
this.sendChat(message);
}
this.messageQueue = [];
}
}
async handleChatEvent(event) {
// 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) {
this.instance.sendTo("allInstances", new messages_1.ChatEvent(this.instance.name, message));
}
async onOutput(output) {
if (output.type === "action" && output.action === "CHAT") {
if (this.host.connector.connected) {
this.sendChat(output.message);
}
else {
this.messageQueue.push(output.message);
}
}
}
}
exports.InstancePlugin = InstancePlugin;
// For testing only
exports._removeTags = removeTags;
//# sourceMappingURL=instance.js.map