Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geobalas committed Jul 30, 2014
1 parent 55e9a27 commit b959774
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 30 deletions.
26 changes: 7 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,10 @@ io.sockets.on('connection', function( socket ) {
else if( data.chips > tables[data.table_id].public.max_buy_in || data.chips < tables[data.table_id].public.min_buy_in )
callback( { 'success': false, 'error': 'The amount of chips should be between the maximum and the minimum amount of allowed buy in' } );
else {
// Remove the chips that player will have on the table, from the player object
players[socket.id].chips -= data.chips;
players[socket.id].public.chips_in_play = data.chips;
// Add the table info in the player object
players[socket.id].seat = data.seat;
players[socket.id].sitting_on_table = data.table_id;

// Give the response to the user
callback( { 'success': true } );
// Add the player to the table
tables[data.table_id].player_sat_on_the_table( players[socket.id], data.seat );
tables[data.table_id].player_sat_on_the_table( players[socket.id], data.seat, data.chips );
}
} else {
// If the user is not allowed to sit in, notify the user
Expand Down Expand Up @@ -272,6 +265,7 @@ io.sockets.on('connection', function( socket ) {
socket.on('post_blind', function( posted_blind, callback ) {
if( players[socket.id].sitting_on_table !== false ) {
var table_id = players[socket.id].sitting_on_table;
console.log( table_id );
var active_seat = tables[table_id].public.active_seat;

if( tables[table_id]
Expand All @@ -282,11 +276,10 @@ io.sockets.on('connection', function( socket ) {
if( posted_blind ) {
callback( { 'success': true } );
if( tables[table_id].public.phase === 'small_blind' ) {
players[socket.id].bet( tables[table_id].public.small_blind );
// The player posted the small blind
tables[table_id].player_posted_small_blind();
} else {
// The player posted the big blind
players[socket.id].bet( tables[table_id].public.big_blind );
tables[table_id].player_posted_big_blind();
}
} else {
Expand Down Expand Up @@ -330,7 +323,6 @@ io.sockets.on('connection', function( socket ) {
if( tables[table_id] && tables[table_id].seats[active_seat].socket.id === socket.id && ['preflop','flop','turn','river'].indexOf(tables[table_id].public.phase) > -1 ) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
players[socket.id].fold();
tables[table_id].player_folded();
}
}
Expand All @@ -346,8 +338,6 @@ io.sockets.on('connection', function( socket ) {
var active_seat = tables[table_id].public.active_seat;

if( tables[table_id] && tables[table_id].seats[active_seat].socket.id === socket.id && tables[table_id].public.biggest_bet && ['preflop','flop','turn','river'].indexOf(tables[table_id].public.phase) > -1 ) {
var called_amount = tables[table_id].public.biggest_bet - players[socket.id].public.bet;
players[socket.id].bet( called_amount );
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
tables[table_id].player_called();
Expand All @@ -371,8 +361,7 @@ io.sockets.on('connection', function( socket ) {
if ( amount && isFinite( amount ) && amount <= tables[table_id].seats[active_seat].public.chips_in_play ) {
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
players[socket.id].bet( amount );
tables[table_id].player_betted();
tables[table_id].player_betted( amount );
}
}
}
Expand Down Expand Up @@ -402,13 +391,12 @@ io.sockets.on('connection', function( socket ) {
amount = parseInt( amount );
if ( amount && isFinite( amount ) ) {
amount -= tables[table_id].seats[active_seat].public.bet;
console.log( 'THE AMOUNT IS: ' + amount );
if( amount <= tables[table_id].seats[active_seat].public.chips_in_play ) {
console.log('passed');
// Sending the callback first, because the next functions may need to send data to the same player, that shouldn't be overwritten
callback( { 'success': true } );
players[socket.id].raise( amount );
tables[table_id].player_raised( players[socket.id].seat );
console
// The amount should not include amounts previously betted
tables[table_id].player_raised( amount );
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions poker_modules/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var Player = function( socket, name, chips ) {
this.seat = null;
// The cards that the player is holding
this.cards = [];
// The total bets by this player in this round
this.total_bets = 0;
// The hand that the player has in the current poker round and its rating
this.evaluated_hand = {};
}
Expand All @@ -55,6 +53,21 @@ Player.prototype.leave_table = function() {
}
}

/**
* Sits the player on the table
* @param string table_id
* @param number seat
* @param number chips
*/
Player.prototype.sit_on_table = function( table_id, seat, chips ) {
// Remove the chips that player will have on the table, from the player object
this.chips -= chips;
this.public.chips_in_play = chips;
// Add the table info in the player object
this.seat = seat;
this.sitting_on_table = table_id;
}

/**
* Updates the player data when they sit out
*/
Expand Down Expand Up @@ -85,7 +98,6 @@ Player.prototype.bet = function( amount ) {
}
this.public.chips_in_play -= amount;
this.public.bet += +amount;
this.total_bets += +amount;
}

/**
Expand All @@ -98,7 +110,6 @@ Player.prototype.raise = function( amount ) {
}
this.public.chips_in_play -= amount;
this.public.bet += +amount;
this.total_bets += +amount;
}

/**
Expand All @@ -109,7 +120,6 @@ Player.prototype.prepare_for_new_round = function() {
this.public.cards = [];
this.public.has_cards = false;
this.public.bet = 0;
this.total_bets = 0;
this.public.in_hand = true;
this.evaluated_hand = {};
}
Expand Down
9 changes: 8 additions & 1 deletion poker_modules/pot.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ Pot.prototype.add_table_bets = function( players ) {
}
}

/**
* Adds the player's bets to the pot
* @param {[type]} player [description]
*/
Pot.prototype.add_players_bets = function( player ) {
// Getting the current pot (the one in which new bets should be added)
var current_pot = this.pots.length-1;

this.pots[current_pot].amount += player.public.bet;
player.public.bet = 0;
// If the player is not in the list of contributors, add them
if( !this.pots[current_pot].contributors.indexOf( player.seat ) ) {
this.pots[current_pot].contributors.push( player.seat );
}
Expand All @@ -108,6 +113,7 @@ Pot.prototype.destribute_to_winners = function( players, first_player_to_act ) {
var pots_count = this.pots.length;
var messages = [];

// For each one of the pots, starting from the last one
for( var i=pots_count-1 ; i>=0 ; i-- ) {
var winners = [];
var best_rating = 0;
Expand All @@ -132,10 +138,11 @@ Pot.prototype.destribute_to_winners = function( players, first_player_to_act ) {
var winners_count = winners.length;

var winnings = ~~( this.pots[i].amount / winners_count );
var odd_chip = winnings * winners_count !== this.pots[i].amount;

for( var j in winners ) {
var players_winnings = 0;
if( players[winners[j]].seat === first_player_to_act ) {
if( odd_chip && players[winners[j]].seat === first_player_to_act ) {
players_winnings = winnings + 1;
} else {
players_winnings = winnings;
Expand Down
24 changes: 19 additions & 5 deletions poker_modules/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,11 @@ Table.prototype.end_phase = function() {
* @param int seat
*/
Table.prototype.player_posted_small_blind = function() {
var bet = this.seats[this.public.active_seat].public.chips_in_play >= this.public.small_blind ? this.public.small_blind : this.seats[this.public.active_seat].public.chips_in_play;
this.seats[this.public.active_seat].bet( bet );
this.public.log.message = this.seats[this.public.active_seat].public.name + ' posted the small blind';
this.public.log.action = 'bet';
this.public.biggest_bet = this.public.small_blind;
this.public.biggest_bet = bet;
this.emit_event( 'table_data', this.public );
this.initialize_big_blind();
}
Expand All @@ -421,9 +423,11 @@ Table.prototype.player_posted_small_blind = function() {
* @param int seat
*/
Table.prototype.player_posted_big_blind = function() {
var bet = this.seats[this.public.active_seat].public.chips_in_play >= this.public.big_blind ? this.public.big_blind : this.seats[this.public.active_seat].public.chips_in_play;
this.seats[this.public.active_seat].bet( bet );
this.public.log.message = this.seats[this.public.active_seat].public.name + ' posted the big blind';
this.public.log.action = 'bet';
this.public.biggest_bet = this.public.big_blind;
this.public.biggest_bet = this.public.biggest_bet < bet ? bet : this.public.biggest_bet;
this.emit_event( 'table_data', this.public );
this.initialize_preflop();
}
Expand All @@ -432,6 +436,8 @@ Table.prototype.player_posted_big_blind = function() {
* Checks if the round should continue after a player has folded
*/
Table.prototype.player_folded = function() {
this.seats[this.public.active_seat].fold();

this.public.log.message = this.seats[this.public.active_seat].public.name + ' folded';
this.public.log.action = 'fold';
this.emit_event( 'table_data', this.public );
Expand Down Expand Up @@ -471,6 +477,9 @@ Table.prototype.player_checked = function() {
* When a player calls
*/
Table.prototype.player_called = function() {
var called_amount = this.public.biggest_bet - this.seats[this.public.active_seat].public.bet;
this.seats[this.public.active_seat].bet( called_amount );

this.public.log.message = this.seats[this.public.active_seat].public.name + ' called';
this.public.log.action = 'call';
this.emit_event( 'table_data', this.public );
Expand All @@ -485,7 +494,9 @@ Table.prototype.player_called = function() {
/**
* When a player bets
*/
Table.prototype.player_betted = function() {
Table.prototype.player_betted = function( amount ) {
this.seats[this.public.active_seat].bet( amount );

this.public.biggest_bet = this.seats[this.public.active_seat].public.bet;
this.public.log.message = this.seats[this.public.active_seat].public.name + ' betted';
this.public.log.action = 'bet';
Expand All @@ -498,7 +509,8 @@ Table.prototype.player_betted = function() {
/**
* When a player raises
*/
Table.prototype.player_raised = function() {
Table.prototype.player_raised = function( amount ) {
this.seats[this.public.active_seat].raise( amount );
this.public.biggest_bet = this.seats[this.public.active_seat].public.bet;
this.public.log.message = this.seats[this.public.active_seat].public.name + ' raised';
this.public.log.action = 'raise';
Expand All @@ -513,10 +525,12 @@ Table.prototype.player_raised = function() {
* @param object player
* @param int seat
*/
Table.prototype.player_sat_on_the_table = function( player, seat ) {
Table.prototype.player_sat_on_the_table = function( player, seat, chips ) {
this.seats[seat] = player;
this.public.seats[seat] = player.public;

this.seats[seat].sit_on_table( this.public.id, seat, chips );

// Increase the counters of the table
this.public.players_seated_count++;

Expand Down
66 changes: 66 additions & 0 deletions spec/preflop_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var Player = require('../poker_modules/player.js');
var Table = require('../poker_modules/table.js');

var event_emitter = function( table_id ) {
return function ( event_name, event_data ) {
}
}

var socket = {
emit: function() {
return;
}
};

function initialize_test_table() {
var players = [],
table;

table = new Table( 0, 'Sample 10-handed Table', event_emitter(0), 10, 2, 1, 200, 40, false );

for( var i=0 ; i<4 ; i++ ) {
players[i] = new Player( socket, 'Player_'+i, 1000 );
players[i].socket = socket;

}

table.player_sat_on_the_table( players[0], 2, 1000 );
table.player_sat_on_the_table( players[1], 6, 1000 );
table.player_sat_on_the_table( players[2], 4, 1000 );
table.player_sat_on_the_table( players[3], 8, 1000 );

return table;
}

describe("Posting the small blind", function() {

var table;

beforeEach(function() {
table = initialize_test_table();
});

it("should make the next player active", function() {
if( table.public.active_seat === 2 ) {
table.player_posted_small_blind();
expect( table.public.active_seat ).toEqual( 6 );
} else {
table.player_posted_small_blind();
expect( table.public.active_seat ).toEqual( 2 );
}
});

it("should not affect the players sitting out", function() {
table.player_posted_small_blind();
var current_player = table.public.active_seat;
for( var i=0 ; i<table.players_seated_count ; i++ ) {
table.find_next_player();
}
expect( table.public.active_seat ).toEqual( current_player );
});

it("should proceed to the big blind phase", function() {
table.player_posted_small_blind();
expect( table.public.phase ).toEqual( 'big_blind' );
});
});
Loading

0 comments on commit b959774

Please sign in to comment.