Example #1
0
        private async void BtnCloneDeck_Click(object sender, RoutedEventArgs e)
        {
            var cloneStats =
                (await
                 this.ShowMessageAsync("Clone game history?", "(Cloned games do not count towards class or overall stats.)",
                                       MessageDialogStyle.AffirmativeAndNegative,
                                       new MetroDialogSettings
            {
                AffirmativeButtonText = "clone history",
                NegativeButtonText = "do not clone history"
            })) == MessageDialogResult.Affirmative;

            var clone = (Deck)DeckPickerList.SelectedDeck.Clone();
            var originalStatsEntry = clone.DeckStats;

            while (DeckList.DecksList.Any(d => d.Name == clone.Name))
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Set", DefaultText = clone.Name
                };
                var name =
                    await
                    this.ShowInputAsync("Name already exists", "You already have a deck with that name, please select a different one.", settings);

                if (string.IsNullOrEmpty(name))
                {
                    return;
                }

                clone.Name = name;
            }

            DeckList.DecksList.Add(clone);
            DeckPickerList.AddAndSelectDeck(clone);
            WriteDecks();

            var newStatsEntry = DeckStatsList.Instance.DeckStats.FirstOrDefault(d => d.Name == clone.Name);

            if (newStatsEntry == null)
            {
                newStatsEntry = new DeckStats(clone.Name);
                DeckStatsList.Instance.DeckStats.Add(newStatsEntry);
            }

            //clone game stats
            if (cloneStats)
            {
                foreach (var game in originalStatsEntry.Games)
                {
                    newStatsEntry.AddGameResult(game.CloneWithNewId());
                }
                Logger.WriteLine("cloned gamestats");
            }

            DeckStatsList.Save();
            DeckPickerList.UpdateList();
        }
        private async void SaveDeck(bool overwrite)
        {
            var deckName = TextBoxDeckName.Text;

            if (string.IsNullOrEmpty(deckName))
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Set", DefaultText = deckName
                };

                var name = await this.ShowInputAsync("No name set", "Please set a name for the deck", settings);

                if (String.IsNullOrEmpty(name))
                {
                    return;
                }

                deckName             = name;
                TextBoxDeckName.Text = name;
            }

            while (DeckList.DecksList.Any(d => d.Name == deckName) && (!EditingDeck || !overwrite))
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Set", DefaultText = deckName
                };
                var name =
                    await
                    this.ShowInputAsync("Name already exists", "You already have a deck with that name, please select a different one.", settings);

                if (String.IsNullOrEmpty(name))
                {
                    return;
                }

                deckName             = name;
                TextBoxDeckName.Text = name;
            }

            if (_newDeck.Cards.Sum(c => c.Count) != 30)
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Yes", NegativeButtonText = "No"
                };

                var result =
                    await
                    this.ShowMessageAsync("Not 30 cards",
                                          string.Format("Deck contains {0} cards. Is this what you want to save anyway?",
                                                        _newDeck.Cards.Sum(c => c.Count)),
                                          MessageDialogStyle.AffirmativeAndNegative, settings);

                if (result != MessageDialogResult.Affirmative)
                {
                    return;
                }
            }

            if (EditingDeck && overwrite)
            {
                DeckList.DecksList.Remove(_newDeck);
                DeckPickerList.RemoveDeck(_newDeck);
            }

            var oldDeckName = _newDeck.Name;

            _newDeck.Name = deckName;
            _newDeck.Tags = TagControlEdit.GetTags();

            var newDeckClone = (Deck)_newDeck.Clone();

            DeckList.DecksList.Add(newDeckClone);

            newDeckClone.LastEdited = DateTime.Now;

            WriteDecks();
            Logger.WriteLine("Saved Decks");

            if (EditingDeck)
            {
                TagControlEdit.SetSelectedTags(new List <string>());
                if (deckName != oldDeckName)
                {
                    var statsEntry = DeckStatsList.Instance.DeckStats.FirstOrDefault(d => d.Name == oldDeckName);
                    if (statsEntry != null)
                    {
                        if (overwrite)
                        {
                            statsEntry.Name = deckName;
                            Logger.WriteLine("Deck has new name, updated deckstats");
                        }
                        else
                        {
                            var newStatsEntry = DeckStatsList.Instance.DeckStats.FirstOrDefault(d => d.Name == deckName);
                            if (newStatsEntry == null)
                            {
                                newStatsEntry = new DeckStats(deckName);
                                DeckStatsList.Instance.DeckStats.Add(newStatsEntry);
                            }
                            foreach (var game in statsEntry.Games)
                            {
                                newStatsEntry.AddGameResult(game.CloneWithNewId());
                            }
                            Logger.WriteLine("cloned gamestats for \"Set as new\"");
                        }
                        DeckStatsList.Save();
                    }
                }
            }

            //after cloning the stats, otherwise new stats will be generated
            DeckPickerList.AddAndSelectDeck(newDeckClone);

            EditingDeck = false;

            foreach (var tag in _newDeck.Tags)
            {
                SortFilterDecksFlyout.AddSelectedTag(tag);
            }

            DeckPickerList.UpdateList();
            DeckPickerList.SelectDeck(newDeckClone);

            CloseNewDeck();
            ClearNewDeckSection();
        }