public async Task <IActionResult> Post([FromBody] Bet bet)
        {
            try
            {
                var User = await _userRepository.GetUser(bet.UserId);

                var Balance = User.Balance - bet.Stake;
                if (Balance < 0)
                {
                    // Insuffient funds
                    return(BadRequest(new { reason = "insufficient balance" }));
                }

                User.Balance = Balance;

                await _userRepository.Update(User);

                await _betRepository.Create(bet);


                return(Ok(bet));
            }
            catch (Exception e)
            {
                // LogException(e);
                return(StatusCode(500, new { reason = "bet api unavailable" }));
            }
        }
Ejemplo n.º 2
0
        public Task <HttpResponseMessage> Bet(int userId, [FromBody] BetRequest betRequest)
        {
            string token = Request.Headers.GetValues("DiceGaming-Token").FirstOrDefault();

            if (!loginRepository.HasUserAndToken(userId, token))
            {
                throw new ForbiddenException();
            }

            int     userBet = betRequest.Bet;
            decimal stake   = betRequest.Stake;

            var user = userRepository.Get(userId);

            if (user.Money < stake)
            {
                throw new BadRequestException("Not enough money in balance!");
            }

            decimal win  = 0;
            int     roll = RollDices();

            if (roll == userBet)
            {
                decimal multiplier = StakeMultiplier(userBet);
                win  = stake * multiplier;
                win -= win / 10; // for the house
            }

            var bet = new BetDto()
            {
                UserId        = userId,
                DiceSumBet    = userBet,
                DiceSumResult = roll,
                Stake         = stake,
                Win           = win,
                CreationDate  = DateTime.Now
            };

            bet = betRepository.Create(bet);

            user.Money = user.Money - stake + win;

            userRepository.UpdateMoney(user.Id, user.Money);

            var response = new
            {
                BetId     = bet.Id,
                Bet       = bet.DiceSumBet,
                Stake     = bet.Stake,
                Win       = bet.Win,
                Timestamp = bet.CreationDate
            };

            return(Task.FromResult(Request.CreateResponse(HttpStatusCode.Created, response)));
        }
Ejemplo n.º 3
0
        public async Task <Bet> CreateBet(IUser user, string description, string forOdds, string againstOdds)
        {
            var bet = new Bet
            {
                UserId      = user.Id,
                Username    = user.Username,
                Description = description,
                ForOdds     = forOdds,
                AgainstOdds = againstOdds,
                HasEnded    = false
            };

            var newBet = await _betRepository.Create(bet);

            return(newBet);
        }
Ejemplo n.º 4
0
        public void PlaceBet(BetDto dto)
        {
            var offer = gameQuery.AsGameOfferView();
            var bestOffersCoefficients = offer.BestOffers.SelectMany(e => e.Coefficients).Where(e => dto.CoefficientIds.Contains(e.Id));
            var allOffersCoefficients  = offer.OtherOffers.SelectMany(e => e.Coefficients).Concat(bestOffersCoefficients).Where(e => dto.CoefficientIds.Contains(e.Id));

            if (allOffersCoefficients.Count() == 0)
            {
                throw new Exception("At least one current offer coefficient needs to be selected.");
            }

            if (allOffersCoefficients.GroupBy(x => x.GameId).Any(g => g.Count() > 1))
            {
                throw new Exception("There can only be one coefficient selected per offered game in a bet.");
            }

            if (bestOffersCoefficients.Count() > 2)
            {
                throw new Exception("There can only be one special offer coefficient selected per bet.");
            }

            var totalFunds = walletTransactionQuery.AsTotalFundsView(includeTransactions: false);

            if (MIN_BET_VALUE >= dto.BetValue || dto.BetValue >= totalFunds.TotalFunds)
            {
                throw new Exception("You can only bet if you have sufficient funds (minimum: " + MIN_BET_VALUE + ")");
            }

            var bet = new BetDomainModel {
                Coefficients = betRepository.GetCoefficientsForBet(dto.CoefficientIds)
            };

            bet.Id = betRepository.Create(bet);

            var transaction = new WalletTransactionDomainModel();

            transaction.AddBetWithValue(bet, dto.BetValue);

            walletTransactionService.CreateTransactionsForBets(new List <Tuple <BetDomainModel, float> >()
            {
                new Tuple <BetDomainModel, float>(bet, dto.BetValue)
            });
        }