Ejemplo n.º 1
0
        public JsonResult IsUserScoreMoreThan21()
        {
            UserModel        userModel = db.Users.FirstOrDefault(u => u.Login == User.Identity.Name);
            List <HandModel> hands     = GetHandsForUserWithId(userModel.Id);

            if (hands.Count != 2)
            {
                string errorMessage = "Hands count != 2 in method IsUserScoreMoreThan21";
                _logger.LogInformation(errorMessage);
                return(Json(new
                {
                    state = gameState.error,
                    message = errorMessage
                }));
            }



            int userScore = 0;

            List <CardModel> userCards = new List <CardModel>();

            foreach (var hand in hands)
            {
                if (hand.hand_type_id == USER_HAND_TYPE)
                {
                    userCards = GetAllCardsByHandModel(hand);
                    userScore = CountScore(userCards);
                }
            }

            if (userScore > 21)
            {
                _logger.LogDebug(string.Format("Hit: user lose. User score - {0}", userScore));
                ClearHandCardsByHand(hands[0]);
                ClearHandCardsByHand(hands[1]);
                BetsModel betModel = db.Bets.FirstOrDefault(b => b.UserId == userModel.Id);
                decimal   bet      = betModel.Bet;
                UserLoseIntoGameStatTable(userModel.Id, betModel.Bet);
                db.GameStats.FirstOrDefault(u => u.Id == userModel.Id).chips_loosed += betModel.Bet;
                db.Remove(betModel);
                return(Json(new
                {
                    state = gameState.userLose,
                    UserCards = userCards,
                    UserScore = userScore,
                    userBet = bet
                }));
            }

            return(Json(new
            {
                state = gameState.ok,
                UserCards = userCards,
                UserScore = userScore
            }));
        }
Ejemplo n.º 2
0
 public static BetsModel Map(BetsModel bet)
 {
     return(new BetsModel()
     {
         id = bet.id,
         number = bet.number,
         color = bet.color,
         money = bet.money,
         winOperation = bet.winOperation
     });
 }
Ejemplo n.º 3
0
 public static OpenBetModel MapBet(BetsModel bets)
 {
     if (bets.id > 0)
     {
         return(new OpenBetModel()
         {
             success = true
         });
     }
     else
     {
         return(new OpenBetModel()
         {
             success = false
         });
     }
 }
Ejemplo n.º 4
0
        public JsonResult Bet(int count)
        {
            const bool NOT_ENOUGH_CHIPS = false;
            const bool OK                 = true;
            const int  USER_HAND_TYPE     = 1;
            const int  COMPUTER_HAND_TYPE = 2;
            UserModel  userModel          = db.Users.FirstOrDefault(u => u.Login == User.Identity.Name);
            MoneyModel moneyModel         = db.Money.FirstOrDefault(u => u.Id == userModel.Id);

            _logger.LogDebug("BlackJack - Bet -  count: " + count);
            // ViewBag.Count = count;
            bool status;

            if (count > int.MaxValue)
            {
                RedirectToAction("Error", "Home");
            }
            if (moneyModel == null)
            {
                RedirectToAction("Error", "Home");
            }


            List <CardModel> userCards     = new List <CardModel>();
            List <CardModel> computerCards = new List <CardModel>();

            if (moneyModel.Chips >= count && count > 0)
            {
                moneyModel.Chips -= count;
                status            = OK;
                BetsModel betsModel = db.Bets.FirstOrDefault(b => b.UserId == userModel.Id);
                if (betsModel != null)
                {
                    List <HandModel> hands = GetHandsForUserWithId(userModel.Id);
                    foreach (HandModel hand in hands)
                    {
                        ClearHandCardsByHand(hand);
                    }
                    db.Bets.Remove((betsModel));
                }
                betsModel = new BetsModel()
                {
                    Bet = count, UserId = userModel.Id
                };
                db.Bets.Add(betsModel);

                db.SaveChanges();


                userCards = getNCardForUserWithId(2, userModel.Id);
                setCardsForUserWithIdAndHandTypeId(userCards, userModel.Id, USER_HAND_TYPE);
                computerCards = getNCardForUserWithId(1, userModel.Id);
                setCardsForUserWithIdAndHandTypeId(computerCards, userModel.Id, COMPUTER_HAND_TYPE);
            }
            else
            {
                status = NOT_ENOUGH_CHIPS;
            }



            int userScore     = CountScore(userCards);
            int computerScore = CountScore(computerCards);

            StringBuilder userScoreStringBuilder = new StringBuilder(userScore.ToString());

            return(Json(new
            {
                chips = moneyModel.Chips,
                operationStatus = status,
                cards = new
                {
                    user = userCards,
                    computer = computerCards
                },
                userScore = userScoreStringBuilder.ToString(),
                ComputerScore = computerScore
            }));
        }
Ejemplo n.º 5
0
        public JsonResult Stand()
        {
            const int USER_HAND_TYPE     = 1;
            const int COMPUTER_HAND_TYPE = 2;

            UserModel        userModel = db.Users.FirstOrDefault(u => u.Login == User.Identity.Name);
            List <HandModel> hands     = GetHandsForUserWithId(userModel.Id);

            if (hands.Count != 2)
            {
                _logger.LogInformation("Hands count != 2 in method Stand");
                return(Json(new
                {
                    state = gameState.error,
                    message = "Hands count != 2 in method Stand"
                }));
            }


            int computerScore = 0;
            int userScore     = 0;

            List <CardModel> computerCards = new List <CardModel>();

            foreach (var hand in hands)
            {
                if (hand.hand_type_id == COMPUTER_HAND_TYPE)
                {
                    computerCards = GetAllCardsByHandModel(hand);
                    computerScore = CountScore(computerCards);
                    while (computerScore < 17)
                    {
                        List <CardModel> addCards = getNCardForUserWithId(1, userModel.Id);
                        computerCards.Add(addCards[0]);
                        setCardsForUserWithIdAndHandTypeId(addCards, userModel.Id, COMPUTER_HAND_TYPE);
                        computerScore += CountScore(addCards);
                    }
                }

                if (hand.hand_type_id == USER_HAND_TYPE)
                {
                    userScore = CountScore(GetAllCardsByHandModel(hand));
                }

                ClearHandCardsByHand(hand);
            }

            BetsModel betModel = db.Bets.FirstOrDefault(b => b.UserId == userModel.Id);
            decimal   bet      = betModel.Bet;

            db.Remove(betModel);
            // GameStatsModel gameStatsModel = db.GameStats.FirstOrDefault(s => s.Id == userModel.Id);
            MoneyModel moneyModel = db.Money.FirstOrDefault(u => u.Id == userModel.Id);

            if (userScore > computerScore && userScore <= 21)
            {
                moneyModel.Chips += bet * 2;
                UserWinIntoGameStatTable(userModel.Id, bet);
                db.SaveChanges();
                _logger.LogDebug(string.Format("Stand: user win. User score - {0}, computer score - {1}", userScore, computerScore));
                return(Json(new
                {
                    ComputerCards = computerCards,
                    state = gameState.userWin,
                    message = "",
                    userBet = bet,
                    ComputerScore = computerScore
                }));
            }

            if (computerScore > userScore && computerScore <= 21)
            {
                _logger.LogDebug(string.Format("Stand: user lose. User score - {0}, computer score - {1}", userScore, computerScore));
                UserLoseIntoGameStatTable(userModel.Id, bet);
                return(Json(new
                {
                    ComputerCards = computerCards,
                    state = gameState.userLose,
                    message = "",
                    userBet = bet,
                    ComputerScore = computerScore
                }));
            }

            if (computerScore > 21)
            {
                moneyModel.Chips += bet * 2;
                db.SaveChanges();
                UserWinIntoGameStatTable(userModel.Id, bet);
                _logger.LogDebug(string.Format("Stand: user win (computer score > 21). User score - {0}, computer score - {1}", userScore, computerScore));
                return(Json(new
                {
                    ComputerCards = computerCards,
                    state = gameState.userWin,
                    message = "",
                    userBet = bet,
                    ComputerScore = computerScore
                }));
            }

            if (userScore > 21)
            {
                _logger.LogDebug(string.Format("Stand: user lose (user score > 21). User score - {0}, computer score - {1}", userScore, computerScore));
                UserLoseIntoGameStatTable(userModel.Id, bet);
                return(Json(new
                {
                    ComputerCards = computerCards,
                    state = gameState.userLose,
                    message = "",
                    userBet = bet,
                    ComputerScore = computerScore
                }));
            }

            if (userScore == computerScore)
            {
                UserWinIntoGameStatTable(userModel.Id, bet);
                _logger.LogDebug(string.Format("Stand: user win (user score == computer score). User score - {0}, computer score - {1}", userScore, computerScore));
                return(Json(new
                {
                    ComputerCards = computerCards,
                    state = gameState.userWin,
                    message = "",
                    userBet = bet,
                    ComputerScore = computerScore
                }));
            }
            _logger.LogDebug(string.Format("Stand: OK. User score - {0}, computer score - {1}", userScore, computerScore));

            return(Json(new
            {
                state = gameState.ok,
                message = ""
            }));
        }
 // Start is called before the first frame update
 void Start()
 {
     betsModel = JsonUtility.FromJson <BetsModel>(BetsJson.text);
     PopulateBets();
 }