arma-server-web-admin/manager.js

137 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-04-06 22:10:42 +00:00
var fs = require('fs'),
spawn = require('child_process').spawn;
2014-02-13 22:47:51 +00:00
var config = require('./config');
2014-04-06 22:10:42 +00:00
var filePath = "servers.json";
2014-02-13 22:47:51 +00:00
function Server(id, title, port, mods) {
this.id = id;
this.title = title;
this.port = port;
this.mods = mods;
}
Server.prototype.armaServerPath = function() {
return config.path + '/arma3server';
}
Server.prototype.makeModsParameter = function() {
return '-mod=' + this.mods.join(';');
}
Server.prototype.makePortParameter = function() {
return '-port=' + this.port;
}
Server.prototype.start = function() {
var mods = this.makeModsParameter();
var port = this.makePortParameter();
2014-04-06 22:58:17 +00:00
var process = spawn(this.armaServerPath(), [mods, port, '-config=server.cfg', '-noSound', '-world=empty']);
var self = this;
2014-02-13 22:47:51 +00:00
2014-04-06 22:58:17 +00:00
process.stdout.on('data', function (data) {
2014-02-13 22:47:51 +00:00
console.log('stdout: ' + data);
});
2014-04-06 22:58:17 +00:00
process.stderr.on('data', function (data) {
2014-02-13 22:47:51 +00:00
console.log('stderr: ' + data);
});
2014-04-06 22:58:17 +00:00
process.on('close', function (code) {
2014-02-13 22:47:51 +00:00
console.log('child process exited with code ' + code);
2014-04-06 22:58:17 +00:00
self.pid = null;
self.process = null;
});
this.pid = process.pid;
this.process = process;
return this;
}
Server.prototype.stop = function(cb) {
var handled = false;
this.process.on('close', function (code) {
if (!handled) {
handled = true;
cb();
}
2014-02-13 22:47:51 +00:00
});
2014-04-06 22:58:17 +00:00
this.process.kill();
setTimeout(function() {
if (!handled) {
handled = true;
cb();
}
}, 5000);
return this;
2014-02-13 22:47:51 +00:00
}
function Manager() {
2014-04-06 21:09:52 +00:00
this.serversArr = [];
2014-04-06 22:10:42 +00:00
this.serversHash = {};
this.load();
2014-02-13 22:47:51 +00:00
};
Manager.prototype.addServer = (function (id, title) {
2014-04-06 23:11:49 +00:00
mods = [];
port = 2302;
var server = this._addServer(id, title, mods, port);
2014-04-06 22:10:42 +00:00
this.save();
return server;
});
2014-04-06 23:11:49 +00:00
Manager.prototype._addServer = (function (id, title, port, mods) {
2014-02-14 01:16:05 +00:00
var server = new Server(id, title, port, mods)
2014-04-06 21:09:52 +00:00
this.serversArr.push(server);
this.serversHash[id] = server;
2014-02-14 01:16:05 +00:00
return server;
2014-02-13 22:47:51 +00:00
});
2014-04-06 21:09:52 +00:00
Manager.prototype.getServer = (function (id) {
return this.serversHash[id];
});
Manager.prototype.getServers = (function () {
return this.serversArr;
});
2014-04-06 22:10:42 +00:00
Manager.prototype.load = (function () {
var self = this;
fs.readFile(filePath, function (err, data) {
if (err) {
console.log(err);
return;
}
JSON.parse(data).forEach(function (server) {
2014-04-06 23:11:49 +00:00
self._addServer(server.id, server.title, server.port, server.mods);
2014-04-06 22:10:42 +00:00
});
});
});
Manager.prototype.save = (function () {
2014-04-06 22:58:17 +00:00
var data = [];
this.serversArr.forEach(function (server) {
data.push({
id: server.id,
title: server.title,
port: server.port,
mods: server.mods,
})
});
fs.writeFile(filePath, JSON.stringify(data), function(err) {
2014-04-06 22:10:42 +00:00
if(err) throw err;
});
});
2014-04-06 21:09:52 +00:00
module.exports = Manager;