arma-server-web-admin/lib/server.js

148 lines
3.0 KiB
JavaScript
Raw Normal View History

var events = require('events');
var gamedig = require('gamedig');
var slug = require('slug');
var spawn = require('child_process').spawn;
2014-02-13 22:47:51 +00:00
var ArmaServer = require('arma-server');
2015-01-03 02:51:21 +00:00
var queryInterval = 5000;
2014-02-13 22:47:51 +00:00
var Server = function (path, type, options) {
this.path = path;
this.type = type;
this.update(options);
};
Server.prototype = new events.EventEmitter();
2014-02-13 22:47:51 +00:00
Server.prototype.update = function (options) {
this.admin_password = options.admin_password;
this.battle_eye = options.battle_eye;
this.max_players = options.max_players;
this.mods = options.mods || [];
this.password = options.password;
this.persistent = options.persistent;
this.port = options.port || 2302;
this.title = options.title;
this.von = options.von;
this.id = slug(this.title).replace('.', '-');
2015-01-17 02:40:22 +00:00
this.port = parseInt(this.port, 10); // If port is a string then gamedig fails
};
2014-02-13 22:47:51 +00:00
Server.prototype.queryStatus = function() {
var self = this;
Gamedig.query(
{
type: 'arma3',
host: '127.0.0.1',
port: self.port,
},
function(state) {
if(state.error) {
self.state = null;
} else {
self.state = state;
}
self.emit('state');
}
);
};
2014-02-13 22:47:51 +00:00
Server.prototype.start = function() {
var server = new ArmaServer({
battleEye: this.battle_eye ? 1 : 0,
config: this.id,
disableVoN: this.von ? 0 : 1,
hostname: this.title,
mods: this.mods,
password: this.password,
passwordAdmin: this.admin_password,
path: this.path,
persistent: this.persistent ? 1 : 0,
platform: this.type,
players: this.max_players,
port: this.port,
});
server.writeServerConfig();
var instance = server.start();
2014-04-06 22:58:17 +00:00
var self = this;
2014-02-13 22:47:51 +00:00
2014-11-24 00:56:54 +00:00
instance.stdout.on('data', function (data) {
2014-02-13 22:47:51 +00:00
console.log('stdout: ' + data);
});
2014-11-24 00:56:54 +00:00
instance.stderr.on('data', function (data) {
2014-02-13 22:47:51 +00:00
console.log('stderr: ' + data);
});
2014-11-24 00:56:54 +00:00
instance.on('close', function (code) {
2014-02-13 22:47:51 +00:00
console.log('child process exited with code ' + code);
clearInterval(self.queryStatusInterval);
self.state = null;
2014-04-06 22:58:17 +00:00
self.pid = null;
2014-11-24 00:56:54 +00:00
self.instance = null;
self.emit('state');
2014-04-06 22:58:17 +00:00
});
2014-11-24 00:56:54 +00:00
this.pid = instance.pid;
this.instance = instance;
this.queryStatusInterval = setInterval(function () {
self.queryStatus();
}, queryInterval);
2014-04-06 22:58:17 +00:00
this.emit('state');
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
2014-11-24 00:56:54 +00:00
this.instance.on('close', function (code) {
2014-04-06 22:58:17 +00:00
if (!handled) {
handled = true;
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
2014-11-24 00:56:54 +00:00
this.instance.kill();
2014-04-06 22:58:17 +00:00
setTimeout(function() {
if (!handled) {
handled = true;
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 {
admin_password: this.admin_password,
battle_eye: this.battle_eye,
2014-06-04 22:17:18 +00:00
id: this.id,
max_players: this.max_players,
2014-06-04 22:17:18 +00:00
mods: this.mods,
password: this.password,
persistent: this.persistent,
2014-06-04 22:17:18 +00:00
pid: this.pid,
port: this.port,
state: this.state,
title: this.title,
von: this.von,
2014-06-04 22:17:18 +00:00
};
};
2014-02-13 22:47:51 +00:00
module.exports = Server;