2014-06-04 22:19:54 +00:00
|
|
|
var events = require('events');
|
2014-11-09 18:07:00 +00:00
|
|
|
var gamedig = require('gamedig');
|
2014-09-12 23:51:31 +00:00
|
|
|
var slug = require('slug');
|
2014-06-04 22:19:54 +00:00
|
|
|
var spawn = require('child_process').spawn;
|
2014-02-13 22:47:51 +00:00
|
|
|
|
2014-09-12 23:51:31 +00:00
|
|
|
var ArmaServer = require('arma-server');
|
|
|
|
|
2015-04-03 10:23:17 +00:00
|
|
|
var config = require('../config.js');
|
|
|
|
|
2015-01-03 02:51:21 +00:00
|
|
|
var queryInterval = 5000;
|
2015-04-25 10:17:24 +00:00
|
|
|
var queryTypes = {
|
|
|
|
arma1: 'arma',
|
|
|
|
arma2: 'arma2',
|
|
|
|
arma2oa: 'arma2',
|
|
|
|
arma3: 'arma3',
|
|
|
|
cwa: 'operationflashpoint',
|
|
|
|
ofp: 'operationflashpoint',
|
|
|
|
ofpresistance: 'operationflashpoint',
|
|
|
|
};
|
2014-02-13 22:47:51 +00:00
|
|
|
|
2015-08-15 11:56:43 +00:00
|
|
|
var createServerTitle = function(title) {
|
|
|
|
if (config.prefix) {
|
|
|
|
title = config.prefix + title;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.suffix) {
|
|
|
|
title = title + config.suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return title;
|
|
|
|
};
|
|
|
|
|
2015-01-21 02:18:45 +00:00
|
|
|
var Server = function (path, type, options) {
|
|
|
|
this.path = path;
|
|
|
|
this.type = type;
|
2014-09-12 23:51:31 +00:00
|
|
|
this.update(options);
|
2014-06-04 22:19:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Server.prototype = new events.EventEmitter();
|
2014-02-13 22:47:51 +00:00
|
|
|
|
2014-09-12 23:51:31 +00:00
|
|
|
Server.prototype.update = function (options) {
|
|
|
|
this.admin_password = options.admin_password;
|
|
|
|
this.battle_eye = options.battle_eye;
|
2015-02-13 16:13:55 +00:00
|
|
|
this.headless = options.headless;
|
2014-09-12 23:51:31 +00:00
|
|
|
this.max_players = options.max_players;
|
2015-08-23 22:16:16 +00:00
|
|
|
this.missions = options.missions;
|
2015-01-16 22:09:53 +00:00
|
|
|
this.mods = options.mods || [];
|
2014-09-12 23:51:31 +00:00
|
|
|
this.password = options.password;
|
|
|
|
this.persistent = options.persistent;
|
2015-01-16 22:09:53 +00:00
|
|
|
this.port = options.port || 2302;
|
2014-09-12 23:51:31 +00:00
|
|
|
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-06-04 22:19:54 +00:00
|
|
|
};
|
2014-02-13 22:47:51 +00:00
|
|
|
|
2014-11-09 18:07:00 +00:00
|
|
|
Server.prototype.queryStatus = function() {
|
|
|
|
var self = this;
|
|
|
|
Gamedig.query(
|
2015-04-25 10:17:48 +00:00
|
|
|
{
|
|
|
|
type: queryTypes[config.game],
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: self.port,
|
|
|
|
},
|
|
|
|
function(state) {
|
|
|
|
if(state.error) {
|
|
|
|
self.state = null;
|
|
|
|
} else {
|
|
|
|
self.state = state;
|
|
|
|
}
|
2015-01-21 02:18:45 +00:00
|
|
|
|
2015-04-25 10:17:48 +00:00
|
|
|
self.emit('state');
|
|
|
|
}
|
|
|
|
);
|
2014-11-09 18:07:00 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 22:47:51 +00:00
|
|
|
Server.prototype.start = function() {
|
2015-02-13 16:13:55 +00:00
|
|
|
var server = new ArmaServer.Server({
|
2014-09-12 23:51:31 +00:00
|
|
|
battleEye: this.battle_eye ? 1 : 0,
|
|
|
|
config: this.id,
|
2015-01-16 21:50:31 +00:00
|
|
|
disableVoN: this.von ? 0 : 1,
|
2015-04-03 10:23:17 +00:00
|
|
|
game: config.game,
|
2015-02-13 16:13:55 +00:00
|
|
|
headlessClients: this.headless ? ["127.0.0.1"] : null,
|
2015-08-15 11:56:43 +00:00
|
|
|
hostname: createServerTitle(this.title),
|
2015-02-13 16:13:55 +00:00
|
|
|
localClient: this.headless ? ["127.0.0.1"] : null,
|
2015-08-23 22:16:16 +00:00
|
|
|
missions: this.missions,
|
2014-09-12 23:51:31 +00:00
|
|
|
mods: this.mods,
|
|
|
|
password: this.password,
|
|
|
|
passwordAdmin: this.admin_password,
|
2015-01-21 02:18:45 +00:00
|
|
|
path: this.path,
|
2014-09-12 23:51:31 +00:00
|
|
|
persistent: this.persistent ? 1 : 0,
|
2015-01-21 02:18:45 +00:00
|
|
|
platform: this.type,
|
2014-09-12 23:51:31 +00:00
|
|
|
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) {
|
2015-02-13 16:13:55 +00:00
|
|
|
console.log(self.id + ': ' + data);
|
2014-02-13 22:47:51 +00:00
|
|
|
});
|
|
|
|
|
2014-11-24 00:56:54 +00:00
|
|
|
instance.stderr.on('data', function (data) {
|
2015-02-13 16:13:55 +00:00
|
|
|
console.log(self.id + ' err: ' + data);
|
2014-02-13 22:47:51 +00:00
|
|
|
});
|
|
|
|
|
2014-11-24 00:56:54 +00:00
|
|
|
instance.on('close', function (code) {
|
2015-02-13 16:13:55 +00:00
|
|
|
console.log(self.id + ' exited with code ' + code);
|
2014-11-09 18:07:00 +00:00
|
|
|
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;
|
2014-11-09 18:07:00 +00:00
|
|
|
|
|
|
|
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;
|
2014-11-09 18:07:00 +00:00
|
|
|
this.queryStatusInterval = setInterval(function () {
|
|
|
|
self.queryStatus();
|
|
|
|
}, queryInterval);
|
2014-04-06 22:58:17 +00:00
|
|
|
|
2015-02-13 16:13:55 +00:00
|
|
|
if (this.headless) {
|
|
|
|
var headless = new ArmaServer.Headless({
|
2015-04-03 10:23:17 +00:00
|
|
|
game: config.game,
|
2015-02-13 16:13:55 +00:00
|
|
|
host: "127.0.0.1",
|
|
|
|
mods: this.mods,
|
|
|
|
password: this.password,
|
|
|
|
path: this.path,
|
|
|
|
platform: this.type,
|
|
|
|
port: this.port,
|
|
|
|
});
|
|
|
|
var headlessInstance = headless.start();
|
|
|
|
|
|
|
|
headlessInstance.stdout.on('data', function (data) {
|
|
|
|
console.log(self.id + ' HC: ' + data);
|
|
|
|
});
|
|
|
|
|
|
|
|
headlessInstance.stderr.on('data', function (data) {
|
|
|
|
console.log(self.id + ' HC err: ' + data);
|
|
|
|
});
|
|
|
|
|
|
|
|
headlessInstance.on('close', function (code) {
|
|
|
|
console.log(self.id + ' HC exited with code ' + code);
|
|
|
|
self.headlessInstance = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
self.headlessInstance = headlessInstance;
|
|
|
|
}
|
|
|
|
|
2014-11-09 18:07:00 +00:00
|
|
|
this.emit('state');
|
2014-06-04 22:19:54 +00:00
|
|
|
|
2014-04-06 22:58:17 +00:00
|
|
|
return this;
|
2014-06-04 22:19:54 +00:00
|
|
|
};
|
2014-04-06 22:58:17 +00:00
|
|
|
|
|
|
|
Server.prototype.stop = function(cb) {
|
|
|
|
var handled = false;
|
2014-06-04 22:19:54 +00:00
|
|
|
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;
|
2014-06-04 23:02:09 +00:00
|
|
|
|
|
|
|
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();
|
2015-02-13 16:13:55 +00:00
|
|
|
if (this.headlessInstance) {
|
|
|
|
this.headlessInstance.kill();
|
|
|
|
}
|
2014-04-06 22:58:17 +00:00
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
if (!handled) {
|
|
|
|
handled = true;
|
2014-06-04 23:02:09 +00:00
|
|
|
|
|
|
|
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 {
|
2014-09-12 23:51:31 +00:00
|
|
|
admin_password: this.admin_password,
|
|
|
|
battle_eye: this.battle_eye,
|
2015-02-13 16:13:55 +00:00
|
|
|
headless: this.headless,
|
2014-06-04 22:17:18 +00:00
|
|
|
id: this.id,
|
2014-09-12 23:51:31 +00:00
|
|
|
max_players: this.max_players,
|
2015-08-23 22:16:16 +00:00
|
|
|
missions: this.missions,
|
2014-06-04 22:17:18 +00:00
|
|
|
mods: this.mods,
|
2014-09-12 23:51:31 +00:00
|
|
|
password: this.password,
|
|
|
|
persistent: this.persistent,
|
2014-06-04 22:17:18 +00:00
|
|
|
pid: this.pid,
|
2014-09-12 23:51:31 +00:00
|
|
|
port: this.port,
|
2014-11-09 18:07:00 +00:00
|
|
|
state: this.state,
|
2014-09-12 23:51:31 +00:00
|
|
|
title: this.title,
|
|
|
|
von: this.von,
|
2014-06-04 22:17:18 +00:00
|
|
|
};
|
|
|
|
};
|
2014-02-13 22:47:51 +00:00
|
|
|
|
2015-01-21 02:18:45 +00:00
|
|
|
module.exports = Server;
|