예제 #1
0
        internal GiveStockResponse AttemptToGiveStock(TraderHandler sender, string recipient)
        {
            GiveStockResponse response;
            string            sendID = sender.GetId();

            lock (traders)
            {
                lock (theMarket)
                {
                    if (sendID.Equals(stockholder))
                    {
                        if (traders.ContainsKey(recipient))
                        {
                            if (sendID.Equals(recipient))
                            {
                                //if sender sent it to themselves, don't bother updating anything because they were being greedy.
                                response = new GiveStockResponse(true, theMarket.CopyThis(), GiveStockResponse.WOW_GREEDY);
                                logger.LogTrade(sendID, recipient);
                            }
                            else
                            {
                                //if sender sent it to somebody else, it's time to actually update the state of the market
                                stockholder = recipient;
                                theMarket.UpdateStockholder(stockholder);
                                //also let the user know that they successfully managed to trade their item
                                response = new GiveStockResponse(true, theMarket.CopyThis(), GiveStockResponse.GAVE_STOCK);
                                logger.LogTrade(sendID, recipient);
                                logger.LogMarketInfo(theMarket);
                            }
                        }
                        else
                        {
                            //cant give stocks to people what don't exist
                            response = new GiveStockResponse(false, theMarket.CopyThis(), GiveStockResponse.RECIPIENT_DOESNT_EXIST);
                        }
                    }
                    else
                    {
                        //can't send stonks you don't own
                        response = new GiveStockResponse(false, theMarket.CopyThis(), GiveStockResponse.NOT_STOCK_OWNER);
                    }
                }
            }
            return(response);
        }