
'use strict';

const config = require('./config');

const computeSB = require('./holdem/get-smallblind-amount');
const assignDB = require('./holdem/assign-dealer-button');
const payBlinds = require('./holdem/pay-blinds');
const shuffleCards = require('./holdem/shuffle-cards');
const assignCards = require('./holdem/assign-player-cards');

exports = module.exports = function setup(gs){

  // prepare a poker hand, so that it can be played...


  //
  // wait a few time after an hand is completed
  // before a new one can start
  return new Promise(function(res, rej){
    setTimeout(function() {

      let hasBB = Symbol.for('hasBB');

      //
      // 0) reset initial conditions

      // the amount of chips bet by all the players
      gs.pot = 0;

      // the amount of chips each player must bet
      // to continue to be "active" in the current hand
      gs.callAmount = 0;

      // the cards on the table
      gs.community_cards = [];

      gs.players.forEach(player => { player[hasBB] = false; player.chipsBet = 0; });


      //
      // 1) compute the small blind level for the current hand
      // big blinds is always the double

      gs.sb = computeSB(gs);

      //
      // 2) assign to the player in turn the dealer button
      // only active player can receive the button

      assignDB(gs);


      //
      // 3) pay small/big blinds
      // and updates players/table's chips

      payBlinds(gs);


      //
      // 4) prepare a random sorted deck of cards
      // i'm using Fisher–Yates algorithm to achieve 'randomness'

      let deck = gs._deck = shuffleCards();

      //
      // 5) assign two cards to each active player
      // first player who receive a card is the one next the dealer button
      res(assignCards(gs, deck));

    }, config.HANDWAIT);
  });

};

@@ -46,16 +46,67 @@
 fig');%0A%0A
+const status = require('./domain/player-status');%0A%0A
 const co
@@ -999,16 +999,49 @@
  = %5B%5D;%0A%0A
+      // reset player conditions%0A
       gs
@@ -1068,16 +1068,24 @@
 yer =%3E %7B
+%0A       
  player%5B
@@ -1099,16 +1099,24 @@
 = false;
+%0A       
  player.
@@ -1128,16 +1128,145 @@
 Bet = 0;
+%0A        player.cards = %5B%5D;%0A        if (player.status == status.folded)%7B%0A          player.status = status.active;%0A        %7D%0A     
  %7D);%0A%0A%0A 

reset folded during setup