node-gamedig/protocols/m2mp.js

40 lines
1 KiB
JavaScript
Raw Normal View History

class M2mp extends require('./core') {
2017-08-09 12:32:09 +02:00
constructor() {
super();
this.encoding = 'latin1';
}
2014-10-29 08:02:03 +01:00
2017-08-09 12:32:09 +02:00
run(state) {
this.udpSend('M2MP',(buffer) => {
const reader = this.reader(buffer);
const header = reader.string({length:4});
2017-08-09 12:32:09 +02:00
if(header !== 'M2MP') return;
state.name = this.readString(reader);
state.raw.numplayers = this.readString(reader);
state.maxplayers = this.readString(reader);
state.raw.gamemode = this.readString(reader);
state.password = !!reader.uint(1);
while(!reader.done()) {
const name = this.readString(reader);
2017-08-09 12:32:09 +02:00
if(!name) break;
state.players.push({
name:name
});
}
this.finish(state);
return true;
});
}
2017-08-09 12:32:09 +02:00
readString(reader) {
const length = reader.uint(1);
return reader.string({length:length-1});
}
}
module.exports = M2mp;