internal void ShowDialogOptions() { try { var ratingSources = DraftRatings.Get().Keys.ToArray(); OptionsWindow optionsWindow = new OptionsWindow().Init(ConfigModel, ratingSources); optionsWindow.Owner = this; optionsWindow.ShowDialog(); // The code will continue here only when the options window gets closed var newConfig = JsonConvert.DeserializeObject <ConfigModel>(JsonConvert.SerializeObject(ConfigModel)); newConfig.LogFilePath = optionsWindow.LogFilePathTextBox.Text.Trim(); newConfig.GameFilePath = optionsWindow.GameFilePathTextBox.Text.Trim(); newConfig.RunOnStartup = optionsWindow.RunOnStartupCheckbox.IsChecked ?? false; newConfig.ShowOpponentCardsAuto = optionsWindow.ShowOpponentCardsCheckbox.IsChecked ?? false; newConfig.ShowOpponentCardsExternal = optionsWindow.ShowOpponentCardsExternalCheckBox.IsChecked ?? true; newConfig.MinimizeToSystemTray = optionsWindow.MinimizeToTrayCheckBox.IsChecked ?? false; newConfig.AutoShowHideForMatch = optionsWindow.AutoShowHideForMatchCheckBox.IsChecked ?? false; newConfig.ForceCardPopup = optionsWindow.ForceCardPopupCheckbox.IsChecked ?? false; newConfig.OrderLibraryCardsBy = optionsWindow.OrderLibraryComboBox.SelectedValue.ToString(); newConfig.ForceCardPopupSide = optionsWindow.ForceCardPopupSideComboBox?.SelectedValue?.ToString() ?? ConfigModel.ForceCardPopupSide; newConfig.ShowLimitedRatingsSource = optionsWindow.ShowLimitedRatingsSourceComboBox?.SelectedValue?.ToString() ?? ConfigModel.ShowLimitedRatingsSource; if (JsonConvert.SerializeObject(ConfigModel) != JsonConvert.SerializeObject(newConfig)) { bool oldLimitedRatings = ConfigModel.ShowLimitedRatings; string oldLimitedRatingsSource = ConfigModel.ShowLimitedRatingsSource; newConfig.Save(); Utilities.CopyProperties(newConfig, ConfigModel); if (MainWindowVM.MainWindowContext == WindowContext.Drafting && (ConfigModel.ShowLimitedRatings != oldLimitedRatings || ConfigModel.ShowLimitedRatingsSource != oldLimitedRatingsSource)) { if (ConfigModel.ShowLimitedRatingsSource != oldLimitedRatingsSource) { SetCardsDraft(MainWindowVM.DraftingVM.CurrentDraftPickProgress); } DraftingControl.SetPopupRatingsSource(ConfigModel.ShowLimitedRatings, ConfigModel.ShowLimitedRatingsSource); } ResourcesLocator.LocateLogFilePath(ConfigModel); ResourcesLocator.LocateGameClientFilePath(ConfigModel); FileMonitor.SetFilePath(newConfig.LogFilePath); //vm.ValidateUserId(newConfig.UserId); //ServerApiGetCollection(); StartupManager.ManageRunOnStartup(newConfig.RunOnStartup); ReadyControl.Init(ConfigModel.GameFilePath); MainWindowVM.OrderLibraryCardsBy = ConfigModel.OrderLibraryCardsBy == "Converted Mana Cost" ? CardsListOrder.ManaCost : CardsListOrder.DrawChance; } UpdateCardPopupPosition(); } catch (Exception ex) { Log.Write(LogEventLevel.Error, ex, "Unexpected error:"); } }
public MainWindow( ConfigModel configApp, ICollection <Card> allCards, MainWindowVM viewModel, ProcessMonitor processMonitor, LogFileZipper zipper, ServerApiCaller api, StartupShortcutManager startupManager, LogSplitter logSplitter, MtgaResourcesLocator resourcesLocator, FileMonitor fileMonitor, DraftCardsPicker draftHelper, ReaderMtgaOutputLog readerMtgaOutputLog, InGameTracker2 inMatchTracker, ExternalProviderTokenManager tokenManager, PasswordHasher passwordHasher, CacheSingleton <Dictionary <string, DraftRatings> > draftRatings, DraftHelperRunner draftHelperRunner) { // Set the config model reference ConfigModel = configApp; Reader = readerMtgaOutputLog; processMonitor.OnProcessMonitorStatusChanged += OnProcessMonitorStatusChanged; Zipper = zipper; Api = api; StartupManager = startupManager; LogSplitter = logSplitter; ResourcesLocator = resourcesLocator; FileMonitor = fileMonitor; fileMonitor.OnFileSizeChangedNewText += OnFileSizeChangedNewText; DraftHelper = draftHelper; //this.logProcessor = logProcessor; InGameTracker = inMatchTracker; TokenManager = tokenManager; PasswordHasher = passwordHasher; DraftRatings = draftRatings; DraftHelperRunner = draftHelperRunner; ResourcesLocator.LocateLogFilePath(ConfigModel); ResourcesLocator.LocateGameClientFilePath(ConfigModel); fileMonitor.SetFilePath(ConfigModel.LogFilePath); // Set the view model MainWindowVM = viewModel; // Set the data context to the view model DataContext = MainWindowVM; InitializeComponent(); WelcomeControl.Init(tokenManager); PlayingControl.Init(MainWindowVM); StatusBarTop.Init(this, MainWindowVM); ReadyControl.Init(ConfigModel.GameFilePath); DraftingControl.Init(allCards, MainWindowVM.DraftingVM); DraftingControl.SetPopupRatingsSource(ConfigModel.ShowLimitedRatings, ConfigModel.ShowLimitedRatingsSource); processMonitor.Start(new System.Threading.CancellationToken()); FileMonitor.Start(new System.Threading.CancellationToken()); var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(200) }; timer.Tick += (sender, e) => { MainWindowVM.SetCardsDraftFromBuffered(); MainWindowVM.SetCardsInMatchTrackingFromBuffered(); }; timer.Start(); var timerTokenRefresh = new DispatcherTimer { Interval = TimeSpan.FromMinutes(9) }; timerTokenRefresh.Tick += (sender, e) => { RefreshAccessToken(); }; timerTokenRefresh.Start(); }
public MainWindow( //OptionsWindow optionsWindow, IOptionsMonitor <ConfigModelApp> configApp, ICollection <Card> allCards, MainWindowVM viewModel, ProcessMonitor processMonitor, LogFileZipper zipper, ServerApiCaller api, StartupShortcutManager startupManager, LogSplitter logSplitter, MtgaResourcesLocator resourcesLocator, FileMonitor fileMonitor, DraftHelper draftHelper, //LogProcessor logProcessor, ReaderMtgaOutputLog readerMtgaOutputLog, //CacheSingleton<ICollection<Card>> allCards, InGameTracker inMatchTracker, ExternalProviderTokenManager tokenManager, PasswordHasher passwordHasher, NotifyIconManager notifyIconManager, CacheSingleton <Dictionary <string, DraftRatings> > draftRatings ) { this.configApp = configApp.CurrentValue; //optionsWindow.Init(this.configApp); //optionsWindow.Owner = Window.GetWindow(this); //this.optionsWindow = optionsWindow; this.reader = readerMtgaOutputLog; this.processMonitor = processMonitor; processMonitor.OnProcessMonitorStatusChanged += OnProcessMonitorStatusChanged; this.zipper = zipper; this.api = api; this.startupManager = startupManager; this.logSplitter = logSplitter; this.resourcesLocator = resourcesLocator; this.fileMonitor = fileMonitor; fileMonitor.OnFileSizeChangedNewText += OnFileSizeChangedNewText; this.draftHelper = draftHelper; //this.logProcessor = logProcessor; this.inGameTracker = inMatchTracker; this.tokenManager = tokenManager; this.passwordHasher = passwordHasher; this.notifyIconManager = notifyIconManager; this.draftRatings = draftRatings; this.resourcesLocator.LocateLogFilePath(this.configApp); this.resourcesLocator.LocateGameClientFilePath(this.configApp); fileMonitor.SetFilePath(this.configApp.LogFilePath); //viewModel.ValidateUserId(this.configApp.UserId); viewModel.Opacity = this.configApp.Opacity; vm = viewModel; DataContext = vm; InitializeComponent(); ucWelcome.Init(tokenManager); ucPlaying.Init(vm, this.configApp.WindowSettingsOpponentCards); //trayIcon = new System.Windows.Forms.NotifyIcon { Text = "MTGAHelper Tracker" }; //trayIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("pack://application:,,,/Assets/Images/wcC.ico")).Stream); //trayIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(TrayIcon_MouseClick); //trayIcon.ContextMenu = new System.Windows.Forms.ContextMenu(new System.Windows.Forms.MenuItem[] //{ // new System.Windows.Forms.MenuItem("Quit", new EventHandler(TrayIcon_Quit)) //}); statusBarTop.Init(this, vm /*, draftHelper, logProcessor, this.configApp.UserId,*/); ucReady.Init(this.configApp.GameFilePath); ucDraftHelper.Init(allCards, vm.DraftingVM); //ucPlaying.Init(vm); ucDraftHelper.SetPopupRatingsSource(this.configApp.ShowLimitedRatings, this.configApp.ShowLimitedRatingsSource); this.processMonitor.Start(new System.Threading.CancellationToken()); this.fileMonitor.Start(new System.Threading.CancellationToken()); var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(200) }; timer.Tick += (object sender, EventArgs e) => { vm.SetCardsDraftFromBuffered(); vm.SetCardsInMatchTrackingFromBuffered(); }; timer.Start(); var timerTokenRefresh = new DispatcherTimer { Interval = TimeSpan.FromMinutes(9) }; timerTokenRefresh.Tick += (object sender, EventArgs e) => { RefreshAccessToken(); }; timerTokenRefresh.Start(); }
internal void ShowDialogOptions() { try { var ratingSources = draftRatings.Get().Keys.ToArray(); var optionsWindow = new OptionsWindow().Init(this.configApp, ratingSources); optionsWindow.Owner = this; optionsWindow.ShowDialog(); // The code will continue here only when the options window gets closed var newConfig = JsonConvert.DeserializeObject <ConfigModelApp>(JsonConvert.SerializeObject(configApp)); newConfig.LogFilePath = optionsWindow.txtLogFilePath.Text.Trim(); newConfig.GameFilePath = optionsWindow.txtGameFilePath.Text.Trim(); newConfig.RunOnStartup = optionsWindow.chkRunOnStartup.IsChecked.Value; newConfig.ShowOpponentCards = optionsWindow.chkShowOpponentCards.IsChecked.Value; newConfig.MinimizeToSystemTray = optionsWindow.chkMinimizeToSystemTray.IsChecked.Value; newConfig.AutoShowHideForMatch = optionsWindow.chkAutoShowHideForMatch.IsChecked.Value; newConfig.ForceCardPopup = optionsWindow.chkForceCardPopup.IsChecked.Value; newConfig.OrderLibraryCardsBy = optionsWindow.lstOrderLibraryCardsBy.SelectedValue.ToString(); newConfig.ForceCardPopupSide = optionsWindow.lstForceCardPopupSide?.SelectedValue?.ToString() ?? configApp.ForceCardPopupSide; newConfig.ShowLimitedRatingsSource = optionsWindow.lstShowLimitedRatingsSource?.SelectedValue?.ToString() ?? configApp.ShowLimitedRatingsSource; ShowInTaskbar = !newConfig.MinimizeToSystemTray; if (configApp.MinimizeToSystemTray != newConfig.MinimizeToSystemTray) { if (newConfig.MinimizeToSystemTray) { notifyIconManager.AddNotifyIcon(this); } else { notifyIconManager.RemoveNotifyIcon(); } } if (JsonConvert.SerializeObject(configApp) != JsonConvert.SerializeObject(newConfig)) { var oldLimitedRatings = configApp.ShowLimitedRatings; var oldLimitedRatingsSource = configApp.ShowLimitedRatingsSource; newConfig.Save(); configApp = newConfig; if (vm.MainWindowContext == MainWindowContextEnum.Drafting && (configApp.ShowLimitedRatings != oldLimitedRatings || configApp.ShowLimitedRatingsSource != oldLimitedRatingsSource)) { if (configApp.ShowLimitedRatingsSource != oldLimitedRatingsSource) { SetCardsDraft(vm.DraftingVM.CurrentDraftPickProgress); } ucDraftHelper.SetPopupRatingsSource(configApp.ShowLimitedRatings, configApp.ShowLimitedRatingsSource); } resourcesLocator.LocateLogFilePath(configApp); resourcesLocator.LocateGameClientFilePath(configApp); fileMonitor.SetFilePath(newConfig.LogFilePath); //vm.ValidateUserId(newConfig.UserId); //ServerApiGetCollection(); startupManager.ManageRunOnStartup(newConfig.RunOnStartup); ucReady.Init(configApp.GameFilePath); vm.OrderLibraryCardsBy = configApp.OrderLibraryCardsBy == "Converted Mana Cost" ? CardsListOrder.Cmc : CardsListOrder.DrawChance; } UpdateCardPopupPosition(); } catch (Exception ex) { Log.Write(LogEventLevel.Error, ex, "Unexpected error:"); } }
/// <summary> /// Complete constructor /// </summary> public MainWindow(MainWindowVM viewModel) { mapper = viewModel.Mapper; ViewModel = viewModel; // Set the main window view model actions ViewModel.UploadLogAction = UploadLogAction; ViewModel.ShowOptionsAction = ShowOptionsWindow; ViewModel.LaunchArenaAction = LaunchArenaAction; ViewModel.ValidateUserAction = ValidateUserAction; // Set the resource locator ResourcesLocator = viewModel.ResourcesLocator; // Set the problem action ResourcesLocator.SetProblem = ViewModel.SetProblem; // Locate the log file ResourcesLocator.LocateLogFilePath(ViewModel.Config); // Locate the game path ResourcesLocator.LocateGameClientFilePath(ViewModel.Config); // Set the reference to the draft helper DraftHelperRunner = viewModel.DraftHelperRunner; Reader = viewModel.ReaderMtgaOutputLog; Api = viewModel.Api; StartupManager = viewModel.StartupManager; FileMonitor = viewModel.FileMonitor; FileMonitor.OnFileSizeChangedNewText += OnFileSizeChangedNewText; DraftHelper = viewModel.DraftHelper; //this.logProcessor = logProcessor; InGameTracker = viewModel.InMatchTracker; TokenManager = viewModel.TokenManager; PasswordHasher = viewModel.PasswordHasher; DraftRatings = viewModel.DraftRatings; FileMonitor.SetFilePath(ViewModel.Config.LogFilePath); // Set the data context to the view model DataContext = ViewModel; InitializeComponent(); PlayingControl.Init(ViewModel); DraftingControl.Init(ViewModel.DraftingVM, ViewModel.Config.LimitedRatingsSource); DraftingControl.SetPopupRatingsSource(ViewModel.Config.ShowLimitedRatings, ViewModel.Config.LimitedRatingsSource); // Set the process monitor status changed action viewModel.ProcessMonitor.OnProcessMonitorStatusChanged = OnProcessMonitorStatusChanged; // Start the process monitor without awaiting the task completion _ = viewModel.ProcessMonitor.Start(new System.Threading.CancellationToken()); // Start the file monitor without awaiting the task completion _ = FileMonitor.Start(new System.Threading.CancellationToken()); var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(200) }; timer.Tick += (sender, e) => { ViewModel.DraftingVM.SetCardsDraftFromBuffered(); ViewModel.InMatchState.SetInMatchStateFromBuffered(); }; timer.Start(); var timerTokenRefresh = new DispatcherTimer { Interval = TimeSpan.FromMinutes(9) }; timerTokenRefresh.Tick += (sender, e) => { RefreshAccessToken(); }; timerTokenRefresh.Start(); }
/// <summary> /// Show the options window as a dialog and handle changes /// </summary> private void ShowOptionsWindow() { try { // Create the window, set the owner and show as a dialog window (blocking execution until closed) var optionsVM = mapper.Map <OptionsWindowVM>(ViewModel.Config); optionsVM.Sets = ViewModel.Sets.ToDictionary(i => i.Code, i => i); optionsVM.DraftRatings = DraftRatings; optionsVM.LimitedRatingsSource = optionsVM.LimitedRatingsSourcesDict.First(i => i.Key == optionsVM.LimitedRatingsSource.Key); OptionsWindow win = new OptionsWindow().Init(ViewModel, optionsVM); win.Owner = this; win.ShowDialog(); // Get a reference to the view model OptionsWindowVM ovm = win.OptionsViewModel; // Serialize the original config string origConfig = JsonConvert.SerializeObject(ViewModel.Config); // Perform a deep copy on the existing config var configModel = JsonConvert.DeserializeObject <ConfigModel>(origConfig); // Store the setting from the options window configModel.LogFilePath = ovm.LogFilePath.Trim(); configModel.GameFilePath = ovm.GameFilePath.Trim(); configModel.RunOnStartup = ovm.RunOnStartup; configModel.ShowOpponentCardsAuto = ovm.ShowOpponentCardsAuto; configModel.ShowOpponentCardsExternal = ovm.ShowOpponentCardsExternal; configModel.Minimize = ovm.Minimize; configModel.AutoShowHideForMatch = ovm.AutoShowHideForMatch; configModel.OrderLibraryCardsBy = ovm.OrderLibraryCardsBy; configModel.ForceCardPopupSide = ovm.ForceCardPopupSide; configModel.ShowLimitedRatings = ovm.ShowLimitedRatings; configModel.LimitedRatingsSource = ovm.LimitedRatingsSource.Key; configModel.ForceGameResolution = ovm.ForceGameResolution; //configModel.GameResolutionBlackBorders = ovm.GameResolutionBlackBorders; configModel.GameResolution = ovm.GameResolution; // Serialize the new config string newConfig = JsonConvert.SerializeObject(configModel); // Check if the settings have changed if (origConfig != newConfig) { var showLimitedRatingsChanged = configModel.ShowLimitedRatings != ViewModel.Config.ShowLimitedRatings; var limitedRatingsSourceChanged = configModel.LimitedRatingsSource != ViewModel.Config.LimitedRatingsSource; // Copy the properties and save the config ViewModel.Config.CopyPropertiesFrom(configModel); ViewModel.Config.Save(); // Refresh custom ratings with latest from server if (ViewModel.Config.LimitedRatingsSource == Constants.LIMITEDRATINGS_SOURCE_CUSTOM) { RefreshCustomRatingsFromServer(); } // Handle changing the draft source during a draft if (ViewModel.Context == WindowContext.Drafting && (showLimitedRatingsChanged || limitedRatingsSourceChanged)) { ViewModel.DraftingVM.LimitedRatingsSource = ViewModel.Config.LimitedRatingsSource; if (limitedRatingsSourceChanged) { SetCardsDraft(ViewModel.DraftingVM.DraftInfoBuffered.DraftProgress); } DraftingControl.SetPopupRatingsSource(ViewModel.Config.ShowLimitedRatings, ViewModel.Config.LimitedRatingsSource); } // If the external option is disabled, hide the external window if (!ViewModel.Config.ShowOpponentCardsExternal) { ViewModel.OpponentWindowVM.ShowHideWindow(); } // If auto was just enabled, show the window (the playing state is checked internally) else if (ViewModel.Config.ShowOpponentCardsAuto) { ViewModel.OpponentWindowVM.ShowHideWindow(true); } // If the height is minimized and they changed the option, restore the window if (ViewModel.Config.Minimize != MinimizeOption.Height && ViewModel.IsHeightMinimized) { ViewModel.RestoreWindow(); } // Locate the resources from the new paths ResourcesLocator.LocateLogFilePath(ViewModel.Config); ResourcesLocator.LocateGameClientFilePath(ViewModel.Config); FileMonitor.SetFilePath(configModel.LogFilePath); // Set the start-up option StartupManager.ManageRunOnStartup(configModel.RunOnStartup); // Set the card order ViewModel.OrderLibraryCardsBy = ViewModel.Config.OrderLibraryCardsBy == "Converted Mana Cost" ? CardsListOrder.ManaCost : CardsListOrder.DrawChance; } UpdateCardPopupPosition(); } catch (Exception ex) { //if (ex is InvalidEmailException) // MessageBox.Show(ex.Message, "Not available", MessageBoxButton.OK, MessageBoxImage.Information); //else Log.Write(LogEventLevel.Error, ex, "Unexpected error:"); } }