node-gamedig/protocols/armagetron.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

2013-07-10 12:02:48 +02:00
module.exports = require('./core').extend({
init: function() {
this._super();
this.pretty = 'Armagetron';
2013-07-10 12:02:48 +02:00
this.encoding = 'latin1';
this.byteorder = 'be';
2013-07-10 12:40:41 +02:00
this.options.port = 4534;
2013-07-10 12:02:48 +02:00
},
run: function(state) {
2013-07-10 12:02:48 +02:00
var self = this;
var b = new Buffer([0,0x35,0,0,0,0,0,0x11]);
this.udpSend(b,function(buffer) {
var reader = self.reader(buffer);
reader.skip(6);
state.raw.port = self.readUInt(reader);
state.raw.hostname = self.readString(reader,buffer);
2013-07-10 12:02:48 +02:00
state.name = self.readString(reader,buffer);
state.raw.numplayers = self.readUInt(reader);
state.raw.versionmin = self.readUInt(reader);
state.raw.versionmax = self.readUInt(reader);
state.raw.version = self.readString(reader,buffer);
2013-07-10 12:02:48 +02:00
state.maxplayers = self.readUInt(reader);
var players = self.readString(reader,buffer);
var list = players.split('\n');
for(var i = 0; i < list.length; i++) {
if(!list[i]) continue;
state.players.push({name:list[i]});
}
state.raw.options = self.readString(reader,buffer);
state.raw.uri = self.readString(reader,buffer);
state.raw.globalids = self.readString(reader,buffer);
2013-07-10 12:02:48 +02:00
self.finish(state);
return true;
});
},
readUInt: function(reader) {
var a = reader.uint(2);
var b = reader.uint(2);
return (b<<16) + a;
},
readString: function(reader,b) {
var len = reader.uint(2);
if(!len) return '';
var out = '';
for(var i = 0; i < len; i+=2) {
var hi = reader.uint(1);
var lo = reader.uint(1);
if(i+1<len) out += String.fromCharCode(lo);
if(i+2<len) out += String.fromCharCode(hi);
}
out = out.replace(/0x[0-9a-f]{6}/g,''); // strip color codes
return out;
}
});