Skip to content

Instantly share code, notes, and snippets.

@ctriolo
Created March 26, 2012 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctriolo/2209558 to your computer and use it in GitHub Desktop.
Save ctriolo/2209558 to your computer and use it in GitHub Desktop.
Game Design
/**
* 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);
});
});
});
}
...
...
...
};
/**
* 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