public function dealCardToHand(Player $player, HandBetPair $hand)
 {
     return parent::dealCardToHand($player, $hand)->then(function ($card) use($player, $hand) {
         $this->broadcast(new CardDealtMessage($player, $hand, $card));
         return $card;
     });
 }
Example #2
0
 /**
  * @param Player      $player
  * @param HandBetPair $hand
  *
  * @return Promise
  */
 private function requestPlayerAction(Player $player, HandBetPair $hand)
 {
     if ($hand->isStand() || $hand->getHand()->is21() || $hand->getHand()->isBusted()) {
         return \Blackjack\Promise\timedResolve(self::PAUSE_SPEED)->then(function () use($player) {
             return $this->table->ensurePlayerActive($player);
         });
     }
     return $this->table->requestPlayerAction($player, $hand)->then(function (HandAction $action) use($player, $hand) {
         switch ($action->getAction()) {
             case HandAction::ACTION_STAND:
                 $hand->setStand(true);
                 return $this->requestPlayerAction($player, $hand);
             case HandAction::ACTION_HIT:
                 return $this->table->dealCardToHand($player, $hand)->then(function () use($player, $hand) {
                     return $this->requestPlayerAction($player, $hand);
                 });
             case HandAction::ACTION_DOUBLE_DOWN:
                 $currentBet = $hand->getBet();
                 return $this->table->acceptPlayerBet($player, $hand, $currentBet)->then(function () use($player, $hand) {
                     $hand->setStand(true);
                     return $this->table->dealCardToHand($player, $hand);
                 })->then(function () use($player, $hand) {
                     return $this->requestPlayerAction($player, $hand);
                 });
             case HandAction::ACTION_SPLIT:
                 return $this->table->splitPlayerHand($player, $hand)->then(function () use($player) {
                     return $this->table->dealCardToHand($player, $player->getSeat()->getHands()[0]);
                 })->then(function () use($player) {
                     return $this->table->dealCardToHand($player, $player->getSeat()->getHands()[1]);
                 })->then(function () use($player) {
                     return $this->requestPlayerAction($player, $player->getSeat()->getHands()[0]);
                 })->then(function () use($player) {
                     return $this->requestPlayerAction($player, $player->getSeat()->getHands()[1]);
                 });
             default:
                 // Invalid action received - treat as stand
                 $hand->setStand(true);
                 return $this->requestPlayerAction($player, $hand);
         }
     })->otherwise(function (\Exception $reason) use($player, $hand) {
         $hand->setStand(true);
         $this->logger->error('Error while playing hand', ['player' => $player->getName(), 'reason' => $reason->getMessage()]);
         return $this->requestPlayerAction($player, $hand);
     });
 }