Пример #1
0
    public void OnStockClicked(int ID)
    {
        Stock marketStock    = null;
        Stock portfolioStock = null;

        switch (uiManager.UIState)       // don't like referencing uiManager here
        {
        case UIManager.EUIState.Markets: // BUY
            // find the stock
            marketStock = GetMarketStock(ID);
            if (marketStock != null)
            {
                // check if the player has available funds
                if (PlayerCash >= marketStock.CurrentPrice)
                {
                    // check if this stock is already in the player's portfoli
                    portfolioStock = GetPortfolioStock(ID);
                    if (portfolioStock == null)
                    {
                        marketStock = GetMarketStock(ID);
                        if (marketStock != null)
                        {
                            marketStock.Shares    = 1;
                            marketStock.CostBasis = marketStock.CurrentPrice;
                            marketStock.SellValue = marketStock.CurrentPrice;
                            GameEvents.BroadcastBuy(marketStock);
                            m_portfolio.Add(marketStock);
                            PlayerCash -= marketStock.CurrentPrice;
                            GameEvents.BroadcastCashChanged(PlayerCash);
                        }
                    }
                    else
                    {
                        ++portfolioStock.Shares;     // could calculate other values based on the fundamentals of Shares and Price
                        portfolioStock.CostBasis += marketStock.CurrentPrice;
                        portfolioStock.SellValue  = marketStock.CurrentPrice * portfolioStock.Shares;
                        GameEvents.BroadcastBuy(portfolioStock);
                        PlayerCash -= marketStock.CurrentPrice;
                        GameEvents.BroadcastCashChanged(PlayerCash);
                    }
                }
            }
            break;

        case UIManager.EUIState.Portfolio:     // SELL
            // find the stock
            portfolioStock = GetPortfolioStock(ID);
            if (portfolioStock != null)
            {
                // check if the player has shares
                if (portfolioStock.Shares > 0)
                {
                    --portfolioStock.Shares;
                    PlayerCash += portfolioStock.CurrentPrice;
                    GameEvents.BroadcastSell(portfolioStock);
                    GameEvents.BroadcastCashChanged(PlayerCash);
                }
                if (portfolioStock.Shares <= 0)
                {
                    m_portfolio.Remove(portfolioStock);
                    uiManager.OnCashChanged(PlayerCash);
                }
            }
            break;

        default:
            break;
        }
        uiManager.OnCashChanged(PlayerCash);
    }