From 4130006650c0d55742fd0ceec483e44f0ceda62f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Tue, 7 Apr 2015 08:12:42 +0200 Subject: [PATCH] Track mod download progress --- lib/mods.js | 36 ++++++++++++++++++++ package.json | 1 + public/js/app/models/mod.js | 1 + public/js/app/views/mods/form.js | 9 +++++ public/js/app/views/mods/search/list_item.js | 10 ++++++ public/js/tpl/mods/list_item.html | 8 ++++- public/js/tpl/mods/search/list_item.html | 16 ++++++--- 7 files changed, 76 insertions(+), 5 deletions(-) diff --git a/lib/mods.js b/lib/mods.js index 32bf5b8..3c4483e 100644 --- a/lib/mods.js +++ b/lib/mods.js @@ -2,6 +2,7 @@ var async = require('async'); var events = require('events'); var filesize = require('filesize'); var fs = require('fs'); +var _ = require('lodash'); var path = require('path'); var playwithsix = require('playwithsix'); @@ -26,9 +27,44 @@ Mods.prototype = new events.EventEmitter(); Mods.prototype.download = function (mod, cb) { var self = this; + var currentDownloadMod = null; + var currentDownloadProgress = 0; + playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function(err, mods) { + if (currentDownloadMod) { + delete currentDownloadMod.progress; + } self.updateMods(); cb(err, mods); + }).on('progress', function (progress) { + var modName = progress.mod; + + if (!currentDownloadMod || currentDownloadMod.name != modName) { + if (currentDownloadMod) { + delete currentDownloadMod.progress; + } + + var mod = _.find(self.mods, {name: modName}); + + if (mod) { + currentDownloadMod = mod; + } else { + currentDownloadMod = { + name: modName, + outdated: false, + playWithSix: true, + }; + self.mods = self.mods.push(currentDownloadMod); + } + } + + // Progress in whole percent + var newProgress = parseInt(progress.completed / progress.size * 100, 10); + + if (newProgress != currentDownloadMod.progress) { + currentDownloadMod.progress = newProgress; + self.emit('mods', self.mods); + } }); }; diff --git a/package.json b/package.json index bb56638..6e717c5 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "express-resource": "~1.0.0", "filesize": "^3.1.0", "gamedig": "^0.2.11", + "lodash": "^3.6.0", "playwithsix": "0.0.10", "slug": "~0.4.0", "socket.io": "^1.0.4", diff --git a/public/js/app/models/mod.js b/public/js/app/models/mod.js index 4ac6396..3fa1006 100644 --- a/public/js/app/models/mod.js +++ b/public/js/app/models/mod.js @@ -10,6 +10,7 @@ define(function (require) { defaults: { name: '' }, + idAttribute: 'name', urlRoot: '/api/mods/', }); diff --git a/public/js/app/views/mods/form.js b/public/js/app/views/mods/form.js index bd2e146..e379022 100644 --- a/public/js/app/views/mods/form.js +++ b/public/js/app/views/mods/form.js @@ -27,6 +27,15 @@ define(function (require) { this.collection = new Mods(); this.bind('ok', this.submit); this.bind('shown', this.shown); + + var self = this; + this.listenTo(this.mods, "change reset add remove", function () { + self.collection.trigger('reset'); + }); + }, + + itemViewOptions: function(options) { + options.set('mods', this.mods); }, beforeSubmit: function(e) { diff --git a/public/js/app/views/mods/search/list_item.js b/public/js/app/views/mods/search/list_item.js index b472412..b88edd9 100644 --- a/public/js/app/views/mods/search/list_item.js +++ b/public/js/app/views/mods/search/list_item.js @@ -19,6 +19,16 @@ define(function (require) { "click .install": "install" }, + templateHelpers: { + progress: function() { + if (this.mods.get(this.name)) { + return this.mods.get(this.name).get('progress'); + } + + return null; + } + }, + install: function (event) { var self = this; event.preventDefault(); diff --git a/public/js/tpl/mods/list_item.html b/public/js/tpl/mods/list_item.html index 3ef2fff..a2d986c 100644 --- a/public/js/tpl/mods/list_item.html +++ b/public/js/tpl/mods/list_item.html @@ -6,7 +6,13 @@ <% } %> - <% if (outdated) { %> + <% if (typeof progress !== 'undefined') { %> +
+
+ <%-progress%>% +
+
+ <% } else if (outdated) { %> Update diff --git a/public/js/tpl/mods/search/list_item.html b/public/js/tpl/mods/search/list_item.html index 8a787bf..5cee6ce 100644 --- a/public/js/tpl/mods/search/list_item.html +++ b/public/js/tpl/mods/search/list_item.html @@ -7,10 +7,18 @@

- - - Install - + <% if (progress()) { %> +

+
+ <%-progress()%>% +
+
+ <% } else { %> + + + Install + + <% } %>