Пример #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();
        }
        public MainWindow()
        {
            // Set working directory to path of executable
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

            InitializeComponent();

            Trace.Listeners.Add(new TextBoxTraceListener(Options.TextBoxLog));

            EnableMenuItems(false);

            try
            {
                if (File.Exists("Updater_new.exe"))
                {
                    if (File.Exists("Updater.exe"))
                    {
                        File.Delete("Updater.exe");
                    }
                    File.Move("Updater_new.exe", "Updater.exe");
                }
            }
            catch
            {
                Logger.WriteLine("Error updating updater");
            }

            Helper.MainWindow = this;
            /*_configPath =*/
            Config.Load();
            HsLogReader.Create();

            var configVersion = string.IsNullOrEmpty(Config.Instance.CreatedByVersion)
                                    ? null
                                    : new Version(Config.Instance.CreatedByVersion);

            Version currentVersion;

            if (Config.Instance.CheckForUpdates)
            {
                currentVersion   = Helper.CheckForUpdates(out NewVersion);
                _lastUpdateCheck = DateTime.Now;
            }
            else
            {
                currentVersion = Helper.GetCurrentVersion();
            }

            var versionString = string.Empty;

            if (currentVersion != null)
            {
                versionString             = string.Format("{0}.{1}.{2}", currentVersion.Major, currentVersion.Minor, currentVersion.Build);
                Help.TxtblockVersion.Text = "Version: " + versionString;

                // Assign current version to the config instance so that it will be saved when the config
                // is rewritten to disk, thereby telling us what version of the application created it
                Config.Instance.CreatedByVersion = currentVersion.ToString();
            }

            ConvertLegacyConfig(currentVersion, configVersion);

            if (Config.Instance.SelectedTags.Count == 0)
            {
                Config.Instance.SelectedTags.Add("All");
            }

            _foundHsDirectory = FindHearthstoneDir();

            if (_foundHsDirectory)
            {
                _updatedLogConfig = UpdateLogConfigFile();
            }

            //hearthstone, loads db etc - needs to be loaded before playerdecks, since cards are only saved as ids now
            Game.Reset();

            _decksPath = Config.Instance.DataDir + "PlayerDecks.xml";
            SetupDeckListFile();
            try
            {
                DeckList = XmlManager <Decks> .Load(_decksPath);
            }
            catch (Exception e)
            {
                MessageBox.Show(
                    e.Message + "\n\n" + e.InnerException +
                    "\n\n If you don't know how to fix this, please delete " + _decksPath +
                    " (this will cause you to lose your decks).",
                    "Error loading PlayerDecks.xml");
                Application.Current.Shutdown();
            }

            foreach (var deck in DeckList.DecksList)
            {
                DeckPickerList.AddDeck(deck);
            }

            SetupDefaultDeckStatsFile();
            DefaultDeckStats.Load();


            SetupDeckStatsFile();
            DeckStatsList.Load();

            _notifyIcon = new NotifyIcon {
                Icon = new Icon(@"Images/HearthstoneDeckTracker16.ico"), Visible = true, ContextMenu = new ContextMenu(), Text = "Hearthstone Deck Tracker v" + versionString
            };
            _notifyIcon.ContextMenu.MenuItems.Add("Use no deck", (sender, args) => DeselectDeck());
            _notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Autoselect deck")
            {
                MenuItems =
                {
                    new MenuItem("On",
                                 (sender, args) =>
                                 AutoDeckDetection(true)),
                    new MenuItem("Off",
                                 (sender, args) =>
                                 AutoDeckDetection(false))
                }
            }); _notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Class cards first")
            {
                MenuItems =
                {
                    new MenuItem("Yes",
                                 (sender, args) =>
                                 SortClassCardsFirst(true)),
                    new MenuItem("No",
                                 (sender, args) =>
                                 SortClassCardsFirst(false))
                }
            });
            _notifyIcon.ContextMenu.MenuItems.Add("Show", (sender, args) => ActivateWindow());
            _notifyIcon.ContextMenu.MenuItems.Add("Exit", (sender, args) => Close());
            _notifyIcon.MouseClick += (sender, args) => { if (args.Button == MouseButtons.Left)
                                                          {
                                                              ActivateWindow();
                                                          }
            };

            //create overlay
            Overlay = new OverlayWindow {
                Topmost = true
            };

            PlayerWindow   = new PlayerWindow(Config.Instance, Game.IsUsingPremade ? Game.PlayerDeck : Game.PlayerDrawn);
            OpponentWindow = new OpponentWindow(Config.Instance, Game.OpponentCards);
            TimerWindow    = new TimerWindow(Config.Instance);
            StatsWindow    = new StatsWindow();

            if (Config.Instance.PlayerWindowOnStart)
            {
                PlayerWindow.Show();
            }
            if (Config.Instance.OpponentWindowOnStart)
            {
                OpponentWindow.Show();
            }
            if (Config.Instance.TimerWindowOnStartup)
            {
                TimerWindow.Show();
            }
            if (!DeckList.AllTags.Contains("All"))
            {
                DeckList.AllTags.Add("All");
                WriteDecks();
            }
            if (!DeckList.AllTags.Contains("Favorite"))
            {
                if (DeckList.AllTags.Count > 1)
                {
                    DeckList.AllTags.Insert(1, "Favorite");
                }
                else
                {
                    DeckList.AllTags.Add("Favorite");
                }
                WriteDecks();
            }
            if (!DeckList.AllTags.Contains("Arena"))
            {
                DeckList.AllTags.Add("Arena");
                WriteDecks();
            }
            if (!DeckList.AllTags.Contains("Constructed"))
            {
                DeckList.AllTags.Add("Constructed");
                WriteDecks();
            }
            if (!DeckList.AllTags.Contains("None"))
            {
                DeckList.AllTags.Add("None");
                WriteDecks();
            }

            Options.ComboboxAccent.ItemsSource    = ThemeManager.Accents;
            Options.ComboboxTheme.ItemsSource     = ThemeManager.AppThemes;
            Options.ComboboxLanguages.ItemsSource = Helper.LanguageDict.Keys;

            Options.ComboboxKeyPressGameStart.ItemsSource = EventKeys;
            Options.ComboboxKeyPressGameEnd.ItemsSource   = EventKeys;

            LoadConfig();

            FillElementSorters();

            //this has to happen before reader starts
            var lastDeck = DeckList.DecksList.FirstOrDefault(d => d.Name == Config.Instance.LastDeck);

            DeckPickerList.SelectDeck(lastDeck);

            TurnTimer.Create(90);

            SortFilterDecksFlyout.HideStuffToCreateNewTag();
            TagControlEdit.OperationSwitch.Visibility = Visibility.Collapsed;
            TagControlEdit.PnlSortDecks.Visibility    = Visibility.Collapsed;


            UpdateDbListView();

            _doUpdate = _foundHsDirectory;
            UpdateOverlayAsync();

            _initialized = true;
            Options.MainWindowInitialized();

            DeckPickerList.UpdateList();
            if (lastDeck != null)
            {
                DeckPickerList.SelectDeck(lastDeck);
                UpdateDeckList(lastDeck);
                UseDeck(lastDeck);
            }

            if (_foundHsDirectory)
            {
                HsLogReader.Instance.Start();
            }

            Helper.SortCardCollection(ListViewDeck.Items, Config.Instance.CardSortingClassFirst);
            DeckPickerList.SortDecks();

            // Set Language
            var culture = Config.Instance.SelectedLanguage.Insert(2, "-");

            WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = System.Globalization.CultureInfo.GetCultureInfo(culture);
            if (!Directory.Exists(culture))
            {
                var langPath = Path.Combine("Lang", culture);
                if (Directory.Exists(langPath))
                {
                    Helper.CopyFolder(langPath, culture);
                    _showRestartMessage = true;
                }
            }
        }