Created
March 26, 2012 20:48
-
-
Save ctriolo/2209558 to your computer and use it in GitHub Desktop.
Game Design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Game SocketIO Controller | |
* | |
* Controller between the client and the model. | |
*/ | |
var gameProvider = require('../models/GameProvider').getInstance(); | |
module.exports = function(sockets) { | |
sockets.on('connection', function(socket) { | |
socket.on('join', function(gameID, userID) { | |
socket.set('gameID', gameID, function() { | |
socket.set('userID', userID, function() { | |
sockets.to(gameID).emit('playerJoined', playerID, userID); | |
}); | |
}); | |
socket.on('placeSettlement', function(playerID, intersectionID) { | |
socket.get('gameID', function(err, gameID) { | |
var game = GameProvider.getGameById(gameID); | |
try { | |
game.placeSettlement(player, intersectionID); | |
} catch(err) { | |
socket.send(err); | |
return; | |
} | |
GameProvider.save(game); | |
sockets.to(gameID).emit('placeSettlement', playerID, intersectionID); | |
}); | |
}); | |
}); | |
} | |
... | |
... | |
... | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Game.js | |
* | |
* Sever side Game Object | |
*/ | |
function Game() {...}; | |
Game.prototype._validateMethodCall = function(method_name, caller_player) { | |
switch (this.state) { | |
case 'ACTION_PHASE': | |
switch(method_name) { | |
case 'placeSettlement': | |
if (player != this.currentPlayer) throw 'Invalid Action'; | |
... | |
... | |
} | |
}; | |
Game.prototype.addSettlement = function(player, intersectionID) { | |
error = this._validateMethodCall('addSettlement', player); | |
// VALIDATE INTERSECTION | |
if (error) throw 'Not a valid location.'; | |
// VALIDATE USER RESOURCES | |
if (error) throw 'Not enough resources.'; | |
// DO WORK | |
// DO NOT RETURN ANYTHING | |
}; | |
module.exports = Game; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment