Example #1
0
        public decimal SetUserBalance(string userId, decimal newBalance)
        {
            UserState userState  = GetUserState(userId);
            decimal   oldBalance = userState.Balance;

            userState.SetBalance(newBalance);
            decimal balanceDelta = newBalance - oldBalance;

            // Log Balance Change
            SetUserStatus(userState, GameStatus.BalanceChanged, Convert.ToDouble(balanceDelta), $"New Balance: {newBalance}");

            return(newBalance);
        }
Example #2
0
        private GameBet PlaceNewBet(string userId, string assetPair, string box, decimal bet, out string message)
        {
            message = "Placing Bet";

            // Get user state
            UserState userState = GetUserState(userId);

            // Validate balance
            if (bet > userState.Balance)
            {
                message = "User has no balance for the bet.";
                SetUserStatus(userState, GameStatus.Error, 0, "PlaceBet Failed:" + message);
                return(null);
            }

            Box boxObject = Box.FromJson(box);

            // Get Current Coeffs for Game's Assetpair
            var assetConfig = dbBoxConfig.Where(b => b.GameAllowed).Where(b => b.AssetPair == assetPair).FirstOrDefault();

            if (assetConfig == null)
            {
                message = $"Box Size parameters are not set for Asset Pair[{ assetPair}].";
                SetUserStatus(userState, GameStatus.Error, 0, "PlaceBet Failed:" + message);
                return(null);
            }

            // Validate Coeff:
            bool IsCoefValid = ValidateCoeff(assetPair, boxObject, assetConfig);

            if (!IsCoefValid)
            {
                message = $"Invalid Coefficient[{boxObject.Coefficient}].";
                SetUserStatus(userState, GameStatus.Error, 0, "PlaceBet Failed:" + message);
                return(null);
            }

            // Place Bet
            GameBet newBet = userState.PlaceBet(boxObject, assetPair, bet, assetConfig);

            newBet.TimeToGraphReached += Bet_TimeToGraphReached;
            newBet.TimeLenghFinished  += Bet_TimeLenghFinished;

            // Update user balance
            userState.SetBalance(userState.Balance - bet);

            // Run bet
            newBet.StartWaitTimeToGraph();

            // Async Save to Database
            Task.Run(() =>
            {
                // Save bet to DB
                database.SaveGameBet(newBet);

                // Set Status, saves User to DB
                SetUserStatus(userState, GameStatus.BetPlaced, -Convert.ToDouble(bet), $"BetPlaced[{boxObject.Id}]. Asset:{assetPair}  Bet:{bet} Balance:{userState.Balance}");
            });

            message = "OK";
            return(newBet);
        }