node-gamedig/protocols/m2mp.js

40 lines
1.0 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-02-05 02:58:28 +01:00
const header = reader.string(4);
2019-01-10 13:03:07 +01:00
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) {
2019-02-05 02:58:28 +01:00
return reader.pascalString(1,-1);
2017-08-09 12:32:09 +02:00
}
}
module.exports = M2mp;