Skip to content

Commit dda5a0d

Browse files
committed
feat(Choices): start parsing SendChoices
1 parent 5e5bef4 commit dda5a0d

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using Hearthstone_Deck_Tracker.Hearthstone.Entities;
3+
using HearthDb.Enums;
4+
using System;
5+
6+
namespace Hearthstone_Deck_Tracker.Hearthstone
7+
{
8+
[Serializable]
9+
public class Choice
10+
{
11+
public readonly int Id;
12+
public readonly ChoiceType ChoiceType;
13+
public readonly List<Entity> ChosenEntities = new List<Entity>();
14+
15+
public Choice(int id, ChoiceType choiceType)
16+
{
17+
Id = id;
18+
ChoiceType = choiceType;
19+
}
20+
21+
public void AttachChosenEntity(int index, Entity entity)
22+
{
23+
ChosenEntities.Add(entity);
24+
}
25+
}
26+
}

Hearthstone Deck Tracker/IGameHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using HearthDb.Enums;
55
using Hearthstone_Deck_Tracker.Enums;
6+
using Hearthstone_Deck_Tracker.Hearthstone;
67
using Hearthstone_Deck_Tracker.Hearthstone.Entities;
78

89
#endregion
@@ -21,6 +22,7 @@ public interface IGameHandler
2122
void HandlePlayerDeckDiscard(Entity entity, string cardId, int turn);
2223
void HandlePlayerPlayToDeck(Entity entity, string cardId, int turn);
2324
void HandlePlayerHeroPower(string cardId, int turn);
25+
void HandlePlayerSendChoices(Choice choice);
2426
void SetPlayerHero(string? cardId);
2527
void HandlePlayerGetToDeck(Entity entity, string cardId, int turn);
2628
void TurnStart(ActivePlayer player, int turnNumber);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Hearthstone_Deck_Tracker.Hearthstone;
2+
using Hearthstone_Deck_Tracker.LogReader.Interfaces;
3+
using System;
4+
using HearthDb.Enums;
5+
using static Hearthstone_Deck_Tracker.LogReader.LogConstants.Choices;
6+
7+
namespace Hearthstone_Deck_Tracker.LogReader.Handlers
8+
{
9+
internal class ChoicesHandler
10+
{
11+
SendChoicesHandler _sendChoicesHandler = new SendChoicesHandler();
12+
13+
public void Handle(string logLine, IHsGameState gameState, IGame game)
14+
{
15+
if(SendChoicesHeaderRegex.IsMatch(logLine))
16+
{
17+
var match = SendChoicesHeaderRegex.Match(logLine);
18+
var choiceId = int.Parse(match.Groups["id"].Value);
19+
if(!Enum.TryParse(match.Groups["choiceType"].Value, out ChoiceType choiceType))
20+
choiceType = ChoiceType.INVALID;
21+
_sendChoicesHandler.SendChoices(choiceId, choiceType, gameState, game);
22+
}
23+
else if(SendChoicesBodyRegex.IsMatch(logLine))
24+
{
25+
var match = SendChoicesBodyRegex.Match(logLine);
26+
var index = int.Parse(match.Groups["index"].Value);
27+
var entityId = int.Parse(match.Groups["id"].Value);
28+
_sendChoicesHandler.SendChoice(index, entityId, gameState, game);
29+
}
30+
else
31+
{
32+
// Terminate the current Choice.
33+
_sendChoicesHandler.Flush(gameState, game);
34+
}
35+
}
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Hearthstone_Deck_Tracker.Hearthstone;
2+
using Hearthstone_Deck_Tracker.LogReader.Interfaces;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using HearthDb.Enums;
9+
10+
namespace Hearthstone_Deck_Tracker.LogReader.Handlers
11+
{
12+
internal class SendChoicesHandler
13+
{
14+
private Choice? Choice;
15+
16+
public void SendChoices(int id, ChoiceType choiceType, IHsGameState gameState, IGame game)
17+
{
18+
Choice = new Choice(id, choiceType);
19+
}
20+
21+
public void SendChoice(int index, int entityId, IHsGameState gameState, IGame game)
22+
{
23+
if(Choice is null) return;
24+
25+
if(game.Entities.TryGetValue(entityId, out var entity))
26+
Choice.AttachChosenEntity(index, entity);
27+
}
28+
29+
public void Flush(IHsGameState gameState, IGame game)
30+
{
31+
if(Choice is null) return;
32+
33+
gameState.GameHandler?.HandlePlayerSendChoices(Choice);
34+
Choice = null;
35+
}
36+
}
37+
}

Hearthstone Deck Tracker/LogReader/LogConstants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ public static class PowerTaskList
4949

5050
public static readonly Regex ShuffleRegex = new Regex(@"SHUFFLE_DECK\ PlayerID=(?<id>(\d+))");
5151
}
52+
53+
public static class Choices
54+
{
55+
public static readonly Regex SendChoicesHeaderRegex = new Regex(@"id=(?<id>(\d+)) ChoiceType=(?<choiceType>(\w+))");
56+
public static readonly Regex SendChoicesBodyRegex = new Regex(@"m_chosenEntities\[(?<index>(\d+))]=.* id=(?<id>(\d+))");
57+
}
5258
}
5359
}

Hearthstone Deck Tracker/LogReader/LogWatcherManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Hearthstone_Deck_Tracker.LogReader
2222
public class LogWatcherManager
2323
{
2424
private readonly PowerHandler _powerLineHandler = new PowerHandler();
25+
private readonly ChoicesHandler _choicesHandler = new ChoicesHandler();
2526
private readonly ArenaHandler _arenaHandler = new ArenaHandler();
2627
private readonly LoadingScreenHandler _loadingScreenHandler = new LoadingScreenHandler();
2728
private HsGameState? _gameState;
@@ -128,7 +129,11 @@ private void OnNewLines(List<LogLine> lines)
128129
break;
129130
case "Power":
130131
if(line.LineContent.StartsWith("GameState."))
132+
{
131133
_game.PowerLog.Add(line.Line);
134+
if(line.LineContent.StartsWith("GameState.SendChoices") || line.LineContent.StartsWith("GameState.DebugPrintEntitiesChosen"))
135+
_choicesHandler.Handle(line.Line, _gameState, _game);
136+
}
132137
else
133138
{
134139
_powerLineHandler.Handle(line.Line, _gameState, _game);

0 commit comments

Comments
 (0)