node-red-contrib-gamedig/query-game-server.js
2019-02-16 16:08:01 -07:00

42 lines
1.3 KiB
JavaScript

console.log("test");
module.exports = function(RED) {
var Gamedig = require('gamedig');
function queryGameServer(config) {
console.log("CREATED YAY");
console.log(Gamedig);
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function(msg) {
var serverInfo = {
'type': this.nodeConfig.type,
'host': this.nodeConfig.host
};
if(this.nodeConfig.port) {
serverInfo['port'] = this.nodeConfig.port;
}
Gamedig.query(serverInfo)
.then(function(state) {
msg.payload = 'online';
msg.data = state;
const shouldHaltIfState = this.nodeConfig.halt_if && ('online' === this.nodeConfig.halt_if);
if (shouldHaltIfState) {
return null;
}
node.send(msg);
}).catch(function(error) {
msg.payload = 'offline';
msg.data = {
'error': error
};
const shouldHaltIfState = this.nodeConfig.halt_if && ('offline' === this.nodeConfig.halt_if);
if (shouldHaltIfState) {
return null;
}
node.send(msg);
});
});
}
RED.nodes.registerType("query-game-server", queryGameServer);
};