Ejemplo n.º 1
0
        /// <summary>
        /// Calculates equity for the specified player if possible
        /// </summary>
        /// <param name="equityPlayers">Players to calculate equity</param>
        /// <param name="handHistory">Hand history</param>
        private void CalculateEquity(List <EquityPlayer> equityPlayers, List <EquityPlayer> mainPotPlayers, HandHistory handHistory, GeneralGameTypeEnum gameType, int potIndex)
        {
            var playersByName = handHistory.Players
                                .GroupBy(x => x.PlayerName)
                                .ToDictionary(x => x.Key, x => x.FirstOrDefault());

            // equity can be calculated only for player who didn't fold, whose last action was before river, whose hole cards are known
            var eligibleEquityPlayers = equityPlayers
                                        .Where(x => x.LastAction.Street != Street.River &&
                                               !x.LastAction.IsFold &&
                                               playersByName.ContainsKey(x.Name) && playersByName[x.Name].hasHoleCards)
                                        .ToList();

            // equity can't be calculated for single player
            if (eligibleEquityPlayers.Count < 2)
            {
                return;
            }

            // set known hole cards
            eligibleEquityPlayers.ForEach(p => p.HoleCards = playersByName[p.Name].HoleCards);

            try
            {
                var street = eligibleEquityPlayers.Select(x => x.LastAction.Street).Distinct().Max();

                var boardCards = CardHelper.IsStreetAvailable(handHistory.CommunityCards.ToString(), street)
                     ? handHistory.CommunityCards.GetBoardOnStreet(street)
                     : handHistory.CommunityCards;

                var deadCards = mainPotPlayers.Except(equityPlayers).Where(x => x.HoleCards != null).SelectMany(x => x.HoleCards).ToArray();

                if (handHistory.GameDescription.TableType.Contains(HandHistories.Objects.GameDescription.TableTypeDescription.ShortDeck))
                {
                    deadCards = deadCards.Concat(CardGroup.GetDeadCardsForHoldem6Plus()).ToArray();
                }

                if (pokerEvalLibLoaded)
                {
                    CalculateEquity(eligibleEquityPlayers, street, boardCards, deadCards, gameType, potIndex);
                }
                else if (gameType == GeneralGameTypeEnum.Holdem)
                {
                    CalculateHoldemEquity(eligibleEquityPlayers, street, boardCards, deadCards, potIndex);
                }
                else
                {
                    CalculateOmahaEquity(eligibleEquityPlayers, street, boardCards, gameType == GeneralGameTypeEnum.OmahaHiLo, potIndex);
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(this, $"Could not calculate equity for hand #{handHistory.HandId}", e);
            }
        }
Ejemplo n.º 2
0
        private void UpdatePlayersEquityWin(ReplayerTableState state)
        {
            if (state == null)
            {
                return;
            }

            //preparing for formula Card on the Board in dependence of street in current state
            switch (state.CurrentAction.Street)
            {
            case Street.Preflop:
                CurrentBoard      = new Card[] { };
                CurrentBoardCards = string.Empty;
                break;

            case Street.Flop:
                CurrentBoard      = CurrentGame.CommunityCards.Take(3).ToArray();
                CurrentBoardCards = new string(CurrentGame.CommunityCardsString.Take(6).ToArray());
                break;

            case Street.Turn:
                CurrentBoard      = CurrentGame.CommunityCards.Take(4).ToArray();
                CurrentBoardCards = new string(CurrentGame.CommunityCardsString.Take(8).ToArray());
                break;

            case Street.River:
                CurrentBoard      = CurrentGame.CommunityCards.ToArray();
                CurrentBoardCards = CurrentGame.CommunityCardsString;
                break;

            case Street.Showdown:
                CurrentBoard      = CurrentGame.CommunityCards.ToArray();
                CurrentBoardCards = CurrentGame.CommunityCardsString;
                break;

            case Street.Summary:
                CurrentBoard      = CurrentGame.CommunityCards.ToArray();
                CurrentBoardCards = CurrentGame.CommunityCardsString;
                break;
            }

            // finding all players having hole cards
            ActivePlayerHasHoleCard = CurrentGame.Players.Where(pl => pl.hasHoleCards).ToList();

            // searching for dead cards and removing this player from list of ActivePlayerHasHoleCard
            ActivePlayerHasHoleCardFolded = new List <Player>();

            AllDeadCards.Clear();

            foreach (ReplayerTableState replayerTableState in TableStateList)
            {
                Player playerInTableState = CurrentGame.Players.FirstOrDefault(x => x.PlayerName == replayerTableState.CurrentAction.PlayerName);

                if (playerInTableState != null &&
                    TableStateList.IndexOf(replayerTableState) <= TableStateList.IndexOf(state) &&
                    replayerTableState.CurrentAction.IsFold &&
                    playerInTableState.hasHoleCards)
                {
                    ActivePlayerHasHoleCardFolded.Add(playerInTableState);
                    ActivePlayerHasHoleCard.Remove(playerInTableState);
                    AllDeadCards.Add(playerInTableState.HoleCards);
                    AllDeadCardsString += playerInTableState.Cards;
                }
            }

            var equitySolver = ServiceLocator.Current.GetInstance <IEquitySolver>();

            var gameType = new GeneralGameTypeEnum().ParseGameType(CurrentGame.GameDescription.GameType);

            var isShortDeck = CurrentGame.GameDescription.TableTypeDescriptors.Contains(HandHistories.Objects.GameDescription.TableTypeDescription.ShortDeck);

            var equitySolverParams = new EquitySolverParams
            {
                PlayersCards = ActivePlayerHasHoleCard.Select(x => x.HoleCards).ToArray(),
                BoardCards   = CurrentBoard,
                Dead         = isShortDeck ?
                               AllDeadCards
                               .Distinct(new LambdaComparer <HoleCards>((x, y) => x.ToString().Equals(y.ToString())))
                               .SelectMany(x => x)
                               .Concat(CardGroup.GetDeadCardsForHoldem6Plus())
                               .ToArray() :
                               AllDeadCards
                               .Distinct(new LambdaComparer <HoleCards>((x, y) => x.ToString().Equals(y.ToString())))
                               .SelectMany(x => x)
                               .ToArray(),
                GameType = gameType
            };

            var equities = equitySolver.CalculateEquity(equitySolverParams)
                           .Select(x => Math.Round((decimal)x.Equity * 100, 2))
                           .ToArray();

            // updating states in replayer view
            if (equities != null)
            {
                RefreshBoard(equities, state.CurrentStreet);
            }

            //case of last state. Needed for All-in before River for some cases
            if (TableStateList.IndexOf(state) + 1 == TableStateList.Count &&
                equities != null)
            {
                // updating states in replayer view
                RefreshBoard(equities, Street.Preflop);
            }
        }