node-gamedig/protocols/mafia2mp.js
Pedro Ivo Hudson a7c3b5474c
feat: Add version as a top level field (#532)
* add top level version on existing entries

* start adding version on new protocols WIP

* add version to more games

* more games with version

* add more games

* more version

* even more games with version

* add 'delete state.raw.version'

* fix delete version

* Update CHANGELOG.md

* add version in Results.js

* more games

* add new game

* more games

* add version on README

* add new game

* other game

* new game

* add unreal2 version

* add ventrilo version

* add eldewrito eldewrito

* add beammp version

* fix starmade version

* add new version in samp protocol

* docs: tweak the changelog line a bit

---------

Co-authored-by: CosminPerRam <cosmin.p@live.com>
2024-02-24 20:46:40 +02:00

42 lines
1.1 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.numplayers = parseInt(this.readString(reader))
state.maxplayers = parseInt(this.readString(reader))
state.raw.gamemode = this.readString(reader)
state.version = state.raw.gamemode
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)
}
}