mirror of
https://github.com/Dahlgren/arma-server-web-admin.git
synced 2024-08-30 17:22:10 +00:00
Push missions with socket connection
This commit is contained in:
parent
e16b558df6
commit
ae21680da8
5
app.js
5
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)
|
||||
})
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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) {
|
||||
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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>
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user