예제 #1
0
        /// <summary>
        /// Trades the goods.
        /// </summary>
        /// <returns><c>true</c>, if goods was traded, <c>false</c> otherwise.</returns>
        /// <param name="user">User.</param>
        /// <param name="goods">Goods.</param>
        /// <param name="goodsNum">Goods number.</param>
        /// <param name="errorCode">Error code.</param>
        public bool TradeGoods(BaseUser user, GoodsData goods, int goodsNum, out TradeErrorCode errorCode)
        {
            bool result = false;

            if (_tradeStrategy.TryEnterTraded(goods))
            {
                try
                {
                    double payAmount;
                    if (HasTraded(user, goods, goodsNum, out payAmount, out errorCode))
                    {
                        var items = ProduceGoods(goods, goodsNum);
                        if (SendGoods(user, items) && PayAmount(user, goods.TradeType, payAmount))
                        {
                            TradeSuccess(user, goods, goodsNum, payAmount, items);
                            errorCode = TradeErrorCode.Sucess;
                            result    = true;
                        }
                        else
                        {
                            errorCode = TradeErrorCode.Fail;
                        }
                    }
                }
                finally
                {
                    _tradeStrategy.ExitTraded();
                }
            }
            else
            {
                errorCode = TradeErrorCode.TradeTimeout;
            }
            return(result);
        }