diff --git a/main.js b/main.js index c332110..07a3a8d 100644 --- a/main.js +++ b/main.js @@ -15,9 +15,12 @@ var servers = require('./routes/servers'); app.resource('api/missions', require('./routes/missions')); app.resource('api/mods', require('./routes/mods')); -app.resource('api/servers', servers); +var serversResource = app.resource('api/servers', servers); app.resource('api/settings', require('./routes/settings')); +var gamespyResource = app.resource('gamespy', require('./routes/servers/gamespy')); +serversResource.add(gamespyResource); + app.get('/api/servers/:server/start', servers.start); app.get('/api/servers/:server/stop', servers.stop); diff --git a/package.json b/package.json index f08f3fd..23625ed 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "express": "3.x", "slug": "~0.4.0", "express-resource": "~1.0.0", - "when": "~3.1.0" + "when": "~3.1.0", + "gamedig": "^0.2.4", + "ip": "^0.3.0" } } diff --git a/public/js/app/models/gamespy.js b/public/js/app/models/gamespy.js new file mode 100644 index 0000000..ae10d74 --- /dev/null +++ b/public/js/app/models/gamespy.js @@ -0,0 +1,19 @@ +define(function (require) { + + "use strict"; + + var $ = require('jquery'), + _ = require('underscore'), + Backbone = require('backbone'); + + return Backbone.Model.extend({ + initialize: function(attributes, options) { + this.server = options.server; + }, + + url : function(){ + return this.server.url() + '/gamespy'; + } + }); + +}); diff --git a/public/js/app/views/servers/gamespy.js b/public/js/app/views/servers/gamespy.js new file mode 100644 index 0000000..53c32d8 --- /dev/null +++ b/public/js/app/views/servers/gamespy.js @@ -0,0 +1,54 @@ +define(function (require) { + + "use strict"; + + var $ = require('jquery'), + _ = require('underscore'), + Backbone = require('backbone'), + Marionette = require('marionette'), + Gamespy = require('app/models/gamespy'), + tpl = require('text!tpl/servers/gamespy.html'); + + return Marionette.ItemView.extend({ + template: _.template(tpl), + + initialize: function (options) { + this.model = new Gamespy({running: false}, {server: options.server}); + + this.clockInterval = this.startUpdating(); + this.refresh(); + }, + + onBeforeClose: function(){ + this.stopUpdating(); + + return true; + }, + + refresh: function () { + if (this.options.server.get("pid")) { + var self = this; + this.model.fetch({ + success: function() { + self.model.set("running", true); + self.render(); + } + }); + } else { + this.model.set("running", false) + } + }, + + startUpdating: function(){ + return setInterval( + this.refresh.bind(this), + 5000 + ) + }, + + stopUpdating: function(){ + clearInterval(this.clockInterval); + }, + }); + +}); diff --git a/public/js/app/views/servers/info.js b/public/js/app/views/servers/info.js index 0410649..2335442 100644 --- a/public/js/app/views/servers/info.js +++ b/public/js/app/views/servers/info.js @@ -6,16 +6,25 @@ define(function (require) { _ = require('underscore'), Backbone = require('backbone'), Marionette = require('marionette'), + GamespyView = require('app/views/servers/gamespy'), tpl = require('text!tpl/servers/info.html'); - return Marionette.ItemView.extend({ + return Marionette.Layout.extend({ template: _.template(tpl), + regions: { + gamespyView: "#gamespy", + }, + events: { "click #start": "start", "click #stop": "stop", }, + onRender: function() { + this.gamespyView.show(new GamespyView({server: this.model})); + }, + start: function (event) { var self = this; event.preventDefault(); diff --git a/public/js/tpl/servers/gamespy.html b/public/js/tpl/servers/gamespy.html new file mode 100644 index 0000000..a165031 --- /dev/null +++ b/public/js/tpl/servers/gamespy.html @@ -0,0 +1,27 @@ +