Пример #1
0
        private async Task UpdateCatalog_ExecuteAsync()
        {
            Reporter.StartBusy();

            BulkData    oracleBulkData = (await ScryfallService.GetBulkDataInfo(Reporter)).Data.Single(bd => bd.Name.Contains("Oracle"));
            List <Card> cards          = await ScryfallService.GetBulkCards(oracleBulkData.PermalinkUri, Reporter);

            if (cards == null || cards.Count == 0)
            {
                Reporter.Report("Cards not found or empty", true);
                Reporter.StopBusy();
                return;
            }

            Reporter.Report("Transcribing secrets of the fish men");
            var catalog = new CardCatalog(oracleBulkData, cards, DateTime.Now);

            try
            {
                catalog.SaveToFile();
            }
            catch (Exception e)
            {
                DisplayError(e, "Could not save card catalog to local disk.");
                Reporter.Report(e.Message, true);
                Reporter.StopBusy();
                return;
            }

            OracleCatalog = catalog;
            Reporter.Report("Secrets hidden in a safe place");
            Reporter.StopBusy();
        }
Пример #2
0
        /// <summary>
        /// Gets the cached bitmap image representing the card object. Will query the API if it has not been loaded, otherwise gets the cached version.
        /// </summary>
        public static async Task <Bitmap> GetImageAsync(string imageUri, IProgress <string> reporter)
        {
            if (string.IsNullOrWhiteSpace(imageUri))
            {
                throw new ArgumentNullException(nameof(imageUri), @"Image request URI cannot be null or empty/whitespace. Consumer must check before using this method.");
            }

            while (IsCacheBeingAccessed)
            {
                await Task.Delay(1);
            }

            IsCacheBeingAccessed = true;

            if (ImageCache.ContainsKey(imageUri))
            {
                await Task.Delay(1);

                IsCacheBeingAccessed = false;
                return(ImageCache[imageUri]);
            }

            Bitmap bitmap = await ScryfallService.GetImageAsync(imageUri, reporter);

            ImageCache.Add(imageUri, bitmap);

            if (ImageCache.Count > 100)
            {
                ImageCache.Remove(ImageCache.First().Key);
            }

            IsCacheBeingAccessed = false;
            return(bitmap);
        }
Пример #3
0
        public virtual void Init()
        {
            _mockApi  = new Mock <IApiRepository>();
            _mockRepo = new Mock <IScryfallRepository>();

            _service = new ScryfallService(_mockApi.Object, _mockRepo.Object);
        }
Пример #4
0
        public static void GetAllCardsForSet(string setCode)
        {
            var scryfall = new ScryfallService();
            var cards    = scryfall.GetBySet(setCode);

            foreach (var card in cards)
            {
                System.Console.WriteLine($"{card.Name} ({card.Cmc})");
            }
        }
Пример #5
0
        private async void SubmitButton_OnClick(object sender, RoutedEventArgs e)
        {
            BusyBorder.Visibility = Visibility.Visible;
            var text = new TextRange(SubmitTextBox.Document.ContentStart, SubmitTextBox.Document.ContentEnd).Text;
            var card = await ScryfallService.GetCardsAsync(text);

            var bitmap = await ImageCaching.GetImageAsync(card);

            ImageDisplay.Source   = ImageHelper.LoadBitmap(bitmap);
            BusyBorder.Visibility = Visibility.Collapsed;
        }
Пример #6
0
        public static void OutputAllSets()
        {
            var scryfall = new ScryfallService();
            var sets     = scryfall.GetSets().OrderBy(s => s.ReleasedAt);

            foreach (var s in sets)
            {
                System.Console.WriteLine($"{s.Id} : {s.Name} ({s.Code})");
                System.Console.WriteLine($"Set Type = {s.SetType}");
                System.Console.WriteLine($"Release At = {s.ReleasedAt}");
                System.Console.WriteLine($"Block = {s.Block} ({s.BlockCode})");
                System.Console.WriteLine($"Card Count = {s.CardCount}");
                System.Console.WriteLine($"Digital = {s.Digital}");
                System.Console.WriteLine($"Foil = {s.FoilOnly}");
                System.Console.WriteLine("");
            }
        }
Пример #7
0
        /// <summary>
        /// Select the "preferred" print using <see cref="ArtworkPreferences"/>.
        /// </summary>
        private async void LoadPrints(Card card)
        {
            IsPopulatingPrints = true;

            List <Card> prints = await ScryfallService.GetAllPrintingsAsync(card, Reporter);

            if (IsFront)
            {
                var prices = new List <double>();
                foreach (Card print in prints)
                {
                    if (double.TryParse(print.Prices?.Usd, out double valAsDouble))
                    {
                        prices.Add(valAsDouble);
                    }
                }

                LowestPrice = prices.Any() ? prices.Min() : 0;
            }
            else
            {
                LowestPrice = 0;
            }

            var indexCounter = 0;

            foreach (Card print in prints)
            {
                if (card.Id == print.Id)
                {
                    SelectedPrintingIndex = indexCounter;
                }

                AllPrintings.Add(print);
                indexCounter++;
            }

            if (!AllPrintings.Any())
            {
                AllPrintings.Add(card);
            }

            IsPopulatingPrints = false;
            SelectedPrinting   = AllPrintings[SelectedPrintingIndex];
        }
Пример #8
0
        public async Task LoadImagesFromCards()
        {
            for (var i = 0; i < Cards.Count; i++)
            {
                Card card = Cards[i];
                if (string.IsNullOrWhiteSpace(card?.ImageUris?.Small))
                {
                    continue;
                }

                BitmapSource image = ImageHelper.LoadBitmap(await ScryfallService.GetImageAsync(card.ImageUris.BorderCrop, Reporter));

                if (image != null)
                {
                    Images.Add(new ImageWithIndex {
                        Image = image, Index = i
                    });
                }
            }
        }
Пример #9
0
        private async void ButtonBase_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = false;

            if (!(sender is Button))
            {
                return;
            }

            var random = new Random();

            while (e.LeftButton == MouseButtonState.Pressed)
            {
                await Task.Delay(50);

                Card card = await ScryfallService.GetRandomCard(((MainViewModel)DataContext).Reporter);

                int    qty         = random.Next(1, 5);
                string qtyAsString = qty == 1 ? string.Empty : $"{qty} ";
                DecklistTextBox.AppendText(qtyAsString + card.Name + "\r\n");
            }
        }
Пример #10
0
        private async Task SearchSubmit_ExecuteAsync()
        {
            DisplayErrors.Clear();
            TimeSpan?timeSinceUpdate = DateTime.Now - OracleCatalog.UpdateTime;

            if (timeSinceUpdate == null)
            {
                var yesNoDialog = new YesNoDialogViewModel("Card Catalog must be updated before continuing. Would you like to update the Card Catalog (~65 MB) now?", "Update?");
                if (!(DialogService.ShowDialog(yesNoDialog) ?? false))
                {
                    return;
                }

                await UpdateCatalog_ExecuteAsync();
            }

            if (timeSinceUpdate > TimeSpan.FromDays(7))
            {
                var yesNoDialog = new YesNoDialogViewModel("Card Catalog is out of date, it is recommended you get a new catalog now." +
                                                           "If you don't, cards may not appear in search results or you may receive old " +
                                                           "imagery. Click 'Yes' to update the Card Catalog (~65 MB) now or 'No' use the old catalog.", "Update?");
                if (!(DialogService.ShowDialog(yesNoDialog) ?? false))
                {
                    return;
                }

                await UpdateCatalog_ExecuteAsync();
            }

            DisplayedCards.Clear();
            await Task.Delay(100);

            Reporter.StartBusy();
            Reporter.StartProgress();
            Reporter.Report("Deciphering old one's poem");
            Reporter.StatusReported += BuildingCardsErrors;

            List <SearchLine> lines = DecklistText
                                      .Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries)
                                      .Where(s => !string.IsNullOrWhiteSpace(s))
                                      .Select(l => new SearchLine(l))
                                      .ToList();

            for (var i = 0; i < lines.Count; i++)
            {
                List <Card> cards = OracleCatalog.FindExactCard(lines[i].SearchTerm);

                if (!cards.Any())
                {
                    cards.Add(await ScryfallService.GetFuzzyCardAsync(lines[i].SearchTerm, Reporter));
                }

                if (!cards.Any() || cards.Any(c => c == null))
                {
                    Reporter.Report($"[{lines[i].SearchTerm}] returned no results", true);
                    continue;
                }

                Card preferredCard;

                if (cards.Count > 1)
                {
                    var cardChooser = new ChooseCardDialogViewModel(cards, Reporter);
                    await cardChooser.LoadImagesFromCards();

                    if (!(DialogService.ShowDialog(cardChooser) ?? false))
                    {
                        continue;
                    }

                    preferredCard = ArtPreferences.GetPreferredCard(cardChooser.ChosenCard);
                }
                else
                {
                    preferredCard = ArtPreferences.GetPreferredCard(cards.Single());
                }

                if (preferredCard.IsDoubleFaced)
                {
                    BitmapSource frontImage = ImageHelper.LoadBitmap(await ImageCaching.GetImageAsync(preferredCard.CardFaces[0].ImageUris.BorderCrop, Reporter));
                    var          frontVm    = new CardViewModel(Reporter, ArtPreferences, preferredCard, frontImage, lines[i].Quantity, ZoomPercent);

                    BitmapSource backImage = ImageHelper.LoadBitmap(await ImageCaching.GetImageAsync(preferredCard.CardFaces[1].ImageUris.BorderCrop, Reporter));
                    var          backVm    = new CardViewModel(Reporter, ArtPreferences, preferredCard, backImage, lines[i].Quantity, ZoomPercent, false);

                    DisplayedCards.Add(frontVm);
                    await Task.Delay(10);

                    DisplayedCards.Add(backVm);
                    await Task.Delay(10);

                    Reporter.Progress(i, 0, lines.Count - 1);
                }
                else
                {
                    BitmapSource preferredImage = ImageHelper.LoadBitmap(await ImageCaching.GetImageAsync(preferredCard.ImageUris.BorderCrop, Reporter));
                    var          cardVm         = new CardViewModel(Reporter, ArtPreferences, preferredCard, preferredImage, lines[i].Quantity, ZoomPercent);
                    DisplayedCards.Add(cardVm);
                    await Task.Delay(10);

                    Reporter.Progress(i, 0, lines.Count - 1);
                }
            }

            Reporter.StatusReported -= BuildingCardsErrors;
            Reporter.StopBusy();
            Reporter.StopProgress();
        }