public void BuyStocks(int gameId, int playerId, int sectorId, int stockId, int quantity) { GameLogicManager gameLogic = new GameLogicManager(); lock (_syncRoot) { var game = EntityStateManager.CurrentGames.FirstOrDefault(x => x.GameId == gameId); if (game != null) { var turn = game.GameDetail.TurnDetail.FirstOrDefault(x => x.Turn == game.CurrentRound); if (turn != null) { var sector = turn.Sectors.FirstOrDefault(y => y.Sector.SectorId == sectorId); if (sector != null) { var stock = sector.Stocks.FirstOrDefault(z => z.StockId == stockId); var player = game.Players.FirstOrDefault(o => o.PlayerId == playerId); var token = gameLogic.BuyStocks(playerId, stock, quantity, stock.CurrentPrice); if (token.Success) { if (token.Data != null) { var temp = (PlayerTransactionsDTO)token.Data; temp.PlayerName = player.PlayerName; temp.StockName = stock.StockName; PlayerStock pStock = new PlayerStock(); pStock.Quantity = quantity; pStock.SectorId = sectorId; pStock.StockId = stock.StockId; pStock.StockName = stock.StockName; pStock.SectorName = sector.Sector.SectorName; pStock.BoughtPrice = stock.CurrentPrice; player.PlayerStocks.Add(pStock); player.BankAccount.Balance -= (quantity * stock.CurrentPrice); player.NoOfTransactions += 1; player.SpendAmount += quantity * stock.CurrentPrice; BalanceDTO balance = new BalanceDTO(); balance.OpeningBalance = 1000; balance.AllocatedPrice = player.SpendAmount; balance.ProfitPrice = player.GainAmount; balance.Balance = player.BankAccount.Balance; if (!player.IsPlayerAI) { Clients.Client(Context.ConnectionId).stockBuySuccess(balance); } Clients.Group(game.GameCode).playerBoughtStock(temp); GetGameLeaders(game.GameId); } else { token.Success = false; token.Message = "An error occurred"; } } if (!token.Success && !player.IsPlayerAI) { Clients.Client(Context.ConnectionId).stockBuyFailed(token); } } } } } }