node-gamedig/protocols/ase.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

const Core = require('./core');
class Ase extends Core {
2019-01-07 07:52:29 +01:00
async run(state) {
const buffer = await this.udpSend('s',(buffer) => {
2017-08-09 12:32:09 +02:00
const reader = this.reader(buffer);
2019-01-07 07:52:29 +01:00
const header = reader.string({length: 4});
2019-01-12 11:43:36 +01:00
if (header === 'EYE1') return reader.rest();
2017-08-09 12:32:09 +02:00
});
2019-01-07 07:52:29 +01:00
const reader = this.reader(buffer);
state.raw.gamename = this.readString(reader);
2019-01-12 11:43:36 +01:00
state.gamePort = parseInt(this.readString(reader));
2019-01-07 07:52:29 +01:00
state.name = this.readString(reader);
state.raw.gametype = this.readString(reader);
state.map = this.readString(reader);
state.raw.version = this.readString(reader);
state.password = this.readString(reader) === '1';
state.raw.numplayers = parseInt(this.readString(reader));
state.maxplayers = parseInt(this.readString(reader));
while(!reader.done()) {
const key = this.readString(reader);
if(!key) break;
const value = this.readString(reader);
state.raw[key] = value;
}
while(!reader.done()) {
const flags = reader.uint(1);
const player = {};
if(flags & 1) player.name = this.readString(reader);
if(flags & 2) player.team = this.readString(reader);
if(flags & 4) player.skin = this.readString(reader);
if(flags & 8) player.score = parseInt(this.readString(reader));
if(flags & 16) player.ping = parseInt(this.readString(reader));
if(flags & 32) player.time = parseInt(this.readString(reader));
state.players.push(player);
}
2017-08-09 12:32:09 +02:00
}
readString(reader) {
const len = reader.uint(1);
2017-08-09 12:32:09 +02:00
return reader.string({length:len-1});
}
}
module.exports = Ase;