private async Task <RoundGameView> MappingToViewModel(List <Round> rounds)
        {
            var result         = rounds.GroupBy(p => p.Player.Name);
            var roundViewModel = new RoundGameView();

            foreach (var round in result)
            {
                var userModel = new PlayerNextRoundViewItem();
                userModel.UserName = round.Key;

                foreach (var item in round)
                {
                    var         cardViewModel = new CardGameViewItem();
                    PlayerGames playerGames   = await GetPlayerStatus(item.PlayerId, item.GameId);

                    userModel.PlayerId     = item.PlayerId;
                    userModel.GameId       = item.GameId;
                    userModel.PlayerRole   = (PlayerRoleEnumView)item.Player.PlayerRole;
                    userModel.PlayerStatus = (PlayerStatusEnumView)playerGames.Status;

                    cardViewModel.Rank  = item.Card.Rank.ToString();
                    cardViewModel.Suit  = item.Card.Suit.ToString();
                    cardViewModel.Value = item.Card.Value;
                    userModel.CardSum  += item.Card.Value;

                    userModel.Cards.Add(cardViewModel);
                }
                roundViewModel.Users.Add(userModel);
            }
            roundViewModel.GameId = rounds[0].GameId;

            return(roundViewModel);
        }
 private void UpdateStatus(RoundGameView roundModel)
 {
     foreach (var player in roundModel.Users)
     {
         int status = (int)player.PlayerStatus;
         _playersGameRepository.UpdateStatus(player.GameId, player.PlayerId, status);
     }
 }
        private async Task <RoundGameView> CompleteRound(RoundGameView roundModel)
        {
            var humanPlayer = GetHumanPlayer(roundModel.Users);
            var dealer      = GetDealer(roundModel.Users);

            if (humanPlayer.PlayerStatus == PlayerStatusEnumView.Lose &&
                dealer.PlayerStatus == PlayerStatusEnumView.DefaultValue)
            {
                if (roundModel.Users.Count > 2)
                {
                    await CheckDealer(roundModel.Users);
                    await FinalPointsCount(roundModel);
                }
                roundModel.IsResultComplete = true;
                return(roundModel);
            }
            if (dealer.PlayerStatus == PlayerStatusEnumView.Winner)
            {
                foreach (var player in roundModel.Users)
                {
                    if (player.PlayerStatus == PlayerStatusEnumView.DefaultValue)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Lose;
                    }
                }
                ;
                roundModel.IsResultComplete = true;

                return(roundModel);
            }

            if (dealer.PlayerStatus == PlayerStatusEnumView.Lose)
            {
                foreach (var player in roundModel.Users)
                {
                    if (player.PlayerStatus == PlayerStatusEnumView.DefaultValue)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Winner;
                    }
                }
                ;
                roundModel.IsResultComplete = true;
                return(roundModel);
            }

            if (humanPlayer.PlayerStatus == PlayerStatusEnumView.Winner)
            {
                return(await FinalPointsCount(roundModel));
            }
            UpdateStatus(roundModel);
            return(roundModel);
        }
 private async Task CheckRules(RoundGameView roundModel)
 {
     foreach (var player in roundModel.Users) // check for blackJack
     {
         if (player.CardSum == twentyOnePoint)
         {
             player.PlayerStatus = PlayerStatusEnumView.Winner;
         }
         if (player.CardSum > twentyOnePoint) //check for more than twenty one
         {
             player.PlayerStatus = PlayerStatusEnumView.Lose;
         }
     }
 }
        private async Task <RoundGameView> FinalPointsCount(RoundGameView roundModel)
        {
            var dealer = GetDealer(roundModel.Users);

            foreach (var player in roundModel.Users)
            {
                if (player.PlayerStatus == PlayerStatusEnumView.DefaultValue)
                {
                    if (player.CardSum > dealer.CardSum &&
                        player.CardSum < twentyOnePoint)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Winner;
                    }
                    if (player.CardSum < twentyOnePoint &&
                        player.CardSum > dealer.CardSum)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Winner;
                    }
                    if (player.CardSum < twentyOnePoint &&
                        player.CardSum < dealer.CardSum)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Lose;
                    }
                    if (player.CardSum < dealer.CardSum &&
                        dealer.CardSum < twentyOnePoint)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Lose;
                    }
                    if (player.CardSum == dealer.CardSum)
                    {
                        player.PlayerStatus = PlayerStatusEnumView.Draw;
                    }
                }
                if (player.PlayerRole == PlayerRoleEnumView.Dealer)
                {
                    player.PlayerStatus = dealer.PlayerStatus;
                }
            }

            roundModel.IsResultComplete = true;
            return(roundModel);
        }