use serverId in protocol instead of legacy info object

This commit is contained in:
Nick Krecklow
2020-04-29 04:01:10 -05:00
parent 36c3e056c2
commit be92449d52
8 changed files with 59 additions and 93 deletions

View File

@ -2,22 +2,22 @@ const config = require('../config')
const minecraftVersions = require('../minecraft_versions')
class ServerRegistration {
serverId
lastFavicon
versions = []
recordData
graphData = []
constructor (data) {
constructor (serverId, data) {
this.serverId = serverId
this.data = data
this._pingHistory = []
}
getUpdate (timestamp, resp, err, version) {
const update = {
info: {
name: this.data.name,
timestamp: timestamp
}
serverId: this.serverId,
timestamp: timestamp
}
if (resp) {
@ -48,6 +48,14 @@ class ServerRegistration {
update.result = {
players: resp.players
}
if (config.logToDatabase) {
// Update calculated graph peak regardless if the graph is being updated
// This can cause a (harmless) desync between live and stored data, but it allows it to be more accurate for long surviving processes
if (this.findNewGraphPeak()) {
update.graphPeakData = this.getGraphPeak()
}
}
} else if (err) {
// Append a filtered copy of err
// This ensures any unintended data is not leaked
@ -94,15 +102,21 @@ class ServerRegistration {
const lastPing = this._pingHistory[this._pingHistory.length - 1]
const payload = {
info: {
name: this.data.name
},
serverId: this.serverId,
timestamp: lastPing.timestamp,
versions: this.versions,
recordData: this.recordData,
favicon: this.lastFavicon
}
// Only append graphPeakData if defined
// The value is lazy computed and conditional that config->logToDatabase == true
const graphPeakData = this.getGraphPeak()
if (graphPeakData) {
payload.graphPeakData = graphPeakData
}
// Conditionally append to avoid defining fields with undefined values
if (lastPing.result) {
payload.result = lastPing.result
@ -118,14 +132,12 @@ class ServerRegistration {
}
return [{
serverId: this.serverId,
timestamp: new Date().getTime(),
error: {
message: 'Waiting...',
placeholder: true
},
timestamp: new Date().getTime(),
info: {
name: this.data.name
},
recordData: this.recordData
}]
}