Push missions with socket connection

This commit is contained in:
Björn Dahlgren 2018-08-18 13:04:33 +02:00
parent e16b558df6
commit ae21680da8
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))
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)
})

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,6 +13,23 @@
</div>
<div class="panel-body" id="workshop"></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 class="col-sm-8 col-sm-pull-4" id="list"></div>
</div>

View File

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