private void MoveGameToOtherDeck(GameStats selectedGame) { if (selectedGame == null) { return; } var heroes = new Dictionary <string, int>(); foreach (var turn in selectedGame.TurnStats) { foreach (var play in turn.Plays) { if (!play.Type.ToString().Contains("Player")) { continue; } var hero = Game.GetCardFromId(play.CardId).PlayerClass; if (hero == null) { continue; } if (!heroes.ContainsKey(hero)) { heroes.Add(hero, 0); } heroes[hero]++; } } var heroPlayed = heroes.Any() ? heroes.OrderByDescending(x => x.Value).First().Key : "Any"; var possibleTargets = Helper.MainWindow.DeckList.DecksList.Where(d => d.Class == heroPlayed || heroPlayed == "Any"); var dialog = new MoveGameDialog(possibleTargets); if (Config.Instance.StatsInWindow) { dialog.Owner = Helper.MainWindow.StatsWindow; } else { dialog.Owner = Helper.MainWindow; } dialog.ShowDialog(); var selectedDeck = dialog.SelectedDeck; if (selectedDeck == null) { return; } var defaultDeck = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (defaultDeck != null) { defaultDeck.Games.Remove(selectedGame); DefaultDeckStats.Save(); } else { _deck.DeckStats.Games.Remove(selectedGame); } selectedGame.PlayerDeckVersion = selectedDeck.Version; //move to latest version selectedDeck.DeckStats.Games.Add(selectedGame); DeckStatsList.Save(); Helper.MainWindow.WriteDecks(); Refresh(); Helper.MainWindow.DeckPickerList.UpdateList(); }
private async void DeleteGames(DataGrid dataGrid, bool overall) { MetroWindow window; if (Config.Instance.StatsInWindow) { window = Helper.MainWindow.StatsWindow; } else { window = Helper.MainWindow; } var count = dataGrid.SelectedItems.Count; if (count == 1) { var selectedGame = dataGrid.SelectedItem as GameStats; if (selectedGame == null) { return; } if (await window.ShowDeleteGameStatsMessage(selectedGame) != MessageDialogResult.Affirmative) { return; } if (!overall) { if (_deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); _deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game: " + selectedGame); DeckStatsList.Save(); } } else { var deck = Helper.MainWindow.DeckList.DecksList.FirstOrDefault(d => d.DeckStats.Games.Contains(selectedGame)); if (deck != null) { if (deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game: " + selectedGame); DefaultDeckStats.Save(); } } else { var deckstats = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (deckstats != null) { selectedGame.DeleteGameFile(); deckstats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game: " + selectedGame); DefaultDeckStats.Save(); } } } Helper.MainWindow.DeckPickerList.Items.Refresh(); Refresh(); } else if (count > 1) { if (await window.ShowDeleteMultipleGameStatsMessage(count) != MessageDialogResult.Affirmative) { return; } foreach (var selectedItem in dataGrid.SelectedItems) { var selectedGame = selectedItem as GameStats; if (selectedGame == null) { continue; } if (!overall) { if (_deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); _deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game: " + selectedGame); } } else { var deck = Helper.MainWindow.DeckList.DecksList.FirstOrDefault(d => d.DeckStats.Games.Contains(selectedGame)); if (deck != null) { if (deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game: " + selectedGame); } } else { var deckstats = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (deckstats != null) { selectedGame.DeleteGameFile(); deckstats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game: " + selectedGame); } } } } DeckStatsList.Save(); DefaultDeckStats.Save(); Logger.WriteLine("Deleted " + count + " games"); Helper.MainWindow.DeckPickerList.Items.Refresh(); Refresh(); } }
private void MoveGameToOtherDeck(List <GameStats> selectedGames) { if (selectedGames == null) { return; } var heroes = new Dictionary <string, int>(); foreach (var game in selectedGames) { if (!heroes.ContainsKey(game.PlayerHero)) { heroes.Add(game.PlayerHero, 0); } heroes[game.PlayerHero]++; } var heroPlayed = heroes.Any() ? heroes.OrderByDescending(x => x.Value).First().Key : "Any"; var possibleTargets = DeckList.Instance.Decks.Where(d => d.Class == heroPlayed || heroPlayed == "Any"); var dialog = new MoveGameDialog(possibleTargets); if (Config.Instance.StatsInWindow) { dialog.Owner = Helper.MainWindow.StatsWindow; } else { dialog.Owner = Helper.MainWindow; } dialog.ShowDialog(); var selectedDeck = dialog.SelectedDeck; if (selectedDeck == null) { return; } foreach (var game in selectedGames) { var defaultDeck = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(game)); if (defaultDeck != null) { defaultDeck.Games.Remove(game); DefaultDeckStats.Save(); } else { var deck = DeckList.Instance.Decks.FirstOrDefault(d => game.DeckId == d.DeckId); if (deck != null) { deck.DeckStats.Games.Remove(game); } } game.PlayerDeckVersion = dialog.SelectedVersion; game.HearthStatsDeckVersionId = selectedDeck.GetVersion(dialog.SelectedVersion).HearthStatsDeckVersionId; game.DeckId = selectedDeck.DeckId; game.DeckName = selectedDeck.Name; selectedDeck.DeckStats.Games.Add(game); if (HearthStatsAPI.IsLoggedIn && Config.Instance.HearthStatsAutoUploadNewGames) { HearthStatsManager.MoveMatchAsync(game, selectedDeck, background: true); } } DeckStatsList.Save(); DeckList.Save(); Refresh(); Helper.MainWindow.DeckPickerList.UpdateDecks(); }
private async void DeleteGames(DataGrid dataGrid, bool overall) { MetroWindow window; if (Config.Instance.StatsInWindow) { window = Helper.MainWindow.StatsWindow; } else { window = Helper.MainWindow; } var count = dataGrid.SelectedItems.Count; if (count == 1) { var selectedGame = dataGrid.SelectedItem as GameStats; if (selectedGame == null) { return; } if (await window.ShowDeleteGameStatsMessage(selectedGame) != MessageDialogResult.Affirmative) { return; } if (!overall) { if (_deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); _deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game " + selectedGame + "(overall=" + overall + ")", "DeckStatsControl"); DeckStatsList.Save(); } } else { var deck = DeckList.Instance.Decks.FirstOrDefault(d => d.DeckStats.Games.Contains(selectedGame)); if (deck != null) { if (deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game " + selectedGame + "(overall=" + overall + ")", "DeckStatsControl"); DefaultDeckStats.Save(); } } else { var deckstats = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (deckstats != null) { selectedGame.DeleteGameFile(); deckstats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game " + selectedGame + "(overall=" + overall + ")", "DeckStatsControl"); DefaultDeckStats.Save(); } } } if (HearthStatsAPI.IsLoggedIn && selectedGame.HasHearthStatsId && await Helper.MainWindow.CheckHearthStatsMatchDeletion()) { HearthStatsManager.DeleteMatchesAsync(new List <GameStats> { selectedGame }); } //Helper.MainWindow.DeckPickerList.Items.Refresh(); Helper.MainWindow.DeckPickerList.UpdateDecks(); Refresh(); } else if (count > 1) { if (await window.ShowDeleteMultipleGameStatsMessage(count) != MessageDialogResult.Affirmative) { return; } var selectedGames = dataGrid.SelectedItems.Cast <GameStats>().Where(g => g != null).ToList(); foreach (var selectedGame in selectedGames) { if (!overall) { if (_deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); _deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game " + selectedGame + "(overall=" + overall + ")", "DeckStatsControl"); } } else { var deck = DeckList.Instance.Decks.FirstOrDefault(d => d.DeckStats.Games.Contains(selectedGame)); if (deck != null) { if (deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); deck.DeckStats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game " + selectedGame + "(overall=" + overall + ")", "DeckStatsControl"); } } else { var deckstats = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (deckstats != null) { selectedGame.DeleteGameFile(); deckstats.Games.Remove(selectedGame); Logger.WriteLine("Deleted game " + selectedGame + "(overall=" + overall + ")", "DeckStatsControl"); } } } } if (HearthStatsAPI.IsLoggedIn && selectedGames.Any(g => g.HasHearthStatsId) && await Helper.MainWindow.CheckHearthStatsMatchDeletion()) { HearthStatsManager.DeleteMatchesAsync(selectedGames); } DeckStatsList.Save(); DefaultDeckStats.Save(); Logger.WriteLine("Deleted " + count + " games", "DeckStatsControl"); Helper.MainWindow.DeckPickerList.UpdateDecks(); Refresh(); } }
private void SaveAndUpdateStats() { if (RecordCurrentGameMode) { if (Config.Instance.ShowNoteDialogAfterGame && Config.Instance.NoteDialogDelayed && !_showedNoteDialog) { _showedNoteDialog = true; new NoteDialog(_game.CurrentGameStats); } if (_game.CurrentGameStats != null) { _game.CurrentGameStats.Turns = _game.GetTurnNumber(); if (Config.Instance.DiscardZeroTurnGame && _game.CurrentGameStats.Turns < 1) { Log.Info("Game has 0 turns, discarded. (DiscardZeroTurnGame)"); return; } if (_game.CurrentGameStats.GameMode != _game.CurrentGameMode) { _game.CurrentGameStats.GameMode = _game.CurrentGameMode; Log.Info("Set CurrentGameStats.GameMode to " + _game.CurrentGameMode); } if (_game.CurrentGameStats.GameMode == Arena) { ArenaStats.Instance.UpdateArenaStats(); ArenaStats.Instance.UpdateArenaRuns(); ArenaStats.Instance.UpdateArenaStatsHighlights(); } else { ConstructedStats.Instance.UpdateConstructedStats(); } } if (_assignedDeck == null) { Log.Info("Saving DefaultDeckStats"); DefaultDeckStats.Save(); } else { _assignedDeck.StatsUpdated(); Log.Info("Saving DeckStats"); DeckStatsList.Save(); } LastGames.Instance.Add(_game.CurrentGameStats); LastGames.Save(); } else if (_assignedDeck != null && _assignedDeck.DeckStats.Games.Contains(_game.CurrentGameStats)) { //game was not supposed to be recorded, remove from deck again. _assignedDeck.DeckStats.Games.Remove(_game.CurrentGameStats); Log.Info($"Gamemode {_game.CurrentGameMode} is not supposed to be saved. Removed game from {_assignedDeck}."); } else if (_assignedDeck == null) { var defaultDeck = DefaultDeckStats.Instance.GetDeckStats(_game.Player.Class); if (defaultDeck != null) { defaultDeck.Games.Remove(_game.CurrentGameStats); Log.Info($"Gamemode {_game.CurrentGameMode} is not supposed to be saved. Removed game from default {_game.Player.Class}."); } } }
public int AddGames(List <Game> games) { Common.Log.Debug($"Tracker: Adding {games.Count} games"); var count = 0; var newGames = new List <Game>(); Common.Log.Debug("Tracker: Reloading deck stats"); Reload <DeckStatsList>(); Reload <DefaultDeckStats>(); var gameStats = new List <GameStats>(DeckStatsList.Instance.DeckStats.Values .SelectMany(x => x.Games)); gameStats.AddRange(DefaultDeckStats.Instance.DeckStats .SelectMany(x => x.Games)); // create index maps into the list on id and GameIndex var indexById = gameStats.ToDictionary(x => x.GameId, x => gameStats.IndexOf(x)); // create a fuzzy index based on start time and opponent var fuzzyIndex = gameStats.ToDictionary(x => new GameIndex(x), x => indexById[x.GameId]); foreach (var game in games) { // set default indices var index = -1; var gameIndex = new GameIndex(game); if (game.Id != null && indexById.ContainsKey(game.Id)) { // look for id match first index = indexById[game.Id]; Common.Log.Debug($"Tracker: Existing game found with id {game.Id}"); } else if (fuzzyIndex.ContainsKey(gameIndex)) { // try a fuzzy match index = fuzzyIndex[gameIndex]; Common.Log.Debug($"Tracker: Game fuzzy matched ({gameIndex.StartTime})"); } // if the game was matched then edit it, otherwise add new game if (index >= 0 && index < gameStats.Count) { var stats = gameStats[index]; // set the deck first, so it isn't overwritten game.Deck = new Deck() { Name = stats.DeckName, Id = stats.DeckId }; game.CopyTo(stats); count++; Common.Log.Debug($"Tracker: Game updated ({game.Id})"); } else { newGames.Add(game); } } DeckStatsList.Save(); DefaultDeckStats.Save(); var newGamesAdded = 0; if (newGames.Count > 0) { newGamesAdded = AddNewGames(newGames); } return(count + newGamesAdded); }
private int AddNewGames(List <Game> games) { Common.Log.Debug($"Tracker: Adding {games.Count} new games"); Common.Log.Debug("Tracker: Refreshing deck status"); Reload <DeckStatsList>(); Reload <DefaultDeckStats>(); Reload <DeckList>(); var count = 0; foreach (var g in games) { var success = false; var name = g.Deck.Name.ToLowerInvariant(); var klass = GetClass(g); if (!string.IsNullOrWhiteSpace(name)) { // find a deck by name // add it if it exists (latest version) // if not create empty deck, and add to that var decks = DeckList.Instance.Decks; var deckMatches = decks.Where(d => d.Name.ToLowerInvariant() == name && d.Class.ToLower() == klass && !d.Archived); // if only a single match use that deck if (deckMatches.Count() == 1) { var match = deckMatches.First(); Common.Log.Debug($"Tracker: Matching deck found '{match.Name}' {match.DeckId}"); var gameStats = new GameStats(); // copy the game info g.CopyTo(gameStats); // add the matched deck to GameStats object gameStats.DeckId = match.DeckId; gameStats.DeckName = match.Name; // if no deck version matches use latest if (gameStats.PlayerDeckVersion == null || !match.VersionsIncludingSelf.Contains(gameStats.PlayerDeckVersion)) { gameStats.PlayerDeckVersion = match.GetMaxVerion(); } // if game id is empty create new one if (gameStats.GameId == Guid.Empty) { gameStats.GameId = Guid.NewGuid(); } // add game to hdt stats DeckStatsList.Instance.DeckStats[match.DeckId].AddGameResult(gameStats); success = true; Common.Log.Debug($"Tracker: Game added to {match.Name} stats"); } else { Common.Log.Debug($"Single deck matching '{name}' not found, using default stats"); } } if (!success) { // add to default class deck stats if (!string.IsNullOrEmpty(klass)) { var stats = DefaultDeckStats.Instance.GetDeckStats(klass); var gameStats = new GameStats(); g.CopyTo(gameStats); // if game id is empty create new one if (gameStats.GameId == Guid.Empty) { gameStats.GameId = Guid.NewGuid(); } stats.AddGameResult(gameStats); success = true; Common.Log.Debug($"Tracker: Game added to default {klass} stats"); } else { Common.Log.Debug($"Tracker: Failed to add game {g.Id}"); } } count += success ? 1 : 0; } Common.Log.Debug($"Tracker: Saving deck stats"); DeckList.Save(); DeckStatsList.Save(); DefaultDeckStats.Save(); return(count); }
private async void DeleteDeck(Deck deck, bool saveAndUpdate = true) { if (deck == null) { return; } var deckStats = DeckStatsList.Instance.DeckStats.FirstOrDefault(ds => ds.BelongsToDeck(deck)); if (deckStats != null) { if (deckStats.Games.Any()) { if (Config.Instance.KeepStatsWhenDeletingDeck) { var defaultDeck = DefaultDeckStats.Instance.GetDeckStats(deck.Class); if (defaultDeck != null) { defaultDeck.Games.AddRange(deckStats.Games); } DefaultDeckStats.Save(); Logger.WriteLine(string.Format("Moved deckstats for deck {0} to default stats", deck.Name), "Edit"); } else { try { foreach (var game in deckStats.Games) { game.DeleteGameFile(); } Logger.WriteLine("Deleted games from deck: " + deck.Name, "Edit"); } catch (Exception) { Logger.WriteLine("Error deleting games", "Edit"); } } } DeckStatsList.Instance.DeckStats.Remove(deckStats); if (saveAndUpdate) { DeckStatsList.Save(); } Logger.WriteLine("Removed deckstats from deck: " + deck.Name, "Edit"); } if (HearthStatsAPI.IsLoggedIn && deck.HasHearthStatsId && await CheckHearthStatsDeckDeletion()) { HearthStatsManager.DeleteDeckAsync(deck, false, true); } DeckList.Instance.Decks.Remove(deck); if (saveAndUpdate) { DeckList.Save(); DeckPickerList.UpdateDecks(); DeckPickerList.UpdateArchivedClassVisibility(); } ListViewDeck.ItemsSource = null; Logger.WriteLine("Deleted deck: " + deck.Name, "Edit"); }
private async void DeleteGames(DataGrid dataGrid, bool overall) { var window = Config.Instance.StatsInWindow ? (MetroWindow)Core.Windows.StatsWindow : Core.MainWindow; var count = dataGrid.SelectedItems.Count; if (count == 1) { var selectedGame = dataGrid.SelectedItem as GameStats; if (selectedGame == null) { return; } if (await window.ShowDeleteGameStatsMessage(selectedGame) != MessageDialogResult.Affirmative) { return; } if (!overall) { if (_deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); _deck.DeckStats.Games.Remove(selectedGame); Log.Info($"Deleted game {selectedGame} (overall=false)"); DeckStatsList.Save(); } } else { var deck = DeckList.Instance.Decks.FirstOrDefault(d => d.DeckStats.Games.Contains(selectedGame)); if (deck != null) { if (deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); deck.DeckStats.Games.Remove(selectedGame); Log.Info($"Deleted game {selectedGame} (overall=true)"); DefaultDeckStats.Save(); } } else { var deckstats = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (deckstats != null) { selectedGame.DeleteGameFile(); deckstats.Games.Remove(selectedGame); Log.Info($"Deleted game {selectedGame} (overall=true)"); DefaultDeckStats.Save(); } } } if (HearthStatsAPI.IsLoggedIn && selectedGame.HasHearthStatsId && await Core.MainWindow.ShowCheckHearthStatsMatchDeletionDialog()) { HearthStatsManager.DeleteMatchesAsync(new List <GameStats> { selectedGame }).Forget(); } //Core.MainWindow.DeckPickerList.Items.Refresh(); Core.MainWindow.DeckPickerList.UpdateDecks(); Refresh(); } else if (count > 1) { if (await window.ShowDeleteMultipleGameStatsMessage(count) != MessageDialogResult.Affirmative) { return; } var selectedGames = dataGrid.SelectedItems.Cast <GameStats>().Where(g => g != null).ToList(); foreach (var selectedGame in selectedGames) { if (!overall) { if (_deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); _deck.DeckStats.Games.Remove(selectedGame); Log.Info($"Deleted game {selectedGame} (overall=false)"); } } else { var deck = DeckList.Instance.Decks.FirstOrDefault(d => d.DeckStats.Games.Contains(selectedGame)); if (deck != null) { if (deck.DeckStats.Games.Contains(selectedGame)) { selectedGame.DeleteGameFile(); deck.DeckStats.Games.Remove(selectedGame); Log.Info($"Deleted game {selectedGame} (overall=true)"); } } else { var deckstats = DefaultDeckStats.Instance.DeckStats.FirstOrDefault(ds => ds.Games.Contains(selectedGame)); if (deckstats != null) { selectedGame.DeleteGameFile(); deckstats.Games.Remove(selectedGame); Log.Info($"Deleted game {selectedGame} (overall=true)"); } } } } if (HearthStatsAPI.IsLoggedIn && selectedGames.Any(g => g.HasHearthStatsId) && await Core.MainWindow.ShowCheckHearthStatsMatchDeletionDialog()) { HearthStatsManager.DeleteMatchesAsync(selectedGames).Forget(); } DeckStatsList.Save(); DefaultDeckStats.Save(); Log.Info("Deleted " + count + " games"); Core.MainWindow.DeckPickerList.UpdateDecks(); Refresh(); } }