node-gamedig/protocols/terraria.js

37 lines
790 B
JavaScript
Raw Normal View History

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