Skip to content

Commit

Permalink
add id on gameserver
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Sep 30, 2014
1 parent f863265 commit 8085080
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/test/test-game.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ var TestGame = function(options) {
return id;
}

this.getIdFromGameServer = function() {
return server.id
};

this.close = function() {
wsclient.close();
};
Expand Down
28 changes: 28 additions & 0 deletions public/hft/0.x.x/scripts/gameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ define([
var _players = {}; // by id
var _totalPlayerCount = 0;
var _eventListeners = {};
var _id;
var _getters = {
id: function() {
throw "you can't read the id before you've connected";
},
};

if (!options.gameId) {
var m = /games\/([^\/]+)\//.exec(window.location.href);
Expand All @@ -81,6 +87,13 @@ define([
}
}

Object.defineProperty(this, 'id', {
get: function() { return _getters.id(); },
set: function() {},
enumerable: true,
configurable: true,
});

/**
* Event that we've connected to happyFunTimes
*
Expand Down Expand Up @@ -189,6 +202,10 @@ define([
};

var handleGameStart_ = function(msg) {
_id = msg.data.id;
_getters.id = function() {
return _id;
};
sendEvent_('connect', [msg.data]);
};

Expand Down Expand Up @@ -234,6 +251,17 @@ define([
_socket.send(_sendQueue[ii]);
}
_sendQueue = [];

// This `preconnect` message is a kind of hack.
// This used to be a `connect` message but
// I needed to be able to pass an `id` back to the
// connect message. On top of that, GameSupport
// was using the `connect` message. In order to allow
// games using `GameSupport` to use the `connect` message
// I changed this to `_hft_preconnect`. Games can therefore
// receive teh `connect` message. I changed `GameSupport`
// to a watch for `_hft_preconnect`
sendEvent_('_hft_preconnect');
}.bind(this);

var disconnected_ = function() {
Expand Down
8 changes: 8 additions & 0 deletions test/server/server-version-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ describe('server versions', function() {
testGame1.isConnected().should.be.ok;
testGame2.isConnected().should.be.ok;

testGame1.getId().should.be.eql("aaa");
testGame2.getId().should.be.eql("bbb");
testGame1.getId().should.be.eql(testGame1.getIdFromGameServer());
testGame2.getId().should.be.eql(testGame2.getIdFromGameServer());

var testCtrl = new TestController({
gameId: gameId,
hftServer: hftServer,
Expand Down Expand Up @@ -266,6 +271,9 @@ describe('server versions', function() {
var testGame1Id = testGame1.getId();
var testGame2Id = testGame2.getId();

testGame1Id.should.be.eql(testGame1.getIdFromGameServer());
testGame2Id.should.be.eql(testGame2.getIdFromGameServer());

var testCtrl = new TestController({
gameId: gameId,
hftServer: hftServer,
Expand Down

0 comments on commit 8085080

Please sign in to comment.