Skip to content

Commit

Permalink
Include apples and powerups in game reindex response
Browse files Browse the repository at this point in the history
  • Loading branch information
blaise-io committed Jan 13, 2013
1 parent 2f6f2d6 commit c4be391
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
27 changes: 26 additions & 1 deletion server/lib/game.js
Expand Up @@ -144,11 +144,36 @@ Game.prototype = {
* @param client
*/
emitState: function(client) {
this.emitSnakes(client);
this.emitSpawns(client);
},

/**
* @param client
*/
emitSnakes: function(client) {
var data = [];
for (var i = 0, m = this.snakes.length; i < m; i++) {
data.push([i, this.snakes[i].parts, this.snakes[i].direction]);
}
client.emit(events.CLIENT_GAME_STATE, data);
client.emit(events.CLIENT_GAME_SNAKES, data);
},

/**
* Spawns a sent as separate messages.
* @param client
*/
emitSpawns: function(client) {
var spawner = this.spawner,
spawns = spawner.spawns,
data = [];
for (var i = 0, m = spawns.length; i < m; i++) {
var spawn = spawns[i];
if (null !== spawn) {
data.push([spawner.EVENTS[spawn.type], [i, spawn.location]]);
}
}
client.emit(events.CLIENT_GAME_SPAWNS, data);
},

/**
Expand Down
2 changes: 1 addition & 1 deletion server/lib/spawner.js
Expand Up @@ -97,7 +97,7 @@ Spawner.prototype = {
handleHits: function(client, location) {
var hits = [];
for (var i = 0, m = this.spawns.length; i < m; i++) {
if (this.spawns[i] && Util.eq(this.spawns[i].location, location)) {
if (null !== this.spawns[i] && Util.eq(this.spawns[i].location, location)) {
hits.push(i);
this.hit(client, i);
}
Expand Down
3 changes: 2 additions & 1 deletion server/shared/events.js
Expand Up @@ -15,7 +15,8 @@ module.exports = {

CLIENT_GAME_COUNTDOWN: 'CG1',
CLIENT_GAME_START : 'CG2',
CLIENT_GAME_STATE : 'CG3',
CLIENT_GAME_SNAKES : 'CG3',
CLIENT_GAME_SPAWNS : 'CG4',

CLIENT_POWERUP_HIT : 'CP1',
CLIENT_POWERUP_SPAWN : 'CP2',
Expand Down
4 changes: 2 additions & 2 deletions source/js/consts.js
Expand Up @@ -5,8 +5,8 @@
/** @const */ XSS.PIXELS_H = 256;
/** @const */ XSS.PIXELS_V = 161;

/** @const */ XSS.MIN_DELTA = 7;
/** @const */ XSS.MAX_DELTA = 200;
/** @const */ XSS.MIN_DELTA = 5;
/** @const */ XSS.MAX_DELTA = 250;

/** @const */ XSS.UI_MAX_NAME_WIDTH = 40;

Expand Down
8 changes: 6 additions & 2 deletions source/js/game.js
Expand Up @@ -190,9 +190,13 @@ Game.prototype = {
snake.elapsed += delta;
}
} else {
if (!this.gameStateReq) {
// TODO: Remove deltaOK as trigger for reindexing
if (!this._gameStateReq) {
XSS.socket.emit(XSS.events.SERVER_GAME_STATE);
this.gameStateReq = true;
this._gameStateReq = true;
window.setTimeout(function() {
this._gameStateReq = false;
}.bind(this), 1500);
}
}
},
Expand Down
19 changes: 16 additions & 3 deletions source/js/socket.js
Expand Up @@ -33,6 +33,8 @@ Socket.prototype = {
_addEventListeners: function(callback) {
var events = XSS.events, map = {};

this.map = map;

map[events.CLIENT_CONNECT] = callback;
map['disconnect'] = this.disconnect;

Expand All @@ -43,7 +45,8 @@ Socket.prototype = {
map[events.CLIENT_CHAT_NOTICE] = this.chatNotice;
map[events.CLIENT_GAME_COUNTDOWN] = this.gameCountdown;
map[events.CLIENT_GAME_START] = this.gameStart;
map[events.CLIENT_GAME_STATE] = this.gameState;
map[events.CLIENT_GAME_SNAKES] = this.gameSnakes;
map[events.CLIENT_GAME_SPAWNS] = this.gameSpawns;
map[events.CLIENT_SNAKE_UPDATE] = this.snakeUpdate;
map[events.CLIENT_SNAKE_CRASH] = this.snakeCrash;
map[events.CLIENT_SNAKE_ACTION] = this.snakeAction;
Expand Down Expand Up @@ -123,13 +126,23 @@ Socket.prototype = {
},

/**
* Combined package, delegate.
* @param {Array.<Array>} data
*/
gameState: function(data) {
gameSnakes: function(data) {
for (var i = 0, m = data.length; i < m; i++) {
this.snakeUpdate(data[i]);
}
XSS.room.game.gameStateReq = false;
},

/**
* Combined package, delegate.
* @param {Array.<Array>} data
*/
gameSpawns: function(data) {
for (var i = 0, m = data.length; i < m; i++) {
this.map[data[i][0]](data[i][1]);
}
},

/**
Expand Down

0 comments on commit c4be391

Please sign in to comment.