Beispiel #1
0
        public async Task <RoundViewModel> StartNextRoundForPlayers(List <UserViewModel> players)
        {
            foreach (var player in players)
            {
                if (player.PlayerRole != PlayerRole.Dealer &&
                    player.PlayerStatus == PlayerStatus.DefaultValue)
                {
                    await CreateNextRound(player);
                }
                if (player.PlayerRole == PlayerRole.Dealer)
                {
                    List <Round> rounds = await _roundRepository.GetRoundsForPlayer(player.GameId, player.UserName);

                    AddCardViewModelToPlayer(rounds, player);
                }
            }
            var roundModel = new RoundViewModel();

            roundModel.Users = players;

            await CheckPlayersForMoreThanTwentyOne(roundModel);

            var humanPlayer = GetHumanPlayer(roundModel.Users);
            var dealer      = GetDealer(roundModel.Users);

            if (humanPlayer.PlayerStatus == PlayerStatus.Lose)
            {
                await CheckDealer(roundModel.Users);

                return(await FinalPointsCount(roundModel.Users));
            }
            return(roundModel);
        }
Beispiel #2
0
        private async Task <RoundViewModel> FinalPointsCount(List <UserViewModel> players)
        {
            var roundModel  = new RoundViewModel();
            var dealer      = GetDealer(players);
            var humanPlayer = GetHumanPlayer(players);

            foreach (var player in players)
            {
                if (player.PlayerStatus == PlayerStatus.DefaultValue)
                {
                    if (player.CardSum > dealer.CardSum)
                    {
                        player.PlayerStatus = PlayerStatus.Winner;
                        dealer.PlayerStatus = PlayerStatus.Lose;
                    }
                    if (player.CardSum < dealer.CardSum)
                    {
                        dealer.PlayerStatus = PlayerStatus.Winner;
                        player.PlayerStatus = PlayerStatus.Lose;
                    }
                }
                if (player.PlayerRole == PlayerRole.Dealer)
                {
                    player.PlayerStatus = dealer.PlayerStatus;
                }
            }
            roundModel.Users            = players;
            roundModel.isResultComplete = true;
            return(roundModel);
        }
Beispiel #3
0
 private async Task <RoundViewModel> CheckPlayersForBlackJack(RoundViewModel viewModel)
 {
     foreach (var player in viewModel.Users)
     {
         if (player.CardSum == twentyOnePoint)
         {
             player.PlayerStatus = PlayerStatus.Winner;
         }
     }
     return(viewModel);
 }
Beispiel #4
0
        private async Task CheckPlayersForMoreThanTwentyOne(RoundViewModel roundModel)
        {
            var dealer = GetDealer(roundModel.Users);

            foreach (var player in roundModel.Users)
            {
                if (player.CardSum > twentyOnePoint)
                {
                    player.PlayerStatus = PlayerStatus.Lose;
                }
            }
        }
Beispiel #5
0
        private RoundViewModel MappingToViewModel(List <Round> rounds)
        {
            var result = rounds.GroupBy(p => p.Player.Name);

            var roundViewModelList = new RoundViewModel();

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

                foreach (var item in round)
                {
                    var cardViewModel = new CardServiceViewModel();

                    cardViewModel.CardRank   = item.Card.Rank.ToString();
                    cardViewModel.CardSuit   = item.Card.Suit.ToString();
                    cardViewModel.CardWeight = item.Card.Weight;

                    userModel.CardSum += item.Card.Weight;
                    userModel.PlayerId = item.PlayerId;
                    userModel.GameId   = item.GameId;

                    if (item.Player.PlayerRole == EntityPlayerRole.Human)
                    {
                        userModel.PlayerRole = PlayerRole.Human;
                    }

                    if (item.Player.PlayerRole == EntityPlayerRole.Bot)
                    {
                        userModel.PlayerRole = PlayerRole.Bot;
                    }

                    if (item.Player.PlayerRole == EntityPlayerRole.Dealer)
                    {
                        userModel.PlayerRole = PlayerRole.Dealer;
                    }

                    userModel.Cards.Add(cardViewModel);
                }
                roundViewModelList.Users.Add(userModel);
            }
            return(roundViewModelList);
        }