From 39bbe2e0c9499f55b10f218e1d67137f72ebd000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Tue, 8 Apr 2014 23:49:40 +0200 Subject: [PATCH] Simple gamespy status --- main.js | 5 ++- package.json | 4 +- public/js/app/models/gamespy.js | 19 +++++++++ public/js/app/views/servers/gamespy.js | 54 ++++++++++++++++++++++++++ public/js/app/views/servers/info.js | 11 +++++- public/js/tpl/servers/gamespy.html | 27 +++++++++++++ public/js/tpl/servers/info.html | 2 + routes/servers/gamespy.js | 17 ++++++++ 8 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 public/js/app/models/gamespy.js create mode 100644 public/js/app/views/servers/gamespy.js create mode 100644 public/js/tpl/servers/gamespy.html create mode 100644 routes/servers/gamespy.js 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 @@ +

Server Status from Gamespy

+ +<% if (typeof(error) != "undefined") { %> +
+
+ +
+

<%= error %>

+
+
+
+<% } else if (typeof(running) != "undefined" && running) { %> +
+
+ +
+

<%= numplayers %> / <%= maxplayers %>

+
+
+
+ +
+

<%= mission %>

+
+
+
+<% } %> diff --git a/public/js/tpl/servers/info.html b/public/js/tpl/servers/info.html index bb187b8..3b05950 100644 --- a/public/js/tpl/servers/info.html +++ b/public/js/tpl/servers/info.html @@ -5,3 +5,5 @@ <% } else { %> Start <% } %> + +
diff --git a/routes/servers/gamespy.js b/routes/servers/gamespy.js new file mode 100644 index 0000000..357308a --- /dev/null +++ b/routes/servers/gamespy.js @@ -0,0 +1,17 @@ +var Gamedig = require('gamedig'); +var ip = require('ip'); + +exports.index = function (req, res){ + Gamedig.query({ + type: 'arma3', + host: ip.address() + }, + function(state) { + if(state.error) { + res.send({error: state.error}); + } else { + res.send(state.raw); + } + } + ); +};