Permalink
Browse files

HSReplay: implement pack uploading

1 parent a4fe930 commit 57c20f696b7a2b2add480a8a4c2cc4d86cf2436c @azeier azeier committed Oct 7, 2016
@@ -16,9 +16,10 @@ public class PackOpeningWatcher
private readonly int _delay;
private bool _running;
private bool _watch;
+ private bool _invokeEvent;
private readonly IPackProvider _packProvider;
- public PackOpeningWatcher(IPackProvider packProvider, int delay = 1000)
+ public PackOpeningWatcher(IPackProvider packProvider, int delay = 500)
{
if(packProvider == null)
throw new ArgumentNullException(nameof(packProvider));
@@ -45,14 +46,19 @@ public void Run()
if(!_watch)
break;
var cards = _packProvider.GetCards();
- if(cards?.Any() ?? false)
+ if(cards?.Count == 5)
{
if(cards.All(x => _previousPack.Any(c => c.Id == x.Id & c.Premium == x.Premium)))
continue;
+ if(_previousPack.Any())
+ _invokeEvent = true;
_previousPack.Clear();
_previousPack.AddRange(cards);
- NewPackEventHandler?.Invoke(this, new PackEventArgs(cards, _packProvider.GetPackId()));
+ if(_invokeEvent)
+ NewPackEventHandler?.Invoke(this, new PackEventArgs(cards, _packProvider.GetPackId()));
}
+ else
+ _invokeEvent = true;
}
_running = false;
}
@@ -551,6 +551,9 @@ public class Config
[DefaultValue(true)]
public bool HsReplayUploadSpectator = true;
+ [DefaultValue(null)]
+ public bool? HsReplayUploadPacks = null;
+
[DefaultValue("00000000-0000-0000-0000-000000000000")]
public string Id = Guid.Empty.ToString();
@@ -391,6 +391,8 @@
<Compile Include="Enums\YesNo.cs" />
<Compile Include="Enums\Language.cs" />
<Compile Include="Hearthstone\Watchers.cs" />
+ <Compile Include="HsReplay\PackDataGenerator.cs" />
+ <Compile Include="HsReplay\PackUploader.cs" />
<Compile Include="HsReplay\UploadMetaDataGenerator.cs" />
<Compile Include="Importing\Game\ImportOptions\ExistingDeck.cs" />
<Compile Include="Exporting\ExportingActions.cs" />
@@ -1,10 +1,9 @@
using System.Collections.Generic;
-using System.Linq;
using HearthMirror;
using HearthMirror.Objects;
using Hearthstone_Deck_Tracker.Enums;
+using Hearthstone_Deck_Tracker.HsReplay;
using Hearthstone_Deck_Tracker.Importing;
-using Hearthstone_Deck_Tracker.Utility.Logging;
using HearthWatcher;
using HearthWatcher.Providers;
@@ -15,8 +14,9 @@ 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)" : "")))}]");
+ PackWatcher.NewPackEventHandler += (sender, args) => PackUploader.UploadPack(args.PackId, args.Cards);
}
+
public static ArenaWatcher ArenaWatcher { get; } = new ArenaWatcher(new HearthMirrorArenaProvider());
public static PackOpeningWatcher PackWatcher { get; } = new PackOpeningWatcher(new HearthMirrorPackProvider());
}
@@ -100,6 +100,8 @@ internal class ApiWrapper
public static async Task UploadLog(LogUploadRequest uploadRequest, string[] logLines)
=> await Client.UploadLog(uploadRequest, logLines);
+ public static async Task UploadPack(PackData data) => await Client.UploadPack(data, await GetUploadToken());
+
private static ClientConfig TryGetConfig()
{
var file = new FileInfo(Path.Combine(Config.AppDataPath, "hsreplaynet.xml"));
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Hearthstone_Deck_Tracker.Utility.Logging;
+using HSReplay;
+
+namespace Hearthstone_Deck_Tracker.HsReplay
+{
+ public static class PackDataGenerator
+ {
+ public static PackData Generate(int packId, IEnumerable<CardData> cardData)
+ {
+ var cards = cardData.ToArray();
+ if(cards.Length != 5)
+ {
+ Log.Error("Invalid card count: " + cards.Length);
+ return null;
+ }
+ var accId = HearthMirror.Reflection.GetAccountId();
+ if(accId == null || accId.Hi == 0 || accId.Lo == 0)
+ {
+ Log.Error("Could not get account id");
+ return null;
+ }
+ var data = new PackData
+ {
+ AccountHi = accId.Hi,
+ AccountLo = accId.Lo,
+ BoosterType = packId,
+ Date = DateTime.Now.ToString("o"),
+ Cards = cards
+
+ };
+ return data;
+ }
+ }
+}
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using HearthMirror.Objects;
+using Hearthstone_Deck_Tracker.Utility.Logging;
+using HSReplay;
+
+namespace Hearthstone_Deck_Tracker.HsReplay
+{
+ internal class PackUploader
+ {
+ internal static async void UploadPack(int packId, List<Card> cards)
+ {
+ Log.Info($"New Pack! Id={packId}, Cards=[{string.Join(", ", cards.Select(x => x.Id + (x.Premium ? " (g)" : "")))}]");
+ if(Config.Instance.HsReplayUploadPacks == true)
+ {
+ try
+ {
+ var packData = PackDataGenerator.Generate(packId, cards.Select(x => new CardData { CardId = x.Id, Premium = x.Premium }));
+ await ApiWrapper.UploadPack(packData);
+ Log.Info("Successfully uploaded pack");
+ }
+ catch(Exception ex)
+ {
+ Log.Error(ex);
+ }
+ }
+ }
+ }
+}

0 comments on commit 57c20f6

Please sign in to comment.