node-gamedig/protocols/tshock.js

38 lines
827 B
JavaScript
Raw Normal View History

2013-07-10 17:34:16 +02:00
var request = require('request');
module.exports = require('./core').extend({
init: function() {
this._super();
this.pretty = 'Terraria';
2013-07-10 17:34:16 +02:00
this.options.port = 7878;
},
run: function(state) {
2013-07-10 17:34:16 +02:00
var self = this;
request({
uri: 'http://'+this.options.address+':'+this.options.port+'/status',
timeout: 3000,
}, function(e,r,body) {
if(e) return self.error('HTTP error');
var json;
try {
json = JSON.parse(body);
} catch(e) {
return self.error('Invalid JSON');
}
if(json.status != 200) return self.error('Invalid status');
var split = json.players.split(',');
split.forEach(function(one) {
state.players.push({name:one});
2013-07-10 17:34:16 +02:00
});
state.name = json.name;
state.raw.port = json.port;
state.raw.numplayers = json.playercount;
2013-07-10 17:34:16 +02:00
self.finish(state);
2013-07-10 17:34:16 +02:00
});
}
});