Updated to PWS 0.0.9 client and moved som functions from routes

This commit is contained in:
Björn Dahlgren 2015-02-01 02:43:52 +01:00
parent 5d46fd7fc4
commit 5f6108c98d
4 changed files with 44 additions and 35 deletions

2
app.js
View File

@ -19,7 +19,7 @@ var manager = new Manager(config);
manager.load();
var mods = new Mods(config);
var servers = require('./routes/servers')(manager);
var servers = require('./routes/servers')(manager, mods);
app.resource('api/logs', require('./routes/logs'));
app.resource('api/missions', require('./routes/missions'));

View File

@ -5,21 +5,13 @@ var playwithsix = require('playwithsix');
var traverse = require('./mods/traverse');
function isPlayWithSixMod(modPath, cb) {
var pwsFile = path.join(modPath, '.synq.json');
fs.exists(pwsFile, function (exists) {
if (cb) {
cb(exists);
}
});
}
var Mods = function (config) {
this.config = config;
this.liteMods = true;
};
Mods.prototype.download = function (mod, cb) {
playwithsix.downloadMod(this.config.path, mod, cb);
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, cb);
};
Mods.prototype.getMods = function (callback) {
@ -35,7 +27,7 @@ Mods.prototype.getMods = function (callback) {
playwithsix.checkOutdated(self.config.path, function (err, outdatedMods) {
async.map(mods, function (mod, cb) {
var modPath = path.join(self.config.path, mod);
isPlayWithSixMod(modPath, function (isPlayWithSixMod) {
self.isPlayWithSixMod(modPath, function (isPlayWithSixMod) {
cb(null, {
name: mod,
outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0,
@ -50,6 +42,33 @@ Mods.prototype.getMods = function (callback) {
});
};
Mods.prototype.isPlayWithSixMod = function (modPath, cb) {
var pwsFile = path.join(modPath, '.synq.json');
fs.exists(pwsFile, function (exists) {
if (cb) {
cb(exists);
}
});
};
Mods.prototype.removeDuplicates = function (mods) {
return mods.reduce(function(a,b){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);
};
Mods.prototype.resolveMods = function (modsToResolve, cb) {
var self = this;
playwithsix.resolveDependencies(modsToResolve, {lite: this.liteMods}, function (err, mods) {
if (!err && mods) {
cb(null, self.removeDuplicates(modsToResolve.concat(mods)));
} else {
cb(err);
}
});
};
Mods.prototype.traverse = function (mod, cb) {
traverse(path.join(this.config.path, mod), cb);
};

View File

@ -12,7 +12,7 @@
"express": "3.x",
"express-resource": "~1.0.0",
"gamedig": "^0.2.11",
"playwithsix": "0.0.8",
"playwithsix": "0.0.9",
"slug": "~0.4.0",
"socket.io": "^1.0.4",
"userhome": "^1.0.0",

View File

@ -1,24 +1,14 @@
var playwithsix = require('playwithsix');
module.exports = function (manager, mods) {
var resolveMods = function(server, cb) {
mods.resolveMods(server.mods, function(err, resolvedMods) {
if (!err) {
server.mods = resolvedMods;
manager.save();
}
cb();
});
};
function removeDuplicates(mods) {
return mods.reduce(function(a,b){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);
}
function resolveMods(server, cb) {
playwithsix.resolveDependencies(server.mods, function (err, mods) {
if (!err && mods) {
server.mods = removeDuplicates(server.mods.concat(mods));
manager.save();
}
cb(err);
});
}
module.exports = function (manager) {
return {
index: function (req, res){
res.send(manager.getServers());
@ -27,7 +17,7 @@ module.exports = function (manager) {
create: function (req, res) {
var server = manager.addServer(req.body);
if (server.mods.length > 0) {
resolveMods(server, function(err) {
resolveMods(server, function() {
res.send(server);
});
} else {
@ -46,7 +36,7 @@ module.exports = function (manager) {
manager.save();
if (server.mods.length > 0) {
resolveMods(server, function(err) {
resolveMods(server, function() {
res.send(server);
});
} else {