Keep mods in sync with clients through socket

This commit is contained in:
Björn Dahlgren 2015-02-01 05:27:12 +01:00
parent d086b789f8
commit 353b4e15e5
7 changed files with 28 additions and 26 deletions

5
app.js
View File

@ -37,9 +37,14 @@ app.get('/', function (req, res){
});
io.on('connection', function (socket) {
socket.emit('mods', mods.mods);
socket.emit('servers', manager.getServers());
});
mods.on('mods', function(mods) {
io.emit('mods', mods);
});
manager.on('servers', function() {
io.emit('servers', manager.getServers());
});

View File

@ -1,4 +1,5 @@
var async = require('async');
var events = require('events');
var fs = require('fs');
var path = require('path');
var playwithsix = require('playwithsix');
@ -8,13 +9,23 @@ var traverse = require('./mods/traverse');
var Mods = function (config) {
this.config = config;
this.liteMods = true;
this.mods = [];
var self = this;
this.updateMods();
};
Mods.prototype = new events.EventEmitter();
Mods.prototype.download = function (mod, cb) {
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, cb);
var self = this;
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function(err, mods) {
self.updateMods();
cb(err, mods);
});
};
Mods.prototype.getMods = function (callback) {
Mods.prototype.updateMods = function () {
var self = this;
fs.readdir(self.config.path, function (err, files) {
if (err) {
@ -35,7 +46,10 @@ Mods.prototype.getMods = function (callback) {
});
});
}, function (err, mods) {
callback(err, mods);
if (!err) {
self.mods = mods;
self.emit('mods', mods);
}
});
});
}

View File

@ -39,9 +39,11 @@ define(function (require) {
initialize: function () {
layoutView.navigation.show(navigationView);
missions.fetch();
mods.fetch();
var socket = io.connect();
socket.on('mods', function (_mods) {
mods.set(_mods);
});
socket.on('servers', function (_servers) {
servers.set(_servers);
});

View File

@ -56,12 +56,8 @@ define(function (require) {
this.model.save({ name: $form.find('.mod').val() }, {
success: function () {
self.mods.fetch({
success : function () {
self.laddaBtn.stop();
self.modal.close();
}
});
self.laddaBtn.stop();
self.modal.close();
},
error: function () {
self.laddaBtn.stop();

View File

@ -21,14 +21,6 @@ define(function (require) {
"click #download": "download"
},
initialize: function (options) {
this.on("itemview:mods:update", this.update, this);
},
update: function() {
this.collection.fetch();
},
download: function (event) {
event.preventDefault();
var view = new FormView({mods: this.collection});

View File

@ -33,7 +33,6 @@ define(function (require) {
success: function (resp) {
self.laddaBtn.stop();
self.$el.find('.ladda-button').removeClass('disabled');
self.trigger("mods:update", mods);
},
error: function (resp) {
self.laddaBtn.stop();

View File

@ -1,13 +1,7 @@
module.exports = function (modsManager) {
return {
index: function(req, res){
modsManager.getMods(function (err, mods) {
if (err) {
res.send(500, err);
} else {
res.send(mods);
}
});
res.send(modsManager.mods);
},
create: function(req, res){