private function distributeWinnings()
    {
        $this->chipPots()
            ->reverse()
            ->each(function (ChipPot $chipPot) {
                // if only 1 player participated to pot, he wins it no arguments
                if ($chipPot->players()->count() === 1) {
                    $potTotal = $chipPot->chips()->total();

                    $chipPot->players()->first()->chipStack()->add($potTotal);

                    $this->chipPots()->remove($chipPot);

                    return;
                }

                $activePlayers = $chipPot->players()->diff($this->foldedPlayers());

                $playerHands = $this->dealer()->hands()->findByPlayers($activePlayers);
                $evaluate = $this->dealer()->evaluateHands($this->dealer()->communityCards(), $playerHands);

                // if just 1, the player with that hand wins
                if ($evaluate->count() === 1) {
                    $player = $evaluate->first()->hand()->player();
                    $potTotal = $chipPot->chips()->total();

                    $player->chipStack()->add($potTotal);

                    $this->chipPots()->remove($chipPot);
                } else {
                    // if > 1 hand is evaluated as highest, split the pot evenly between the players

                    $potTotal = $chipPot->chips()->total();

                    // split the pot between the number of players
                    $splitTotal = Chips::fromAmount(($potTotal->amount() / $evaluate->count()));
                    $evaluate->each(function (CardResults $result) use ($splitTotal) {
                        $result->hand()->player()->chipStack()->add($splitTotal);
                    });

                    $this->chipPots()->remove($chipPot);
                }
            })
        ;
    }
private function distributeWinnings()
    {
        $this->chipPots()
            ->reverse()
            ->each(function (ChipPot $chipPot) {
                // if only 1 player participated to pot, he wins it no arguments
                if ($chipPot->players()->count() === 1) {
                    $potTotal = $chipPot->chips()->total();

                    $chipPot->players()->first()->chipStack()->add($potTotal);

                    $this->chipPots()->remove($chipPot);

                    return;
                }

                $activePlayers = $chipPot->players()->diff($this->foldedPlayers());

                $playerHands = $this->dealer()->hands()->findByPlayers($activePlayers);
                $evaluate = $this->dealer()->evaluateHands($this->dealer()->communityCards(), $playerHands);

                // if just 1, the player with that hand wins
                if ($evaluate->count() === 1) {
                    $player = $evaluate->first()->hand()->player();
                    $potTotal = $chipPot->chips()->total();

                    $player->chipStack()->add($potTotal);

                    $this->chipPots()->remove($chipPot);
                } else {
                    // if > 1 hand is evaluated as highest, split the pot evenly between the players

                    $potTotal = $chipPot->chips()->total();

                    // split the pot between the number of players
                    $splitTotal = Chips::fromAmount(($potTotal->amount() / $evaluate->count()));
                    $evaluate->each(function (CardResults $result) use ($splitTotal) {
                        $result->hand()->player()->chipStack()->add($splitTotal);
                    });

                    $this->chipPots()->remove($chipPot);
                }
            })
        ;
    }
private function distributeWinnings ( ) { $ this -> chipPots ( ) -> reverse ( ) -> each ( function ( ChipPot $ chipPot ) { // if only 1 player participated to pot, he wins it no arguments if ( $ chipPot -> players ( ) -> count ( ) === 1 ) { $ potTotal = $ chipPot -> chips ( ) -> total ( ) ; $ chipPot -> players ( ) -> first ( ) -> chipStack ( ) -> add ( $ potTotal ) ; $ this -> chipPots ( ) -> remove ( $ chipPot ) ; return ; } $ activePlayers = $ chipPot -> players ( ) -> diff ( $ this -> foldedPlayers ( ) ) ; $ playerHands = $ this -> dealer ( ) -> hands ( ) -> findByPlayers ( $ activePlayers ) ; $ evaluate = $ this -> dealer ( ) -> evaluateHands ( $ this -> dealer ( ) -> communityCards ( ) , $ playerHands ) ; // if just 1, the player with that hand wins if ( $ evaluate -> count ( ) === 1 ) { $ player = $ evaluate -> first ( ) -> hand ( ) -> player ( ) ; $ potTotal = $ chipPot -> chips ( ) -> total ( ) ; $ player -> chipStack ( ) -> add ( $ potTotal ) ; $ this -> chipPots ( ) -> remove ( $ chipPot ) ; } else { // if > 1 hand is evaluated as highest, split the pot evenly between the players $ potTotal = $ chipPot -> chips ( ) -> total ( ) ; // split the pot between the number of players $ splitTotal = Chips :: fromAmount ( ( $ potTotal -> amount ( ) / $ evaluate -> count ( ) ) ) ; $ evaluate -> each ( function ( CardResults $ result ) use ( $ splitTotal ) { $ result -> hand ( ) -> player ( ) -> chipStack ( ) -> add ( $ splitTotal ) ; } ) ; $ this -> chipPots ( ) -> remove ( $ chipPot ) ; } } ) ; }
Runs over each chipPot and assigns the chips to the winning player.
Runs over each chipPot and assigns the chips to the winning player .