Esempio n. 1
0
        public IActionResult SlotGame(int N, int M)
        {
            var model = new SlotGameViewModel {
                N = N, M = M
            };

            return(View("SlotGame", model));
        }
Esempio n. 2
0
        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));
        }