public async Task <BrokerAccount> CreateAccount(string playerName) { var Bank = cache.Get <AllBankRecords>(playerName + "_Bank"); if (Bank == null) { throw new System.Exception("Bank account needs to be created first!"); } var Broker = cache.Get <AllBankRecords>(playerName + "_Broker"); if (Broker != null) { throw new System.Exception("Broker account has been already created for this user!"); } BrokerAccount newPlayer = new BrokerAccount() { }; newPlayer.PlayerName = playerName; var allBrokerData = new AllBrokerData() { }; allBrokerData.Accounts = newPlayer; cache.Set(newPlayer.PlayerName + "_Broker", allBrokerData, Constants.cacheTime); return(newPlayer); }
public async Task <AllBrokerData> SellStock(BrokerInfo brokerInfo) { 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; brokerRecords.BrokerInfos.Add(brokerInfo); cache.Set(brokerInfo.PlayerName + "_Broker", brokerRecords, Constants.cacheTime); return(brokerRecords); }
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); }
public async Task <AllBrokerData> GetAvailableStocks(string playerName) { AllBrokerData playerBrokerAccount = cache.Get <AllBrokerData>(playerName + "_Broker"); if (playerBrokerAccount == null) { throw new Exception("No broker account exists for provided data"); } var brokerInfo = (from Info in playerBrokerAccount.BrokerInfos where Info.IsAvailable == true select Info).ToList(); playerBrokerAccount.BrokerInfos = brokerInfo; return(playerBrokerAccount); }
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); }
public async Task <AllBrokerData> BuyStock(BrokerInfo brokerInfo) { 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; brokerRecords.BrokerInfos.Add(brokerInfo); cache.Set(brokerInfo.PlayerName + "_Broker", brokerRecords, Constants.cacheTime); return(brokerRecords); }