Example #1
0
        public static void UpdatePrice(
            IMagicCardDefinition definition,
            MagicCardPrice price,
            bool autoWrite,
            string additionalLogText,
            bool forcePriceUpdate)
        {
            try
            {
                lock (_sync)
                {
                    if (!forcePriceUpdate && price.IsPriceUpToDate())
                    {
                        return;
                    }

                    var request = new CardPriceRequest(_notificationCenter);
                    request.PerformRequest(definition, price, true, additionalLogText);

                    if (autoWrite)
                    {
                        Write();
                    }
                }
            }
            catch (Exception error)
            {
                _notificationCenter.FireNotification(
                    LogLevel.Error,
                    string.Format("Error getting price for {0}({1}): {2} ", definition.NameEN, definition.SetCode, error.Message) + additionalLogText);
            }
        }
Example #2
0
        public void AddCard(
            IMagicCardDefinition cardDefinition,
            int quantity,
            MagicGrade grade,
            MagicLanguage language,
            bool isFoil,
            bool updateTotals)
        {
            var binderCard = new MagicBinderCard()
            {
                CardId   = cardDefinition.CardId,
                Grade    = grade,
                Language = language,
                IsFoil   = isFoil,
                Quantity = quantity
            };

            var viewModel = new MagicBinderCardViewModel(cardDefinition, binderCard);

            _sortedCards.Add(binderCard.RowId, viewModel);
            _cards.Add(viewModel);
            _magicCollection.Cards.Add(binderCard);

            viewModel.PriceChanged += Card_PriceChanged;

            if (updateTotals)
            {
                CalculateTotals();
            }
        }
Example #3
0
 public FoundMagicCardViewModel(IMagicCardDefinition definition)
 {
     _viewModel = new MagicBinderCardViewModel(definition, new MagicBinderCard()
     {
         CardId = definition.CardId
     });
     _viewModel.PropertyChanged += OnViewModelPropertyChanged;
     UpdateSetData();
 }
Example #4
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();
        }
        public void BuildDefaultMkmImagePath(IMagicCardDefinition definition)
        {
            if (string.IsNullOrEmpty(_imagePath))
            {
                var setDefinition = StaticMagicData.SetDefinitionsBySetCode[definition.SetCode];

                var setName   = setDefinition.Name;
                var anthology = setName.IndexOf("Duel Decks Anthology", 0, StringComparison.InvariantCultureIgnoreCase);
                if (anthology == 0)
                {
                    setName = setName.Substring(0, "Duel Decks Anthology".Length);
                }


                _imagePath = string.Format(
                    CultureInfo.InvariantCulture,
                    "./img/cards/{0}/{1}.jpg",
                    setName
                    .Replace(" ", "_")
                    .Replace(":", "")
                    .Replace("-", "_")
                    .Replace(",", "")
                    .Replace("'", ""),
                    definition.NameMkm
                    .Replace("Æ", "ae")
                    .Replace("û", "u")
                    .Replace("â", "a")
                    .Replace("á", "a")
                    .Replace("'", "")
                    .Replace(".", "")
                    .Replace(":", "")
                    .Replace(" // ", "_")
                    .ToLowerInvariant()
                    .Replace(", planeswalker", "")
                    .Replace(" (version 1)", "1")
                    .Replace(" (version 2)", "2")
                    .Replace(" (version 3)", "3")
                    .Replace(" (version 4)", "4")
                    .Replace(" (version 5)", "5")
                    .Replace(" (version 6)", "6")
                    .Replace(" (version 7)", "7")
                    .Replace(" (version 8)", "8")
                    .Replace(" (version 9)", "9")
                    .Replace(" ", "_")
                    .Replace("-", "_")
                    .Replace(",", ""));
            }
        }
Example #6
0
        public static bool IsNameMatch(
            IMagicCardDefinition definition,
            CardLookup lookup)
        {
            if (definition.DisplayNameEn.IndexOf(lookup.SearchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                return(true);
            }

            if (lookup.SearchGerman && definition.NameDE.IndexOf(lookup.SearchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                return(true);
            }

            if (lookup.SearchRules && definition.RulesText.IndexOf(lookup.SearchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                return(true);
            }

            return(false);
        }
Example #7
0
        public static MagicCardPrice FindPrice(
            IMagicCardDefinition definition,
            bool autoUpdate,
            bool saveDatabase,
            string additionalLogText,
            bool forcePriceUpdate)
        {
            MagicCardPrice price = null;

            if (definition == null)
            {
                return(price);
            }

            if (!PriceCache.TryGetValue(definition.CardId, out price))
            {
                price = new MagicCardPrice
                {
                    CardId = definition.CardId,
                };

                PriceCache.TryAdd(definition.CardId, price);
            }

            if (autoUpdate)
            {
                Task.Factory.StartNew(() => UpdatePrice(definition, price, saveDatabase, additionalLogText, forcePriceUpdate));
            }

            if (!autoUpdate && saveDatabase)
            {
                Write();
            }

            return(price);
        }
        public void PerformRequest(
            IMagicCardDefinition card,
            MagicCardPrice cardPrice,
            bool notifyOfPriceUpdate,
            string additionalLogText)
        {
            try
            {
                var foundSet      = false;
                var setDefinition = StaticMagicData.SetDefinitionsBySetCode[card.SetCode];

                var setName = setDefinition.Name;
                switch (setName)
                {
                case "Time Spiral \"Timeshifted\"":
                    setName = "Time Spiral";
                    break;

                case "Friday Night Magic":
                    setName = "Friday Night Magic Promos";
                    break;

                case "Magic 2014 Core Set":
                    setName = "Magic 2014";
                    break;

                case "Magic 2015 Core Set":
                    setName = "Magic 2015";
                    break;
                }

                var cardName = card.NameEN;

                var cardNameMkm = string.IsNullOrWhiteSpace(card.NameMkm)
                    ? card.NameEN
                    : card.NameMkm;

                if (!CheckRequestCount(false, true))
                {
                    return;
                }

                WorkaroundTimeout(_notificationCenter, additionalLogText);
                var helper = new RequestHelper(_notificationCenter, additionalLogText);
                using (var result = helper.MakeRequest(RequestHelper.CreateGetProductsUrl(cardName, MagicLanguage.English, card.MagicCardType != MagicCardType.Token, null)))
                {
                    var productNodes = result.Response.Root.Elements("product");
                    foreach (var productNode in productNodes)
                    {
                        var exactNameMatch = false;

                        var expansion = productNode.Element("expansion");
                        if (expansion == null || !expansion.Value.Equals(setName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }

                        // Lookup correct version:
                        var nameNodes = productNode.Elements("name");
                        foreach (var nameNode in nameNodes)
                        {
                            var idLanguage = nameNode.Element("idLanguage");
                            if (idLanguage != null && idLanguage.Value == "1")
                            {
                                var exactProductName = nameNode.Element("productName");
                                if (exactProductName != null && exactProductName.Value.Equals(cardNameMkm, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    // Exact name matches.
                                    exactNameMatch = true;
                                    break;
                                }
                            }
                        }

                        if (!exactNameMatch)
                        {
                            continue;
                        }

                        foundSet = true;
                        var priceGuide = productNode.Element("priceGuide");
                        if (priceGuide != null)
                        {
                            cardPrice.IsNotifying = false;
                            try
                            {
                                cardPrice.PriceLow     = decimal.Parse(priceGuide.Element("LOW").Value, CultureInfo.InvariantCulture);
                                cardPrice.PriceAvg     = decimal.Parse(priceGuide.Element("AVG").Value, CultureInfo.InvariantCulture);
                                cardPrice.PriceFoilLow = decimal.Parse(priceGuide.Element("LOWFOIL").Value, CultureInfo.InvariantCulture);
                                cardPrice.PriceTrend   = decimal.Parse(priceGuide.Element("TREND").Value, CultureInfo.InvariantCulture);
                                cardPrice.PriceSell    = decimal.Parse(priceGuide.Element("SELL").Value, CultureInfo.InvariantCulture);

                                cardPrice.ImagePath = productNode.Element("image").Value;
                                cardPrice.WebSite   = productNode.Element("website").Value;

                                // Get lowest actual price from german vendor
                                var mkmId = productNode.Element("idProduct");
                                if (mkmId != null)
                                {
                                    cardPrice.MkmId = mkmId.Value;
                                    RequestActualVendorPrice(mkmId.Value, card.NameEN, cardPrice, additionalLogText);
                                }
                            }
                            finally
                            {
                                cardPrice.IsNotifying = true;
                            }
                        }

                        cardPrice.UpdateUtc = DateTime.UtcNow;
                        _notificationCenter.FireNotification(
                            LogLevel.Debug,
                            string.Format("Downloaded price data for '{0}({1})': {2}/{3}/{4}/'{5}'", card.DisplayNameEn, card.SetCode, cardPrice.PriceLow, cardPrice.PriceAvg, cardPrice.PriceTrend, cardPrice.ImagePath) + additionalLogText);

                        // Found my price -> Can exit loop now
                        break;
                    }

                    if (!foundSet)
                    {
                        _notificationCenter.FireNotification(
                            LogLevel.Debug,
                            string.Format("Cannot find price data for '{0}({1})'. Request: {2}", card.DisplayNameEn, setDefinition.Name, result.Response.Root) + additionalLogText);
                    }

                    if (notifyOfPriceUpdate)
                    {
                        cardPrice.RaisePriceChanged();
                        RequestCounter.Save();
                    }
                }
            }
            catch (Exception error)
            {
                if (notifyOfPriceUpdate)
                {
                    RequestCounter.Save();
                }

                _notificationCenter.FireNotification(
                    LogLevel.Error,
                    string.Format("Error downloading price data for '{0}({1})': {2}", card.DisplayNameEn, card.SetCode, error.Message) + additionalLogText);
            }
        }