node-gamedig/protocols/terraria.js

36 lines
801 B
JavaScript
Raw Normal View History

2013-07-10 17:34:16 +02:00
var request = require('request');
module.exports = require('./core').extend({
run: function(state) {
2013-07-10 17:34:16 +02:00
var self = this;
request({
uri: 'http://'+this.options.address+':'+this.options.port_query+'/v2/server/status',
2013-07-10 17:34:16 +02:00
timeout: 3000,
2013-10-12 08:43:16 +02:00
qs: {
players: 'true',
token: this.options.token
}
2013-07-10 17:34:16 +02:00
}, function(e,r,body) {
2014-02-02 12:58:36 +01:00
if(e) return self.fatal('HTTP error');
2013-07-10 17:34:16 +02:00
var json;
try {
json = JSON.parse(body);
} catch(e) {
2014-02-02 12:58:36 +01:00
return self.fatal('Invalid JSON');
2013-07-10 17:34:16 +02:00
}
2014-02-02 12:58:36 +01:00
if(json.status != 200) return self.fatal('Invalid status');
2013-07-10 17:34:16 +02:00
2013-10-12 08:43:16 +02:00
json.players.forEach(function(one) {
state.players.push({name:one.nickname,team:one.team});
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
});
}
});