node-gamedig/protocols/m2mp.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

const Core = require('./core');
class M2mp extends Core {
2017-08-09 12:32:09 +02:00
constructor() {
super();
this.encoding = 'latin1';
}
2014-10-29 08:02:03 +01:00
2019-01-10 13:03:07 +01:00
async run(state) {
const body = await this.udpSend('M2MP',(buffer) => {
const reader = this.reader(buffer);
2019-01-10 13:03:07 +01:00
const header = reader.string({length: 4});
if (header !== 'M2MP') return;
return reader.rest();
2017-08-09 12:32:09 +02:00
});
2019-01-10 13:03:07 +01:00
const reader = this.reader(body);
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);
2019-01-12 12:45:09 +01:00
state.gamePort = this.options.port - 1;
2019-01-10 13:03:07 +01:00
while(!reader.done()) {
const name = this.readString(reader);
if(!name) break;
state.players.push({
name:name
});
}
2017-08-09 12:32:09 +02:00
}
2017-08-09 12:32:09 +02:00
readString(reader) {
const length = reader.uint(1);
return reader.string({length:length-1});
}
}
module.exports = M2mp;