From ce0deef83bb45dca02727543c1b543dab28e5b47 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 26 Apr 2018 18:33:29 +0200 Subject: [PATCH] Websockets for listening to factorio logs and global chat plugin You can now !shout things across the cluster --- .gitignore | 1 + client.js | 4 ++++ master.js | 16 +++++++++++++++- sharedPlugins/globalChat/config.js | 10 ++++++++++ sharedPlugins/globalChat/index.js | 24 ++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 sharedPlugins/globalChat/config.js create mode 100644 sharedPlugins/globalChat/index.js diff --git a/.gitignore b/.gitignore index 3949e538..0270b57f 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ sharedPlugins/* !sharedPlugins/remoteMap/ !sharedPlugins/remoteCommands/ !sharedPlugins/researchSync/ +!sharedPlugins/globalChat/ diff --git a/client.js b/client.js index b4b3d8ec..90d0b351 100644 --- a/client.js +++ b/client.js @@ -469,6 +469,10 @@ function instanceManagement() { }, { // extra functions to pass in object. Should have done it like this from the start, but won't break backwards compat. socket, // socket.io connection to master (and ES6 destructuring, yay) }); + if(plugins[I].factorioOutput && typeof plugins[I].factorioOutput === "function"){ + // when factorio logs a line, send it to the plugin. This includes things like autosaves, chat, errors etc + serverprocess.stdout.on("data", data => plugins[I].factorioOutput(data.toString())); + } if(pluginConfig.scriptOutputFileSubscription && typeof pluginConfig.scriptOutputFileSubscription == "string"){ if(global.subscribedFiles[pluginConfig.scriptOutputFileSubscription]) { // please choose a unique file to subscribe to. If you need plugins to share this interface, set up a direct communication diff --git a/master.js b/master.js index d57154d3..4c110aeb 100644 --- a/master.js +++ b/master.js @@ -940,6 +940,16 @@ class wsSlave { delete this.commandsWaitingForReturn[resp.commandID]; } }); + + this.socket.on("gameChat", data => { + if(typeof data === "object"){ + data.timestamp = Date.now(); + if(!global.wsChatRecievers) global.wsChatRecievers = []; + global.wsChatRecievers.forEach(socket => { + socket.emit("gameChat", data); + }); + } + }); } runCommand(command, callback){ let commandID = Math.random().toString(); @@ -988,7 +998,11 @@ io.on('connection', function (socket) { console.log("SOCKET | Created new wsSlave: "+ data.instanceID); } }); - + socket.on("registerChatReciever", function(data){ + prometheusWsUsageCounter.labels("registerChatReciever").inc(); + if(!global.wsChatRecievers) global.wsChatRecievers = []; + global.wsChatRecievers.push(socket); + }); }); module.exports = app; diff --git a/sharedPlugins/globalChat/config.js b/sharedPlugins/globalChat/config.js new file mode 100644 index 00000000..ca394648 --- /dev/null +++ b/sharedPlugins/globalChat/config.js @@ -0,0 +1,10 @@ +/* + Clusterio plugin to allow for chat between instances. +*/ +module.exports = { + // Name of package. For display somewhere I guess. + name: "globalChat", + version: "1.0.0", + binary: "nodePackage", + description: "Clusterio plugin to allow for chat between instances.", +} \ No newline at end of file diff --git a/sharedPlugins/globalChat/index.js b/sharedPlugins/globalChat/index.js new file mode 100644 index 00000000..283a6184 --- /dev/null +++ b/sharedPlugins/globalChat/index.js @@ -0,0 +1,24 @@ +module.exports = class remoteCommands { + constructor(mergedConfig, messageInterface, extras){ + this.messageInterface = messageInterface; + this.config = mergedConfig; + this.socket = extras.socket; + + this.socket.emit("registerChatReciever"); + this.socket.on("gameChat", data => { + if(data.instanceID && data.instanceID != this.config.unique.toString()){ + if(data.data.includes("[CHAT]") && data.data.toLowerCase().includes("!shout")){ + let chatMessage = "["+data.instanceID+"]"+ data.data.trim().split(" ").shift().shift().shift().join(" "); + this.messageInterface("/c game.print('"+chatMessage+"')"); + } + } + }); + } + factorioOutput(data){ + // console.log("GOT CHAT!"+data) + this.socket.emit("gameChat", { + instanceID: this.config.unique.toString(), + data, + }); + } +}