Track mod download progress

This commit is contained in:
Björn Dahlgren 2015-04-07 08:12:42 +02:00
parent 8e72a0e7fb
commit 4130006650
7 changed files with 76 additions and 5 deletions

View File

@ -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);
}
});
};

View File

@ -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",

View File

@ -10,6 +10,7 @@ define(function (require) {
defaults: {
name: ''
},
idAttribute: 'name',
urlRoot: '/api/mods/',
});

View File

@ -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) {

View File

@ -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();

View File

@ -6,7 +6,13 @@
<% } %>
</td>
<td>
<% if (outdated) { %>
<% if (typeof progress !== 'undefined') { %>
<div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" aria-valuenow="<%-progress%>" aria-valuemin="0" aria-valuemax="100" style="width: <%-progress%>%; min-width: 2em;">
<%-progress%>%
</div>
</div>
<% } else if (outdated) { %>
<a class="btn btn-primary btn-xs update ladda-button pull-right" data-style="expand-left">
<span class="glyphicon glyphicon-save"></span>
Update

View File

@ -7,10 +7,18 @@
</td>
<td>
<p>
<a class="btn btn-primary btn-xs install ladda-button pull-right" data-style="expand-left">
<span class="glyphicon glyphicon-save"></span>
Install
</a>
<% if (progress()) { %>
<div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" aria-valuenow="<%-progress()%>" aria-valuemin="0" aria-valuemax="100" style="width: <%-progress()%>%; min-width: 2em;">
<%-progress()%>%
</div>
</div>
<% } else { %>
<a class="btn btn-primary btn-xs install ladda-button pull-right" data-style="expand-left">
<span class="glyphicon glyphicon-save"></span>
Install
</a>
<% } %>
<div class="clearfix"></div>
</p>
<p>