private async void Window_Closing(object sender, CancelEventArgs e) { try { Log.Info("Shutting down..."); Influx.OnAppExit(Helper.GetCurrentVersion()); LiveDataManager.Stop(); Core.UpdateOverlay = false; Core.Update = false; //wait for update to finish, might otherwise crash when overlay gets disposed for (var i = 0; i < 100; i++) { if (Core.CanShutdown) { break; } await Task.Delay(50); } Config.Instance.SelectedTags = Config.Instance.SelectedTags.Distinct().ToList(); //Config.Instance.ShowAllDecks = DeckPickerList.ShowAll; Config.Instance.SelectedDeckPickerClasses = DeckPickerList.SelectedClasses.ToArray(); Config.Instance.WindowWidth = (int)Width; Config.Instance.WindowHeight = (int)(Height - _heightChangeDueToSearchBox); Config.Instance.TrackerWindowTop = (int)Top; Config.Instance.TrackerWindowLeft = (int)Left; //position of add. windows is NaN if they were never opened. if (!double.IsNaN(Core.Windows.PlayerWindow.Left)) { Config.Instance.PlayerWindowLeft = (int)Core.Windows.PlayerWindow.Left; } if (!double.IsNaN(Core.Windows.PlayerWindow.Top)) { Config.Instance.PlayerWindowTop = (int)Core.Windows.PlayerWindow.Top; } Config.Instance.PlayerWindowHeight = (int)Core.Windows.PlayerWindow.Height; if (!double.IsNaN(Core.Windows.OpponentWindow.Left)) { Config.Instance.OpponentWindowLeft = (int)Core.Windows.OpponentWindow.Left; } if (!double.IsNaN(Core.Windows.OpponentWindow.Top)) { Config.Instance.OpponentWindowTop = (int)Core.Windows.OpponentWindow.Top; } Config.Instance.OpponentWindowHeight = (int)Core.Windows.OpponentWindow.Height; if (!double.IsNaN(Core.Windows.TimerWindow.Left)) { Config.Instance.TimerWindowLeft = (int)Core.Windows.TimerWindow.Left; } if (!double.IsNaN(Core.Windows.TimerWindow.Top)) { Config.Instance.TimerWindowTop = (int)Core.Windows.TimerWindow.Top; } Config.Instance.TimerWindowHeight = (int)Core.Windows.TimerWindow.Height; Config.Instance.TimerWindowWidth = (int)Core.Windows.TimerWindow.Width; Core.TrayIcon.NotifyIcon.Visible = false; Core.Overlay.Close(); await Core.StopLogWacher(); Core.Windows.TimerWindow.Shutdown(); Core.Windows.PlayerWindow.Shutdown(); Core.Windows.OpponentWindow.Shutdown(); Config.Save(); DeckList.Save(); DeckStatsList.Save(); PluginManager.SavePluginsSettings(); PluginManager.Instance.UnloadPlugins(); } catch (Exception) { //doesnt matter } finally { Application.Current.Shutdown(); } }
#pragma warning disable 4014 public async void HandleGameEnd() { try { if (_game.CurrentGameStats == null || _handledGameEnd) { Log.Warn("HandleGameEnd was already called."); return; } _handledGameEnd = true; TurnTimer.Instance.Stop(); Core.Overlay.HideTimers(); DeckManager.ResetAutoSelectCount(); LiveDataManager.Stop(); Log.Info("Game ended..."); _game.InvalidateMatchInfoCache(); if (_game.CurrentGameMode == Spectator && _game.CurrentGameStats.Result == GameResult.None) { Log.Info("Game was spectator mode without a game result. Probably exited spectator mode early."); return; } var player = _game.Entities.FirstOrDefault(e => e.Value?.IsPlayer ?? false).Value; var opponent = _game.Entities.FirstOrDefault(e => e.Value != null && e.Value.HasTag(PLAYER_ID) && !e.Value.IsPlayer).Value; if (player != null) { _game.CurrentGameStats.PlayerName = player.Name; _game.CurrentGameStats.Coin = !player.HasTag(FIRST_PLAYER); } if (opponent != null && CardIds.HeroIdDict.ContainsValue(_game.CurrentGameStats.OpponentHero)) { _game.CurrentGameStats.OpponentName = opponent.Name; } else { _game.CurrentGameStats.OpponentName = _game.CurrentGameStats.OpponentHero; } _game.CurrentGameStats.Turns = _game.GetTurnNumber(); if (Config.Instance.DiscardZeroTurnGame && _game.CurrentGameStats.Turns < 1) { Log.Info("Game has 0 turns, discarded. (DiscardZeroTurnGame)"); _assignedDeck = null; GameEvents.OnGameEnd.Execute(); return; } _game.CurrentGameStats.GameMode = _game.CurrentGameMode; _game.CurrentGameStats.Format = _game.CurrentFormat; Log.Info("Format: " + _game.CurrentGameStats.Format); if (_game.CurrentGameMode == Ranked && _game.MatchInfo != null) { var wild = _game.CurrentFormat == Format.Wild; _game.CurrentGameStats.Rank = wild ? _game.MatchInfo.LocalPlayer.WildRank : _game.MatchInfo.LocalPlayer.StandardRank; _game.CurrentGameStats.OpponentRank = wild ? _game.MatchInfo.OpposingPlayer.WildRank : _game.MatchInfo.OpposingPlayer.StandardRank; _game.CurrentGameStats.LegendRank = wild ? _game.MatchInfo.LocalPlayer.WildLegendRank : _game.MatchInfo.LocalPlayer.StandardLegendRank; _game.CurrentGameStats.OpponentLegendRank = wild ? _game.MatchInfo.OpposingPlayer.WildLegendRank : _game.MatchInfo.OpposingPlayer.StandardLegendRank; _game.CurrentGameStats.Stars = wild ? _game.MatchInfo.LocalPlayer.WildStars : _game.MatchInfo.LocalPlayer.StandardStars; } else if (_game.CurrentGameMode == Arena) { _game.CurrentGameStats.ArenaWins = DeckImporter.ArenaInfoCache?.Wins ?? 0; _game.CurrentGameStats.ArenaLosses = DeckImporter.ArenaInfoCache?.Losses ?? 0; } else if (_game.CurrentGameMode == Brawl && _game.BrawlInfo != null) { _game.CurrentGameStats.BrawlWins = _game.BrawlInfo.Wins; _game.CurrentGameStats.BrawlLosses = _game.BrawlInfo.Losses; } _game.CurrentGameStats.GameType = _game.CurrentGameType; _game.CurrentGameStats.ServerInfo = _game.MetaData.ServerInfo; _game.CurrentGameStats.PlayerCardbackId = _game.MatchInfo?.LocalPlayer.CardBackId ?? 0; _game.CurrentGameStats.OpponentCardbackId = _game.MatchInfo?.OpposingPlayer.CardBackId ?? 0; _game.CurrentGameStats.FriendlyPlayerId = _game.MatchInfo?.LocalPlayer.Id ?? 0; _game.CurrentGameStats.ScenarioId = _game.MatchInfo?.MissionId ?? 0; _game.CurrentGameStats.BrawlSeasonId = _game.MatchInfo?.BrawlSeasonId ?? 0; _game.CurrentGameStats.RankedSeasonId = _game.MatchInfo?.RankedSeasonId ?? 0; var confirmedCards = _game.Player.RevealedCards.Where(x => x.Collectible) .Concat(_game.Player.KnownCardsInDeck.Where(x => x.Collectible && !x.IsCreated)) .ToList(); if (_game.CurrentSelectedDeck != null && _game.CurrentSelectedDeck.Id > 0) { _game.CurrentGameStats.HsDeckId = _game.CurrentSelectedDeck.Id; _game.CurrentGameStats.SetPlayerCards(_game.CurrentSelectedDeck, confirmedCards); } else { _game.CurrentGameStats.HsDeckId = DeckList.Instance.ActiveDeckVersion?.HsId ?? 0; _game.CurrentGameStats.SetPlayerCards(DeckList.Instance.ActiveDeckVersion, confirmedCards); } _game.CurrentGameStats.SetOpponentCards(_game.Opponent.OpponentCardList.Where(x => !x.IsCreated).ToList()); _game.CurrentGameStats.GameEnd(); GameEvents.OnGameEnd.Execute(); _game.CurrentSelectedDeck = null; var selectedDeck = DeckList.Instance.ActiveDeck; if (selectedDeck != null) { var revealed = _game.Player.RevealedEntities.Where(x => x != null).ToList(); if (Config.Instance.DiscardGameIfIncorrectDeck && !revealed.Where(x => x.IsPlayableCard && !x.Info.Created && !x.Info.Stolen && x.Card.Collectible) .GroupBy(x => x.CardId).All(x => selectedDeck.GetSelectedDeckVersion().Cards.Any(c2 => x.Key == c2.Id && x.Count() <= c2.Count))) { if (Config.Instance.AskBeforeDiscardingGame) { var discardDialog = new DiscardGameDialog(_game.CurrentGameStats) { Topmost = true }; discardDialog.ShowDialog(); if (discardDialog.Result == DiscardGameDialogResult.Discard) { Log.Info("Assigned current game to NO deck - selected deck does not match cards played (dialogresult: discard)"); _assignedDeck = null; return; } if (discardDialog.Result == DiscardGameDialogResult.MoveToOther) { var moveDialog = new MoveGameDialog(DeckList.Instance.Decks.Where(d => d.Class == _game.CurrentGameStats.PlayerHero)) { Topmost = true }; moveDialog.ShowDialog(); var targetDeck = moveDialog.SelectedDeck; if (targetDeck != null) { selectedDeck = targetDeck; _game.CurrentGameStats.PlayerDeckVersion = moveDialog.SelectedVersion; //...continue as normal } else { Log.Info("No deck selected in move game dialog after discard dialog, discarding game"); _assignedDeck = null; return; } } } else { Log.Info("Assigned current game to NO deck - selected deck does not match cards played (no dialog)"); _assignedDeck = null; return; } } else { _game.CurrentGameStats.PlayerDeckVersion = selectedDeck.GetSelectedDeckVersion().Version; } _lastGame = _game.CurrentGameStats; selectedDeck.DeckStats.AddGameResult(_lastGame); if (Config.Instance.ArenaRewardDialog && (selectedDeck.IsArenaRunCompleted ?? false)) { if (selectedDeck.ArenaReward.Packs[0] == ArenaRewardPacks.None) { selectedDeck.ArenaReward.Packs[0] = Enum.GetValues(typeof(ArenaRewardPacks)).OfType <ArenaRewardPacks>().Max(); } _arenaRewardDialog = new ArenaRewardDialog(selectedDeck); } if (Config.Instance.ShowNoteDialogAfterGame && !Config.Instance.NoteDialogDelayed && !_showedNoteDialog) { _showedNoteDialog = true; new NoteDialog(_game.CurrentGameStats); } Log.Info("Assigned current game to deck: " + selectedDeck.Name); _assignedDeck = selectedDeck; // Unarchive the active deck after we have played a game with it if (_assignedDeck.Archived) { Log.Info("Automatically unarchiving deck " + selectedDeck.Name + " after assigning current game"); Core.MainWindow.ArchiveDeck(_assignedDeck, false); } _lastGame = null; } else { try { DefaultDeckStats.Instance.GetDeckStats(_game.Player.Class).AddGameResult(_game.CurrentGameStats); Log.Info($"Assigned current deck to default {_game.Player.Class} deck."); } catch (Exception ex) { Log.Error("Error saving to DefaultDeckStats: " + ex); } _assignedDeck = null; } if (_game.StoredGameStats != null) { _game.CurrentGameStats.StartTime = _game.StoredGameStats.StartTime; } await SaveReplays(); if (Config.Instance.ShowGameResultNotifications && RecordCurrentGameMode) { var deckName = _assignedDeck == null ? "No deck - " + _game.CurrentGameStats.PlayerHero : _assignedDeck.NameAndVersion; ToastManager.ShowGameResultToast(deckName, _game.CurrentGameStats); } } catch (Exception ex) { Log.Error(ex); } }