2020-04-21 17:59:53 -05:00
|
|
|
const Database = require('./database')
|
|
|
|
const MojangUpdater = require('./mojang')
|
|
|
|
const PingController = require('./ping')
|
|
|
|
const Server = require('./server')
|
2020-05-11 18:44:29 -05:00
|
|
|
const { TimeTracker } = require('./time')
|
2020-05-05 16:49:01 -05:00
|
|
|
const MessageOf = require('./message')
|
2020-04-21 17:59:53 -05:00
|
|
|
|
|
|
|
const config = require('../config')
|
|
|
|
const minecraftVersions = require('../minecraft_versions')
|
|
|
|
|
|
|
|
class App {
|
|
|
|
serverRegistrations = []
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
this.mojangUpdater = new MojangUpdater(this)
|
|
|
|
this.pingController = new PingController(this)
|
2020-05-08 04:47:10 -05:00
|
|
|
this.server = new Server(this)
|
2020-05-08 01:54:04 -05:00
|
|
|
this.timeTracker = new TimeTracker(this)
|
2020-04-21 17:59:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
loadDatabase (callback) {
|
|
|
|
this.database = new Database(this)
|
|
|
|
|
|
|
|
// Setup database instance
|
|
|
|
this.database.ensureIndexes()
|
|
|
|
|
2020-05-08 02:06:39 -05:00
|
|
|
this.database.loadGraphPoints(config.graphDuration, () => {
|
2020-04-21 17:59:53 -05:00
|
|
|
this.database.loadRecords(callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReady () {
|
|
|
|
this.server.listen(config.site.ip, config.site.port)
|
|
|
|
|
|
|
|
// Allow individual modules to manage their own task scheduling
|
|
|
|
this.mojangUpdater.schedule()
|
|
|
|
this.pingController.schedule()
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClientConnection = (client) => {
|
|
|
|
if (config.logToDatabase) {
|
2020-05-05 16:49:01 -05:00
|
|
|
client.on('message', (message) => {
|
|
|
|
if (message === 'requestHistoryGraph') {
|
2020-05-11 04:12:46 -05:00
|
|
|
// Send historical graphData built from all serverRegistrations
|
2020-05-11 18:12:29 -05:00
|
|
|
const graphData = this.serverRegistrations.map(serverRegistration => serverRegistration.graphData)
|
2020-05-05 16:49:01 -05:00
|
|
|
|
|
|
|
// Send graphData in object wrapper to avoid needing to explicity filter
|
|
|
|
// any header data being appended by #MessageOf since the graph data is fed
|
2020-05-11 02:28:41 -05:00
|
|
|
// directly into the graphing system
|
2020-05-05 16:49:01 -05:00
|
|
|
client.send(MessageOf('historyGraph', {
|
2020-05-11 18:29:26 -05:00
|
|
|
timestamps: this.timeTracker.getGraphPoints(),
|
2020-05-11 04:12:46 -05:00
|
|
|
graphData
|
2020-05-05 16:49:01 -05:00
|
|
|
}))
|
|
|
|
}
|
2020-04-21 17:59:53 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:49:01 -05:00
|
|
|
const initMessage = {
|
|
|
|
config: (() => {
|
|
|
|
// Remap minecraftVersion entries into name values
|
|
|
|
const minecraftVersionNames = {}
|
|
|
|
Object.keys(minecraftVersions).forEach(function (key) {
|
|
|
|
minecraftVersionNames[key] = minecraftVersions[key].map(version => version.name)
|
|
|
|
})
|
2020-04-21 17:59:53 -05:00
|
|
|
|
2020-05-05 16:49:01 -05:00
|
|
|
// Send configuration data for rendering the page
|
|
|
|
return {
|
2020-05-08 01:54:04 -05:00
|
|
|
graphDurationLabel: config.graphDurationLabel || (Math.floor(config.graphDuration / (60 * 60 * 1000)) + 'h'),
|
2020-05-08 02:56:39 -05:00
|
|
|
graphMaxLength: TimeTracker.getMaxGraphDataLength(),
|
|
|
|
serverGraphMaxLength: TimeTracker.getMaxServerGraphDataLength(),
|
2020-05-08 16:22:24 -05:00
|
|
|
servers: this.serverRegistrations.map(serverRegistration => serverRegistration.getPublicData()),
|
2020-05-05 16:49:01 -05:00
|
|
|
minecraftVersions: minecraftVersionNames,
|
|
|
|
isGraphVisible: config.logToDatabase
|
|
|
|
}
|
|
|
|
})(),
|
|
|
|
mojangServices: this.mojangUpdater.getLastUpdate(),
|
2020-05-11 18:29:26 -05:00
|
|
|
timestampPoints: this.timeTracker.getServerGraphPoints(),
|
2020-05-05 16:49:01 -05:00
|
|
|
servers: this.serverRegistrations.map(serverRegistration => serverRegistration.getPingHistory())
|
|
|
|
}
|
2020-04-21 17:59:53 -05:00
|
|
|
|
2020-05-05 16:49:01 -05:00
|
|
|
client.send(MessageOf('init', initMessage))
|
2020-04-21 17:59:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = App
|