node-gamedig/protocols/quake2.js

63 lines
1.4 KiB
JavaScript
Raw Normal View History

2013-07-10 12:02:48 +02:00
module.exports = require('./core').extend({
init: function() {
this._super();
2013-07-10 12:40:41 +02:00
this.options.port = 27910;
2013-07-10 12:02:48 +02:00
this.encoding = 'latin1';
this.delimiter = '\n';
this.sendHeader = 'status';
this.responseHeader = 'print';
},
run: function() {
var self = this;
this.udpSend('\xff\xff\xff\xff'+this.sendHeader+'\x00',function(buffer) {
var reader = self.reader(buffer);
var header = reader.string();
if(header != '\xff\xff\xff\xff'+this.responseHeader) return;
var state = {};
var info = reader.string().split('\\');
if(info[0] == '') info.shift();
while(true) {
var key = info.shift();
var value = info.shift();
if(typeof value == 'undefined') break;
state[key] = value;
}
state.players = [];
while(!reader.done()) {
var player = reader.string();
2013-07-10 17:47:23 +02:00
var args = [];
2013-07-10 12:02:48 +02:00
var split = player.split('"');
2013-07-10 17:47:23 +02:00
var inQuote = false;
2013-07-10 17:52:15 +02:00
split.forEach(function(part,i) {
2013-07-10 17:47:23 +02:00
var inQuote = (i%2 == 1);
2013-07-10 17:52:15 +02:00
if(inQuote) {
args.push(part);
} else {
var splitSpace = part.split(' ');
splitSpace.forEach(function(subpart) {
if(subpart) args.push(subpart);
});
}
});
2013-07-10 17:47:23 +02:00
var frags = parseInt(args[0]);
var ping = parseInt(args[1]);
var name = args[2] || '';
var address = args[3] || '';
2013-07-10 12:02:48 +02:00
state.players.push({
frags:frags, ping:ping, name:name, address:address
});
}
self.finish(state);
return true;
});
}
});