Skip to content

Commit

Permalink
integration with Dearthstone Deck Tracker for gold rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
arkein committed May 8, 2016
1 parent a5f3b02 commit bc5df3c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Hearthstone Treasury/Enums/OriginEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Hearthstone_Treasury.Enums
{
public enum OriginEnum
{
TOURNEY,
ACHIEVEMENT,
UNKNOWN
}
}
2 changes: 2 additions & 0 deletions Hearthstone Treasury/Hearthstone Treasury.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@
<DependentUpon>TreasuryMenuItem.xaml</DependentUpon>
</Compile>
<Compile Include="Enums\CategoryEnum.cs" />
<Compile Include="Enums\OriginEnum.cs" />
<Compile Include="HearthstoneTreasuryPlugin.cs" />
<Compile Include="ViewModels\GoldRewardViewModel.cs" />
<Compile Include="ViewModels\NewTransactionViewModel.cs" />
<Compile Include="ViewModels\StatisticsViewModel.cs" />
<Compile Include="ViewModels\TransactionListViewModel.cs" />
Expand Down
50 changes: 50 additions & 0 deletions Hearthstone Treasury/HearthstoneTreasuryPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
using System.IO;
using System.Windows;
using ReactiveUI;
using Hearthstone_Deck_Tracker.LogReader;
using System.Text.RegularExpressions;
using Hearthstone_Treasury.Enums;
using System.Linq;

namespace Hearthstone_Treasury
{
public class HearthstoneTreasuryPlugin : Hearthstone_Deck_Tracker.Plugins.IPlugin
{
/// <summary>
/// Copied form Hearthstone_Deck_Tracker.HsLogReaderConstants.GoldRewardRegex, extended with Origin info
/// </summary>
public static readonly Regex GoldRewardExtendedRegex = new Regex(@"GoldRewardData: Amount=(?<amount>(\d+)) Origin=(?<origin>(\w+)) OriginData=(?<origindata>(\d+))");

internal static string PluginDataDir => Path.Combine(Hearthstone_Deck_Tracker.Config.Instance.DataDir, "Treasury");
internal static string TransactionsFile => Path.Combine(PluginDataDir, "transactions.xml");
internal static string SettingsFile => Path.Combine(PluginDataDir, "treasury.config.xml");
Expand Down Expand Up @@ -75,6 +82,49 @@ public void OnLoad()
_mainWindow.Activate();
}
};

Hearthstone_Deck_Tracker.API.LogEvents.OnRachelleLogLine.Add(HandleRachelleLogUpdate);
}

private void HandleRachelleLogUpdate(string logLine)
{
if (GoldRewardExtendedRegex.IsMatch(logLine))
{
// Only process new lines
if (IsLogLineOutdated(logLine))
{
return;
}

//parse
var match = GoldRewardExtendedRegex.Match(logLine);
var rewardInfo = new GoldRewardViewModel(logLine, match.Groups["amount"].Value, match.Groups["origin"].Value, match.Groups["origindata"].Value);
_mainWindowModel?.TransactionList.AddTransaction(new TransactionViewModel { Difference = rewardInfo.Amount, Category = rewardInfo.Category, Comment = rewardInfo.Comment });
}
}

private bool IsLogLineOutdated(string logLine)
{
DateTime loglinetime;
if (logLine.Length > 20 && DateTime.TryParse(logLine.Substring(2, 16), out loglinetime))
{
if (loglinetime > DateTime.Now)
{
loglinetime = loglinetime.AddDays(-1);
}

var latestTransaction = _mainWindowModel.TransactionList.Transactions.OrderByDescending(t => t.Moment).FirstOrDefault();
if (latestTransaction != null)
{
var latestTransactionTime = latestTransaction.Moment;

if (loglinetime <= latestTransactionTime)
{
return true;
}
}
}
return false;
}

private void InitializeMainWindow()
Expand Down
91 changes: 91 additions & 0 deletions Hearthstone Treasury/ViewModels/GoldRewardViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Hearthstone_Treasury.Enums;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;

namespace Hearthstone_Treasury.ViewModels
{
public class GoldRewardViewModel : ReactiveObject
{
/// <summary>
/// Mapping between Achievement ID which is basically quest ID and quest text. This mapping should be removed when achievement log parser is implemented in HDT.
/// </summary>
internal readonly Dictionary<int, string> AchievementIdToComment = new Dictionary<int, string>
{
{ 38, "Priest or Warlock Victory" },
{ 45, "Paladin or Priest Dominance" },
};

public GoldRewardViewModel(string sourceString, string amountString, string originString, string originDataString)
{
Source = sourceString;

int amount;
if(int.TryParse(amountString, out amount))
{
Amount = amount;
}

int originData;
if (int.TryParse(originDataString, out originData))
{
OriginData = originData;
} else
{
OriginData = null;
}

OriginEnum origin;
if (Enum.TryParse(originString, out origin))
{
Origin = origin;
} else
{
Origin = OriginEnum.UNKNOWN;
}

//map origin to category
switch (Origin)
{
case OriginEnum.TOURNEY:
Category = CategoryEnum.Wins;
break;
case OriginEnum.ACHIEVEMENT:
Category = CategoryEnum.Daily;
break;
default:
Category = CategoryEnum.Other;
break;
}

//map origindata to known comment
string comment;
if (OriginData.HasValue && AchievementIdToComment.TryGetValue(OriginData.Value, out comment))
{
Comment = comment;
} else
{
Comment = "*";
}
}

[Reactive]
public string Source { get; private set; }

[Reactive]
public int Amount { get; private set; }

[Reactive]
public OriginEnum Origin { get; private set; }

[Reactive]
public int? OriginData { get; private set; }

[Reactive]
public string Comment { get; private set; }

[Reactive]
public CategoryEnum Category { get; private set; }
}
}

0 comments on commit bc5df3c

Please sign in to comment.