Simple gamespy status

This commit is contained in:
Björn Dahlgren 2014-04-08 23:49:40 +02:00
parent f203ad4a11
commit 39bbe2e0c9
8 changed files with 136 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,27 @@
<h3>Server Status from Gamespy</h3>
<% if (typeof(error) != "undefined") { %>
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Error</label>
<div class="col-sm-10">
<p class="form-control-static"><%= error %></p>
</div>
</div>
</form>
<% } else if (typeof(running) != "undefined" && running) { %>
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Players</label>
<div class="col-sm-10">
<p class="form-control-static"><%= numplayers %> / <%= maxplayers %></p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Mission</label>
<div class="col-sm-10">
<p class="form-control-static"><%= mission %></p>
</div>
</div>
</form>
<% } %>

View File

@ -5,3 +5,5 @@
<% } else { %>
<a class="btn btn-primary" href="#" id="start">Start</a>
<% } %>
<div id="gamespy"></div>

17
routes/servers/gamespy.js Normal file
View File

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