mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-06 15:17:36 +00:00
feat(protocol/sdtd): add optional more data via telnet (#697)
* feat(protocol/sdtd): add optional more data via telnet * docs: update CHANGELOG.md * feat: some refactoring * feat: add hordeDay to raw * docs: mention what extra data it is * docs: formatting
This commit is contained in:
parent
b9767ab99d
commit
a3fca5d590
3 changed files with 54 additions and 4 deletions
|
|
@ -1,7 +1,10 @@
|
|||
import Valve from './valve.js'
|
||||
import { Players } from '../lib/Results.js'
|
||||
|
||||
const playerLineRegex = /(?<=id=\d+,\s*)(?<name>\S[^,]*)(?=,)/
|
||||
const playerRegex = /(?<=id=\d+,\s*)(?<name>\S[^,]*)(?=,)/
|
||||
const gameVersionsRegex = /V \d+\.\d+(?: \(b\d+\))?/g
|
||||
const modRegex = /^Mod\s+([^:]+):\s*([\d.]+)$/
|
||||
const dateTimeRegex = /Day\s+(\d+),\s*(\d{2}:\d{2})/
|
||||
|
||||
const sanitizeTelnetResponse = response => {
|
||||
return response
|
||||
|
|
@ -25,7 +28,7 @@ export default class sdtd extends Valve {
|
|||
return
|
||||
}
|
||||
|
||||
if (!this.options.requestPlayers) {
|
||||
if (!this.options.requestPlayers && !this.options.moreData) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +40,13 @@ export default class sdtd extends Valve {
|
|||
echoLines: 0
|
||||
})
|
||||
|
||||
await this.telnetCallPlayers(state)
|
||||
if (this.options.requestPlayers) {
|
||||
await this.telnetCallPlayers(state)
|
||||
}
|
||||
|
||||
if (this.options.moreData) {
|
||||
await this.telnetMoreData(state)
|
||||
}
|
||||
|
||||
await this.telnetClose()
|
||||
}
|
||||
|
|
@ -46,7 +55,7 @@ export default class sdtd extends Valve {
|
|||
const playersResponse = await this.telnetExecute('listplayers')
|
||||
state.players = new Players()
|
||||
for (const possiblePlayerLine of sanitizeTelnetResponse(playersResponse)) {
|
||||
const match = possiblePlayerLine.match(playerLineRegex)
|
||||
const match = possiblePlayerLine.match(playerRegex)
|
||||
|
||||
const name = match?.groups?.name
|
||||
if (name) {
|
||||
|
|
@ -59,4 +68,42 @@ export default class sdtd extends Valve {
|
|||
|
||||
state.raw.telnetPlayersResponse = playersResponse
|
||||
}
|
||||
|
||||
async telnetMoreData (state) {
|
||||
const gettimeResponse = await this.telnetExecute('gettime')
|
||||
const dateTime = sanitizeTelnetResponse(gettimeResponse)[0] || ''
|
||||
const match = dateTime.match(dateTimeRegex)
|
||||
if (match) {
|
||||
state.raw.day = Number(match[1])
|
||||
state.raw.time = match[2]
|
||||
state.raw.hordeDay = state.raw.day % 7 === 0
|
||||
} else {
|
||||
state.raw.hordeDay = false
|
||||
}
|
||||
|
||||
state.raw.telnetGettimeResponse = gettimeResponse
|
||||
|
||||
const versionResponse = await this.telnetExecute('version')
|
||||
const versions = sanitizeTelnetResponse(versionResponse)
|
||||
const gameVersions = versions[0] || ''
|
||||
const gameVersionsMatch = gameVersions.match(gameVersionsRegex)
|
||||
if (gameVersionsMatch) {
|
||||
state.raw.gameVersion = gameVersionsMatch[0]
|
||||
state.raw.compatibilityVersion = gameVersionsMatch[1]
|
||||
}
|
||||
|
||||
const mods = []
|
||||
for (const possibleMod of versions.slice(1)) {
|
||||
const match = possibleMod.match(modRegex)
|
||||
if (match) {
|
||||
mods.push({
|
||||
name: match[1],
|
||||
version: match[2]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
state.raw.mods = mods
|
||||
state.raw.telnetVersionResponse = versionResponse
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue