internal void ImportFromLastGame()
        {
            if (Core.Game.DrawnLastGame == null)
            {
                return;
            }
            var deck = new Deck();

            foreach (var card in Core.Game.DrawnLastGame)
            {
                if (card.IsCreated)
                {
                    continue;
                }

                deck.Cards.Add(card);

                if (string.IsNullOrEmpty(deck.Class) && card.GetPlayerClass != "Neutral")
                {
                    deck.Class = card.PlayerClass;
                }
            }

            ShowDeckEditorFlyout(deck, true);
        }
        internal async void ImportFromIdString()
        {
            try
            {
                var settings  = new MessageDialogs.Settings();
                var clipboard = Clipboard.ContainsText() ? Clipboard.GetText() : "";
                if (clipboard.Count(c => c == ':') > 0 && clipboard.Count(c => c == ';') > 0)
                {
                    settings.DefaultText = clipboard;
                }

                //import dialog
                var idString =
                    await
                    DialogManager.ShowInputAsync(this, "Import deck",
                                                 "id:count;id2:count2;... (e.g. EX1_050:2;EX1_556:1;)\nObtained from: \nEXPORT > COPY IDS TO CLIPBOARD",
                                                 settings);

                if (string.IsNullOrEmpty(idString))
                {
                    return;
                }
                var deck = new Deck();
                foreach (var entry in idString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var splitEntry = entry.Split(':');
                    if (splitEntry.Length != 2)
                    {
                        continue;
                    }
                    var card = Hearthstone.Database.GetCardFromId(splitEntry[0]);
                    if (card.Id == "UNKNOWN")
                    {
                        continue;
                    }
                    int.TryParse(splitEntry[1], out var count);
                    card.Count = count;

                    if (string.IsNullOrEmpty(deck.Class) && card.GetPlayerClass != "Neutral")
                    {
                        deck.Class = card.GetPlayerClass;
                    }

                    deck.Cards.Add(card);
                }
                if (Config.Instance.AutoSaveOnImport)
                {
                    DeckManager.SaveDeck(deck);
                }
                else
                {
                    ShowDeckEditorFlyout(deck, true);
                }
            }
            catch (Exception ex)
            {
                Log.Info("Error importing deck from clipboard(id string): " + ex);
            }
        }
Beispiel #3
0
        public static async Task <Deck> TryFindDeck(string url)
        {
            try
            {
                var doc = await ImportingHelper.GetHtmlDoc(url);

                var deck      = new Deck();
                var metaNodes = doc.DocumentNode.SelectNodes("//meta");
                if (!metaNodes.Any())
                {
                    return(null);
                }
                deck.Name = GetMetaProperty(metaNodes, "x-hearthstone:deck");
                deck.Url  = GetMetaProperty(metaNodes, "x-hearthstone:deck:url") ?? url;

                var deckString = GetMetaProperty(metaNodes, "x-hearthstone:deck:deckstring");
                if (!string.IsNullOrEmpty(deckString))
                {
                    try
                    {
                        var hearthDbDeck   = DeckSerializer.Deserialize(deckString);
                        var fromDeckString = HearthDbConverter.FromHearthDbDeck(hearthDbDeck);
                        fromDeckString.Name = deck.Name;
                        fromDeckString.Url  = deck.Url;
                        return(fromDeckString);
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);
                    }
                }

                var heroId = GetMetaProperty(metaNodes, "x-hearthstone:deck:hero");
                if (!string.IsNullOrEmpty(heroId))
                {
                    deck.Class = Hearthstone.Database.GetCardFromId(heroId).PlayerClass;
                }
                var cardList = GetMetaProperty(metaNodes, "x-hearthstone:deck:cards").Split(',');
                foreach (var idGroup in cardList.GroupBy(x => x))
                {
                    var card = Hearthstone.Database.GetCardFromId(idGroup.Key);
                    card.Count = idGroup.Count();
                    deck.Cards.Add(card);
                    if (deck.Class == null && card.IsClassCard)
                    {
                        deck.Class = card.PlayerClass;
                    }
                }
                return(deck);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(null);
            }
        }
 public static void SaveDeck(Deck baseDeck, Deck newVersion, bool overwriteCurrent = false)
 {
     DeckList.Instance.Decks.Remove(baseDeck);
     baseDeck.Versions?.Clear();
     if (!overwriteCurrent)
     {
         newVersion.Versions.Add(baseDeck);
     }
     newVersion.SelectedVersion = newVersion.Version;
     newVersion.Archived        = false;
     SaveDeck(newVersion, false);
     DeckManagerEvents.OnDeckUpdated.Execute(newVersion);
 }
 public static void SaveDeck(Deck deck, bool invokeApi = true)
 {
     deck.Edited();
     DeckList.Instance.Decks.Add(deck);
     DeckList.Save();
     Core.MainWindow.DeckPickerList.SelectDeckAndAppropriateView(deck);
     Core.MainWindow.DeckPickerList.UpdateDecks(forceUpdate: new[] { deck });
     Core.MainWindow.SelectDeck(deck, true);
     if (invokeApi)
     {
         DeckManagerEvents.OnDeckCreated.Execute(deck);
     }
 }
        internal void ImportFromFile()
        {
            var dialog = new OpenFileDialog {
                Title = "Select Deck File", DefaultExt = "*.xml;*.txt", Filter = "Deck Files|*.txt;*.xml"
            };

            dialog.Multiselect = true;
            var dialogResult = dialog.ShowDialog();

            if (dialogResult == true)
            {
                foreach (var file in dialog.FileNames)
                {
                    try
                    {
                        Deck deck = null;

                        if (file.EndsWith(".txt"))
                        {
                            using (var sr = new StreamReader(file))
                                deck = StringImporter.Import(sr.ReadToEnd());
                        }
                        else if (file.EndsWith(".xml"))
                        {
                            deck = XmlManager <Deck> .Load(file);

                            //not all required information is saved in xml
                            foreach (var card in deck.Cards)
                            {
                                card.Load();
                            }
                            TagControlEdit.SetSelectedTags(deck.Tags);
                        }
                        if (Config.Instance.AutoSaveOnImport || dialog.FileNames.Length > 1)
                        {
                            DeckManager.SaveDeck(deck);
                        }
                        else
                        {
                            ShowDeckEditorFlyout(deck, true);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                    }
                }
            }
        }
        private async Task ShowImportingChoice(Deck deck)
        {
            var choice = Config.Instance.PasteImportingChoice == ImportingChoice.Manual
                                ? await MessageDialogs.ShowImportingChoiceDialog(this) : Config.Instance.PasteImportingChoice;

            if (choice.HasValue)
            {
                if (choice.Value == ImportingChoice.SaveLocal)
                {
                    ShowDeckEditorFlyout(deck, true);
                }
                else
                {
                    ShowExportFlyout(deck);
                }
            }
        }
        public void ImportArenaDeck(Mirror.Objects.Deck deck)
        {
            var arenaDeck = new Deck {
                Class = Hearthstone.Database.GetCardFromId(deck.Hero).PlayerClass,
                HsId  = deck.Id,
                Cards = new ObservableCollection <Card>(deck.Cards.Select(x =>
                {
                    var card   = Hearthstone.Database.GetCardFromId(x.Id);
                    card.Count = x.Count;
                    return(card);
                })),
                LastEdited  = DateTime.Now,
                IsArenaDeck = true
            };

            arenaDeck.Name = Helper.ParseDeckNameTemplate(Config.Instance.ArenaDeckNameTemplate, arenaDeck);
            DeckList.Instance.Decks.Add(arenaDeck);
            DeckPickerList.UpdateDecks();
            SelectDeck(arenaDeck, true);
        }
 public void SetPlayerCards(Deck deck, List <Card> revealedCards) => SetPlayerCards(deck?.Cards, revealedCards);
 public bool BelongsToDeckVerion(Deck deck) => PlayerDeckVersion == deck.Version ||
 !IsAssociatedWithDeckVersion && deck.Version == new SerializableVersion(1, 0);
        private static async Task AutoSelectDeck(Deck currentDeck, string heroClass, GameMode mode, Format?currentFormat, List <IGrouping <string, Entity> > cardEntites = null)
        {
            _waitingForDraws++;
            await Task.Delay(500);

            _waitingForDraws--;
            if (_waitingForDraws > 0)
            {
                return;
            }
            var validDecks = DeckList.Instance.Decks.Where(x => x.Class == heroClass && !x.Archived && !x.IsDungeonDeck).ToList();

            if (currentDeck != null)
            {
                validDecks.Remove(currentDeck);
            }
            validDecks = validDecks.FilterByMode(mode, currentFormat);
            if (cardEntites != null)
            {
                validDecks = validDecks.Where(x => cardEntites.All(ce => x.GetSelectedDeckVersion().Cards.Any(c => c.Id == ce.Key && c.Count >= ce.Count()))).ToList();
            }
            if (_autoSelectCount > 1)
            {
                Log.Info("Too many auto selects. Showing dialog.");
                ShowDeckSelectionDialog(validDecks);
                return;
            }
            if (validDecks.Count == 0)
            {
                if (cardEntites == null || !AutoSelectDeckVersion(heroClass, mode, currentFormat, cardEntites))
                {
                    Log.Info("No matching deck found, using no-deck mode");
                    Core.MainWindow.SelectDeck(null, true);
                }
                return;
            }
            if (validDecks.Count == 1)
            {
                var deck = validDecks.Single();
                Log.Info("Found one matching deck: " + deck);
                Core.MainWindow.SelectDeck(deck, true);
                _autoSelectCount++;
                return;
            }
            var lastUsed = DeckList.Instance.LastDeckClass.FirstOrDefault(x => x.Class == heroClass);

            if (lastUsed != null)
            {
                var deck = validDecks.FirstOrDefault(x => x.DeckId == lastUsed.Id);
                if (deck != null)
                {
                    Log.Info($"Last used {heroClass} deck matches!");
                    Core.MainWindow.SelectDeck(deck, true);
                    _autoSelectCount++;
                    return;
                }
            }
            if (cardEntites == null || !AutoSelectDeckVersion(heroClass, mode, currentFormat, cardEntites))
            {
                ShowDeckSelectionDialog(validDecks);
            }
        }
 private static List <IGrouping <string, Entity> > GetMissingCards(List <IGrouping <string, Entity> > revealed, Deck deck) =>
 revealed.Where(x => !deck.GetSelectedDeckVersion().Cards.Any(c => c.Id == x.Key && c.Count >= x.Count())).ToList();
        public static List <Deck> ImportDecksTo(ICollection <Deck> targetList, IEnumerable <ImportedDeck> decks, bool brawl, bool importNew, bool updateExisting)
        {
            var importedDecks = new List <Deck>();

            foreach (var deck in decks)
            {
                if (deck.SelectedImportOption is NewDeck)
                {
                    if (!importNew)
                    {
                        continue;
                    }
                    Log.Info($"Saving {deck.Deck.Name} as new deck.");
                    var newDeck = new Deck
                    {
                        Class = deck.Class,
                        Name  = deck.Deck.Name,
                        HsId  = deck.Deck.Id,
                        Cards = new ObservableCollection <Card>(deck.Deck.Cards.Select(x =>
                        {
                            var card   = Hearthstone.Database.GetCardFromId(x.Id);
                            card.Count = x.Count;
                            return(card);
                        })),
                        LastEdited  = DateTime.Now,
                        IsArenaDeck = false
                    };
                    if (brawl)
                    {
                        newDeck.Tags.Add("Brawl");
                        newDeck.Name = Helper.ParseDeckNameTemplate(Config.Instance.BrawlDeckNameTemplate, newDeck);
                    }

                    var existingWithId = targetList.FirstOrDefault(d => d.HsId == deck.Deck.Id);
                    if (existingWithId != null)
                    {
                        existingWithId.HsId = 0;
                    }

                    targetList.Add(newDeck);
                    importedDecks.Add(newDeck);
                }
                else
                {
                    if (!updateExisting)
                    {
                        continue;
                    }
                    var existing = deck.SelectedImportOption as ExistingDeck;
                    if (existing == null)
                    {
                        continue;
                    }
                    var target = existing.Deck;
                    target.HsId = deck.Deck.Id;
                    if (brawl && !target.Tags.Any(x => x.ToUpper().Contains("BRAWL")))
                    {
                        target.Tags.Add("Brawl");
                    }
                    if (target.Archived)
                    {
                        target.Archived = false;
                        Log.Info($"Unarchiving deck: {deck.Deck.Name}.");
                    }
                    if (existing.NewVersion.Major == 0)
                    {
                        Log.Info($"Assinging id to existing deck: {deck.Deck.Name}.");
                    }
                    else
                    {
                        Log.Info(
                            $"Saving {deck.Deck.Name} as {existing.NewVersion.ShortVersionString} (prev={target.Version.ShortVersionString}).");
                        targetList.Remove(target);
                        var oldDeck = (Deck)target.Clone();
                        oldDeck.Versions = new List <Deck>();
                        if (!brawl)
                        {
                            target.Name = deck.Deck.Name;
                        }
                        target.LastEdited = DateTime.Now;
                        target.Versions.Add(oldDeck);
                        target.Version         = existing.NewVersion;
                        target.SelectedVersion = existing.NewVersion;
                        target.Cards.Clear();
                        var cards = deck.Deck.Cards.Select(x =>
                        {
                            var card   = Hearthstone.Database.GetCardFromId(x.Id);
                            card.Count = x.Count;
                            return(card);
                        });
                        foreach (var card in cards)
                        {
                            target.Cards.Add(card);
                        }
                        var clone = (Deck)target.Clone();
                        targetList.Add(clone);
                        importedDecks.Add(clone);
                    }
                }
            }
            return(importedDecks);
        }