From 38d935dd1cd61eac6b52c5e72f1574ffae874364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Sat, 15 Aug 2015 17:12:50 +0200 Subject: [PATCH] Download missions from Steam Workshop --- app.js | 1 + package.json | 1 + public/js/app/views/missions/index.js | 9 ++- .../app/views/missions/{form.js => upload.js} | 4 +- public/js/app/views/missions/workshop.js | 58 +++++++++++++++++++ public/js/tpl/missions/index.html | 7 ++- .../tpl/missions/{form.html => upload.html} | 0 public/js/tpl/missions/workshop.html | 11 ++++ routes/missions.js | 4 +- routes/workshop.js | 16 +++++ 10 files changed, 104 insertions(+), 7 deletions(-) rename public/js/app/views/missions/{form.js => upload.js} (92%) create mode 100644 public/js/app/views/missions/workshop.js rename public/js/tpl/missions/{form.html => upload.html} (100%) create mode 100644 public/js/tpl/missions/workshop.html create mode 100644 routes/workshop.js diff --git a/app.js b/app.js index 8a077df..7bb2a92 100644 --- a/app.js +++ b/app.js @@ -33,6 +33,7 @@ app.resource('api/mods', modsRoutes); var serversResource = app.resource('api/servers', serversRoutes); app.resource('api/settings', require('./routes/settings')); +app.post('/api/missions/workshop', require('./routes/workshop').mission); app.post('/api/mods/refresh', modsRoutes.refresh); app.post('/api/mods/search', modsRoutes.search); app.get('/api/servers/:server/start', serversRoutes.start); diff --git a/package.json b/package.json index c552038..543d18e 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "playwithsix": "0.0.10", "slug": "~0.4.0", "socket.io": "^1.0.4", + "steam-workshop": "0.0.1", "userhome": "^1.0.0", "when": "~3.1.0" }, diff --git a/public/js/app/views/missions/index.js b/public/js/app/views/missions/index.js index 68442e7..636400f 100644 --- a/public/js/app/views/missions/index.js +++ b/public/js/app/views/missions/index.js @@ -6,7 +6,8 @@ define(function (require) { _ = require('underscore'), Backbone = require('backbone'), Marionette = require('marionette'), - FormView = require('app/views/missions/form'), + UploadView = require('app/views/missions/upload'), + WorkshopView = require('app/views/missions/workshop'), ListView = require('app/views/missions/list'), tpl = require('text!tpl/missions/index.html'); @@ -14,12 +15,14 @@ define(function (require) { template: _.template(tpl), regions: { - formView: "#form", + uploadView: "#upload", + workshopView: "#workshop", listView: "#list", }, onRender: function() { - this.formView.show(new FormView({missions: this.options.missions})); + this.uploadView.show(new UploadView({missions: this.options.missions})); + this.workshopView.show(new WorkshopView({missions: this.options.missions})); this.listView.show(new ListView({collection: this.options.missions})); }, }); diff --git a/public/js/app/views/missions/form.js b/public/js/app/views/missions/upload.js similarity index 92% rename from public/js/app/views/missions/form.js rename to public/js/app/views/missions/upload.js index d00a7c6..937258f 100644 --- a/public/js/app/views/missions/form.js +++ b/public/js/app/views/missions/upload.js @@ -10,7 +10,7 @@ define(function (require) { Ladda = require('ladda'), IframeTransport = require('jquery.iframe-transport'), Mission = require('app/models/mission'), - tpl = require('text!tpl/missions/form.html'); + tpl = require('text!tpl/missions/upload.html'); return Marionette.ItemView.extend({ template: _.template(tpl), @@ -33,7 +33,7 @@ define(function (require) { submit: function () { var self = this; - var $form = $('form'); + var $form = this.$el.find('form'); this.laddaBtn.start(); diff --git a/public/js/app/views/missions/workshop.js b/public/js/app/views/missions/workshop.js new file mode 100644 index 0000000..8986bef --- /dev/null +++ b/public/js/app/views/missions/workshop.js @@ -0,0 +1,58 @@ +define(function (require) { + + "use strict"; + + var $ = require('jquery'), + _ = require('underscore'), + Backbone = require('backbone'), + Marionette = require('marionette'), + FormView = require('marionette-formview'), + Ladda = require('ladda'), + IframeTransport = require('jquery.iframe-transport'), + Mission = require('app/models/mission'), + tpl = require('text!tpl/missions/workshop.html'); + + return Marionette.ItemView.extend({ + template: _.template(tpl), + + events: { + 'click form button': 'submit', + }, + + initialize: function (options) { + this.missions = options.missions; + }, + + onShow: function () { + var $okBtn = this.$el.find('form button[type=submit]'); + $okBtn.addClass('ladda-button').attr('data-style', 'expand-left'); + + this.laddaBtn = Ladda.create($okBtn.get(0)); + }, + + submit: function () { + var self = this; + var $form = this.$el.find('form'); + + this.laddaBtn.start(); + + $.ajax({ + url: '/api/missions/workshop', + type: 'POST', + data: { + id: $form.find("input.workshop").val(), + }, + dataType: 'json', + success: function (data) { + self.missions.fetch({success : function () { + self.laddaBtn.stop(); + self.render(); + }}); + }, + error: function () { + self.laddaBtn.stop(); + }, + }); + }, + }); +}); diff --git a/public/js/tpl/missions/index.html b/public/js/tpl/missions/index.html index 48c2e53..6265037 100644 --- a/public/js/tpl/missions/index.html +++ b/public/js/tpl/missions/index.html @@ -1,4 +1,9 @@
-
+
+

Upload Mission

+
+

Download Mission

+
+
diff --git a/public/js/tpl/missions/form.html b/public/js/tpl/missions/upload.html similarity index 100% rename from public/js/tpl/missions/form.html rename to public/js/tpl/missions/upload.html diff --git a/public/js/tpl/missions/workshop.html b/public/js/tpl/missions/workshop.html new file mode 100644 index 0000000..cf34439 --- /dev/null +++ b/public/js/tpl/missions/workshop.html @@ -0,0 +1,11 @@ +
+
+ + + + ID can be found in the URL of Workshop i.e. + https://steamcommunity.com/workshop/filedetails/?id=$id + +
+ +
diff --git a/routes/missions.js b/routes/missions.js index ec0e5b5..f7826a4 100644 --- a/routes/missions.js +++ b/routes/missions.js @@ -9,7 +9,9 @@ exports.index = function(req, res){ res.send(err); } else { var missions = files.map(function (filename) { - return { name: filename } + return { + name: filename, + }; }); res.send(missions); } diff --git a/routes/workshop.js b/routes/workshop.js new file mode 100644 index 0000000..7f02ccc --- /dev/null +++ b/routes/workshop.js @@ -0,0 +1,16 @@ +var path = require('path'); +var SteamWorkshop = require('steam-workshop'); + +var config = require('./../config'); + +exports.mission = function(req, res){ + var outputFolder = path.join(config.path, 'mpmissions'); + var steamWorkshop = new SteamWorkshop(outputFolder); + steamWorkshop.downloadFile(req.body.id, function (err, files) { + if (err) { + res.json(500, {success: false}); + } else { + res.json({success: true}); + } + }); +};