2014-05-21 23:44:39 +00:00
|
|
|
var playwithsix = require('playwithsix');
|
2014-04-04 08:37:43 +00:00
|
|
|
|
2014-06-04 22:19:29 +00:00
|
|
|
var manager = require('./../manager');
|
2014-04-04 08:37:43 +00:00
|
|
|
|
2014-05-29 19:21:22 +00:00
|
|
|
function removeDuplicates(mods) {
|
|
|
|
return mods.reduce(function(a,b){
|
|
|
|
if (a.indexOf(b) < 0 ) a.push(b);
|
|
|
|
return a;
|
|
|
|
},[]);
|
|
|
|
}
|
|
|
|
|
2014-09-12 23:51:31 +00:00
|
|
|
function resolveMods(server, cb) {
|
2015-01-03 02:51:03 +00:00
|
|
|
playwithsix.resolveDependencies(server.mods, function (err, mods) {
|
2014-09-12 23:51:31 +00:00
|
|
|
if (!err && mods) {
|
|
|
|
server.mods = removeDuplicates(server.mods.concat(mods));
|
|
|
|
manager.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:37:43 +00:00
|
|
|
exports.index = function (req, res){
|
2014-04-06 22:58:17 +00:00
|
|
|
var servers = [];
|
|
|
|
|
|
|
|
manager.getServers().forEach(function (server) {
|
|
|
|
servers.push({
|
|
|
|
id: server.id,
|
|
|
|
title: server.title,
|
|
|
|
port: server.port,
|
|
|
|
mods: server.mods,
|
2014-04-08 22:08:36 +00:00
|
|
|
pid: server.pid,
|
|
|
|
});
|
2014-04-06 22:58:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
res.send(servers);
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.create = function (req, res){
|
2014-09-12 23:51:31 +00:00
|
|
|
var server = manager.addServer(req.body);
|
|
|
|
if (server.mods.length > 0) {
|
|
|
|
resolveMods(server, function(err) {
|
|
|
|
res.send(server);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.send(server);
|
|
|
|
}
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
2014-04-06 21:09:52 +00:00
|
|
|
exports.show = function (req, res){
|
2014-05-21 23:44:39 +00:00
|
|
|
var server = manager.getServer(req.params.server);
|
2014-06-04 22:17:18 +00:00
|
|
|
res.send(server);
|
2014-04-06 21:09:52 +00:00
|
|
|
};
|
|
|
|
|
2014-04-04 08:37:43 +00:00
|
|
|
exports.update = function(req, res){
|
2014-04-08 20:59:59 +00:00
|
|
|
var server = manager.getServer(req.params.server);
|
2014-09-12 23:51:31 +00:00
|
|
|
server.update(req.body);
|
|
|
|
manager.save();
|
2014-04-08 20:59:59 +00:00
|
|
|
|
2014-09-12 23:51:31 +00:00
|
|
|
if (server.mods.length > 0) {
|
|
|
|
resolveMods(server, function(err) {
|
2014-06-04 22:17:18 +00:00
|
|
|
res.send(server);
|
2014-05-21 23:44:39 +00:00
|
|
|
});
|
|
|
|
} else {
|
2014-06-04 22:17:18 +00:00
|
|
|
res.send(server);
|
2014-05-21 23:44:39 +00:00
|
|
|
}
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.destroy = function(req, res){
|
2014-06-04 23:02:09 +00:00
|
|
|
var server = manager.removeServer(req.params.server);
|
|
|
|
res.send(server);
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
2014-04-06 21:09:52 +00:00
|
|
|
|
|
|
|
exports.start = function (req, res){
|
|
|
|
var server = manager.getServer(req.params.server);
|
|
|
|
server.start();
|
2014-04-06 22:58:17 +00:00
|
|
|
res.send({status:"ok", pid: server.pid});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.stop = function (req, res){
|
|
|
|
var server = manager.getServer(req.params.server);
|
|
|
|
server.stop(function () {
|
|
|
|
if (!server.pid) {
|
|
|
|
res.send({status: true, pid: server.pid});
|
|
|
|
} else {
|
|
|
|
res.send({status: false, pid: server.pid});
|
|
|
|
}
|
|
|
|
});
|
2014-04-06 21:09:52 +00:00
|
|
|
};
|