Beispiel #1
0
        public void UpdatePriceData(bool writeDatabase, bool async, string additionalLogText, bool forcePriceUpdate)
        {
            var action = new Action(() =>
            {
                var update = !_price.IsPriceUpToDate();
                if (forcePriceUpdate || update)
                {
                    StaticPriceDatabase.UpdatePrice(_definition, _price, writeDatabase, additionalLogText, forcePriceUpdate);
                    UpdatePrice();
                }
                else
                {
                    _notificationCenter.FireNotification(
                        LogLevel.Debug,
                        "Skipping price update for " + _definition.DisplayNameEn + " " + additionalLogText);
                }
            });

            if (async)
            {
                Task.Factory.StartNew(action);
            }
            else
            {
                action();
            }
        }
Beispiel #2
0
 private void OnResetImages(object sender, RoutedEventArgs e)
 {
     Task.Factory
     .StartNew(() =>
     {
         StaticPriceDatabase.ClearImageData(NotificationCenter.Instance);
     });
 }
Beispiel #3
0
        public MagicBinderCardViewModel(
            IMagicCardDefinition definition,
            MagicBinderCard card)
        {
            _notificationCenter = NotificationCenter.Instance;
            _definition         = definition;
            _card  = card;
            _price = StaticPriceDatabase.FindPrice(_definition, false, false, "", false);
            _price.PriceChanged += OnPricePriceChanged;

            UpdatePrice();
        }
Beispiel #4
0
        public void PriceSearchResult()
        {
            var searchResult = _searchResult;

            if (searchResult == null || !searchResult.Any())
            {
                return;
            }

            searchResult = searchResult
                           .DistinctBy(c => c.NameEN)
                           .Where(c => c.CardPrice == null || !c.CardPrice.IsPriceUpToDate())
                           .OrderBy(c => !c.CardPrice.CheapestPrice.HasValue)
                           .ThenByDescending(c => c.CardPrice.CheapestPrice)
                           .ToList();

            _notificationCenter.FireNotification(
                LogLevel.Info,
                string.Format("Starting price lookup for {0} cards", searchResult.Count()));

            Task.Factory.StartNew(() =>
            {
                var count     = searchResult.Count();
                var index     = 0;
                var stopwatch = Stopwatch.StartNew();
                foreach (var card in searchResult)
                {
                    ++index;
                    card.UpdatePriceData(false, false, " (card " + index + " of " + count + ")", false);

                    if (index % 30 == 0)
                    {
                        StaticPriceDatabase.Write();

                        ////var seconds = 30;
                        ////_notificationCenter.FireNotification(
                        ////	LogLevel.Debug,
                        ////	string.Format("Waiting {0} seconds to avoid being locked out by MKM", seconds));

                        ////// Wait a bit between requests
                        ////Thread.Sleep(seconds * 1000);
                    }
                }

                StaticPriceDatabase.Write();

                stopwatch.Stop();

                _notificationCenter.FireNotification(
                    LogLevel.Info,
                    string.Format("Price lookup for {0} cards took {1}", searchResult.Count(), stopwatch.Elapsed));
            });
        }
        public static void OnImageChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var instance = d as CardImage;

            if (instance != null)
            {
                instance.imageControl.Source = instance._emptyImage;

                // Trigger download and display
                var card = instance.SelectedCard;

                instance.SetDefinition = card != null
                    ? StaticMagicData.SetDefinitionsBySetCode[card.SetCode]
                    : null;

                MagicCardPrice cardPrice    = null;
                var            cardFileName = string.Empty;
                Task.Factory.StartNew(() =>
                {
                    cardPrice = StaticPriceDatabase.FindPrice(card, false, false, "CardImage control", false);

                    var download = new CardImageDownload(instance._notificationCenter);
                    cardFileName = download.DownloadImage(card, cardPrice);
                }).ContinueWith(task =>
                {
                    // Lookup card price:
                    instance.CardPrice = cardPrice;

                    if (string.IsNullOrWhiteSpace(cardFileName))
                    {
                        instance.imageControl.Source = instance._emptyImage;
                    }
                    else
                    {
                        var uri    = new Uri(cardFileName);
                        var bitmap = new BitmapImage(uri);
                        bitmap.Freeze();
                        instance.imageControl.Source = bitmap;
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }
Beispiel #6
0
        public void PriceBinder()
        {
            Task.Factory.StartNew(() =>
            {
                var stopwatch = Stopwatch.StartNew();

                _notificationCenter.FireNotification(LogLevel.Info, "Starting price current binder...");

                var cards = _cards.DistinctBy(c => c.CardId).ToList();

                var request = new CardPriceRequest(_notificationCenter);
                request.PerformRequest(cards);
                StaticPriceDatabase.Write();
                CalculateTotals();

                stopwatch.Stop();
                _notificationCenter.FireNotification(LogLevel.Info, string.Format("Done price current binder ({0}).", stopwatch.Elapsed));
            });
        }
        public string DownloadImage(MagicCardDefinition card, MagicCardPrice cardPrice)
        {
            if (card == null)
            {
                return(null);
            }

            if (cardPrice == null)
            {
                cardPrice = StaticPriceDatabase.FindPrice(card, false, false, "CardImage download", false);
            }

            // Add default image path if needed
            cardPrice.BuildDefaultMkmImagePath(card);

            FileInfo localStorage = null;
            string   fullUrl      = null;

            try
            {
                var cache = PathHelper.CardImageCacheFolder;
                localStorage = new FileInfo(Path.Combine(cache, CreateCardIdPart(card, '\\', true, true).TrimStart('\\')));
                if (localStorage.Exists)
                {
                    return(localStorage.FullName);
                }

                var url = CreateCardIdPart(card, '/', false, false);
                if (url == null)
                {
                    return(null);
                }

                var stopwatch = Stopwatch.StartNew();

                if (!localStorage.Directory.Exists)
                {
                    localStorage.Directory.Create();
                }

                using (var client = new WebClient())
                {
                    var rootUrl = card.MagicCardType != MagicCardType.Token
                        ? "http://magiccards.info/scans/en"
                        : "http://magiccards.info/extras/token";

                    fullUrl = !string.IsNullOrWhiteSpace(cardPrice.ImagePath)
                        ? "http://www.magickartenmarkt.de/" + cardPrice.ImagePath
                        : rootUrl + url;

                    client.DownloadFile(new Uri(fullUrl), localStorage.FullName);
                }

                stopwatch.Stop();
                _notificationCenter.FireNotification(
                    LogLevel.Debug,
                    string.Format("Downloaded image for '{0}[{1}]' in {2}", card.NameEN, card.SetCode, stopwatch.Elapsed));
            }
            catch (Exception error)
            {
                _notificationCenter.FireNotification(
                    LogLevel.Debug,
                    string.Format("Error downloading image for '{0}[{1}]': {2} ({3})", card.NameEN, card.SetCode, error.Message, fullUrl));

                return(null);
            }

            return(localStorage.FullName);
        }