Пример #1
0
        public async Task <AllBrokerData> SellStock(BrokerInfo brokerInfo)
        {
            var stockbuy            = new StockMarketService();
            var markets             = cache.Get <List <AllStockMarketRecords> >(Constants.marketData);
            var playerBrokerAccount = cache.Get <AllBrokerData>(brokerInfo.PlayerName + "_Broker");

            if (playerBrokerAccount == null)
            {
                throw new Exception("No broker account exists for provided player");
            }

            var playerBankAccount = cache.Get <AllBankRecords>(brokerInfo.PlayerName + "_Bank");
            var turn       = cache.Get <Clock>(brokerInfo.PlayerName + "_Clock");
            var totalPrice = brokerInfo.StockPrice * brokerInfo.Quantity;

            var bankTransaction = new BankTransaction();

            bankTransaction.PlayerName  = playerBankAccount.Accounts.PlayerName;
            bankTransaction.Price       = totalPrice;
            bankTransaction.Transceiver = brokerInfo.Sector;
            bankTransaction.Turn        = turn.PlayerTurn + 1;
            await bankService.Deposit(bankTransaction);

            var brokerRecords = new AllBrokerData()
            {
            };

            brokerRecords          = playerBrokerAccount;
            brokerInfo.Status      = Constants.sellStock;
            brokerInfo.IsAvailable = false;
            brokerRecords.BrokerInfos.Add(brokerInfo);

            foreach (var item in brokerRecords.BrokerInfos)
            {
                if (item.Sector == brokerInfo.Sector && item.Stock == brokerInfo.Stock && item.Quantity == brokerInfo.Quantity && item.Status == Constants.boughtStock)
                {
                    item.IsAvailable = false;
                }
            }

            cache.Set(brokerInfo.PlayerName + "_Broker", brokerRecords, Constants.cacheTime);

            TurnChange();
            var CompaniesList = await GetStocks(brokerInfo.Stock);

            stockbuy.PriceUpdate(brokerInfo, CompaniesList, markets, "Sell");
            return(brokerRecords);
        }
Пример #2
0
        public async Task <AllBrokerData> BuyStock(BrokerInfo brokerInfo)
        {
            var stockbuy = new StockMarketService();
            var markets  = cache.Get <List <AllStockMarketRecords> >(Constants.marketData);

            var playerBrokerAccount = cache.Get <AllBrokerData>(brokerInfo.PlayerName + "_Broker");

            if (playerBrokerAccount == null)
            {
                throw new Exception("No broker account exists for provided player");
            }

            var playerBankAccount = cache.Get <AllBankRecords>(brokerInfo.PlayerName + "_Bank");
            var turn       = cache.Get <Clock>(brokerInfo.PlayerName + "_Clock");
            var totalPrice = brokerInfo.StockPrice * brokerInfo.Quantity;

            if (playerBankAccount != null)
            {
                if (playerBankAccount.Accounts.Balance < totalPrice)
                {
                    throw new Exception("Player doesn't have enough money to buy");
                }
            }

            var bankTransaction = new BankTransaction();

            bankTransaction.PlayerName  = playerBankAccount.Accounts.PlayerName;
            bankTransaction.Price       = totalPrice;
            bankTransaction.Transceiver = brokerInfo.Sector;
            bankTransaction.Turn        = turn.PlayerTurn + 1;
            await bankService.Withdraw(bankTransaction);

            var brokerRecords = new AllBrokerData();

            brokerRecords          = playerBrokerAccount;
            brokerInfo.Status      = Constants.boughtStock;
            brokerInfo.IsAvailable = true;
            brokerRecords.BrokerInfos.Add(brokerInfo);

            cache.Set(brokerInfo.PlayerName + "_Broker", brokerRecords, Constants.cacheTime);
            TurnChange();
            var CompaniesList = await GetStocks(brokerInfo.Stock);

            stockbuy.PriceUpdate(brokerInfo, CompaniesList, markets, "Buy");

            return(brokerRecords);
        }