From ae21680da8e57274739b616792328d69ab730a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Sat, 18 Aug 2018 13:04:33 +0200 Subject: [PATCH] Push missions with socket connection --- app.js | 5 +++ lib/missions.js | 39 +++++++++++++++++++++--- public/js/app/router.js | 4 ++- public/js/app/views/missions/index.js | 22 +++++++++++-- public/js/app/views/missions/upload.js | 12 ++------ public/js/app/views/missions/workshop.js | 6 ++-- public/js/tpl/missions/index.html | 17 +++++++++++ routes/missions.js | 15 +++++---- 8 files changed, 91 insertions(+), 29 deletions(-) diff --git a/app.js b/app.js index ede997b..376bc1d 100644 --- a/app.js +++ b/app.js @@ -53,11 +53,16 @@ app.use('/api/servers', require('./routes/servers')(manager, mods)) app.use('/api/settings', require('./routes/settings')(settings)) io.on('connection', function (socket) { + socket.emit('missions', missions.missions) socket.emit('mods', mods.mods) socket.emit('servers', manager.getServers()) socket.emit('settings', settings.getPublicSettings()) }) +missions.on('missions', function (missions) { + io.emit('missions', missions) +}) + mods.on('mods', function (mods) { io.emit('mods', mods) }) diff --git a/lib/missions.js b/lib/missions.js index dae87d3..f5c4e3c 100644 --- a/lib/missions.js +++ b/lib/missions.js @@ -1,4 +1,5 @@ var async = require('async') +var events = require('events') var filesize = require('filesize') var fs = require('fs.extra') var path = require('path') @@ -6,9 +7,14 @@ var SteamWorkshop = require('steam-workshop') var Missions = function (config) { this.config = config + this.missions = [] this.steamWorkshop = new SteamWorkshop(this.missionsPath()) + + this.updateMissions() } +Missions.prototype = new events.EventEmitter() + Missions.prototype.missionsPath = function () { return path.join(this.config.path, 'mpmissions') } @@ -17,7 +23,7 @@ Missions.prototype.missionPath = function (name) { return path.join(this.missionsPath(), name) } -Missions.prototype.list = function (cb) { +Missions.prototype.updateMissions = function (cb) { var self = this fs.readdir(this.missionsPath(), function (err, files) { if (err) { @@ -38,6 +44,11 @@ Missions.prototype.list = function (cb) { }) }) }, function (err, missions) { + if (!err) { + self.missions = missions + self.emit('missions', missions) + } + if (cb) { cb(err, missions) } @@ -48,13 +59,25 @@ Missions.prototype.list = function (cb) { Missions.prototype.handleUpload = function (uploadedFile, cb) { var filename = decodeURI(uploadedFile.originalname.toLowerCase()) + var self = this fs.move(uploadedFile.path, path.join(this.missionsPath(), filename), function (err) { - cb(err) + self.updateMissions() + + if (cb) { + cb(err) + } }) } Missions.prototype.delete = function (missionName, cb) { - fs.unlink(path.join(this.missionsPath(), missionName), cb) + var self = this + fs.unlink(path.join(this.missionsPath(), missionName), function (err) { + self.updateMissions() + + if (cb) { + cb(err) + } + }) } Missions.prototype.downloadSteamWorkshop = function (id, cb) { @@ -62,7 +85,15 @@ Missions.prototype.downloadSteamWorkshop = function (id, cb) { return cb(new Error('Not a valid Steam Workshop ID: ' + id)) } - this.steamWorkshop.downloadFile(id, cb) + var self = this + + this.steamWorkshop.downloadFile(id, function (err) { + self.updateMissions() + + if (cb) { + cb(err) + } + }) } module.exports = Missions diff --git a/public/js/app/router.js b/public/js/app/router.js index 97dc156..2906bdf 100644 --- a/public/js/app/router.js +++ b/public/js/app/router.js @@ -36,11 +36,13 @@ define(function (require) { initialize: function () { layoutView.navigation.show(new NavigationView({settings: settings, servers: servers})); - missions.fetch(); var initialized = false; var socket = io.connect(); + socket.on('missions', function (_missions) { + missions.set(_missions); + }); socket.on('mods', function (_mods) { mods.set(_mods); }); diff --git a/public/js/app/views/missions/index.js b/public/js/app/views/missions/index.js index f534ee2..76061fd 100644 --- a/public/js/app/views/missions/index.js +++ b/public/js/app/views/missions/index.js @@ -20,11 +20,29 @@ define(function (require) { listView: "#list", }, + events: { + "click #refresh": "refresh", + }, + onRender: function() { - this.uploadView.show(new UploadView({missions: this.options.missions})); - this.workshopView.show(new WorkshopView({missions: this.options.missions})); + this.uploadView.show(new UploadView()); + this.workshopView.show(new WorkshopView()); this.listView.show(new ListView({collection: this.options.missions})); }, + + refresh: function (event) { + event.preventDefault(); + $.ajax({ + url: "/api/missions/refresh", + type: 'POST', + success: function (resp) { + + }, + error: function (resp) { + + }, + }); + }, }); }); diff --git a/public/js/app/views/missions/upload.js b/public/js/app/views/missions/upload.js index 6f7c050..75d1751 100644 --- a/public/js/app/views/missions/upload.js +++ b/public/js/app/views/missions/upload.js @@ -18,10 +18,6 @@ define(function (require) { 'click form button': 'submit', }, - initialize: function (options) { - this.missions = options.missions; - }, - submit: function () { var self = this; var $form = this.$el.find('form'); @@ -32,12 +28,8 @@ define(function (require) { $.ajax("/api/missions", { success: function(data) { - self.missions.fetch({ - success : function () { - laddaBtn.stop(); - self.render(); - } - }); + laddaBtn.stop(); + self.render(); }, error: function () { laddaBtn.stop(); diff --git a/public/js/app/views/missions/workshop.js b/public/js/app/views/missions/workshop.js index 90b17b6..6d19816 100644 --- a/public/js/app/views/missions/workshop.js +++ b/public/js/app/views/missions/workshop.js @@ -38,10 +38,8 @@ define(function (require) { }, dataType: 'json', success: function (data) { - self.missions.fetch({success : function () { - laddaBtn.stop(); - self.render(); - }}); + laddaBtn.stop(); + self.render(); }, error: function () { laddaBtn.stop(); diff --git a/public/js/tpl/missions/index.html b/public/js/tpl/missions/index.html index af52edc..949e126 100644 --- a/public/js/tpl/missions/index.html +++ b/public/js/tpl/missions/index.html @@ -13,6 +13,23 @@
+ +
+
+

Refresh missions list

+
+
+

+ A refresh is needed if a mission is added manually to the missions directory on the server. + Uploads or downloads through this manager will update the list automatically. +

+ + + + Refresh + +
+
diff --git a/routes/missions.js b/routes/missions.js index 1c3c79e..b231e4a 100644 --- a/routes/missions.js +++ b/routes/missions.js @@ -7,13 +7,7 @@ module.exports = function (missionsManager) { var router = express.Router() router.get('/', function (req, res) { - missionsManager.list(function (err, missions) { - if (err) { - res.status(500).send(err) - } else { - res.json(missions) - } - }) + res.json(missionsManager.missions) }) router.post('/', upload.array('missions', 64), function (req, res) { @@ -52,10 +46,15 @@ module.exports = function (missionsManager) { }) }) + router.post('/refresh', function (req, res) { + missionsManager.updateMissions() + res.status(204).send() + }) + router.post('/workshop', function (req, res) { var id = req.body.id - missionsManager.downloadSteamWorkshop(id, function (err, files) { + missionsManager.downloadSteamWorkshop(id, function (err) { if (err) { res.status(500).send(err) } else {