arma-server-web-admin/manager.js

240 lines
4.6 KiB
JavaScript
Raw Normal View History

var events = require('events');
var fs = require('fs');
var 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
var Server = function (id, title, port, mods) {
2014-02-13 22:47:51 +00:00
this.id = id;
this.title = title;
this.port = port;
this.mods = mods;
};
Server.prototype = new events.EventEmitter();
2014-02-13 22:47:51 +00:00
Server.prototype.armaServerPath = function() {
2014-04-06 23:16:08 +00:00
if (config.type === "linux") {
return config.path + '/arma3server';
}
return config.path + '/arma3server.exe';
};
2014-02-13 22:47:51 +00:00
Server.prototype.makeModsParameter = function() {
2014-06-29 11:13:13 +00:00
var mods = this.mods;
["@a3mp", "@a3mp_ap", "@agm"].forEach(function (modToMoveLast) {
if (mods.indexOf(modToMoveLast) > -1) {
mods = mods.filter(function (mod) {
return mod != modToMoveLast;
});
mods.push(modToMoveLast);
}
});
return '-mod=' + mods.join(';');
};
2014-02-13 22:47:51 +00:00
Server.prototype.makePortParameter = function() {
return '-port=' + this.port;
};
2014-02-13 22:47:51 +00:00
Server.prototype.start = function() {
2014-04-06 23:16:08 +00:00
var startParams = [];
var gamePath = this.armaServerPath();
if (config.type === "wine") {
gamePath = "wine";
startParams.push(this.armaServerPath());
}
startParams.push(this.makePortParameter());
startParams.push('-config=server.cfg');
startParams.push('-noSound');
startParams.push('-world=empty');
if (this.mods.length) {
startParams.push(this.makeModsParameter());
}
console.log(gamePath);
console.log(startParams);
2014-02-13 22:47:51 +00:00
2014-04-06 23:16:08 +00:00
var process = spawn(gamePath, startParams);
2014-04-06 22:58:17 +00:00
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;
this.emit('started');
2014-04-06 22:58:17 +00:00
return this;
};
2014-04-06 22:58:17 +00:00
Server.prototype.stop = function(cb) {
var handled = false;
var self = this;
2014-04-06 22:58:17 +00:00
this.process.on('close', function (code) {
if (!handled) {
handled = true;
self.emit('stopped');
if (cb) {
cb();
}
2014-04-06 22:58:17 +00:00
}
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;
self.emit('stopped');
if (cb) {
cb();
}
2014-04-06 22:58:17 +00:00
}
}, 5000);
return this;
2014-06-04 22:17:18 +00:00
};
Server.prototype.toJSON = function () {
return {
id: this.id,
title: this.title,
port: this.port,
mods: this.mods,
pid: this.pid,
};
};
2014-02-13 22:47:51 +00:00
var Manager = function () {
2014-04-06 21:09:52 +00:00
this.serversArr = [];
2014-04-06 22:10:42 +00:00
this.serversHash = {};
2014-02-13 22:47:51 +00:00
};
Manager.prototype = new events.EventEmitter();
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;
2014-04-06 23:33:01 +00:00
var server = this._addServer(id, title, port, mods);
2014-04-06 22:10:42 +00:00
this.save();
return server;
});
Manager.prototype.removeServer = (function (id) {
var server = this.serversHash[id];
if (!server) {
return {};
}
var index = this.serversArr.indexOf(server);
if (index > -1) {
this.serversArr.splice(index, 1);
}
delete this.serversHash[id];
this.save();
if (server.pid) {
server.stop();
}
return server;
});
2014-04-06 23:11:49 +00:00
Manager.prototype._addServer = (function (id, title, port, mods) {
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-07-28 17:50:06 +00:00
this.serversArr.sort(function(a, b) {
return a.title.localeCompare(b.title);
});
var self = this;
var statusChanged = function () {
self.emit('servers');
};
server.on('started', statusChanged);
server.on('stopped', statusChanged);
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;
}
2014-06-04 21:24:48 +00:00
try {
JSON.parse(data).forEach(function (server) {
self._addServer(server.id, server.title, server.port, server.mods);
});
} catch(e) {
console.error("Manager load error: " + e);
}
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,
});
2014-04-06 22:58:17 +00:00
});
var self = this;
2014-04-06 22:58:17 +00:00
fs.writeFile(filePath, JSON.stringify(data), function(err) {
if (err) {
throw err;
} else {
self.emit('servers');
}
2014-04-06 22:10:42 +00:00
});
});
2014-06-04 22:19:29 +00:00
var manager = new Manager();
manager.load();
module.exports = manager;