Permalink
Browse files

move PackOpeningWatcher to HearthWatcher project

  • Loading branch information...
1 parent 2120e39 commit 4013c5ae48096fc7c7e40501e9204518b271ebb7 @azeier azeier committed Oct 6, 2016
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading;
using HearthMirror.Objects;
+using HearthWatcher.Providers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HearthWatcher.Test
@@ -3,6 +3,7 @@
using System.Threading.Tasks;
using HearthMirror.Objects;
using HearthWatcher.EventArgs;
+using HearthWatcher.Providers;
namespace HearthWatcher
{
@@ -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; }
+ }
+}
@@ -65,7 +65,10 @@
<Compile Include="EventArgs\ChoicesChangedEventArgs.cs" />
<Compile Include="EventArgs\CompleteDeckEventArgs.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" />
</ItemGroup>
<ItemGroup>
@@ -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;
-namespace HearthWatcher
+namespace HearthWatcher.Providers
{
public interface IArenaProvider
{
@@ -0,0 +1,11 @@
+using System.Collections.Generic;
+using HearthMirror.Objects;
+
+namespace HearthWatcher.Providers
+{
+ public interface IPackProvider
+ {
+ List<Card> GetCards();
+ int GetPackId();
+ }
+}
@@ -422,7 +422,6 @@
<Compile Include="LogReader\Handlers\FullScreenFxHandler.cs" />
<Compile Include="Importing\Game\ImportOptions\NewDeck.cs" />
<Compile Include="Importing\Game\ImportedDeck.cs" />
- <Compile Include="Hearthstone\PackOpeningWatcher.cs" />
<Compile Include="Properties\Strings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
@@ -1503,20 +1502,12 @@ xcopy /Y /Q "$(SolutionDir)CHANGELOG.md" "$(ProjectDir)Resources"</PreBuildEvent
</Target>
-->
<Target Name="GenerateDesignerFiles">
- <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"
- >
+ <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">
</GenerateResource>
- <Delete Files="Hearthstone_Deck_Tracker.Properties.Strings.resources"/>
+ <Delete Files="Hearthstone_Deck_Tracker.Properties.Strings.resources" />
</Target>
<Target Name="BeforeBuild">
- <CallTarget Targets="GenerateDesignerFiles"/>
+ <CallTarget Targets="GenerateDesignerFiles" />
</Target>
<Target Name="AfterBuild">
<CallTarget Condition=" '$(Configuration)' == 'Squirrel'" Targets="SquirrelAfterBuild" />
@@ -1,66 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-using HearthMirror;
-using Hearthstone_Deck_Tracker.Utility.Logging;
-
-namespace Hearthstone_Deck_Tracker.Hearthstone
-{
- public class PackOpeningWatcher
- {
- public delegate void PackEventHandler(object sender, PackEventArgs args);
-
- private readonly List<HearthMirror.Objects.Card> _previousPack = new List<HearthMirror.Objects.Card>();
- private bool _running;
- private bool _watch;
-
- private PackOpeningWatcher()
- {
- }
-
- public static PackOpeningWatcher Instance { get; } = new PackOpeningWatcher();
- public event PackEventHandler NewPackEventHandler;
-
- internal void Run()
- {
- _watch = true;
- if(!_running)
- CheckForPacks();
- }
-
- internal void Stop() => _watch = false;
-
- private async void CheckForPacks()
- {
- _running = true;
- while(_watch)
- {
- await Task.Delay(1000);
- if(!_watch)
- break;
- var cards = Reflection.GetPackCards();
- if(cards?.Any() ?? false)
- {
- if(cards.All(x => _previousPack.Any(c => c.Id == x.Id & c.Premium == x.Premium)))
- continue;
- Log.Info("Found new pack: " + string.Join(", ", cards.Select(x => $"{x.Id}{(x.Premium ? " (golden)" : "")}")));
- _previousPack.Clear();
- _previousPack.AddRange(cards);
- NewPackEventHandler?.Invoke(this, new PackEventArgs(cards));
- }
- }
- _running = false;
- }
- }
-
- public class PackEventArgs : EventArgs
- {
- public PackEventArgs(List<HearthMirror.Objects.Card> cards)
- {
- Cards = cards;
- }
-
- public List<HearthMirror.Objects.Card> Cards { get; }
- }
-}
@@ -1,8 +1,12 @@
+using System.Collections.Generic;
+using System.Linq;
using HearthMirror;
using HearthMirror.Objects;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Importing;
+using Hearthstone_Deck_Tracker.Utility.Logging;
using HearthWatcher;
+using HearthWatcher.Providers;
namespace Hearthstone_Deck_Tracker.Hearthstone
{
@@ -11,8 +15,16 @@ public static class Watchers
static Watchers()
{
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 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
@@ -1,4 +1,4 @@
-#region
+#region
using System;
using System.ComponentModel;
@@ -55,12 +55,12 @@ public void Handle(LogLineItem logLine, IHsGameState gameState, IGame game)
Watchers.ArenaWatcher.Stop();
if(game.CurrentMode == Mode.PACKOPENING)
- PackOpeningWatcher.Instance.Run();
+ Watchers.PackWatcher.Run();
else
- PackOpeningWatcher.Instance.Stop();
+ Watchers.PackWatcher.Stop();
}
else if(logLine.Line.Contains("Gameplay.Start"))
- {
+ {
gameState.Reset();
gameState.GameHandler.HandleGameStart(logLine.Time);
}
@@ -99,4 +99,4 @@ private Mode GetMode(string modeString)
return Mode.INVALID;
}
}
-}
+}

0 comments on commit 4013c5a

Please sign in to comment.