public IHttpActionResult Post(BetCreate bet) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var service = CreateBetService(); if (!_service.CheckIfGameIdExists(bet.GameId)) { return(BadRequest($"The game with gameId {bet.GameId} does not exist. Please choose another. For a list of games try the following endpoint: " + "/api/PlayerGames")); } if (!service.CheckPlayerBalance(bet.BetAmount))//confirm player has enough funds { return(BadRequest("Bet Amount Exceeds Player Balance")); } var betResult = service.CreateBet(bet); if (betResult is null) { return(BadRequest("Sorry, your bet did not post. You either tried to play a game outside of your access level, or outside the range of the MinMax bet")); } return(Ok(betResult)); }
public BetResult CreateBet(BetCreate model) { double payout; var hasAccess = CheckPlayerAccess(); // Brought _gameSim play game mechanics outside, and captured result as variable. // That result will be fed into added helper method (in gamesimulation.cs) to derive win/loss bool. // Now both PayoutAmount and PlayerWonGame derived // from _gameSim. if (model.GameId == 11) { _gameService = new GameService(_playerGuid); } if (!model.TypeOfBet.HasValue) { payout = _gameService.PlayGame(model.GameId, model.BetAmount, hasAccess, GameService.BetType.pass, model.ValueOfBet); } else { payout = _gameService.PlayGame(model.GameId, model.BetAmount, hasAccess, (GameService.BetType)model.TypeOfBet, model.ValueOfBet); } if (payout == 0) { return(null); //if playing keno or roulette postman needs a list of int. Keno up to 10 int(1-80). roulette } if (payout == 12345) { DeletePlayer(_playerGuid); return(new BetResult { TimeOfBet = DateTime.Now.ToString(), BetId = 000, GameId = 11, GameName = "Russian Roulette", BetAmount = model.BetAmount, PlayerWonGame = false, PayoutAmount = -model.BetAmount, PlayerBankBalance = 0, Message = "Sorry! Game Over! You lost at Russian Roulette!" }); } if (payout == 45678) { return(new BetResult { TimeOfBet = DateTime.Now.ToString(), BetId = 000, GameId = 11, GameName = "Russian Roulette", BetAmount = model.BetAmount, PlayerWonGame = true, PayoutAmount = 0, PlayerBankBalance = model.BetAmount, Message = "You survived. Do you wish to play again!" }); } var entity = new Bet() { PlayerId = _playerGuid, GameId = model.GameId, BetAmount = model.BetAmount, PayoutAmount = payout, PlayerWonGame = _gameSim.GameWinOutcome(payout), DateTimeOfBet = DateTimeOffset.Now, }; using (var ctx = new ApplicationDbContext()) { ctx.Bets.Add(entity); if (ctx.SaveChanges() != 0 && UpdatePlayerBankBalance(_playerGuid, entity.PayoutAmount) && UpdateHouseBankBalance(entity.PayoutAmount * (-1))) { return(GetBetResult(entity.BetId)); } return(null); } }