Пример #1
0
        public IActionResult Confirm(Guid id)
        {
            var model = _db.Matches.FirstOrDefault(r => r.Id.Equals(id));

            if (model == null || model.Status.Equals(MatchStatus.Finished))
            {
                return(RedirectToAction("Index"));
            }

            var usersBets = _db.UsersBets
                            .Include(b => b.Bet)
                            .Include(b => b.Bet.Match)
                            .Where(b => b.Bet.MatchId.Equals(id)).ToList();

            var result = model.Result.Split(':');
            var team1  = result[0];
            var team2  = result[1];

            bool win = false;

            foreach (var userBet in usersBets)
            {
                var user        = _db.Users.Single(u => u.Id.Equals(userBet.UserId));
                var transaction = new Transactions();
                transaction.UserId      = user.Id;
                transaction.User_Bet_Id = userBet.Id;

                switch (userBet.Bet.Description)
                {
                case MatchResult.Draw:
                    if (Convert.ToInt32(team1) == Convert.ToInt32(team2))
                    {
                        win = true;
                    }
                    break;

                case MatchResult.W1:
                    if (Convert.ToInt32(team1) > Convert.ToInt32(team2))
                    {
                        win = true;
                    }
                    break;

                case MatchResult.W2:
                    if (Convert.ToInt32(team1) < Convert.ToInt32(team2))
                    {
                        win = true;
                    }
                    break;
                }

                if (win)
                {
                    userBet.Result = UserBetResult.Won;
                    user.Money    += (decimal)userBet.Coef * userBet.Money;


                    transaction.Date  = DateTime.Now;
                    transaction.Money = (decimal)userBet.Coef * userBet.Money;
                    transaction.Type  = TransactionType.Replenishment;

                    _notify.AboutBetAsync(BetReason.Winned, user.Email, userBet);
                }
                else
                {
                    transaction.Date  = userBet.Time;
                    transaction.Money = userBet.Money;
                    transaction.Type  = TransactionType.Withdrawal;

                    userBet.Result = UserBetResult.Losed;
                    _notify.AboutBetAsync(BetReason.Loosed, user.Email, userBet);
                }

                userBet.Status           = UserBetPaymentStatus.Finished;
                userBet.Bet.Match.Status = MatchStatus.Finished;


                _db.UsersBets.Update(userBet);
                _db.Users.Update(user);
                _db.Matches.Update(userBet.Bet.Match);
                _db.Transactions.Add(transaction);

                _db.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }