Skip to content

@ctriolo /Game.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.