mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-18 09:35:50 +00:00
feat(protocols/sdtd): add telnet support for players (#692)
* feat(protocols/sdtd): add telnet support * docs: update CHANGELOG.md
This commit is contained in:
parent
4db8a473ff
commit
71ce074866
9 changed files with 159 additions and 5 deletions
61
protocols/sdtd.js
Normal file
61
protocols/sdtd.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import Valve from './valve.js'
|
||||
import { Players } from '../lib/Results.js'
|
||||
|
||||
const playerLineRegex = /(?<=id=\d+,\s*)(?<name>\S[^,]*)(?=,)/
|
||||
|
||||
const sanitizeTelnetResponse = response => {
|
||||
return response
|
||||
.split(/\r?\n/)
|
||||
.map(l => l.replace(/\r$/, '').trim())
|
||||
.filter(l => l.length > 0)
|
||||
}
|
||||
|
||||
export default class sdtd extends Valve {
|
||||
async run (state) {
|
||||
await super.run(state)
|
||||
await this.telnetCalls(state)
|
||||
}
|
||||
|
||||
async telnetCalls (state) {
|
||||
const telnetPort = this.options.telnetPort
|
||||
const telnetPassword = this.options.telnetPassword
|
||||
|
||||
if (!telnetPort || !telnetPassword) {
|
||||
this.logger.debug('No telnet args given, skipping.')
|
||||
return
|
||||
}
|
||||
|
||||
if (!this.options.requestPlayers) {
|
||||
return
|
||||
}
|
||||
|
||||
await this.telnetConnect({
|
||||
port: telnetPort,
|
||||
password: telnetPassword,
|
||||
passwordPrompt: /Please enter password:/i,
|
||||
shellPrompt: /\r\n$/
|
||||
})
|
||||
|
||||
await this.telnetCallPlayers(state)
|
||||
|
||||
await this.telnetClose()
|
||||
}
|
||||
|
||||
async telnetCallPlayers (state) {
|
||||
const playersResponse = await this.telnetExecute('listplayers')
|
||||
state.players = new Players()
|
||||
for (const possiblePlayerLine of sanitizeTelnetResponse(playersResponse)) {
|
||||
const match = possiblePlayerLine.match(playerLineRegex)
|
||||
|
||||
const name = match?.groups?.name
|
||||
if (name) {
|
||||
state.players.push({
|
||||
name,
|
||||
responseLine: possiblePlayerLine
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
state.raw.telnetPlayersResponse = playersResponse
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue