Skip to content

Commit

Permalink
feat(Choices): start parsing SendChoices
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Nov 29, 2023
1 parent 5e5bef4 commit 1dfa50b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions Hearthstone Deck Tracker/GameEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,7 @@ public void HandleQuestRewardDatabaseId(int id, int value)
void IGameHandler.SetOpponentHero(string? cardId) => SetOpponentHero(cardId);
void IGameHandler.SetPlayerHero(string? cardId) => SetPlayerHero(cardId);
void IGameHandler.HandleOpponentHeroPower(string cardId, int turn) => HandleOpponentHeroPower(cardId, turn);
void IGameHandler.HandlePlayerSendChoices(Choice choice) {}
void IGameHandler.TurnStart(ActivePlayer player, int turnNumber) => TurnStart(player, turnNumber);
void IGameHandler.HandleGameStart(DateTime timestamp) => HandleGameStart(timestamp);
void IGameHandler.HandleGameEnd(bool stateComplete) => HandleGameEnd(stateComplete);
Expand Down
26 changes: 26 additions & 0 deletions Hearthstone Deck Tracker/Hearthstone/Choice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Hearthstone_Deck_Tracker.Hearthstone.Entities;
using HearthDb.Enums;
using System;

namespace Hearthstone_Deck_Tracker.Hearthstone
{
[Serializable]
public class Choice
{
public readonly int Id;
public readonly ChoiceType ChoiceType;
public readonly List<Entity> ChosenEntities = new List<Entity>();

public Choice(int id, ChoiceType choiceType)
{
Id = id;
ChoiceType = choiceType;
}

public void AttachChosenEntity(int index, Entity entity)
{
ChosenEntities.Add(entity);
}
}
}
2 changes: 2 additions & 0 deletions Hearthstone Deck Tracker/IGameHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using HearthDb.Enums;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Hearthstone.Entities;

#endregion
Expand All @@ -21,6 +22,7 @@ public interface IGameHandler
void HandlePlayerDeckDiscard(Entity entity, string cardId, int turn);
void HandlePlayerPlayToDeck(Entity entity, string cardId, int turn);
void HandlePlayerHeroPower(string cardId, int turn);
void HandlePlayerSendChoices(Choice choice);
void SetPlayerHero(string? cardId);
void HandlePlayerGetToDeck(Entity entity, string cardId, int turn);
void TurnStart(ActivePlayer player, int turnNumber);
Expand Down
37 changes: 37 additions & 0 deletions Hearthstone Deck Tracker/LogReader/Handlers/ChoicesHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.LogReader.Interfaces;
using System;
using HearthDb.Enums;
using static Hearthstone_Deck_Tracker.LogReader.LogConstants.Choices;

namespace Hearthstone_Deck_Tracker.LogReader.Handlers
{
internal class ChoicesHandler
{
SendChoicesHandler _sendChoicesHandler = new SendChoicesHandler();

public void Handle(string logLine, IHsGameState gameState, IGame game)
{
if(SendChoicesHeaderRegex.IsMatch(logLine))
{
var match = SendChoicesHeaderRegex.Match(logLine);
var choiceId = int.Parse(match.Groups["id"].Value);
if(!Enum.TryParse(match.Groups["choiceType"].Value, out ChoiceType choiceType))
choiceType = ChoiceType.INVALID;
_sendChoicesHandler.SendChoices(choiceId, choiceType, gameState, game);
}
else if(SendChoicesBodyRegex.IsMatch(logLine))
{
var match = SendChoicesBodyRegex.Match(logLine);
var index = int.Parse(match.Groups["index"].Value);
var entityId = int.Parse(match.Groups["id"].Value);
_sendChoicesHandler.SendChoice(index, entityId, gameState, game);
}
else
{
// Terminate the current Choice.
_sendChoicesHandler.Flush(gameState, game);
}
}
}
}
37 changes: 37 additions & 0 deletions Hearthstone Deck Tracker/LogReader/Handlers/SendChoicesHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.LogReader.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HearthDb.Enums;

namespace Hearthstone_Deck_Tracker.LogReader.Handlers
{
internal class SendChoicesHandler
{
private Choice? Choice;

public void SendChoices(int id, ChoiceType choiceType, IHsGameState gameState, IGame game)
{
Choice = new Choice(id, choiceType);
}

public void SendChoice(int index, int entityId, IHsGameState gameState, IGame game)
{
if(Choice is null) return;

if(game.Entities.TryGetValue(entityId, out var entity))
Choice.AttachChosenEntity(index, entity);
}

public void Flush(IHsGameState gameState, IGame game)
{
if(Choice is null) return;

gameState.GameHandler?.HandlePlayerSendChoices(Choice);
Choice = null;
}
}
}
6 changes: 6 additions & 0 deletions Hearthstone Deck Tracker/LogReader/LogConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ public static class PowerTaskList

public static readonly Regex ShuffleRegex = new Regex(@"SHUFFLE_DECK\ PlayerID=(?<id>(\d+))");
}

public static class Choices
{
public static readonly Regex SendChoicesHeaderRegex = new Regex(@"id=(?<id>(\d+)) ChoiceType=(?<choiceType>(\w+))");
public static readonly Regex SendChoicesBodyRegex = new Regex(@"m_chosenEntities\[(?<index>(\d+))]=.* id=(?<id>(\d+))");
}
}
}
5 changes: 5 additions & 0 deletions Hearthstone Deck Tracker/LogReader/LogWatcherManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Hearthstone_Deck_Tracker.LogReader
public class LogWatcherManager
{
private readonly PowerHandler _powerLineHandler = new PowerHandler();
private readonly ChoicesHandler _choicesHandler = new ChoicesHandler();
private readonly ArenaHandler _arenaHandler = new ArenaHandler();
private readonly LoadingScreenHandler _loadingScreenHandler = new LoadingScreenHandler();
private HsGameState? _gameState;
Expand Down Expand Up @@ -128,7 +129,11 @@ private void OnNewLines(List<LogLine> lines)
break;
case "Power":
if(line.LineContent.StartsWith("GameState."))
{
_game.PowerLog.Add(line.Line);
if(line.LineContent.StartsWith("GameState.SendChoices") || line.LineContent.StartsWith("GameState.DebugPrintEntitiesChosen"))
_choicesHandler.Handle(line.Line, _gameState, _game);
}
else
{
_powerLineHandler.Handle(line.Line, _gameState, _game);
Expand Down

0 comments on commit 1dfa50b

Please sign in to comment.