Navigation Menu

Skip to content

Commit

Permalink
move PackOpeningWatcher to HearthWatcher project
Browse files Browse the repository at this point in the history
  • Loading branch information
azeier committed Oct 6, 2016
1 parent 2120e39 commit 4013c5a
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 85 deletions.
1 change: 1 addition & 0 deletions HearthWatcher.Test/ArenaWatcherTests.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using HearthMirror.Objects; using HearthMirror.Objects;
using HearthWatcher.Providers;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace HearthWatcher.Test namespace HearthWatcher.Test
Expand Down
1 change: 1 addition & 0 deletions HearthWatcher/ArenaWatcher.cs
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using HearthMirror.Objects; using HearthMirror.Objects;
using HearthWatcher.EventArgs; using HearthWatcher.EventArgs;
using HearthWatcher.Providers;


namespace HearthWatcher namespace HearthWatcher
{ {
Expand Down
17 changes: 17 additions & 0 deletions HearthWatcher/EventArgs/PackEventArgs.cs
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using HearthMirror.Objects;

namespace HearthWatcher.EventArgs
{
public class PackEventArgs : System.EventArgs
{
public PackEventArgs(List<Card> cards, int packId)
{
Cards = cards;
PackId = packId;
}

public List<Card> Cards { get; }
public int PackId { get; set; }
}
}
5 changes: 4 additions & 1 deletion HearthWatcher/HearthWatcher.csproj
Expand Up @@ -65,7 +65,10 @@
<Compile Include="EventArgs\ChoicesChangedEventArgs.cs" /> <Compile Include="EventArgs\ChoicesChangedEventArgs.cs" />
<Compile Include="EventArgs\CompleteDeckEventArgs.cs" /> <Compile Include="EventArgs\CompleteDeckEventArgs.cs" />
<Compile Include="EventArgs\RewardsEventArgs.cs" /> <Compile Include="EventArgs\RewardsEventArgs.cs" />
<Compile Include="IArenaProvider.cs" /> <Compile Include="Providers\IArenaProvider.cs" />
<Compile Include="Providers\IPackProvider.cs" />
<Compile Include="EventArgs\PackEventArgs.cs" />
<Compile Include="PackOpeningWatcher.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
Expand Down
60 changes: 60 additions & 0 deletions HearthWatcher/PackOpeningWatcher.cs
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HearthMirror.Objects;
using HearthWatcher.EventArgs;
using HearthWatcher.Providers;

namespace HearthWatcher
{
public class PackOpeningWatcher
{
public delegate void PackEventHandler(object sender, PackEventArgs args);

private readonly List<Card> _previousPack = new List<Card>();
private readonly int _delay;
private bool _running;
private bool _watch;
private readonly IPackProvider _packProvider;

public PackOpeningWatcher(IPackProvider packProvider, int delay = 1000)
{
if(packProvider == null)
throw new ArgumentNullException(nameof(packProvider));
_packProvider = packProvider;
_delay = delay;
}
public event PackEventHandler NewPackEventHandler;

public void Run()
{
_watch = true;
if(!_running)
CheckForPacks();
}

public void Stop() => _watch = false;

private async void CheckForPacks()
{
_running = true;
while(_watch)
{
await Task.Delay(_delay);
if(!_watch)
break;
var cards = _packProvider.GetCards();
if(cards?.Any() ?? false)
{
if(cards.All(x => _previousPack.Any(c => c.Id == x.Id & c.Premium == x.Premium)))
continue;
_previousPack.Clear();
_previousPack.AddRange(cards);
NewPackEventHandler?.Invoke(this, new PackEventArgs(cards, _packProvider.GetPackId()));
}
}
_running = false;
}
}
}
@@ -1,6 +1,6 @@
using HearthMirror.Objects; using HearthMirror.Objects;


namespace HearthWatcher namespace HearthWatcher.Providers
{ {
public interface IArenaProvider public interface IArenaProvider
{ {
Expand Down
11 changes: 11 additions & 0 deletions HearthWatcher/Providers/IPackProvider.cs
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using HearthMirror.Objects;

namespace HearthWatcher.Providers
{
public interface IPackProvider
{
List<Card> GetCards();
int GetPackId();
}
}
15 changes: 3 additions & 12 deletions Hearthstone Deck Tracker/Hearthstone Deck Tracker.csproj
Expand Up @@ -422,7 +422,6 @@
<Compile Include="LogReader\Handlers\FullScreenFxHandler.cs" /> <Compile Include="LogReader\Handlers\FullScreenFxHandler.cs" />
<Compile Include="Importing\Game\ImportOptions\NewDeck.cs" /> <Compile Include="Importing\Game\ImportOptions\NewDeck.cs" />
<Compile Include="Importing\Game\ImportedDeck.cs" /> <Compile Include="Importing\Game\ImportedDeck.cs" />
<Compile Include="Hearthstone\PackOpeningWatcher.cs" />
<Compile Include="Properties\Strings.Designer.cs"> <Compile Include="Properties\Strings.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
Expand Down Expand Up @@ -1503,20 +1502,12 @@ xcopy /Y /Q "$(SolutionDir)CHANGELOG.md" "$(ProjectDir)Resources"</PreBuildEvent
</Target> </Target>
--> -->
<Target Name="GenerateDesignerFiles"> <Target Name="GenerateDesignerFiles">
<GenerateResource <GenerateResource Sources="Properties/Strings.resx" StronglyTypedLanguage="C#" StronglyTypedClassName="Strings" StronglyTypedNamespace="Hearthstone_Deck_Tracker.Properties" StronglyTypedFileName="Properties/Strings.Designer.cs" OutputResources="Hearthstone_Deck_Tracker.Properties.Strings.resources" PublicClass="true">
Sources="Properties/Strings.resx"
StronglyTypedLanguage="C#"
StronglyTypedClassName="Strings"
StronglyTypedNamespace="Hearthstone_Deck_Tracker.Properties"
StronglyTypedFileName="Properties/Strings.Designer.cs"
OutputResources="Hearthstone_Deck_Tracker.Properties.Strings.resources"
PublicClass="true"
>
</GenerateResource> </GenerateResource>
<Delete Files="Hearthstone_Deck_Tracker.Properties.Strings.resources"/> <Delete Files="Hearthstone_Deck_Tracker.Properties.Strings.resources" />
</Target> </Target>
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">
<CallTarget Targets="GenerateDesignerFiles"/> <CallTarget Targets="GenerateDesignerFiles" />
</Target> </Target>
<Target Name="AfterBuild"> <Target Name="AfterBuild">
<CallTarget Condition=" '$(Configuration)' == 'Squirrel'" Targets="SquirrelAfterBuild" /> <CallTarget Condition=" '$(Configuration)' == 'Squirrel'" Targets="SquirrelAfterBuild" />
Expand Down
66 changes: 0 additions & 66 deletions Hearthstone Deck Tracker/Hearthstone/PackOpeningWatcher.cs

This file was deleted.

12 changes: 12 additions & 0 deletions Hearthstone Deck Tracker/Hearthstone/Watchers.cs
@@ -1,8 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using HearthMirror; using HearthMirror;
using HearthMirror.Objects; using HearthMirror.Objects;
using Hearthstone_Deck_Tracker.Enums; using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Importing; using Hearthstone_Deck_Tracker.Importing;
using Hearthstone_Deck_Tracker.Utility.Logging;
using HearthWatcher; using HearthWatcher;
using HearthWatcher.Providers;


namespace Hearthstone_Deck_Tracker.Hearthstone namespace Hearthstone_Deck_Tracker.Hearthstone
{ {
Expand All @@ -11,8 +15,16 @@ public static class Watchers
static Watchers() static Watchers()
{ {
ArenaWatcher.OnCompleteDeck += (sender, args) => DeckManager.AutoImportArena(Config.Instance.SelectedArenaImportingBehaviour ?? ArenaImportingBehaviour.AutoImportSave, args.Info); ArenaWatcher.OnCompleteDeck += (sender, args) => DeckManager.AutoImportArena(Config.Instance.SelectedArenaImportingBehaviour ?? ArenaImportingBehaviour.AutoImportSave, args.Info);
PackWatcher.NewPackEventHandler += (sender, args) => Log.Debug($"New Pack! Id={args.PackId}, Cards=[{string.Join(", ", args.Cards.Select(x => x.Id + (x.Premium ? " (g)" : "")))}]");
} }
public static ArenaWatcher ArenaWatcher { get; } = new ArenaWatcher(new HearthMirrorArenaProvider()); public static ArenaWatcher ArenaWatcher { get; } = new ArenaWatcher(new HearthMirrorArenaProvider());
public static PackOpeningWatcher PackWatcher { get; } = new PackOpeningWatcher(new HearthMirrorPackProvider());
}

public class HearthMirrorPackProvider : IPackProvider
{
public List<HearthMirror.Objects.Card> GetCards() => Reflection.GetPackCards();
public int GetPackId() => Reflection.GetLastOpenedBoosterId();
} }


public class HearthMirrorArenaProvider : IArenaProvider public class HearthMirrorArenaProvider : IArenaProvider
Expand Down
@@ -1,4 +1,4 @@
#region #region


using System; using System;
using System.ComponentModel; using System.ComponentModel;
Expand Down Expand Up @@ -55,12 +55,12 @@ public void Handle(LogLineItem logLine, IHsGameState gameState, IGame game)
Watchers.ArenaWatcher.Stop(); Watchers.ArenaWatcher.Stop();


if(game.CurrentMode == Mode.PACKOPENING) if(game.CurrentMode == Mode.PACKOPENING)
PackOpeningWatcher.Instance.Run(); Watchers.PackWatcher.Run();
else else
PackOpeningWatcher.Instance.Stop(); Watchers.PackWatcher.Stop();
} }
else if(logLine.Line.Contains("Gameplay.Start")) else if(logLine.Line.Contains("Gameplay.Start"))
{ {
gameState.Reset(); gameState.Reset();
gameState.GameHandler.HandleGameStart(logLine.Time); gameState.GameHandler.HandleGameStart(logLine.Time);
} }
Expand Down Expand Up @@ -99,4 +99,4 @@ private Mode GetMode(string modeString)
return Mode.INVALID; return Mode.INVALID;
} }
} }
} }

0 comments on commit 4013c5a

Please sign in to comment.