node-gamedig/protocols/quake2.js

66 lines
1.7 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 = 'Quake 2';
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(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();
if(header != '\xff\xff\xff\xff'+this.responseHeader) return;
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.raw[key] = value;
2013-07-10 12:02:48 +02:00
}
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
(ping == 0 ? state.bots : state.players).push({
2013-07-10 12:02:48 +02:00
frags:frags, ping:ping, name:name, address:address
});
}
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;
});
}
});