node-gamedig/protocols/quake2.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-07-10 12:02:48 +02:00
module.exports = require('./core').extend({
init: function() {
this._super();
this.encoding = 'latin1';
this.delimiter = '\n';
this.sendHeader = 'status';
this.responseHeader = 'print';
this.isQuake1 = false;
2013-07-10 12:02:48 +02:00
},
run: function(state) {
2013-07-10 12:02:48 +02:00
var self = this;
this.udpSend('\xff\xff\xff\xff'+this.sendHeader+'\x00',function(buffer) {
var reader = self.reader(buffer);
var header = reader.string({length:4});
if(header != '\xff\xff\xff\xff') return;
var response;
if(this.isQuake1) {
response = reader.string({length:1});
} else {
response = reader.string();
}
if(response != this.responseHeader) return;
2013-07-10 12:02:48 +02:00
var info = reader.string().split('\\');
if(info[0] == '') info.shift();
2013-07-10 12:02:48 +02:00
while(true) {
var key = info.shift();
var value = info.shift();
if(typeof value == 'undefined') break;
state.raw[key] = value;
2013-07-10 12:02:48 +02:00
}
while(!reader.done()) {
var line = reader.string();
if(!line || line.charAt(0) == '\0') break;
2013-07-10 17:47:23 +02:00
var args = [];
var split = line.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 player = {};
if(self.isQuake1) {
player.id = parseInt(args.shift());
player.score = parseInt(args.shift());
player.time = parseInt(args.shift());
player.ping = parseInt(args.shift());
player.name = args.shift();
player.skin = args.shift();
player.color1 = parseInt(args.shift());
player.color2 = parseInt(args.shift());
} else {
player.frags = parseInt(args.shift());
player.ping = parseInt(args.shift());
player.name = args.shift() || '';
player.address = args.shift() || '';
}
2013-07-10 12:02:48 +02:00
(player.ping ? state.players : state.bots).push(player);
2013-07-10 12:02:48 +02:00
}
if('g_needpass' in state.raw) state.password = state.raw.g_needpass;
if('mapname' in state.raw) state.map = state.raw.mapname;
if('sv_maxclients' in state.raw) state.maxplayers = state.raw.sv_maxclients;
if('sv_hostname' in state.raw) state.name = state.raw.sv_hostname;
2013-07-10 12:02:48 +02:00
self.finish(state);
return true;
});
}
});