2017-08-24 09:15:06 +00:00
|
|
|
var events = require('events')
|
|
|
|
var fs = require('fs')
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var Server = require('./server')
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var filePath = 'servers.json'
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2015-10-31 14:27:16 +00:00
|
|
|
var Manager = function (config, logs) {
|
2017-08-24 09:15:06 +00:00
|
|
|
this.config = config
|
|
|
|
this.logs = logs
|
|
|
|
this.serversArr = []
|
|
|
|
this.serversHash = {}
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype = new events.EventEmitter()
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype.addServer = function (options) {
|
|
|
|
var server = this._addServer(options)
|
|
|
|
this.save()
|
|
|
|
return server
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype.removeServer = function (id) {
|
|
|
|
var server = this.serversHash[id]
|
2015-01-21 02:18:45 +00:00
|
|
|
|
|
|
|
if (!server) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return {}
|
2015-01-21 02:18:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var index = this.serversArr.indexOf(server)
|
2015-01-21 02:18:45 +00:00
|
|
|
if (index > -1) {
|
2017-08-24 09:15:06 +00:00
|
|
|
this.serversArr.splice(index, 1)
|
2015-01-21 02:18:45 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
this.save()
|
2015-01-21 02:18:45 +00:00
|
|
|
|
|
|
|
if (server.pid) {
|
2017-08-24 09:15:06 +00:00
|
|
|
server.stop()
|
2015-01-21 02:18:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
return server
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype._addServer = function (data) {
|
|
|
|
var server = new Server(this.config, this.logs, data)
|
|
|
|
this.serversArr.push(server)
|
|
|
|
this.serversArr.sort(function (a, b) {
|
|
|
|
return a.title.localeCompare(b.title)
|
|
|
|
})
|
|
|
|
this.serversHash[server.id] = server
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var self = this
|
2015-01-21 02:18:45 +00:00
|
|
|
var statusChanged = function () {
|
2017-08-24 09:15:06 +00:00
|
|
|
self.emit('servers')
|
|
|
|
}
|
|
|
|
server.on('state', statusChanged)
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
return server
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype.getServer = function (id) {
|
|
|
|
return this.serversHash[id]
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype.getServers = function () {
|
|
|
|
return this.serversArr
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype.load = function () {
|
|
|
|
var self = this
|
2015-01-21 02:18:45 +00:00
|
|
|
|
|
|
|
fs.readFile(filePath, function (err, data) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
console.log('Could not load any existing servers configuration, starting fresh')
|
|
|
|
return
|
2015-01-21 02:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSON.parse(data).forEach(function (server) {
|
2017-08-24 09:15:06 +00:00
|
|
|
self._addServer(server)
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Manager load error: ' + e)
|
2015-01-21 02:18:45 +00:00
|
|
|
}
|
2017-02-20 08:44:07 +00:00
|
|
|
|
|
|
|
self.getServers().map(function (server) {
|
|
|
|
if (server.auto_start) {
|
2017-08-24 09:15:06 +00:00
|
|
|
server.start()
|
2017-02-20 08:44:07 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Manager.prototype.save = function () {
|
|
|
|
var data = []
|
|
|
|
var self = this
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2015-08-15 12:03:46 +00:00
|
|
|
this.serversArr.sort(function (a, b) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return a.title.toLowerCase().localeCompare(b.title.toLowerCase())
|
|
|
|
})
|
2015-08-15 12:03:46 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
this.serversHash = {}
|
2015-01-21 02:18:45 +00:00
|
|
|
this.serversArr.forEach(function (server) {
|
|
|
|
data.push({
|
2019-05-22 14:21:28 +00:00
|
|
|
additionalConfigurationOptions: server.additionalConfigurationOptions,
|
2015-01-21 02:18:45 +00:00
|
|
|
admin_password: server.admin_password,
|
2017-10-01 14:58:30 +00:00
|
|
|
allowed_file_patching: server.allowed_file_patching,
|
2017-02-20 08:44:07 +00:00
|
|
|
auto_start: server.auto_start,
|
2015-01-21 02:18:45 +00:00
|
|
|
battle_eye: server.battle_eye,
|
2017-10-01 14:58:30 +00:00
|
|
|
file_patching: server.file_patching,
|
2017-09-30 19:49:42 +00:00
|
|
|
forcedDifficulty: server.forcedDifficulty,
|
2015-01-21 02:18:45 +00:00
|
|
|
max_players: server.max_players,
|
2015-08-23 22:16:16 +00:00
|
|
|
missions: server.missions,
|
2015-01-21 02:18:45 +00:00
|
|
|
mods: server.mods,
|
2017-09-30 19:49:42 +00:00
|
|
|
motd: server.motd,
|
2017-09-11 09:28:47 +00:00
|
|
|
number_of_headless_clients: server.number_of_headless_clients,
|
2016-11-19 22:31:38 +00:00
|
|
|
parameters: server.parameters,
|
2015-01-21 02:18:45 +00:00
|
|
|
password: server.password,
|
|
|
|
persistent: server.persistent,
|
|
|
|
port: server.port,
|
|
|
|
title: server.title,
|
|
|
|
von: server.von,
|
2017-08-24 09:15:06 +00:00
|
|
|
verify_signatures: server.verify_signatures
|
|
|
|
})
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
self.serversHash[server.id] = server
|
|
|
|
})
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
fs.writeFile(filePath, JSON.stringify(data), function (err) {
|
2015-01-21 02:18:45 +00:00
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
throw err
|
2015-01-21 02:18:45 +00:00
|
|
|
} else {
|
2017-08-24 09:15:06 +00:00
|
|
|
self.emit('servers')
|
2015-01-21 02:18:45 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
module.exports = Manager
|