node-gamedig/games/terraria.js
2014-02-02 05:58:36 -06:00

40 lines
902 B
JavaScript

var request = require('request');
module.exports = require('./protocols/core').extend({
init: function() {
this._super();
this.pretty = 'Terraria';
this.options.port = 7878;
},
run: function(state) {
var self = this;
request({
uri: 'http://'+this.options.address+':'+this.options.port+'/v2/server/status',
timeout: 3000,
qs: {
players: 'true',
token: this.options.token
}
}, function(e,r,body) {
if(e) return self.fatal('HTTP error');
var json;
try {
json = JSON.parse(body);
} catch(e) {
return self.fatal('Invalid JSON');
}
if(json.status != 200) return self.fatal('Invalid status');
json.players.forEach(function(one) {
state.players.push({name:one.nickname,team:one.team});
});
state.name = json.name;
state.raw.port = json.port;
state.raw.numplayers = json.playercount;
self.finish(state);
});
}
});