Ejemplo n.º 1
0
        public void SellStocks(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.SellStocks(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;

                                    var tempStocks = player.PlayerStocks.Where(b => b.StockId == stockId).ToList();

                                    foreach (var tempStock in tempStocks)
                                    {
                                        tempStock.Quantity -= quantity;
                                        if (tempStock.Quantity < 0)
                                        {
                                            quantity = -(tempStock.Quantity);
                                        }
                                        else if (tempStock.Quantity == 0)
                                        {
                                            break;
                                        }
                                    }
                                    var stockToRemove = tempStocks.Where(c => c.Quantity <= 0).Select(x => x.StockId).ToList();

                                    foreach (var removeStock in stockToRemove)
                                    {
                                        var tempStockToRemove = tempStocks.Where(a => a.StockId == removeStock).ToList();
                                        foreach (var tempStk in tempStockToRemove)
                                        {
                                            if (tempStk.Quantity <= 0)
                                            {
                                                player.PlayerStocks.Remove(tempStk);
                                            }
                                        }
                                    }

                                    player.BankAccount.Balance += (quantity * stock.CurrentPrice);
                                    player.GainAmount           = quantity * stock.CurrentPrice;
                                    temp.PlayerAccBalance       = player.BankAccount.Balance;

                                    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).stockSellSuccess(balance);
                                    }
                                    Clients.Group(game.GameCode).playerSoldStock(temp);

                                    GetGameLeaders(game.GameId);
                                }
                                else
                                {
                                    token.Success = false;
                                    token.Message = "An error occurred";
                                }
                            }

                            if (!token.Success && !player.IsPlayerAI)
                            {
                                Clients.Client(Context.ConnectionId).stockSellFailed(token);
                            }
                        }
                    }
                }
            }
        }