unify TimeTracker code, explicitly mark nullable playerCounts as unsafe

This commit is contained in:
Nick Krecklow
2020-05-11 18:29:26 -05:00
parent 7136851123
commit 903343fbdf
6 changed files with 62 additions and 44 deletions

View File

@ -3,6 +3,9 @@ const minecraftBedrockPing = require('mcpe-ping-fixed')
const logger = require('./logger')
const MessageOf = require('./message')
const TimeTracker = require('./time')
const { getPlayerCountOrNull } = require('./util')
const config = require('../config')
@ -83,7 +86,7 @@ class PingController {
}
pingAll = () => {
const { timestamp, updateHistoryGraph } = this._app.timeTracker.newPingTimestamp()
const { timestamp, updateHistoryGraph } = this._app.timeTracker.newPointTimestamp()
this.startPingTasks(results => {
const updates = []
@ -94,9 +97,9 @@ class PingController {
// Log to database if enabled
// Use null to represent a failed ping
if (config.logToDatabase) {
const playerCount = result.resp ? result.resp.players.online : null
const unsafePlayerCount = getPlayerCountOrNull(result.resp)
this._app.database.insertPing(serverRegistration.data.ip, timestamp, playerCount)
this._app.database.insertPing(serverRegistration.data.ip, timestamp, unsafePlayerCount)
}
// Generate a combined update payload
@ -110,7 +113,7 @@ class PingController {
// Send object since updates uses serverIds as keys
// Send a single timestamp entry since it is shared
this._app.server.broadcast(MessageOf('updateServers', {
timestamp: Math.floor(timestamp / 1000),
timestamp: TimeTracker.toSeconds(timestamp),
updateHistoryGraph,
updates
}))