From 1fede265648ffcd6260e6f07b6bf42ef9cf8a4d3 Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 1 May 2018 12:22:19 +0200 Subject: [PATCH] Shout displays instance name now --- sharedPlugins/globalChat/index.js | 41 +++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/sharedPlugins/globalChat/index.js b/sharedPlugins/globalChat/index.js index cd77f600..44d480f7 100644 --- a/sharedPlugins/globalChat/index.js +++ b/sharedPlugins/globalChat/index.js @@ -3,24 +3,45 @@ module.exports = class remoteCommands { this.messageInterface = messageInterface; this.config = mergedConfig; this.socket = extras.socket; - + this.instances = {}; 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 message = data.data.trim().split(" "); - message.shift(); - message.shift(); - message.shift(); - let chatMessage = "["+data.instanceID+"]"+ message.join(" "); - chatMessage = chatMessage.replace(/[^a-zA-Z0-9]+/g, " "); - this.messageInterface("/silent-command game.print('"+chatMessage.replace("!shout ","")+"')"); + if(data.data.includes("[CHAT]") && (data.data.toLowerCase().includes("/shout") || data.data.toLowerCase().includes("!shout"))){ + let words = data.data.trim().split(" "); + words.shift(); + words.shift(); + words.shift(); + let message = words.join(" ").replace(/[";]/g, " ").replace(/[']/g,"").replace("/shout ", "").replace("!shout", ""); + let instance = this.instances[data.instanceID]; + if (!instance) { + let me = this; //to pass 'this' down to the callback + require('needle').get(this.config.masterIP + '/api/slaves', function (err, response) { + if(err || response.statusCode != 200) { + console.log("Unable to get JSON master/api/slaves, master might be unaccessible"); + } else if (response && response.body) { + if(Buffer.isBuffer(response.body)) {console.log(response.body.toString("utf-8")); throw new Error();} + try { + for (let index in response.body) + me.instances[index] = response.body[index]["instanceName"]; + } catch (e){ + console.log(e); + return null; + } + instance = me.instances[data.instanceID] + if (!instance) + instance = data.instanceID; //somehow the master doesn't know the instance + me.messageInterface("/silent-command game.print('[" + instance + "] " + message +"')"); + } + }); + } else { + this.messageInterface("/silent-command game.print('[" + instance + "] " + message + "')"); + } } } }); } factorioOutput(data){ - // console.log("GOT CHAT!"+data) this.socket.emit("gameChat", { instanceID: this.config.unique.toString(), data,