Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reworking the code to use the server for all decision making in game …

…instead of client side, removing binary bson stuff
  • Loading branch information...
commit f7c71333d5dd9424e5a79e4d9a4897a71cc9d129 1 parent 0610812
@christkv authored
Showing with 131 additions and 27 deletions.
  1. +1 −0  .gitignore
  2. +20 −4 public/game.js
  3. +15 −0 public/playermongoman.js
  4. +95 −23 server.js
View
1  .gitignore
@@ -4,3 +4,4 @@
.project
.settings
node_modules/
+data/
View
24 public/game.js
@@ -182,6 +182,21 @@ var go = function() {
maingame.hud.setWidget("score",{widget:"label",font:"small",value:0, dx:240, dy:25, clear:true});
// Let's add the object that will draw the maze
gbox.addObject(drawMaze);
+ } else if(message['state'] == 'powerpill') {
+ // console.log("======================================================= powerpill");
+
+ // Set all the ghosts as eatable including the player
+ if(message['value'] == true && !isMongoman) {
+ // Set the players ghost as eatable
+ gbox.getObject("player", "playerghost").makeeatable();
+ }
+
+ // Set up all the remote ghosts
+ var keys = Object.keys(currentGhosts);
+ // For each ghost set them to eatable
+ for(var i = 0; i < keys.length; i++) {
+ currentGhosts[keys[i]].makeeatable();
+ }
} else if(message['state'] == 'dead') {
// Fetch the active mongoman
var mongoman = isMongoman ? gbox.getObject("player", "mongoman") : gbox.getObject("ghosts", "mongoman");
@@ -303,10 +318,11 @@ var go = function() {
if(this.bullettimer > 0) this.bullettimer--;
// If the user changed course send an update to the server
if(updateObject != null) {
- // Serialize the update state and send to server
- var data = BSON.serialize({'$set': {'pos': updateObject}}, false, true, false)
- // Send object
- client.dispatchCommand(data.buffer);
+ // // Serialize the update state and send to server
+ // var data = BSON.serialize({'$set': {'pos': updateObject}}, false, true, false)
+ // // Send object
+ // client.dispatchCommand(data.buffer);
+ client.dispatchCommand({type:'movement', mongoman:isMongoman, object:updateObject});
}
//
View
15 public/playermongoman.js
@@ -8,6 +8,7 @@ var createPlayerMongoman = function() {
tileset:"mongoman",
killed:false,
scorecombo:1,
+ time:-1,
initialize: function() {
// Sets up the objects, properties on our 2D map
@@ -47,6 +48,15 @@ var createPlayerMongoman = function() {
// Handle collisions with the map, accuracy and tolerance controls how precise the collision detection is
toys.topview.tileCollision(this, maze, "map", null, {tolerance: 0, approximation: 1});
+
+ // The nuber of ticks the ghost i in danger
+ if(this.time > 0) {
+ this.time--;
+ } else if(this.time == 0) {
+ // Fire eat a powerpill message
+ client.dispatchCommand({type:'powerpill', value:false});
+ this.time = -1;
+ }
// If we have a collision
if(this.touchedup || this.toucheddown || this.touchedleft || this.touchedright) {
@@ -94,6 +104,11 @@ var createPlayerMongoman = function() {
if(gbox.getObject("ghosts","ghost3")) gbox.getObject("ghosts","ghost3").makeeatable();
if(gbox.getObject("ghosts","ghost4")) gbox.getObject("ghosts","ghost4").makeeatable();
if(gbox.getObject("player", "playerghost")) gbox.getObject("player","playerghost").makeeatable();
+
+ // Fire eat a powerpill message
+ client.dispatchCommand({type:'powerpill', value:true});
+ // Set count down time
+ this.time = 150;
} else {
if(sound) SoundJS.play("eat");
maingame.hud.addValue("score","value",10);
View
118 server.js
@@ -46,7 +46,9 @@ var state = {
// Connections by board id
connectionsByBoardId: {},
- boardIdByConnections: {}
+ boardIdByConnections: {},
+ // Game states
+ gameStatesByBoardId: {}
}
// Create a server instance
@@ -235,6 +237,8 @@ if(cluster.isMaster) {
// Handle incoming messages
connection.on('message', function(message) {
+ // console.log("================================= message")
+ // console.dir(message)
// All basic communication messages are handled as JSON objects
// That includes the request for status of the board.
var self = this;
@@ -270,27 +274,95 @@ if(cluster.isMaster) {
updateGhostDeadStats(state, self, sessionId);
// Signal ghost is dead
ghostDead(state, self, messageObject);
- }
- } else if(message.type == 'binary') {
- // Binary message update player position
- state.gameCollection.update({id: self.connectionId}, message.binaryData);
- // Let's grab the record
- state.gameCollection.findOne({id: self.connectionId, state:'n'}, {raw:true}, function(err, rawDoc) {
- if(rawDoc) {
- // Retrieve the board by id from cache
- var boardId = state.boardIdByConnections[self.connectionId];
- var board = state.connectionsByBoardId[boardId];
- // If we have a board send the messages
- if(board) {
- // Send the data to all the connections expect the originating connection
- for(var i = 0; i < board.length; i++) {
- if(board[i] != self.connectionId) {
- if(state.connections[board[i]] != null) state.connections[board[i]].sendBytes(rawDoc);
- }
- }
+ } else if(messageObject['type'] == 'powerpill') {
+ // console.log("================================================ powerpill :: " + messageObject['value'])
+
+ // Retrieve the board by id from cache
+ var boardId = state.boardIdByConnections[self.connectionId];
+ // Retrieve the game stats for the board
+ var gameState = state.gameStatesByBoardId[boardId];
+ if(gameState == null) {
+ state.gameStatesByBoardId[boardId] = {};
+ gameState = state.gameStatesByBoardId[boardId];
+ }
+
+ // If we have game stats update them
+ if(gameState) {
+ gameState[self.connectionId]['powerpill'] = messageObject['value'];
+ }
+
+ // console.log("====================================================== self :: " + self.connectionId)
+ var keys = Object.keys(gameState);
+ // validate if we have a collision
+ for(var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ // console.log("====================================================== send on :: " + key)
+
+ // If it's not the originator send the powerpill in play
+ if(key != self.connectionId) {
+ state.connections[key].sendUTF(JSON.stringify({state:'powerpill', value:messageObject['value']}));
}
- }
- });
+ }
+ } else if(messageObject['type'] == 'movement') {
+ // console.log("================================================ movement")
+ // console.dir(messageObject)
+
+ // Unpack object
+ var position = messageObject['object'];
+ var mongoman = messageObject['mongoman'];
+
+ // Retrieve the board by id from cache
+ var boardId = state.boardIdByConnections[self.connectionId];
+ // Retrieve the game stats for the board
+ var gameState = state.gameStatesByBoardId[boardId];
+ if(gameState == null) {
+ state.gameStatesByBoardId[boardId] = {};
+ gameState = state.gameStatesByBoardId[boardId];
+ }
+
+ // If we have game stats update them
+ if(gameState) {
+ gameState[self.connectionId] = {mongoman: mongoman, pos: position};
+ }
+
+ var keys = Object.keys(gameState);
+ // validate if we have a collision
+ for(var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+
+ if(key != self.connectionId) {
+ // Get the position
+ var _position = gameState[key].pos;
+ // Let's check if we have a collision
+ if(_position.x < (position.x + 5) && _position.x > (position.x - 5) &&
+ _position.y < (position.y + 5) && _position.y > (position.y - 5)) {
+ console.log("======================================= collision")
+ } else {
+ // console.log("======================================= no collision")
+ }
+ }
+ }
+ }
+ // } else if(message.type == 'binary') {
+ // // Binary message update player position
+ // state.gameCollection.update({id: self.connectionId}, message.binaryData);
+ // // Let's grab the record
+ // state.gameCollection.findOne({id: self.connectionId, state:'n'}, function(err, doc) {
+ // if(doc) {
+ // // Retrieve the board by id from cache
+ // var boardId = state.boardIdByConnections[self.connectionId];
+ // var board = state.connectionsByBoardId[boardId];
+ // // If we have a board send the messages
+ // if(board) {
+ // // Send the data to all the connections expect the originating connection
+ // for(var i = 0; i < board.length; i++) {
+ // if(board[i] != self.connectionId) {
+ // if(state.connections[board[i]] != null) state.connections[board[i]].sendUTF(JSON.stringify(doc));
+ // }
+ // }
+ // }
+ // }
+ // });
}
});
});
@@ -455,10 +527,10 @@ var initializeBoard = function(_state, session, connection) {
// There is a board, we are a ghost, message the user that we are ready and also send the state of the board
connection.sendUTF(JSON.stringify({state:'initialize', isMongoman:false, name:session['name']}));
// Find all board positions and send
- _state.gameCollection.find({b:board._id}, {raw:true}).toArray(function(err, docs) {
+ _state.gameCollection.find({b:board._id}).toArray(function(err, docs) {
if(!err) {
for(var i = 0; i < docs.length; i++) {
- connection.sendBytes(docs[i]);
+ connection.sendUTF(JSON.stringify(docs[i]));
}
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.