simplify server label rendering

This commit is contained in:
Nick Krecklow 2020-05-11 19:23:37 -05:00
parent bbdbe7e599
commit 80c565a6ed
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94

@ -194,76 +194,68 @@ export class ServerRegistration {
document.getElementById('ranking_' + this.serverId).innerText = '#' + (rankIndex + 1) document.getElementById('ranking_' + this.serverId).innerText = '#' + (rankIndex + 1)
} }
updateServerPeak (data) { _renderValue (prefix, handler) {
const peakLabelElement = document.getElementById('peak_' + this.serverId) const labelElement = document.getElementById(prefix + '_' + this.serverId)
// Always set label once any peak data has been received labelElement.style.display = 'block'
peakLabelElement.style.display = 'block'
const peakValueElement = document.getElementById('peak-value_' + this.serverId) const valueElement = document.getElementById(prefix + '-value_' + this.serverId)
const targetElement = valueElement || labelElement
peakValueElement.innerText = formatNumber(data.playerCount) if (targetElement) {
peakLabelElement.title = 'At ' + formatTimestampSeconds(data.timestamp) if (typeof handler === 'function') {
handler(targetElement)
} else {
targetElement.innerText = handler
}
}
}
this.lastPeakData = data _hideValue (prefix) {
const element = document.getElementById(prefix + '_' + this.serverId)
element.style.display = 'none'
} }
updateServerStatus (ping, minecraftVersions) { updateServerStatus (ping, minecraftVersions) {
if (ping.versions) { if (ping.versions) {
const versionsElement = document.getElementById('version_' + this.serverId) this._renderValue('version', formatMinecraftVersions(ping.versions, minecraftVersions[this.data.type]) || '')
versionsElement.style.display = 'block'
versionsElement.innerText = formatMinecraftVersions(ping.versions, minecraftVersions[this.data.type]) || ''
} }
if (ping.recordData) { if (ping.recordData) {
// Always set label once any record data has been received this._renderValue('record', (element) => {
const recordLabelElement = document.getElementById('record_' + this.serverId) if (ping.recordData.timestamp > 0) {
element.innerText = formatNumber(ping.recordData.playerCount) + ' (' + formatDate(ping.recordData.timestamp) + ')'
recordLabelElement.style.display = 'block' element.title = 'At ' + formatDate(ping.recordData.timestamp) + ' ' + formatTimestampSeconds(ping.recordData.timestamp)
const recordValueElement = document.getElementById('record-value_' + this.serverId)
const recordData = ping.recordData
// Safely handle legacy recordData that may not include the timestamp payload
if (recordData.timestamp > 0) {
recordValueElement.innerHTML = formatNumber(recordData.playerCount) + ' (' + formatDate(recordData.timestamp) + ')'
recordLabelElement.title = 'At ' + formatDate(recordData.timestamp) + ' ' + formatTimestampSeconds(recordData.timestamp)
} else { } else {
recordValueElement.innerText = formatNumber(recordData.playerCount) element.innerText = formatNumber(ping.recordData.playerCount)
} }
})
this.lastRecordData = recordData this.lastRecordData = ping.recordData
} }
if (ping.graphPeakData) { if (ping.graphPeakData) {
this.updateServerPeak(ping.graphPeakData) this._renderValue('peak', (element) => {
element.innerText = formatNumber(ping.graphPeakData.playerCount)
element.title = 'At ' + formatTimestampSeconds(ping.graphPeakData.timestamp)
})
this.lastPeakData = ping.graphPeakData
} }
const playerCountLabelElement = document.getElementById('player-count_' + this.serverId)
const errorElement = document.getElementById('error_' + this.serverId)
if (ping.error) { if (ping.error) {
// Hide any visible player-count and show the error element this._hideValue('player-count')
playerCountLabelElement.style.display = 'none' this._renderValue('error', ping.error.message)
errorElement.style.display = 'block'
errorElement.innerText = ping.error.message
} else if (typeof ping.playerCount !== 'number') { } else if (typeof ping.playerCount !== 'number') {
// Hide any visible player-count and show the error element this._hideValue('player-count')
playerCountLabelElement.style.display = 'none'
errorElement.style.display = 'block'
// If the frontend has freshly connection, and the server's last ping was in error, it may not contain an error object // If the frontend has freshly connection, and the server's last ping was in error, it may not contain an error object
// In this case playerCount will safely be null, so provide a generic error message instead // In this case playerCount will safely be null, so provide a generic error message instead
errorElement.innerText = 'Failed to ping' this._renderValue('error', 'Failed to ping')
} else if (typeof ping.playerCount === 'number') { } else if (typeof ping.playerCount === 'number') {
// Ensure the player-count element is visible and hide the error element this._hideValue('error')
playerCountLabelElement.style.display = 'block' this._renderValue('player-count', formatNumber(ping.playerCount))
errorElement.style.display = 'none'
document.getElementById('player-count-value_' + this.serverId).innerText = formatNumber(ping.playerCount)
} }
// An updated favicon has been sent, update the src // An updated favicon has been sent, update the src