node-gamedig/protocols/terraria.js

38 lines
1 KiB
JavaScript
Raw Normal View History

const request = require('request'),
Core = require('./core');
2014-10-29 08:02:03 +01:00
class Terraria extends Core {
2017-08-09 12:32:09 +02:00
run(state) {
request({
uri: 'http://'+this.options.address+':'+this.options.port_query+'/v2/server/status',
2018-02-13 10:24:40 +01:00
timeout: this.options.socketTimeout,
2017-08-09 12:32:09 +02:00
qs: {
players: 'true',
token: this.options.token
}
}, (e,r,body) => {
if(e) return this.fatal('HTTP error');
let json;
try {
json = JSON.parse(body);
} catch(e) {
return this.fatal('Invalid JSON');
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
if(json.status !== 200) return this.fatal('Invalid status');
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
for (const one of json.players) {
state.players.push({name:one.nickname,team:one.team});
}
state.name = json.name;
state.raw.port = json.port;
state.raw.numplayers = json.playercount;
this.finish(state);
});
}
}
module.exports = Terraria;