Skip to content

Commit

Permalink
Room player from room when he leaves. Delete empty rooms.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlas committed Nov 4, 2012
1 parent 26c1b49 commit 8d841a0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
16 changes: 12 additions & 4 deletions game-client.js
Expand Up @@ -41,7 +41,7 @@ var ushapes = {
};

// How long to let each turn last for, in seconds
var COUNTDOWNTIME = 300; // 5 min
var COUNTDOWNTIME = 10; // 5 min

// My Turn state
var HAVETURN = null;
Expand Down Expand Up @@ -125,8 +125,9 @@ function onGetPlayers(pdata) {
$(PLAYERS).empty();
// display all players
for (var p in pdata) {
if (!pdata.hasOwnProperty(p))
if (!pdata.hasOwnProperty(p)) {
continue;
}
var turn = pdata[p]['has_turn'] ? "has_turn" : "";
$(PLAYERS).append("<dt class='" + turn + "'>" + p + "</dt>" +
"<dd>" + pdata[p]['points'] + "</dd>");
Expand Down Expand Up @@ -543,9 +544,16 @@ function drawGame() {
$(SIDEBOARD).append($(CHATPNL));
$(CHATPNL).addClass('game_chat_panel');
$(LEAVEGAME)[0].onclick = function () {
$.removeCookie('game');
location.reload();
clearTimeout(GETPLAYERSTID);
$.ajax({
type: 'DELETE',
url: "/games/" + $.cookie("game") + "/players",
data: {name: $.cookie("player")},
success: function() {
$.removeCookie('game');
location.reload();
}
});
};

// setup future calls
Expand Down
98 changes: 83 additions & 15 deletions game.js
Expand Up @@ -47,20 +47,31 @@ function Game (name) {
this.name = name;
this.board = []; // list representation
this.boardmat = []; // matrix representation
for (var i=0; i<181; i++)
for (var i=0; i<181; i++) {
this.boardmat[i] = new Array(181);
this.pieces = [];
}
this.players = {};
this.turn_pieces = []; // pieces played this turn
this.chat = [] // chat log

// board dimensions
this.dimensions = {'top': 90, 'right': 90, 'bottom': 90, 'left': 90};

/* Keep track of the pieces by having a list of piece objects each with a
* count attribute that tracks how many of that piece are left. When this
* reaches 0, we remove the piece object from the list.
*/
this.pieces = [];
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var shapes = ['circle', 'star', 'diamond', 'square', 'triangle', 'clover'];
for (c in colors) {
if (!colors.hasOwnProperty(c)) {
continue;
}
for (s in shapes) {
if (!shapes.hasOwnProperty(s)) {
continue;
}
this.pieces.push({'piece': new Piece(shapes[s], colors[c]), 'count': 3});
}
}
Expand All @@ -70,6 +81,10 @@ Game.prototype.toJSON = function() {
return {'name': this.name, 'players': this.players};
}

/**
* Draw some number of random pieces from the game stash.
* @param num: {number} number of pieces to draw
*/
Game.prototype.drawPieces = function(num) {
// draw num pieces from the pile
var draw = [];
Expand All @@ -84,6 +99,29 @@ Game.prototype.drawPieces = function(num) {
return draw;
}

/**
* Return a list of pieces to the game.
* @param pieces: {array} of Piece objects
*/
Game.prototype.returnPieces = function(pieces) {
for (var p in pieces) {
if (!pieces.hasOwnProperty(p)) {
continue;
}
var piece = pieces[p];
var found = this.pieces.some(function(x) {
if (piece.equals(x.piece)) {
x.count += 1;
return true;
}
});
if (!found) { // first piece of its kind
this.pieces.push({'piece': new Piece(piece.shape, piece.color),
'count': 1});
}
}
}

function Player (name) {
this.name = name;
this.pieces = [];
Expand Down Expand Up @@ -259,27 +297,37 @@ function requestBody(request, onEnd) {
}

/**
* End the turn for the player and start for the next.
* @param {obj} player: the player whose turn will end
* Pass the turn to the next player,
* @param game: {obj} game object
* @param player: {obj} player object
*/
function switchPlayers(game, player) {
function nextTurn(game, player) {
if (player.has_turn === false) { // we assume that player has the turn
return;
}
player.has_turn = false;

// clear pieces played this turn
game.turn_pieces = [];

// give next player the turn
var _players = Object.keys(game.players);
var next_idx = (_players.indexOf(player['name']) + 1) %
_players.length;
var next = game.players[_players[next_idx]];
next.has_turn = true;

// draw new pieces
// next player draws new pieces
next.pieces = next.pieces.concat(game.drawPieces(
6 - next.pieces.length));
}

/**
* End the turn for the player and start for the next.
* @param {obj} player: the player whose turn will end
*/
function switchPlayers(game, player) {
// clear pieces played this turn
game.turn_pieces = [];
nextTurn(game, player);
}

/**
* Create and add a player to a game.
* @param game: {obj} game object
Expand All @@ -291,7 +339,7 @@ function addPlayerToGame(game, playernm) {
game.players[p.name] = p;

// if first player, make it his turn
if (Object.keys(game.players).length == 1) {
if (Object.keys(game.players).length === 1) {
p.has_turn = true;
}
}
Expand All @@ -318,7 +366,7 @@ function handlePlayers(request, response, game, path) {
}
}
} else {
// add player
// add player to a game
var func = function(form) {
if (form && form.name) {
addPlayerToGame(game, form.name);
Expand All @@ -334,15 +382,35 @@ function handlePlayers(request, response, game, path) {
}
requestBody(request, func);
return;
} else {
} else if (request.method == 'DELETE') {
// delete player from a game
var func = function(form) {
if (form && form.name) {
player = game.players[form.name];
if (player === undefined) {
// huh? player is not in this game
response.writeHead(404, {'Content-Type': 'text/json'});
response.end();
return;
}
nextTurn(game, player);
game.returnPieces(player.pieces);
delete game.players[form.name];
if (Object.keys(game.players).length === 0) {
delete games[game.name];
}
respOk(response);
}
}
requestBody(request, func);
return;
} else {
var r = JSON.stringify(game.players);
}

} else {
// return info on a specific player

var player = game.players[path[0]];

if (typeof player === 'undefined') {
// player not found
response.writeHead(404, {'Content-Type': 'text/json'});
Expand Down
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -46,7 +46,7 @@
</div>
<div id="add_game">
<input type="text" class="text" />
<button>Create Game</button>
<button>New Game</button>
</div>
<hr/>
<div id="games"><table id="games_tbl"></table></div>
Expand Down
1 change: 1 addition & 0 deletions typography.css
Expand Up @@ -23,6 +23,7 @@ button {
text-shadow: rgba(0,0,0,.4) 0 1px 0;
font-size: 1.4em;
}
#add_game > button { font-size: 1.2em; }

#loading_game, #add_guest_form, #no_games { font-size: 2em; }

Expand Down

0 comments on commit 8d841a0

Please sign in to comment.