コード例 #1
0
        public IActionResult Spin(SlotMachineSpinReq model)
        {
            var response = _slotMachineService.Spin(model);

            if (response == null)
            {
                return(BadRequest(new { message = "User do not exist or not enough balance" }));
            }

            return(Ok(response));
        }
コード例 #2
0
        public SlotMachineSpinRes Spin(SlotMachineSpinReq model)
        {
            List <SlotSymbol> symbolsList = new List <SlotSymbol>();

            var user = _context.Users
                       .Where(u => u.Id == model.UserId)
                       .SingleOrDefault();

            //Check if user exists and have enough balance for the spin
            if (user.Balance < model.Bet)
            {
                return(null);
            }

            //Remove the bet from user's balance
            user.Balance -= model.Bet;
            _context.SaveChanges();

            //Fill list with random symbols based on certain odds
            for (var i = 0; i < 9; i++)
            {
                symbolsList.Add(getSymbolFromRandomNumber());
            }

            //Calculate reward(winning) from the spin
            var reward = calculateReward(symbolsList, model.Bet);

            //DB object to insert in 'SlotMachineSpins'
            var dbSpinData = new SlotMachineSpin {
                Id     = 0,
                Time   = DateTime.Now,
                User   = user,
                Game   = model.Game,
                Bet    = model.Bet,
                Reward = reward
            };

            //Insert the object
            _context.SlotMachineSpins.Add(dbSpinData);
            _context.SaveChanges();

            //Add reward from the spin to user's balance
            user.Balance += reward;
            _context.SaveChanges();

            //Return slot symbols to display in FE and the reward(winning)
            return(new SlotMachineSpinRes {
                SymbolsList = symbolsList,
                Reward = reward
            });
        }