protected TraderBase(List<CardSet> setSearchOrder, TimeSpan maxTradeTime, string botName, string tradee)
 {
     MagicCardList = IoC.Resolve<IMagicCardList>();
     SetSearchOrder = setSearchOrder;
     MaxTradeTime = maxTradeTime;
     MagicOnlineInterface = IoC.Resolve<ITradeHandler>();
     Pbv = IoC.Resolve<IPixelBasedVariables>();
     ApplicationSettings = IoC.Resolve<IApplicationSettings>();
     WinManager = IoC.Resolve<IWindowManager>();
     MessageHandler = IoC.Resolve<IMessageHandler>();
     TradeFilterHandler = IoC.Resolve<ITradeFilterHandler>();
     MagicOnlineInterface.YouGiveAmountChangedEvent += ExamineYouGiveCards;
     BotName = botName;
     TimeInTrade = new Stopwatch();
     TimeInTrade.Start();
     TimeSinceLastAction = new Stopwatch();
     CardsYouGive = new MagicCardCollection();
     CardsYouGet = new MagicCardCollection();
     TheirCollection = new MagicCardCollection();
     SelectionHasChanged = false;
     ConfirmMessageOnly = false;
     TradeeName = tradee;
     _logger.Trace("Tradee set to: " + tradee);
 }
        public override void StartTrade()
        {
            base.StartTrade();

            if (!WinManager.InTrade()) return;

            MessageHandler.SendMessage(ExaminingCollection);

            if (!WinManager.InTrade()) return;

            SetFilters();
            GetTheirCollection(ApplicationSettings.OwnedLessThan);

            if (!WinManager.InTrade()) return;

            CardsYouGet = new MagicCardCollection();

            while (WinManager.InTrade() && CheckTimer())
            {
                string message;
                MagicOnlineInterface.DetermineIfMessageAreaHasChanged();
                //Watch for user selecting cards to price check
                do
                {
                    message = string.Empty;
                    MagicOnlineInterface.DetermineIfYouGiveAmountChanged(true);
                    if (MagicOnlineInterface.DetermineIfMessageAreaHasChanged())
                    {
                        message = MessageHandler.GetLastTradeChatMessage();
                    }
                    AutoItX.Sleep(200);
                } while ((string.IsNullOrWhiteSpace(message)
                    || !message.Contains(TradeeName.ToLower())
                    || !message.ToLower().Contains(Constants.DoneMessage.ToLower()))
                    && CheckTimer()
                    && WinManager.InTrade());

                if (!WinManager.InTrade()) return;

                DetermineCardsYouGiveAmount();

                //If I have already either choosen the cards to buy or they have lots of credit
                if (CardsYouGet.BuySum() >= (CardsYouGiveAmount - Credit) && CardsYouGiveAmount > 0)
                {
                    if (CardsYouGet.CountOfCards() <= 0 || YouGiveAmountHasChanged)
                    {
                        SelectionHasChanged = false;
                        ConfirmMessageOnly = true;
                        SendPreConfirmMessage();
                    }

                    do
                    {
                        if (WinManager.OnTradeScreen() && CheckTimer())
                        {
                            MagicOnlineInterface.PressConfirmButton();
                            AutoItX.Sleep(10000);
                        }
                    } while (!WinManager.OnTradeConfirmScreen() && CheckTimer() && WinManager.OnTradeScreen());

                    if (!WinManager.InTrade()) return;

                    ConfirmationScreenValidationChecks();

                    if (!WinManager.InTrade()) return;

                    CompleteConfirmationScreenTrade();
                    return;
                }

                _logger.TraceFormat("CardsYouGiveAmount: {0}, MaxYouGiveAmount: {1}", CardsYouGiveAmount, MaxYouGiveAmount);
                if (CardsYouGiveAmount <= MaxYouGiveAmount && CardsYouGiveAmount > 0)
                {
                    DetermineCardsYouTake();
                    SendPreConfirmMessage();
                    YouGiveAmountHasChanged = false;
                }
                else
                {
                    MessageHandler.SendMessage(string.Format(SelectFewerMessage, MaxYouGiveAmount));
                }

                AutoItX.Sleep(1000);
            }

            if (!CheckTimer())
            {
                MagicOnlineInterface.CancelTrade();
            }
        }
 private void PopulateCardsYouGet()
 {
     MagicOnlineInterface.DetermineCardsYouGet();
     CardsYouGet = new MagicCardCollection();
     foreach (var cardYouGet in MagicOnlineInterface.CardsYouGet.Values)
     {
         CardsYouGet[cardYouGet.Id] = cardYouGet;
     }
 }
        /// <summary>
        /// Goes through the List View of their collection to get the first 25 Mythic and Rares to be used to price out trades quickly.
        /// </summary>
        internal override void GetTheirCollection(int ownLessThan)
        {
            TheirCollection = new MagicCardCollection();

            LessThanValue = ownLessThan;
            TradeFilterHandler.SetOwnedFilterNumberValue(LessThanValue);

            AddToTheirCollection(GetPartOfTheirCollection(CardSet.AllCards, RaritySet.Mythic).Values);
            AddToTheirCollection(GetPartOfTheirCollection(CardSet.AllCards, RaritySet.Rare).Values);

            var sum =
                TheirCollection.ToList.OrderByDescending(p => p.BuyPrice).Take(MaxTradeAmount).Sum(
                    p => p.BuyPrice*p.CopiesOfCard);

            MaxYouGiveAmount = sum + Credit;
            MessageHandler.SendPriceMessage(
                TheirCollection.ToList.OrderByDescending(p => p.BuyPrice).Take(MaxTradeAmount).ToList(), false);

            MessageHandler.SendMessage(
                string.Format(
                    CollectionFoundMessage,
                    sum, MaxYouGiveAmount));

            if (MaxYouGiveAmount == 0)
            {
                MessageHandler.SendMessage(NothingTradableMessage);
                AutoItX.Sleep(5000);
                if (WinManager.InTrade())
                {
                    MagicOnlineInterface.CancelTrade();
                }
                return;
            }

            var databaseInteractions = new DatabaseInteractions();
            using (var connection = Connection.NewConnection())
            {
                connection.BeginTransaction();
                foreach (var card in TheirCollection.ToList)
                {
                    databaseInteractions.AddCardForTrader(TradeeName, card.Id, card.CopiesOfCard, connection);
                }

                connection.CommitTransaction();
            }
        }