private function getResult()
 {
     if ($this->winnings === $this->hand->getBet()) {
         return ['status' => 'push', 'winnings' => $this->winnings];
     } elseif ($this->winnings >= $this->hand->getBet()) {
         return ['status' => 'win', 'winnings' => $this->winnings];
     } else {
         return ['status' => 'lose'];
     }
 }
 /**
  * @param HandBetPair $hand
  * @param DealerHand  $dealerHand
  *
  * @return Promise
  */
 public function requestAction(HandBetPair $hand, DealerHand $dealerHand)
 {
     return \Blackjack\Promise\timedResolve(self::RESPONSE_SPEED)->then(function () use($hand, $dealerHand) {
         if ($this->getSeat() === null) {
             return \React\Promise\reject(new \RuntimeException('Player kicked'));
         }
         if ($hand->getHand()->canSplitPair()) {
             return \React\Promise\resolve(new HandAction(HandAction::ACTION_SPLIT));
         }
         if ($hand->getHand()->getValue() === 10) {
             return \React\Promise\resolve(new HandAction(HandAction::ACTION_DOUBLE_DOWN));
         }
         if ($hand->getHand()->getValue() < 17) {
             return \React\Promise\resolve(new HandAction(HandAction::ACTION_HIT));
         } else {
             return \React\Promise\resolve(new HandAction(HandAction::ACTION_STAND));
         }
     });
 }
Example #3
0
 public function distributePlayerWinnings(Player $player, HandBetPair $hand)
 {
     return \Blackjack\Promise\timedResolve(self::ACTION_SPEED)->then(function () use($player) {
         return $this->ensurePlayerActive($player);
     })->then(function () use($player, $hand) {
         if ($hand->getHand()->beatsHand($this->dealerHand)) {
             // Player wins
             $winnings = $hand->getBet() + (int) ($hand->getBet() * $this->computeWinningModifier($hand->getHand()));
             $player->getChips()->acceptWinnings($winnings);
         } elseif ($hand->getHand()->drawnWithHand($this->dealerHand)) {
             // It's a push
             $winnings = $hand->getBet();
             $player->getChips()->acceptWinnings($winnings);
         } else {
             // Player lost
             $winnings = 0;
         }
         return $winnings;
     });
 }
 /**
  * @param HandBetPair $hand
  * @param DealerHand  $dealerHand
  *
  * @return Promise
  */
 public function requestAction(HandBetPair $hand, DealerHand $dealerHand)
 {
     $this->connection->sendMessage(new RequestActionMessage($this, $dealerHand, $hand->getHand()));
     return $this->connection->waitOnMessage(PlayerActionMessage::class);
 }
Example #5
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);
     });
 }