Lint and format all backend code with standard.js

This commit is contained in:
Björn Dahlgren 2017-08-24 11:15:06 +02:00
parent f919f32bf3
commit 853a79546a
16 changed files with 555 additions and 546 deletions

View File

@ -11,3 +11,6 @@ node_js:
- "0.10"
before_install:
- cp config.js.example config.js
script:
- npm run lint
- npm test

78
app.js
View File

@ -1,60 +1,60 @@
var express = require('express');
var express = require('express')
var basicAuth = require('express-basic-auth')
var bodyParser = require('body-parser');
var morgan = require('morgan');
var path = require('path');
var bodyParser = require('body-parser')
var morgan = require('morgan')
var path = require('path')
var serveStatic = require('serve-static')
var config = require('./config');
var Manager = require('./lib/manager');
var Missions = require('./lib/missions');
var Mods = require('./lib/mods');
var Logs = require('./lib/logs');
var config = require('./config')
var Manager = require('./lib/manager')
var Missions = require('./lib/missions')
var Mods = require('./lib/mods')
var Logs = require('./lib/logs')
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
if (config.auth && config.auth.username && config.auth.password) {
var basicAuthUsers = {}
basicAuthUsers[config.auth.username] = config.auth.password;
basicAuthUsers[config.auth.username] = config.auth.password
app.use(basicAuth({
challenge: true,
users: basicAuthUsers
}));
}))
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan('dev'));
app.use(serveStatic(path.join(__dirname, 'public')));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(morgan('dev'))
app.use(serveStatic(path.join(__dirname, 'public')))
var logs = new Logs(config);
var logs = new Logs(config)
var manager = new Manager(config, logs);
manager.load();
var manager = new Manager(config, logs)
manager.load()
var missions = new Missions(config);
var mods = new Mods(config);
mods.updateMods();
var missions = new Missions(config)
var mods = new Mods(config)
mods.updateMods()
app.use('/api/logs', require('./routes/logs')(logs));
app.use('/api/missions', require('./routes/missions')(missions));
app.use('/api/mods', require('./routes/mods')(mods));
app.use('/api/servers', require('./routes/servers')(manager, mods));
app.use('/api/settings', require('./routes/settings')(config));
app.use('/api/logs', require('./routes/logs')(logs))
app.use('/api/missions', require('./routes/missions')(missions))
app.use('/api/mods', require('./routes/mods')(mods))
app.use('/api/servers', require('./routes/servers')(manager, mods))
app.use('/api/settings', require('./routes/settings')(config))
io.on('connection', function (socket) {
socket.emit('mods', mods.mods);
socket.emit('servers', manager.getServers());
});
socket.emit('mods', mods.mods)
socket.emit('servers', manager.getServers())
})
mods.on('mods', function(mods) {
io.emit('mods', mods);
});
mods.on('mods', function (mods) {
io.emit('mods', mods)
})
manager.on('servers', function() {
io.emit('servers', manager.getServers());
});
manager.on('servers', function () {
io.emit('servers', manager.getServers())
})
server.listen(config.port,config.host);
server.listen(config.port, config.host)

View File

@ -1,96 +1,96 @@
var async = require('async');
var fs = require('fs.extra');
var path = require('path');
var userhome = require('userhome');
var async = require('async')
var fs = require('fs.extra')
var path = require('path')
var userhome = require('userhome')
var Logs = function (config) {
this.config = config;
this.config = config
if (this.config.type === 'linux') {
fs.mkdirp(this.logsPath());
fs.mkdirp(this.logsPath())
}
};
}
Logs.generateLogFileName = function () {
var dateStr = new Date().toISOString().
replace(/:/g, '-'). // Replace time dividers with dash
replace(/T/, '_'). // Remove date and time divider
replace(/\..+/, ''); // Remove milliseconds
return 'arma3server_' + dateStr + '.log';
};
var dateStr = new Date().toISOString()
.replace(/:/g, '-') // Replace time dividers with dash
.replace(/T/, '_') // Remove date and time divider
.replace(/\..+/, '') // Remove milliseconds
return 'arma3server_' + dateStr + '.log'
}
Logs.prototype.generateLogFilePath = function () {
return path.join(this.logsPath(), Logs.generateLogFileName());
};
return path.join(this.logsPath(), Logs.generateLogFileName())
}
Logs.prototype.logsPath = function () {
if (this.config.type === 'linux') {
return path.join(this.config.path, 'logs');
return path.join(this.config.path, 'logs')
}
if (this.config.type === 'windows') {
return userhome('AppData', 'Local', 'Arma 3');
return userhome('AppData', 'Local', 'Arma 3')
}
if (this.config.type === 'wine') {
var username = process.env.USER;
return userhome('.wine', 'drive_c', 'users', username, 'Local Settings', 'Application Data', 'Arma 3');
var username = process.env.USER
return userhome('.wine', 'drive_c', 'users', username, 'Local Settings', 'Application Data', 'Arma 3')
}
return null;
};
return null
}
Logs.prototype.logFiles = function (callback) {
var directory = this.logsPath();
var directory = this.logsPath()
if (directory === null) {
callback(null, []);
callback(null, [])
}
fs.readdir(directory, function (err, files) {
if (err) {
callback (err);
return;
callback(err)
return
}
files = files.map(function (file) {
return {
name: file,
path: path.join(directory, file),
};
});
path: path.join(directory, file)
}
})
async.filter(files, function(file, cb) {
async.filter(files, function (file, cb) {
fs.stat(file.path, function (err, stat) {
file.size = stat.size;
cb(!err && stat.isFile());
});
file.size = stat.size
cb(!err && stat.isFile())
})
}, function (files) {
files.sort(function (a, b) {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
});
return a.name.toLowerCase().localeCompare(b.name.toLowerCase())
})
callback(null, files);
});
});
};
callback(null, files)
})
})
}
Logs.prototype.getLogFile = function (filename, callback) {
this.logFiles(function (err, files) {
if (err) {
callback(err);
callback(err)
} else {
var validLogs = files.filter(function (file) {
return file.name === filename;
});
return file.name === filename
})
if (validLogs.length > 0) {
callback(null, validLogs[0]);
callback(null, validLogs[0])
} else {
callback(null, null);
callback(null, null)
}
}
});
};
})
}
module.exports = Logs;
module.exports = Logs

View File

@ -1,104 +1,104 @@
var events = require('events');
var fs = require('fs');
var events = require('events')
var fs = require('fs')
var Server = require('./server');
var Server = require('./server')
var filePath = "servers.json";
var filePath = 'servers.json'
var Manager = function (config, logs) {
this.config = config;
this.logs = logs;
this.serversArr = [];
this.serversHash = {};
};
this.config = config
this.logs = logs
this.serversArr = []
this.serversHash = {}
}
Manager.prototype = new events.EventEmitter();
Manager.prototype = new events.EventEmitter()
Manager.prototype.addServer = (function (options) {
var server = this._addServer(options);
this.save();
return server;
});
Manager.prototype.addServer = function (options) {
var server = this._addServer(options)
this.save()
return server
}
Manager.prototype.removeServer = (function (id) {
var server = this.serversHash[id];
Manager.prototype.removeServer = function (id) {
var server = this.serversHash[id]
if (!server) {
return {};
return {}
}
var index = this.serversArr.indexOf(server);
var index = this.serversArr.indexOf(server)
if (index > -1) {
this.serversArr.splice(index, 1);
this.serversArr.splice(index, 1)
}
this.save();
this.save()
if (server.pid) {
server.stop();
server.stop()
}
return server;
});
return server
}
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;
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
var self = this;
var self = this
var statusChanged = function () {
self.emit('servers');
};
server.on('state', statusChanged);
self.emit('servers')
}
server.on('state', statusChanged)
return server;
});
return server
}
Manager.prototype.getServer = (function (id) {
return this.serversHash[id];
});
Manager.prototype.getServer = function (id) {
return this.serversHash[id]
}
Manager.prototype.getServers = (function () {
return this.serversArr;
});
Manager.prototype.getServers = function () {
return this.serversArr
}
Manager.prototype.load = (function () {
var self = this;
Manager.prototype.load = function () {
var self = this
fs.readFile(filePath, function (err, data) {
if (err) {
console.log("Could not load any existing servers configuration, starting fresh");
return;
console.log('Could not load any existing servers configuration, starting fresh')
return
}
try {
JSON.parse(data).forEach(function (server) {
self._addServer(server);
});
} catch(e) {
console.error("Manager load error: " + e);
self._addServer(server)
})
} catch (e) {
console.error('Manager load error: ' + e)
}
self.getServers().map(function (server) {
if (server.auto_start) {
server.start();
server.start()
}
});
});
});
})
})
}
Manager.prototype.save = (function () {
var data = [];
var self = this;
Manager.prototype.save = function () {
var data = []
var self = this
this.serversArr.sort(function (a, b) {
return a.title.toLowerCase().localeCompare(b.title.toLowerCase());
});
return a.title.toLowerCase().localeCompare(b.title.toLowerCase())
})
this.serversHash = {};
this.serversHash = {}
this.serversArr.forEach(function (server) {
data.push({
admin_password: server.admin_password,
@ -114,19 +114,19 @@ Manager.prototype.save = (function () {
port: server.port,
title: server.title,
von: server.von,
verify_signatures: server.verify_signatures,
});
verify_signatures: server.verify_signatures
})
self.serversHash[server.id] = server;
});
self.serversHash[server.id] = server
})
fs.writeFile(filePath, JSON.stringify(data), function(err) {
fs.writeFile(filePath, JSON.stringify(data), function (err) {
if (err) {
throw err;
throw err
} else {
self.emit('servers');
self.emit('servers')
}
});
});
})
}
module.exports = Manager;
module.exports = Manager

View File

@ -1,32 +1,32 @@
var async = require('async');
var filesize = require('filesize');
var fs = require ('fs.extra');
var path = require('path');
var SteamWorkshop = require('steam-workshop');
var async = require('async')
var filesize = require('filesize')
var fs = require('fs.extra')
var path = require('path')
var SteamWorkshop = require('steam-workshop')
var Missions = function (config) {
this.config = config;
this.steamWorkshop = new SteamWorkshop(this.missionsPath());
};
Missions.prototype.missionsPath = function() {
return path.join(this.config.path, 'mpmissions');
};
Missions.prototype.missionPath = function (name) {
return path.join(this.missionsPath(), name);
this.config = config
this.steamWorkshop = new SteamWorkshop(this.missionsPath())
}
Missions.prototype.list = function (cb){
var self = this;
Missions.prototype.missionsPath = function () {
return path.join(this.config.path, 'mpmissions')
}
Missions.prototype.missionPath = function (name) {
return path.join(this.missionsPath(), name)
}
Missions.prototype.list = function (cb) {
var self = this
fs.readdir(this.missionsPath(), function (err, files) {
if (err) {
cb(err);
cb(err)
} else {
async.map(files, function (filename, cb) {
fs.stat(self.missionPath(filename), function (err, stat) {
if (err) {
cb(err);
cb(err)
}
cb(null, {
@ -34,35 +34,35 @@ Missions.prototype.list = function (cb){
dateModified: new Date(stat.mtime),
name: filename,
size: stat.size,
sizeFormatted: filesize(stat.size),
});
});
sizeFormatted: filesize(stat.size)
})
})
}, function (err, missions) {
if (cb) {
cb(err, missions);
cb(err, missions)
}
});
})
}
});
};
})
}
Missions.prototype.handleUpload = function (uploadedFile, cb) {
var filename = decodeURI(uploadedFile.originalname.toLowerCase());
fs.move (uploadedFile.path, path.join(this.missionsPath(), filename), function (err) {
cb(err);
});
};
var filename = decodeURI(uploadedFile.originalname.toLowerCase())
fs.move(uploadedFile.path, path.join(this.missionsPath(), filename), function (err) {
cb(err)
})
}
Missions.prototype.delete = function (missionName, cb) {
fs.unlink(path.join(this.missionsPath(), missionName), cb);
};
fs.unlink(path.join(this.missionsPath(), missionName), cb)
}
Missions.prototype.downloadSteamWorkshop = function (id, cb) {
if (!id) {
return cb(new Error('Not a valid Steam Workshop ID: ' + id));
return cb(new Error('Not a valid Steam Workshop ID: ' + id))
}
this.steamWorkshop.downloadFile(id, cb);
this.steamWorkshop.downloadFile(id, cb)
}
module.exports = Missions;
module.exports = Missions

View File

@ -1,131 +1,132 @@
var async = require('async');
var events = require('events');
var filesize = require('filesize');
var fs = require('fs.extra');
var _ = require('lodash');
var path = require('path');
var playwithsix = require('playwithsix');
var async = require('async')
var events = require('events')
var filesize = require('filesize')
var fs = require('fs.extra')
var _ = require('lodash')
var path = require('path')
var playwithsix = require('playwithsix')
var Mods = function (config) {
this.config = config;
this.liteMods = true;
this.mods = [];
this.config = config
this.liteMods = true
this.mods = []
}
var self = this;
};
Mods.prototype = new events.EventEmitter();
Mods.prototype = new events.EventEmitter()
Mods.prototype.delete = function (mod, cb) {
var self = this;
var self = this
fs.rmrf(path.join(this.config.path, mod), function (err) {
cb(err);
cb(err)
if (!err) {
self.updateMods();
self.updateMods()
}
});
};
})
}
Mods.prototype.download = function (mod, cb) {
var self = this;
var currentDownloadMod = null;
var currentDownloadProgress = 0;
var self = this
var currentDownloadMod = null
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function(err, mods) {
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function (err, mods) {
if (currentDownloadMod) {
currentDownloadMod.progress = null;
self.emit('mods', self.mods);
currentDownloadMod.progress = null
self.emit('mods', self.mods)
}
self.updateMods();
self.updateMods()
if (cb) {
cb(err, mods);
cb(err, mods)
}
}).on('progress', function (progress) {
var modName = progress.mod;
var modName = progress.mod
if (!currentDownloadMod || currentDownloadMod.name != modName) {
if (!currentDownloadMod || currentDownloadMod.name !== modName) {
if (currentDownloadMod) {
currentDownloadMod.progress = null;
currentDownloadMod.progress = null
}
var mod = _.find(self.mods, {name: modName});
var mod = _.find(self.mods, {name: modName})
if (mod) {
currentDownloadMod = mod;
currentDownloadMod = mod
} else {
currentDownloadMod = {
name: modName,
outdated: false,
playWithSix: true,
};
self.mods.push(currentDownloadMod);
playWithSix: true
}
self.mods.push(currentDownloadMod)
}
}
// Progress in whole percent
var newProgress = parseInt(progress.completed / progress.size * 100, 10);
var newProgress = parseInt(progress.completed / progress.size * 100, 10)
if (newProgress != currentDownloadMod.progress) {
currentDownloadMod.progress = newProgress;
self.emit('mods', self.mods);
if (newProgress !== currentDownloadMod.progress) {
currentDownloadMod.progress = newProgress
self.emit('mods', self.mods)
}
});
};
})
}
Mods.prototype.updateMods = function () {
var self = this;
var self = this
fs.readdir(self.config.path, function (err, files) {
if (err) {
console.log(err);
console.log(err)
} else {
var mods = files.filter(function (file) {
return file.charAt(0) == "@";
});
return file.charAt(0) === '@'
})
playwithsix.checkOutdated(self.config.path, function (err, outdatedMods) {
if (err) {
console.log('Error checking for outdated mods: ' + err)
}
async.map(mods, function (mod, cb) {
var modPath = path.join(self.config.path, mod);
var modPath = path.join(self.config.path, mod)
self.isPlayWithSixMod(modPath, function (isPlayWithSixMod) {
cb(null, {
name: mod,
outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0,
progress: null,
playWithSix: isPlayWithSixMod,
});
});
playWithSix: isPlayWithSixMod
})
})
}, function (err, mods) {
if (!err) {
self.mods = mods;
self.emit('mods', mods);
self.mods = mods
self.emit('mods', mods)
}
});
});
})
})
}
});
};
})
}
Mods.prototype.isPlayWithSixMod = function (modPath, cb) {
var pwsFile = path.join(modPath, '.synq.json');
var pwsFile = path.join(modPath, '.synq.json')
fs.exists(pwsFile, function (exists) {
if (cb) {
cb(exists);
cb(exists)
}
});
};
})
}
Mods.prototype.search = function (query, cb) {
playwithsix.search(query, function (err, mods) {
if (err) {
cb(err);
cb(err)
} else {
mods.map(function (mod) {
mod.formattedSize = filesize(mod.size);
});
cb(null, mods);
mod.formattedSize = filesize(mod.size)
})
cb(null, mods)
}
});
};
})
}
module.exports = Mods;
module.exports = Mods

View File

@ -1,15 +1,14 @@
var _ = require('lodash');
var events = require('events');
var fs = require('fs');
var gamedig = require('gamedig');
var slugify = require('slugify');
var spawn = require('child_process').spawn;
var _ = require('lodash')
var events = require('events')
var fs = require('fs')
var Gamedig = require('gamedig')
var slugify = require('slugify')
var ArmaServer = require('arma-server');
var ArmaServer = require('arma-server')
var config = require('../config.js');
var config = require('../config.js')
var queryInterval = 5000;
var queryInterval = 5000
var queryTypes = {
arma1: 'arma',
arma2: 'arma2',
@ -18,105 +17,105 @@ var queryTypes = {
arma3_x64: 'arma3',
cwa: 'operationflashpoint',
ofp: 'operationflashpoint',
ofpresistance: 'operationflashpoint',
};
ofpresistance: 'operationflashpoint'
}
var createServerTitle = function(title) {
var createServerTitle = function (title) {
if (config.prefix) {
title = config.prefix + title;
title = config.prefix + title
}
if (config.suffix) {
title = title + config.suffix;
title = title + config.suffix
}
return title;
};
return title
}
var Server = function (config, logs, options) {
this.config = config;
this.logs = logs;
this.update(options);
};
this.config = config
this.logs = logs
this.update(options)
}
Server.prototype = new events.EventEmitter();
Server.prototype = new events.EventEmitter()
Server.prototype.generateId = function () {
return slugify(this.title).replace(/\./g, '-');
};
return slugify(this.title).replace(/\./g, '-')
}
Server.prototype.update = function (options) {
this.admin_password = options.admin_password;
this.auto_start = options.auto_start;
this.battle_eye = options.battle_eye;
this.max_players = options.max_players;
this.missions = options.missions;
this.mods = options.mods || [];
this.number_of_headless_clients = options.number_of_headless_clients || 0;
this.password = options.password;
this.parameters = options.parameters;
this.persistent = options.persistent;
this.port = options.port || 2302;
this.title = options.title;
this.von = options.von;
this.verify_signatures = options.verify_signatures;
this.admin_password = options.admin_password
this.auto_start = options.auto_start
this.battle_eye = options.battle_eye
this.max_players = options.max_players
this.missions = options.missions
this.mods = options.mods || []
this.number_of_headless_clients = options.number_of_headless_clients || 0
this.password = options.password
this.parameters = options.parameters
this.persistent = options.persistent
this.port = options.port || 2302
this.title = options.title
this.von = options.von
this.verify_signatures = options.verify_signatures
this.id = this.generateId();
this.port = parseInt(this.port, 10); // If port is a string then gamedig fails
};
this.id = this.generateId()
this.port = parseInt(this.port, 10) // If port is a string then gamedig fails
}
Server.prototype.queryStatus = function() {
Server.prototype.queryStatus = function () {
if (!this.instance) {
return;
return
}
var self = this;
var self = this
Gamedig.query(
{
type: queryTypes[config.game],
host: '127.0.0.1',
port: self.port,
port: self.port
},
function(state) {
function (state) {
if (!self.instance) {
return;
return
}
if(state.error) {
self.state = null;
if (state.error) {
self.state = null
} else {
self.state = state;
self.state = state
}
self.emit('state');
self.emit('state')
}
);
};
)
}
Server.prototype.getParameters = function() {
var parameters = [];
Server.prototype.getParameters = function () {
var parameters = []
if (config.parameters && Array.isArray(config.parameters)) {
parameters = parameters.concat(config.parameters);
parameters = parameters.concat(config.parameters)
}
if (this.parameters && Array.isArray(this.parameters)) {
parameters = parameters.concat(this.parameters);
parameters = parameters.concat(this.parameters)
}
return parameters;
return parameters
}
Server.prototype.start = function() {
var parameters = this.getParameters();
Server.prototype.start = function () {
var parameters = this.getParameters()
var server = new ArmaServer.Server({
battleEye: this.battle_eye ? 1 : 0,
config: this.id,
disableVoN: this.von ? 0 : 1,
game: config.game,
headlessClients: this.number_of_headless_clients > 0 ? ["127.0.0.1"] : null,
headlessClients: this.number_of_headless_clients > 0 ? ['127.0.0.1'] : null,
hostname: createServerTitle(this.title),
localClient: this.number_of_headless_clients > 0 ? ["127.0.0.1"] : null,
localClient: this.number_of_headless_clients > 0 ? ['127.0.0.1'] : null,
missions: this.missions,
mods: this.mods,
parameters: parameters,
@ -128,142 +127,141 @@ Server.prototype.start = function() {
players: this.max_players,
port: this.port,
serverMods: config.serverMods,
verifySignatures: this.verify_signatures ? 2 : 0,
});
server.writeServerConfig();
var instance = server.start();
var self = this;
verifySignatures: this.verify_signatures ? 2 : 0
})
server.writeServerConfig()
var instance = server.start()
var self = this
var logStream = null;
var logStream = null
if (this.config.type === 'linux') {
logStream = fs.createWriteStream(this.logs.generateLogFilePath(), {
'flags': 'a'
});
})
}
instance.stdout.on('data', function (data) {
if (logStream) {
logStream.write(data);
logStream.write(data)
}
});
})
instance.stderr.on('data', function (data) {
if (logStream) {
logStream.write(data);
logStream.write(data)
}
});
})
instance.on('close', function (code) {
if (logStream) {
logStream.end();
logStream.end()
}
clearInterval(self.queryStatusInterval);
self.state = null;
self.pid = null;
self.instance = null;
clearInterval(self.queryStatusInterval)
self.state = null
self.pid = null
self.instance = null
self.stopHeadlessClients();
self.stopHeadlessClients()
self.emit('state');
});
self.emit('state')
})
instance.on('error', function (err) {
console.log(err);
});
console.log(err)
})
this.pid = instance.pid;
this.instance = instance;
this.pid = instance.pid
this.instance = instance
this.queryStatusInterval = setInterval(function () {
self.queryStatus();
}, queryInterval);
self.queryStatus()
}, queryInterval)
this.startHeadlessClients()
this.emit('state');
this.emit('state')
return this;
};
return this
}
Server.prototype.startHeadlessClients = function() {
var parameters = this.getParameters();
var self = this;
Server.prototype.startHeadlessClients = function () {
var parameters = this.getParameters()
var self = this
var headlessClientInstances = _.times(this.number_of_headless_clients, function (i) {
var headless = new ArmaServer.Headless({
game: config.game,
host: "127.0.0.1",
host: '127.0.0.1',
mods: self.mods,
parameters: parameters,
password: self.password,
path: self.config.path,
platform: self.config.type,
port: self.port,
});
var headlessInstance = headless.start();
var name = 'HC_' + i;
var logPrefix = self.id + ' ' + name;
console.log(logPrefix + ' starting');
port: self.port
})
var headlessInstance = headless.start()
var name = 'HC_' + i
var logPrefix = self.id + ' ' + name
console.log(logPrefix + ' starting')
headlessInstance.stdout.on('data', function (data) {
console.log(logPrefix + ' stdout: ' + data);
});
console.log(logPrefix + ' stdout: ' + data)
})
headlessInstance.stderr.on('data', function (data) {
console.log(logPrefix + ' stderr: ' + data);
});
console.log(logPrefix + ' stderr: ' + data)
})
headlessInstance.on('close', function (code) {
console.log(logPrefix + ' exited: ' + code);
console.log(logPrefix + ' exited: ' + code)
var elementIndex = headlessClientInstances.indexOf(headlessInstance);
if (elementIndex != -1) {
headlessClientInstances.splice(elementIndex, 1);
var elementIndex = headlessClientInstances.indexOf(headlessInstance)
if (elementIndex !== -1) {
headlessClientInstances.splice(elementIndex, 1)
}
});
})
headlessInstance.on('error', function (err) {
console.log(logPrefix + ' error: ' + err);
});
console.log(logPrefix + ' error: ' + err)
})
return headlessInstance;
});
return headlessInstance
})
this.headlessClientInstances = headlessClientInstances;
this.headlessClientInstances = headlessClientInstances
}
Server.prototype.stop = function(cb) {
var handled = false;
var self = this;
Server.prototype.stop = function (cb) {
var handled = false
this.instance.on('close', function (code) {
if (!handled) {
handled = true;
handled = true
if (cb) {
cb();
cb()
}
}
});
})
this.instance.kill();
this.instance.kill()
setTimeout(function() {
setTimeout(function () {
if (!handled) {
handled = true;
handled = true
if (cb) {
cb();
cb()
}
}
}, 5000);
}, 5000)
return this;
};
return this
}
Server.prototype.stopHeadlessClients = function() {
Server.prototype.stopHeadlessClients = function () {
this.headlessClientInstances.map(function (headlessClientInstance) {
headlessClientInstance.kill();
});
headlessClientInstance.kill()
})
}
Server.prototype.toJSON = function () {
@ -284,8 +282,8 @@ Server.prototype.toJSON = function () {
state: this.state,
title: this.title,
von: this.von,
verify_signatures: this.verify_signatures,
};
};
verify_signatures: this.verify_signatures
}
}
module.exports = Server;
module.exports = Server

View File

@ -6,9 +6,15 @@
"scripts": {
"install-windows-service": "winser -i -a -c",
"uninstall-windows-service": "winser -r -x -c",
"lint": "standard --verbose lib/**/*.js routes/**/*.js test/**/*.js app.js",
"start": "node app.js",
"test": "node node_modules/mocha/bin/mocha --recursive"
},
"standard": {
"env": [
"mocha"
]
},
"dependencies": {
"arma-server": "0.0.5",
"async": "^0.9.0",
@ -31,7 +37,8 @@
},
"devDependencies": {
"mocha": "~3.2.0",
"should": "~5.0.0",
"should": "~13.0.1",
"standard": "^10.0.3",
"timekeeper": "0.0.5"
}
}

View File

@ -1,33 +1,33 @@
var express = require('express');
var express = require('express')
module.exports = function (logsManager) {
var router = express.Router();
var router = express.Router()
router.get('/', function (req, res) {
logsManager.logFiles(function (err, files) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.json(files);
res.json(files)
}
});
});
})
})
router.get('/:log', function(req, res) {
var requestedFilename = req.params.log;
router.get('/:log', function (req, res) {
var requestedFilename = req.params.log
logsManager.getLogFile(requestedFilename, function (err, file) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
if (file) {
res.download(file.path);
res.download(file.path)
} else {
res.status(404).send(new Error("File not found"));
res.status(404).send(new Error('File not found'))
}
}
});
});
})
})
return router;
};
return router
}

View File

@ -1,65 +1,65 @@
var express = require('express');
var multer = require('multer');
var upload = multer({ storage: multer.diskStorage({}) });
var express = require('express')
var multer = require('multer')
var upload = multer({ storage: multer.diskStorage({}) })
module.exports = function (missionsManager) {
var router = express.Router();
var router = express.Router()
router.get('/', function (req, res) {
missionsManager.list(function (err, missions) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.json(missions);
res.json(missions)
}
});
});
})
})
router.post('/', upload.single('mission'), function (req, res) {
var missionFile = req.file;
var missionFile = req.file
if (!missionFile) {
return res.status(400).send('No mission file uploaded');;
return res.status(400).send('No mission file uploaded')
}
missionsManager.handleUpload(missionFile, function (err) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.status(200).json({success: true});
res.status(200).json({success: true})
}
});
});
})
})
router.get('/:mission', function (req, res) {
var filename = req.params.mission;
var filename = req.params.mission
res.download(missionsManager.missionPath(filename), decodeURI(filename));
});
res.download(missionsManager.missionPath(filename), decodeURI(filename))
})
router.delete('/:mission', function (req, res) {
var filename = req.params.mission;
var filename = req.params.mission
missionsManager.delete(filename, function (err) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.json({success: true});
res.json({success: true})
}
});
});
})
})
router.post('/workshop', function (req, res) {
var id = req.body.id;
var id = req.body.id
missionsManager.downloadSteamWorkshop(id, function (err, files) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.json({success: true});
res.json({success: true})
}
});
});
})
})
return router;
};
return router
}

View File

@ -1,47 +1,47 @@
var express = require('express');
var express = require('express')
module.exports = function (modsManager) {
var router = express.Router();
var router = express.Router()
router.get('/', function (req, res) {
res.send(modsManager.mods);
});
res.send(modsManager.mods)
})
router.post('/', function (req, res) {
modsManager.download(req.body.name);
res.status(204);
});
modsManager.download(req.body.name)
res.status(204)
})
router.put('/:mod', function (req, res) {
modsManager.download(req.params.mod);
res.status(204);
});
modsManager.download(req.params.mod)
res.status(204)
})
router.delete('/:mod', function (req, res) {
modsManager.delete(req.params.mod, function (err) {
if (err) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.status(204);
res.status(204)
}
});
});
})
})
router.post('/refresh', function (req, res) {
modsManager.updateMods();
res.status(204);
});
modsManager.updateMods()
res.status(204)
})
router.post('/search', function (req, res) {
var query = req.body.query || "";
var query = req.body.query || ''
modsManager.search(query, function (err, mods) {
if (err || !mods) {
res.status(500).send(err);
res.status(500).send(err)
} else {
res.send(mods);
res.send(mods)
}
});
});
})
})
return router;
};
return router
}

View File

@ -1,50 +1,50 @@
var express = require('express');
var express = require('express')
module.exports = function (manager, mods) {
var router = express.Router();
var router = express.Router()
router.get('/', function (req, res) {
res.json(manager.getServers());
});
res.json(manager.getServers())
})
router.post('/', function (req, res) {
var server = manager.addServer(req.body);
res.json(server);
});
var server = manager.addServer(req.body)
res.json(server)
})
router.get('/:server', function (req, res) {
var server = manager.getServer(req.params.server);
res.json(server);
});
var server = manager.getServer(req.params.server)
res.json(server)
})
router.put('/:server', function (req, res) {
var server = manager.getServer(req.params.server);
server.update(req.body);
manager.save();
res.json(server);
});
var server = manager.getServer(req.params.server)
server.update(req.body)
manager.save()
res.json(server)
})
router.delete('/:server', function (req, res) {
var server = manager.removeServer(req.params.server);
res.json(server);
});
var server = manager.removeServer(req.params.server)
res.json(server)
})
router.post('/:server/start', function (req, res) {
var server = manager.getServer(req.params.server);
server.start();
res.json({status:"ok", pid: server.pid});
});
var server = manager.getServer(req.params.server)
server.start()
res.json({status: 'ok', pid: server.pid})
})
router.post('/:server/stop', function (req, res) {
var server = manager.getServer(req.params.server);
var server = manager.getServer(req.params.server)
server.stop(function () {
if (!server.pid) {
res.json({status: true, pid: server.pid});
res.json({status: true, pid: server.pid})
} else {
res.json({status: false, pid: server.pid});
res.json({status: false, pid: server.pid})
}
});
});
})
})
return router;
};
return router
}

View File

@ -1,11 +1,11 @@
var express = require('express');
var express = require('express')
module.exports = function (config) {
var router = express.Router();
var router = express.Router()
router.get('/', function (req, res) {
res.json(config);
});
res.json(config)
})
return router;
};
return router
}

View File

@ -1,30 +1,30 @@
var should = require('should');
var tk = require('timekeeper');
require('should')
var tk = require('timekeeper')
var Logs = require('../../lib/logs.js');
var Logs = require('../../lib/logs.js')
var logs = new Logs({
path: '/tmp/',
type: 'linux'
});
})
describe('Logs', function() {
beforeEach(function() {
tk.freeze(1445455712000); // 2015-10-21 19:28:32
});
describe('Logs', function () {
beforeEach(function () {
tk.freeze(1445455712000) // 2015-10-21 19:28:32
})
afterEach(function() {
tk.reset();
});
afterEach(function () {
tk.reset()
})
describe('generateLogFileName()', function() {
it('should generate valid file name', function() {
Logs.generateLogFileName().should.eql('arma3server_2015-10-21_19-28-32.log');
});
});
describe('generateLogFileName()', function () {
it('should generate valid file name', function () {
Logs.generateLogFileName().should.eql('arma3server_2015-10-21_19-28-32.log')
})
})
describe('generateLogFilePath()', function() {
it('should generate valid file path', function() {
logs.generateLogFilePath().should.eql('/tmp/logs/arma3server_2015-10-21_19-28-32.log');
});
});
});
describe('generateLogFilePath()', function () {
it('should generate valid file path', function () {
logs.generateLogFilePath().should.eql('/tmp/logs/arma3server_2015-10-21_19-28-32.log')
})
})
})

View File

@ -1,16 +1,16 @@
var should = require('should');
var should = require('should')
var Mods = require('../../lib/mods.js');
var Mods = require('../../lib/mods.js')
describe('Mods', function() {
describe('search()', function() {
it('should find mods', function(done) {
var mods = new Mods();
describe('Mods', function () {
describe('search()', function () {
it('should find mods', function (done) {
var mods = new Mods()
mods.search('', function (err, mods) {
should(err).be.null;
mods.should.not.be.empty;
done();
});
});
});
});
should(err).be.null()
mods.should.not.be.empty()
done()
})
})
})
})

View File

@ -1,19 +1,19 @@
var should = require('should');
require('should')
var Server = require('../../lib/server.js');
var Server = require('../../lib/server.js')
describe('Server', function() {
describe('generateId()', function() {
it('should include title', function() {
var server = new Server(null, null, {title: 'title.with.lot.of.dots'});
server.generateId().should.eql('title-with-lot-of-dots');
});
});
describe('Server', function () {
describe('generateId()', function () {
it('should include title', function () {
var server = new Server(null, null, {title: 'title.with.lot.of.dots'})
server.generateId().should.eql('title-with-lot-of-dots')
})
})
describe('toJSON()', function() {
it('should include title', function() {
var server = new Server(null, null, {title: 'test'});
server.toJSON().should.have.property('title', 'test');
});
});
});
describe('toJSON()', function () {
it('should include title', function () {
var server = new Server(null, null, {title: 'test'})
server.toJSON().should.have.property('title', 'test')
})
})
})