public
Created

Game Design

  • Download Gist
Game.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
* 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;
game.io.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
* 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);
});
});
});
 
 
}
 
...
...
...
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.