Skip to content

Commit

Permalink
feat: Satisfactory add lightweight query (#652)
Browse files Browse the repository at this point in the history
* feat: add lightweight query

* docs: update CHANGELOG
  • Loading branch information
CosminPerRam authored Oct 13, 2024
1 parent 899a39a commit 5b1da1a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 5.X.Y
* Feat: Replaced `punycode` package usage with `url.domainToASCII` (#630).
* Feat: World of Padman (2007) - Added support (#636)
* Feat: Satisfactory - Added support (By @Smidy13 #645)
* Feat: Satisfactory - Added support (By @Smidy13 #645, #652)
* Feat: Update Soldat protocol (#642)
* Feat: TOXIKK (2016) - Added support (#641)
* Feat: Renegade X (2014) - Added support (#643)
Expand Down
38 changes: 27 additions & 11 deletions protocols/satisfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ export default class satisfactory extends Core {

// Don't use the tcp ping probing
this.usedTcp = true

}

async run (state) {
const packet = Buffer.from([0xD5, 0xF6, 0, 1, 5, 5, 5, 5, 5, 5, 5, 5, 1])
const response = await this.udpSend(packet, packet => {
const reader = this.reader(packet)
const header = reader.part(4)
if (header.equals(Buffer.from([0xD5, 0xF6, 1, 2]))) return
reader.skip(8) // skip the cookie
return reader
})

state.raw.serverState = response.int(1)
state.version = response.int(4).toString()
state.raw.serverFlags = response.int(8)

const subStatesCount = response.int(1)
response.skip(subStatesCount * 3)

const nameLength = response.int(2)
state.name = response.part(nameLength).toString('utf-8')

/**
* To get information about the Satisfactory game server, you need to first obtain a client authenticationToken.
Expand All @@ -27,7 +44,7 @@ export default class satisfactory extends Core {
function: 'QueryServerState'
}

let headers = {
const headers = {
'Content-Type': 'application/json'
}

Expand All @@ -38,25 +55,24 @@ export default class satisfactory extends Core {
*/
if (!this.options.rejectUnauthorized) this.options.rejectUnauthorized = false

let tokenRequestResponse = await this.queryInfo(tokenRequestJson, headers)
const tokenRequestResponse = await this.queryInfo(tokenRequestJson, headers)

headers.Authorization = `Bearer ${tokenRequestResponse.data.authenticationToken}`

let queryResponse = await this.queryInfo(queryJson, headers)
const { data: queryResponse } = await this.queryInfo(queryJson, {
...headers,
Authorization: `Bearer ${tokenRequestResponse.data.authenticationToken}`
})

/**
* Satisfactory API cannot pull Server Name at the moment, see QA and vote for fix here
* https://questions.satisfactorygame.com/post/66ebebad772a987f4a8b9ef8
*/

state.numplayers = queryResponse.data.serverGameState.numConnectedPlayers
state.maxplayers = queryResponse.data.serverGameState.playerLimit
state.raw = queryResponse

state.numplayers = queryResponse.serverGameState.numConnectedPlayers
state.maxplayers = queryResponse.serverGameState.playerLimit
state.raw.http = queryResponse
}

async queryInfo (json, headers) {

const url = `https://${this.options.host}:${this.options.port}/api/v1/`

this.logger.debug(`POST: ${url}`)
Expand Down

0 comments on commit 5b1da1a

Please sign in to comment.