Skip to content

Commit

Permalink
fix gold progress tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Epix37 committed Oct 5, 2015
1 parent 95ec670 commit 5359c46
Showing 1 changed file with 23 additions and 54 deletions.
77 changes: 23 additions & 54 deletions Hearthstone Deck Tracker/LogReader/Handlers/RachelleHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ namespace Hearthstone_Deck_Tracker.LogReader.Handlers
{
public class RachelleHandler
{
private const string GoldRewardLogLine =
"RewardUtils.GetViewableRewards() - processing reward [GoldRewardData: Amount=10 Origin=TOURNEY OriginData=0]";

private const string ProgressLogLine = "VictoryScreen.InitGoldRewardUI(): goldRewardState = INVALID";
private const int WinsRequiredForReward = 3;
private const int GoldRewardAmount = 10;
private const int TotalMaxGoldReward = 100;
private readonly GameMode[] _goldTrackingGameModes = {GameMode.Casual, GameMode.Ranked, GameMode.Brawl};
private bool _receivedGoldReward;

public void Handle(string logLine, IHsGameState gameState, IGame game)
{
if(HsLogReaderConstants.CardAlreadyInCacheRegex.IsMatch(logLine))
Expand All @@ -32,12 +22,22 @@ public void Handle(string logLine, IHsGameState gameState, IGame game)
else
gameState.GameHandler.HandlePossibleConstructedCard(id, false);
}
else if((DateTime.Now - gameState.LastGameStart) > TimeSpan.FromSeconds(10))
else if(HsLogReaderConstants.GoldProgressRegex.IsMatch(logLine)
&& (DateTime.Now - gameState.LastGameStart) > TimeSpan.FromSeconds(10)
&& game.CurrentGameMode != GameMode.Spectator)
{
if(logLine.Contains(GoldRewardLogLine))
_receivedGoldReward = true;
else if(logLine.Contains(ProgressLogLine))
HandleGoldProgress(game);

int wins;
var rawWins = HsLogReaderConstants.GoldProgressRegex.Match(logLine).Groups["wins"].Value;
if(int.TryParse(rawWins, out wins))
{
var timeZone = GetTimeZoneInfo(game.CurrentRegion);
if(timeZone != null)
{
UpdateGoldProgress(wins, game, timeZone);
}
}

}
else if(HsLogReaderConstants.DustRewardRegex.IsMatch(logLine))
{
Expand All @@ -53,21 +53,6 @@ public void Handle(string logLine, IHsGameState gameState, IGame game)
}
}

private async void HandleGoldProgress(IGame game)
{
Logger.WriteLine("Updating GoldProgress, waiting for GameModeDetection...", "RachelleHandler");
await game.GameModeDetection(300);
if(_goldTrackingGameModes.All(mode => game.CurrentGameMode != mode))
{
Logger.WriteLine(string.Format("Current GameMode is {0} - cancelled GoldProgress update.", game.CurrentGameMode), "RachelleHandler");
return;
}

var timeZone = GetTimeZoneInfo(game.CurrentRegion);
if(timeZone != null)
UpdateGoldProgress(game, timeZone);
}

private TimeZoneInfo GetTimeZoneInfo(Region region)
{
try
Expand All @@ -94,42 +79,26 @@ private TimeZoneInfo GetTimeZoneInfo(Region region)
return null;
}

private void UpdateGoldProgress(IGame game, TimeZoneInfo timeZone)
private void UpdateGoldProgress(int wins, IGame game, TimeZoneInfo timeZone)
{
try
{
var regionIndex = (int)game.CurrentRegion - 1;
var previousGoldProgress = GetGoldProgressString(regionIndex);
var wins = Config.Instance.GoldProgress[regionIndex] + 1;
var region = (int)game.CurrentRegion - 1;
var date = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZone).Date;
if(Config.Instance.GoldProgressLastReset[regionIndex].Date != date)
{
wins = 1;
Config.Instance.GoldProgressTotal[regionIndex] = 0;
Config.Instance.GoldProgressLastReset[regionIndex] = date;
}
if(_receivedGoldReward)
if(Config.Instance.GoldProgressLastReset[region].Date != date)
{
wins = 0;
_receivedGoldReward = false;
Config.Instance.GoldProgressTotal[regionIndex] += GoldRewardAmount;
Config.Instance.GoldProgressTotal[region] = 0;
Config.Instance.GoldProgressLastReset[region] = date;
}
if(Config.Instance.GoldProgressTotal[regionIndex] == TotalMaxGoldReward)
wins = 0;
Config.Instance.GoldProgress[regionIndex] = wins;
Config.Instance.GoldProgress[region] = wins == 3 ? 0 : wins;
if(wins == 3)
Config.Instance.GoldProgressTotal[region] += 10;
Config.Save();
Logger.WriteLine(string.Format("Updated GoldProgress from {0} to {1} for region {2}.", previousGoldProgress, GetGoldProgressString(regionIndex), game.CurrentRegion));
}
catch(Exception ex)
{
Logger.WriteLine("Error updating GoldProgress: " + ex, "RachelleHandler");
}
}

private string GetGoldProgressString(int regionIndex)
{
return string.Format("{0}/{1} - {2}/{3}", Config.Instance.GoldProgress[regionIndex], WinsRequiredForReward,
Config.Instance.GoldProgressTotal[regionIndex], TotalMaxGoldReward);
}
}
}

0 comments on commit 5359c46

Please sign in to comment.