feat: add players, settings and metrics queries

This commit is contained in:
CosminPerRam 2024-04-08 19:58:50 +03:00
parent 2d4d0f50db
commit fd648da1b1
1 changed files with 19 additions and 3 deletions

View File

@ -1,16 +1,32 @@
import Core from './core.js'
export default class palworld extends Core {
async run (state) {
const url = `http://${this.options.host}:${this.options.port}/v1/api/info`
async makeCall (endpoint) {
const url = `http://${this.options.host}:${this.options.port}/v1/api/${endpoint}`
const headers = {
Authorization: `Basic ${Buffer.from(`${this.options.username}:${this.options.password}`).toString('base64')}`,
Accept: 'application/json'
}
const serverInfo = await this.request({ url, headers, method: 'GET' })
return await this.request({ url, headers, method: 'GET', responseType: 'json' })
}
async run (state) {
const serverInfo = await this.makeCall('info')
state.version = serverInfo.version
state.name = serverInfo.servername
state.raw.serverInfo = serverInfo
const { players } = await this.makeCall('players')
state.numplayers = players.length
state.players = players.map(p => p.name)
state.raw.players = players
state.raw.settings = await this.makeCall('settings')
const metrics = await this.makeCall('metrics')
state.numplayers = metrics.currentplayernum
state.maxplayers = metrics.maxplayernum
state.raw.metrics = metrics
}
}