Merge pull request #116 from Dahlgren/feature/socket-missions

Push missions with socket connection
This commit is contained in:
Björn Dahlgren 2018-08-18 13:10:08 +02:00 committed by GitHub
commit 0d6e66e131
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 29 deletions

5
app.js
View File

@ -53,11 +53,16 @@ app.use('/api/servers', require('./routes/servers')(manager, mods))
app.use('/api/settings', require('./routes/settings')(settings)) app.use('/api/settings', require('./routes/settings')(settings))
io.on('connection', function (socket) { io.on('connection', function (socket) {
socket.emit('missions', missions.missions)
socket.emit('mods', mods.mods) socket.emit('mods', mods.mods)
socket.emit('servers', manager.getServers()) socket.emit('servers', manager.getServers())
socket.emit('settings', settings.getPublicSettings()) socket.emit('settings', settings.getPublicSettings())
}) })
missions.on('missions', function (missions) {
io.emit('missions', missions)
})
mods.on('mods', function (mods) { mods.on('mods', function (mods) {
io.emit('mods', mods) io.emit('mods', mods)
}) })

View File

@ -1,4 +1,5 @@
var async = require('async') var async = require('async')
var events = require('events')
var filesize = require('filesize') var filesize = require('filesize')
var fs = require('fs.extra') var fs = require('fs.extra')
var path = require('path') var path = require('path')
@ -6,9 +7,14 @@ var SteamWorkshop = require('steam-workshop')
var Missions = function (config) { var Missions = function (config) {
this.config = config this.config = config
this.missions = []
this.steamWorkshop = new SteamWorkshop(this.missionsPath()) this.steamWorkshop = new SteamWorkshop(this.missionsPath())
this.updateMissions()
} }
Missions.prototype = new events.EventEmitter()
Missions.prototype.missionsPath = function () { Missions.prototype.missionsPath = function () {
return path.join(this.config.path, 'mpmissions') return path.join(this.config.path, 'mpmissions')
} }
@ -17,7 +23,7 @@ Missions.prototype.missionPath = function (name) {
return path.join(this.missionsPath(), name) return path.join(this.missionsPath(), name)
} }
Missions.prototype.list = function (cb) { Missions.prototype.updateMissions = function (cb) {
var self = this var self = this
fs.readdir(this.missionsPath(), function (err, files) { fs.readdir(this.missionsPath(), function (err, files) {
if (err) { if (err) {
@ -38,6 +44,11 @@ Missions.prototype.list = function (cb) {
}) })
}) })
}, function (err, missions) { }, function (err, missions) {
if (!err) {
self.missions = missions
self.emit('missions', missions)
}
if (cb) { if (cb) {
cb(err, missions) cb(err, missions)
} }
@ -48,13 +59,25 @@ Missions.prototype.list = function (cb) {
Missions.prototype.handleUpload = function (uploadedFile, cb) { Missions.prototype.handleUpload = function (uploadedFile, cb) {
var filename = decodeURI(uploadedFile.originalname.toLowerCase()) var filename = decodeURI(uploadedFile.originalname.toLowerCase())
var self = this
fs.move(uploadedFile.path, path.join(this.missionsPath(), filename), function (err) { 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) { 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) { 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)) 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 module.exports = Missions

View File

@ -36,11 +36,13 @@ define(function (require) {
initialize: function () { initialize: function () {
layoutView.navigation.show(new NavigationView({settings: settings, servers: servers})); layoutView.navigation.show(new NavigationView({settings: settings, servers: servers}));
missions.fetch();
var initialized = false; var initialized = false;
var socket = io.connect(); var socket = io.connect();
socket.on('missions', function (_missions) {
missions.set(_missions);
});
socket.on('mods', function (_mods) { socket.on('mods', function (_mods) {
mods.set(_mods); mods.set(_mods);
}); });

View File

@ -20,11 +20,29 @@ define(function (require) {
listView: "#list", listView: "#list",
}, },
events: {
"click #refresh": "refresh",
},
onRender: function() { onRender: function() {
this.uploadView.show(new UploadView({missions: this.options.missions})); this.uploadView.show(new UploadView());
this.workshopView.show(new WorkshopView({missions: this.options.missions})); this.workshopView.show(new WorkshopView());
this.listView.show(new ListView({collection: this.options.missions})); 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) {
},
});
},
}); });
}); });

View File

@ -18,10 +18,6 @@ define(function (require) {
'click form button': 'submit', 'click form button': 'submit',
}, },
initialize: function (options) {
this.missions = options.missions;
},
submit: function () { submit: function () {
var self = this; var self = this;
var $form = this.$el.find('form'); var $form = this.$el.find('form');
@ -32,12 +28,8 @@ define(function (require) {
$.ajax("/api/missions", { $.ajax("/api/missions", {
success: function(data) { success: function(data) {
self.missions.fetch({ laddaBtn.stop();
success : function () { self.render();
laddaBtn.stop();
self.render();
}
});
}, },
error: function () { error: function () {
laddaBtn.stop(); laddaBtn.stop();

View File

@ -38,10 +38,8 @@ define(function (require) {
}, },
dataType: 'json', dataType: 'json',
success: function (data) { success: function (data) {
self.missions.fetch({success : function () { laddaBtn.stop();
laddaBtn.stop(); self.render();
self.render();
}});
}, },
error: function () { error: function () {
laddaBtn.stop(); laddaBtn.stop();

View File

@ -13,6 +13,23 @@
</div> </div>
<div class="panel-body" id="workshop"></div> <div class="panel-body" id="workshop"></div>
</div> </div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Refresh missions list</h3>
</div>
<div class="panel-body">
<p>
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.
</p>
<a class="btn btn-primary" id="refresh" href="#">
<span class="glyphicon glyphicon-refresh"></span>
Refresh
</a>
</div>
</div>
</div> </div>
<div class="col-sm-8 col-sm-pull-4" id="list"></div> <div class="col-sm-8 col-sm-pull-4" id="list"></div>
</div> </div>

View File

@ -7,13 +7,7 @@ module.exports = function (missionsManager) {
var router = express.Router() var router = express.Router()
router.get('/', function (req, res) { router.get('/', function (req, res) {
missionsManager.list(function (err, missions) { res.json(missionsManager.missions)
if (err) {
res.status(500).send(err)
} else {
res.json(missions)
}
})
}) })
router.post('/', upload.array('missions', 64), function (req, res) { 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) { router.post('/workshop', function (req, res) {
var id = req.body.id var id = req.body.id
missionsManager.downloadSteamWorkshop(id, function (err, files) { missionsManager.downloadSteamWorkshop(id, function (err) {
if (err) { if (err) {
res.status(500).send(err) res.status(500).send(err)
} else { } else {