node-gamedig/protocols/mafia2mp.js
CosminPerRam ad9adff06c
Move to ES6 module (#357)
* Redo imports and exports for lib

* Redo imports and exports for bim

* Redo imports and exports for games

* Remove remaining module.exports

* Use export default in lib

* Use export default in protocols

* Fix import in genreadme.js

* Make package module and solve __dirname

* Fix minecraft protocol imports

* Fix imports on games and make binary runnable

* Renamed protocol class exports to lowercase

* Export promises class as default

* Update README.md to use imports instead of require

* Update CHANGELOG to mention the changes.

* Remove Valve unused imports

* Fix iconv import
2023-09-14 23:28:31 +03:00

42 lines
1.2 KiB
JavaScript

import Core from './core.js';
export default class mafia2mp extends Core {
constructor() {
super();
this.encoding = 'latin1';
this.header = 'M2MP';
this.isMafia2Online = false;
}
async run(state) {
const body = await this.udpSend(this.header,(buffer) => {
const reader = this.reader(buffer);
const header = reader.string(this.header.length);
if (header !== this.header) return;
return reader.rest();
});
const reader = this.reader(body);
state.name = this.readString(reader);
state.raw.numplayers = this.readString(reader);
state.maxplayers = parseInt(this.readString(reader));
state.raw.gamemode = this.readString(reader);
state.password = !!reader.uint(1);
state.gamePort = this.options.port - 1;
while(!reader.done()) {
const player = {};
player.name = this.readString(reader);
if(!player.name) break;
if (this.isMafia2Online) {
player.ping = parseInt(this.readString(reader));
}
state.players.push(player);
}
}
readString(reader) {
return reader.pascalString(1,-1);
}
}