Skip to content

Commit

Permalink
retroactively fix opponent names (HearthSim#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
epix37 committed May 3, 2015
1 parent 4b860db commit d1b743b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Hearthstone Deck Tracker/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ public class Config
[DefaultValue(false)]
public bool ResolvedDeckStatsIssue = false;

[DefaultValue(false)]
public bool ResolvedOpponentNames = false;

//updating from <= 0.5.1:
//SaveConfigInAppData and SaveDataInAppData are set to SaveInAppData AFTER the config isloaded
//=> Need to be null to avoid creating new config in appdata if config is stored locally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private void OpenGameDetails(GameStats selected)
if(selected != null)
{
if(selected.HasReplayFile && !Keyboard.IsKeyDown(Key.LeftCtrl)) //hold ctrl to open old game viewer
ReplayReader.Read(selected.ReplayFile);
ReplayReader.LaunchReplayViewer(selected.ReplayFile);
else if(Config.Instance.StatsInWindow)
{
Helper.MainWindow.StatsWindow.GameDetailsFlyout.SetGame(selected);
Expand Down
65 changes: 63 additions & 2 deletions Hearthstone Deck Tracker/MainWindow/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -18,6 +19,7 @@
using System.Windows.Media;
using Hearthstone_Deck_Tracker.API;
using Hearthstone_Deck_Tracker.Controls;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.HearthStats.API;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Plugins;
Expand Down Expand Up @@ -419,6 +421,65 @@ private void ResolveDeckStatsIssue()
Config.Save();
}

/// <summary>
/// v0.10.0 caused opponent names to be saved as the hero, rather than the name.
/// </summary>
private async void ResolveOpponentNames()
{
var games =
DeckStatsList.Instance.DeckStats.SelectMany(ds => ds.Games)
.Where(g => g.HasReplayFile && Enum.GetNames(typeof(HeroClass)).Any(x => x == g.OpponentName))
.ToList();
if(!games.Any())
return;
var controller = await this.ShowProgressAsync("Fixing opponent names in recorded games...", "v0.10.0 caused opponent names to be set to their hero, rather than the actual name.\n\nThis may take a moment.\n\nYou can cancel to continue this at a later time (or not at all).", true);
var count = 0;
var lockMe = new object();
await Task.Run(() =>
{
Parallel.ForEach(games, (game, loopState) =>
{
if(controller.IsCanceled)
loopState.Stop();
List<ReplayKeyPoint> replay = ReplayReader.LoadReplay(game.ReplayFile);
if(replay == null)
return;
var last = replay.LastOrDefault();
if(last == null)
return;
var opponent = last.Data.FirstOrDefault(x => x.IsOpponent);
if(opponent == null)
return;
game.OpponentName = opponent.Name;
lock(lockMe)
{
controller.SetProgress(1.0 * ++count / games.Count);
}
});
});

await controller.CloseAsync();
if(controller.IsCanceled)
{
var fix =
await
this.ShowMessageAsync("Cancelled", "Fix remaining names on next start?", MessageDialogStyle.AffirmativeAndNegative,
new MetroDialogSettings() {AffirmativeButtonText = "next time", NegativeButtonText = "don\'t fix"});
if(fix == MessageDialogResult.Negative)
{
Config.Instance.ResolvedOpponentNames = true;
Config.Save();
}

}
else
{
Config.Instance.ResolvedOpponentNames = true;
Config.Save();
}
DeckStatsList.Save();
}

private async void UpdateAsync()
{
const string url = "https://raw.githubusercontent.com/Epix37/HDT-Data/master/news";
Expand Down Expand Up @@ -1327,7 +1388,7 @@ private void MenuItemReplayLastGame_OnClick(object sender, RoutedEventArgs e)
var newest =
Directory.GetFiles(Config.Instance.ReplayDir).Select(x => new FileInfo(x)).OrderByDescending(x => x.CreationTime).FirstOrDefault();
if(newest != null)
ReplayReader.Read(newest.FullName);
ReplayReader.LaunchReplayViewer(newest.FullName);
}
catch(Exception ex)
{
Expand All @@ -1348,7 +1409,7 @@ private void MenuItemReplayFromFile_OnClick(object sender, RoutedEventArgs e)
};
var dialogResult = dialog.ShowDialog();
if(dialogResult == System.Windows.Forms.DialogResult.OK)
ReplayReader.Read(dialog.FileName);
ReplayReader.LaunchReplayViewer(dialog.FileName);
}
catch(Exception ex)
{
Expand Down
6 changes: 4 additions & 2 deletions Hearthstone Deck Tracker/MainWindow/MainWindow_Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,10 @@ private async void MetroWindow_Loaded(object sender, RoutedEventArgs e)
await
this.ShowMessage("Restart Hearthstone",
"This is either your first time starting the tracker or the log.config file has been updated. Please restart Heartstone once, for the tracker to work properly.");
}

}

if(!Config.Instance.ResolvedOpponentNames)
ResolveOpponentNames();
if(!Config.Instance.ResolvedDeckStatsIds)
{
if(ResolveDeckStatsIds())
Expand Down
27 changes: 15 additions & 12 deletions Hearthstone Deck Tracker/Replay/ReplayReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,29 @@ public static class ReplayReader
{
private static readonly List<ReplayViewer> Viewers = new List<ReplayViewer>();

public static void Read(string fileName)
public static void LaunchReplayViewer(string fileName)
{
var replay = LoadReplay(fileName);
var rv = new ReplayViewer();
rv.Show();
rv.Load(replay);
Viewers.Add(rv);
}

public static List<ReplayKeyPoint> LoadReplay(string fileName)
{
var path = Path.Combine(Config.Instance.ReplayDir, fileName);
if(!File.Exists(path))
return;
return new List<ReplayKeyPoint>();
const string jsonFile = "replay.json";
string json;

using(var fs = new FileStream(path, FileMode.Open))
using(var archive = new ZipArchive(fs, ZipArchiveMode.Read))
using(var sr = new StreamReader(archive.GetEntry(jsonFile).Open()))
using (var fs = new FileStream(path, FileMode.Open))
using (var archive = new ZipArchive(fs, ZipArchiveMode.Read))
using (var sr = new StreamReader(archive.GetEntry(jsonFile).Open()))
json = sr.ReadToEnd();

var replay = (List<ReplayKeyPoint>)JsonConvert.DeserializeObject(json, typeof(List<ReplayKeyPoint>));


var rv = new ReplayViewer();
rv.Show();
rv.Load(replay);
Viewers.Add(rv);
return (List<ReplayKeyPoint>)JsonConvert.DeserializeObject(json, typeof(List<ReplayKeyPoint>));
}

public static void CloseViewers()
Expand Down

0 comments on commit d1b743b

Please sign in to comment.