public void SaveCompletedTradeLogUpdatesTradeCreditsOnCompletion()
        {
            string tradee = DateTime.Now.Ticks.ToString();
            BotCredit botCredit = DatabaseInteractions.GetTradeCredits(tradee, _botName);

            var creditAfterTrade = 0.375m;
            DatabaseInteractions.SaveCompletedTradeLog(_botName, tradee, new List<MagicCard>(), new List<MagicCard>(), 0.0m, creditAfterTrade);
            botCredit = DatabaseInteractions.GetTradeCredits(tradee, _botName);
            botCredit.Credit.Should().Be(creditAfterTrade);

            //Cleanup
            var dataContext =
                new MagicOnlineBotDb(
                    ConfigurationManager.ConnectionStrings[
                        "CardDataLayer.Properties.Settings.MagicOnlineBotConnectionString"].
                        ToString());

            var tradeeRecord = dataContext.Tradees.Single(p => p.Name == tradee);
            var botCreditRecord = dataContext.BotCredits.Single(p => p.TradeeId == tradeeRecord.TradeeId);
            var tradeRecord = dataContext.Trades.Single(p => p.TradeeId == tradeeRecord.TradeeId);

            dataContext.Trades.DeleteOnSubmit(tradeRecord);
            dataContext.BotCredits.DeleteOnSubmit(botCreditRecord);
            dataContext.Tradees.DeleteOnSubmit(tradeeRecord);
            dataContext.SubmitChanges();
        }
        public void GetCardValue()
        {
            var dataContext = new MagicOnlineBotDb();

            var card = dataContext.Prices.First(p => p.CardId == 20956);
            var magicCard = new MagicCard(card);
            Console.WriteLine("You get - " + magicCard + " OwnedAmount: " + card.OwnedAmount);
        }
        public void ClearTradableCards(string botName)
        {
            _logger.Trace("Clearing tradable cards.");
            var dataContext = new MagicOnlineBotDb();
            var cards = dataContext.Cards;

            WriteTradeFile(0, 0, cards.Select(p => new MagicCard(p, 0)).ToList());
            LoadTradableCards();
            SetEventTicketsTradable(false);
        }
        public void OwnedAmountIsPopulatedInCardList()
        {
            var dataContext = new MagicOnlineBotDb();

            var card = dataContext.Prices.First(p => p.CardId == 20956);
            var magicCard = new MagicCard(card);
            Console.WriteLine("You get - " + magicCard + " OwnedAmount: " + card.OwnedAmount);
            card.OwnedAmount += 7;
            dataContext.SubmitChanges();

            var wishListCards = IoC.GetInstance<IMagicCardList>().GetComprehensiveCommonsAndUncommons(8, 2);
            Console.WriteLine(wishListCards.FirstOrDefault(p => p.Value.OwnedAmount > 0));

            card = dataContext.Prices.First(p => p.CardId == 12);
            magicCard = new MagicCard(card);
            Console.WriteLine("You get - " + magicCard + " OwnedAmount: " + card.OwnedAmount);
            card.OwnedAmount -= 7;
            dataContext.SubmitChanges();

            IoC.GetInstance<IMagicCardList>().InvalidateCache();

            wishListCards = IoC.GetInstance<IMagicCardList>().GetComprehensiveCommonsAndUncommons(8, 2);
            Console.WriteLine(wishListCards.FirstOrDefault(p => p.Value.OwnedAmount > 0));
        }
        public void LoadSetsViaXml()
        {
            MagicOnlineBotDb dataContext = new MagicOnlineBotDb(ConfigurationManager.ConnectionStrings["CardDataLayer.Properties.Settings.MagicOnlineBotConnectionString"].ToString());

            XDocument loadedSets = XDocument.Load(new StringReader(sets));
            var cardSets = from c in loadedSets.Descendants("Set")
                    select new
                               {
                                   Name = c.Attribute("LongName").Value,
                                   Set = c.Attribute("ShortName").Value
                               };

            foreach (var set in cardSets)
            {
                var cardSet = new CardSet();
                cardSet.CardSetId = 0;
                cardSet.CardSetName = set.Name;
                cardSet.CardSetAbrv = set.Set;
                dataContext.CardSets.InsertOnSubmit(cardSet);
            }

            dataContext.SubmitChanges();
        }
        public void WriteDekFileToLoadForUpdatedPricing()
        {
            var dataContext = new MagicOnlineBotDb(ConfigurationManager.ConnectionStrings["CardDataLayer.Properties.Settings.MagicOnlineBotConnectionString"].ToString());
            var cards = dataContext
                .Prices
                .Where(p => p.SellPrice == 999 && p.OwnedAmount > 0)
                .Select(p => new MagicCard(p))
                //.Skip(450)
                .Take(75)
                .ToList();

            WishList wishList = new WishList();
            wishList.WriteWishListFile(cards);
        }
        public void SetupTransactionsForBulkBuyers()
        {
            var dataContext = new MagicOnlineBotDb();
            var druegbotCards =
                dataContext.BotCards.Where(
                    p => p.BotId == 4 && !p.Card.Premium && p.Card.MtgoCardId.GetValueOrDefault(0) > 0 && p.Card.CardSet != "DPA").ToList();
            var cardsToTransfer = new List<Card>();

            foreach (var druegbotCard in druegbotCards)
            {
                for (int i = 0; i < druegbotCard.OwnedAmount && i < 12; i++)
                {
                    cardsToTransfer.Add(druegbotCard.Card);
                }
            }

            var count = cardsToTransfer.Count;
            var transferCount = count/75;

            _logger.Trace("Count" + count);
            _logger.Trace("TransferCount" + transferCount);

            for (int i = 0; i < transferCount; i++)
            {
                var transfer = new Transfer
                                   {
                                       Completed = false,
                                       TradeBotId = 4,
                                       TradeeId = 173,
                                       WishList =
                                           string.Join(",",
                                                       cardsToTransfer.Skip(i*75).Take(75).Select(p => p.MtgoCardId))
                                   };

                dataContext.Transfers.InsertOnSubmit(transfer);
            }

            dataContext.SubmitChanges();
        }
        public void SaveAllImagesForCards()
        {
            var dataContext = new MagicOnlineBotDb();
            var cardSets = dataContext.Cards
                .Select(p => p.CardSet)
                .Distinct()
                .ToList();

            cardSets = cardSets.Where(p => !string.IsNullOrWhiteSpace(p) && p != "DPA").ToList();
            var sets = dataContext.CardSets.ToList();

            foreach (var directory in cardSets)
            {
                string path = @"d:\Projects\Bot\Images\" + directory.Replace("CON", "CFX").ToLower();
                _logger.Trace("Creating directory for " + path);
                if (!Directory.Exists(path))
                {
                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                    }

                }
            }

            foreach (var cardSet in cardSets.Where(p => sets.Any(c => c.CardSetAbrv == p && !string.IsNullOrWhiteSpace(c.WebsiteAbrv))))
            {
                var set = sets.First(c => c.CardSetAbrv == cardSet);
                var max = dataContext.Cards.Count(p => !p.Premium && p.CardSet == cardSet);

                for (int i = 1; i <= max; i++)
                {
                    string filename =
                        string.Format("{0}\\{1}.jpg", set.CardSetAbrv.Replace("CON", "CFX"), i).ToLower();
                    string imageUrl = string.Format("http://magiccards.info/scans/en/{0}/{1}.jpg", set.WebsiteAbrv,
                                                    i).ToLower();

                    var imageDownload = new DownloadImage(imageUrl, filename);
                    imageDownload.Download();
                }
            }
        }
        public void MarkCardsInExcessTradable(int excess, int max, string botName, List<RaritySet> rarities)
        {
            var dataContext = new MagicOnlineBotDb();
            var bot = dataContext.TradeBots.Single(p => p.Name == botName);

            var botCards =
                dataContext
                    .BotCards
                    .Where(p => p.BotId == bot.BotId
                                && p.OwnedAmount > excess
                                && rarities.Contains((RaritySet) p.Card.CardRarity))
                    .Select(p => new MagicCard(p.Card, p.OwnedAmount))
                    .ToList();

            WriteTradeFile(excess, max, botCards);
        }
        public void TradeeAndBotCreditIsCreatedIfDoNotExist()
        {
            string tradee = DateTime.Now.Ticks.ToString();
            BotCredit botCredit = DatabaseInteractions.GetTradeCredits(tradee, _botName);
            botCredit.Should().NotBeNull();
            botCredit.NumOfTrades.Should().Be(1);
            botCredit.Credit.Should().Be((decimal)0.0);

            botCredit = DatabaseInteractions.GetTradeCredits(tradee, _botName);
            botCredit.Should().NotBeNull();
            botCredit.NumOfTrades.Should().Be(2);
            botCredit.Credit.Should().Be((decimal)0.0);

            //Cleanup
            var dataContext =
                new MagicOnlineBotDb();

            var tradeeRecord = dataContext.Tradees.Single(p => p.Name == tradee);
            var botCreditRecord = dataContext.BotCredits.Single(p => p.TradeeId == tradeeRecord.TradeeId);

            dataContext.BotCredits.DeleteOnSubmit(botCreditRecord);
            dataContext.Tradees.DeleteOnSubmit(tradeeRecord);
            dataContext.SubmitChanges();
        }