public async Task <IActionResult> SlotGame(SlotGameViewModel model)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = 400;
                return(PartialView("_StatusMessage", "Error: Incorrect format!"));
            }
            var userId      = userManager.GetUserId(User);
            var balanceInfo = userServices.GetBalanceInformation(userId).Result;

            if (balanceInfo.Amount - model.Stake < 0)
            {
                Response.StatusCode = 400;
                return(PartialView("_StatusMessage", "Error: You cant bet more than what you have!"));
            }
            var usdChangeOfStake = userServices.UpdateUserBalanceByAmount(-model.Stake, userId).Result;

            transactionServices.CreateTransactionAsync(TypeOfTransaction.Stake, "Stake", usdChangeOfStake, userId).Wait();
            var gameMatrix = slotGameServices.Run(model.N, model.M);
            var coef       = slotGameServices.Evaluate(gameMatrix);
            var earnings   = model.Stake * coef;

            if (coef != 0)
            {
                var usdChangeOfEarnings = userServices.UpdateUserBalanceByAmount(earnings, userId).Result;
                transactionServices.CreateTransactionAsync(TypeOfTransaction.Win, "Win", usdChangeOfEarnings, userId).Wait();
            }
            //serialize matrix to json
            string result = jsonConverter.SerializeObject(gameMatrix, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                Formatting        = Formatting.None,
                Converters        = new List <JsonConverter> {
                    new StringEnumConverter()
                }
            });

            return(Json(result));
        }
Exemple #2
0
        public async Task <IActionResult> Deposit(DepositViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var userId = userManager.GetUserId(User);
            var depositedAmountInBaseCurrency = await userServices.UpdateUserBalanceByAmount(model.DepositAmount, userId);

            await transactionServices.CreateTransactionAsync(TypeOfTransaction.Deposit, $"Deposited with credid card #{model.SelectedCard}", depositedAmountInBaseCurrency, userId);

            StatusMessage = $"Balances updated by {model.DepositAmount}.";

            return(RedirectToAction(nameof(Deposit)));
        }