node-gamedig/protocols/quake2.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

class Quake2 extends require('./core') {
constructor() {
super();
2014-10-29 08:02:03 +01:00
this.encoding = 'latin1';
this.delimiter = '\n';
this.sendHeader = 'status';
this.responseHeader = 'print';
this.isQuake1 = false;
}
2014-10-29 08:02:03 +01:00
run(state) {
this.udpSend('\xff\xff\xff\xff'+this.sendHeader+'\x00', (buffer) => {
const reader = this.reader(buffer);
2015-07-19 00:09:36 +02:00
const header = reader.string({length:4});
if(header !== '\xff\xff\xff\xff') return;
2014-10-29 08:02:03 +01:00
this.debugBuffer(buffer);
let response;
2014-10-29 08:02:03 +01:00
if(this.isQuake1) {
response = reader.string({length:this.responseHeader.length});
2014-10-29 08:02:03 +01:00
} else {
response = reader.string();
}
if(response !== this.responseHeader) return;
2014-10-29 08:02:03 +01:00
const info = reader.string().split('\\');
if(info[0] === '') info.shift();
2014-10-29 08:02:03 +01:00
while(true) {
const key = info.shift();
const value = info.shift();
if(typeof value === 'undefined') break;
2014-10-29 08:02:03 +01:00
state.raw[key] = value;
}
while(!reader.done()) {
const line = reader.string();
if(!line || line.charAt(0) === '\0') break;
2014-10-29 08:02:03 +01:00
const args = [];
const split = line.split('"');
split.forEach((part,i) => {
const inQuote = (i%2 === 1);
2014-10-29 08:02:03 +01:00
if(inQuote) {
args.push(part);
} else {
const splitSpace = part.split(' ');
for (const subpart of splitSpace) {
2014-10-29 08:02:03 +01:00
if(subpart) args.push(subpart);
}
2014-10-29 08:02:03 +01:00
}
});
const player = {};
if(this.isQuake1) {
2014-10-29 08:02:03 +01:00
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() || '';
}
(player.ping ? state.players : state.bots).push(player);
}
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;
2015-07-19 00:09:36 +02:00
if('maxclients' in state.raw) state.maxplayers = state.raw.maxclients;
2014-10-29 08:02:03 +01:00
if('sv_hostname' in state.raw) state.name = state.raw.sv_hostname;
if('hostname' in state.raw) state.name = state.raw.hostname;
2014-10-29 08:02:03 +01:00
this.finish(state);
2014-10-29 08:02:03 +01:00
return true;
});
}
}
module.exports = Quake2;