2017-08-09 11:05:55 +02:00
|
|
|
class Gamespy2 extends require('./core') {
|
2017-08-09 12:32:09 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.sessionId = 1;
|
|
|
|
this.encoding = 'latin1';
|
|
|
|
this.byteorder = 'be';
|
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
run(state) {
|
|
|
|
const request = Buffer.from([0xfe,0xfd,0x00,0x00,0x00,0x00,0x01,0xff,0xff,0xff]);
|
2017-08-09 11:05:55 +02:00
|
|
|
const packets = [];
|
2017-08-09 12:32:09 +02:00
|
|
|
this.udpSend(request,
|
|
|
|
(buffer) => {
|
|
|
|
if(packets.length && buffer.readUInt8(0) === 0)
|
|
|
|
buffer = buffer.slice(1);
|
|
|
|
packets.push(buffer);
|
|
|
|
},
|
|
|
|
() => {
|
2017-08-09 11:05:55 +02:00
|
|
|
const buffer = Buffer.concat(packets);
|
|
|
|
const reader = this.reader(buffer);
|
|
|
|
const header = reader.uint(1);
|
2017-08-09 12:32:09 +02:00
|
|
|
if(header !== 0) return;
|
2017-08-09 11:05:55 +02:00
|
|
|
const pingId = reader.uint(4);
|
2017-08-09 12:32:09 +02:00
|
|
|
if(pingId !== 1) return;
|
|
|
|
|
|
|
|
while(!reader.done()) {
|
2017-08-09 11:05:55 +02:00
|
|
|
const key = reader.string();
|
|
|
|
const value = reader.string();
|
2017-08-09 12:32:09 +02:00
|
|
|
if(!key) break;
|
|
|
|
state.raw[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if('hostname' in state.raw) state.name = state.raw.hostname;
|
|
|
|
if('mapname' in state.raw) state.map = state.raw.mapname;
|
|
|
|
if(this.trueTest(state.raw.password)) state.password = true;
|
|
|
|
if('maxplayers' in state.raw) state.maxplayers = parseInt(state.raw.maxplayers);
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
state.players = this.readFieldData(reader);
|
|
|
|
state.raw.teams = this.readFieldData(reader);
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
this.finish(state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
readFieldData(reader) {
|
2017-08-09 11:05:55 +02:00
|
|
|
const count = reader.uint(1);
|
2017-08-09 12:32:09 +02:00
|
|
|
// count is unreliable (often it's wrong), so we don't use it.
|
|
|
|
// read until we hit an empty first field string
|
|
|
|
|
|
|
|
if(this.debug) console.log("Reading fields, starting at: "+reader.rest());
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
const fields = [];
|
2017-08-09 12:32:09 +02:00
|
|
|
while(!reader.done()) {
|
2017-08-09 11:05:55 +02:00
|
|
|
let field = reader.string();
|
2017-08-09 12:32:09 +02:00
|
|
|
if(!field) break;
|
|
|
|
if(field.charCodeAt(0) <= 2) field = field.substring(1);
|
|
|
|
fields.push(field);
|
|
|
|
if(this.debug) console.log("field:"+field);
|
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 11:05:55 +02:00
|
|
|
const units = [];
|
2017-08-09 12:32:09 +02:00
|
|
|
outer: while(!reader.done()) {
|
2017-08-09 11:05:55 +02:00
|
|
|
const unit = {};
|
2017-08-09 12:32:09 +02:00
|
|
|
for(let iField = 0; iField < fields.length; iField++) {
|
2017-08-09 11:05:55 +02:00
|
|
|
let key = fields[iField];
|
|
|
|
let value = reader.string();
|
2017-08-09 12:32:09 +02:00
|
|
|
if(!value && iField === 0) break outer;
|
|
|
|
if(this.debug) console.log("value:"+value);
|
|
|
|
if(key === 'player_') key = 'name';
|
|
|
|
else if(key === 'score_') key = 'score';
|
|
|
|
else if(key === 'deaths_') key = 'deaths';
|
|
|
|
else if(key === 'ping_') key = 'ping';
|
|
|
|
else if(key === 'team_') key = 'team';
|
|
|
|
else if(key === 'kills_') key = 'kills';
|
|
|
|
else if(key === 'team_t') key = 'name';
|
|
|
|
else if(key === 'tickets_t') key = 'tickets';
|
|
|
|
|
|
|
|
if(
|
|
|
|
key === 'score' || key === 'deaths'
|
|
|
|
|| key === 'ping' || key === 'team'
|
|
|
|
|| key === 'kills' || key === 'tickets'
|
|
|
|
) {
|
|
|
|
if(value === '') continue;
|
|
|
|
value = parseInt(value);
|
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
unit[key] = value;
|
|
|
|
}
|
|
|
|
units.push(unit);
|
|
|
|
}
|
2014-10-29 08:02:03 +01:00
|
|
|
|
2017-08-09 12:32:09 +02:00
|
|
|
return units;
|
|
|
|
}
|
2017-08-09 11:05:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Gamespy2;
|