Skip to content

Commit aa7cb39

Browse files
committed
Send neuro context when attacks are happening
1 parent 8afaca6 commit aa7cb39

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

NeuroInscryption/Automation/Game/Combat/TraderBattleTradeFlow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace NeuroInscryption.Automation.Game.Combat;
1919

20-
public class TraderBattleTradeFlow : HarmonyAutomationFlow<IEnumerator>
20+
public sealed class TraderBattleTradeFlow : HarmonyAutomationFlow<IEnumerator>
2121
{
2222
public override FlowHook[] Hooks =>
2323
[
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System.Collections;
2+
using System.Text;
3+
using DiskCardGame;
4+
using HarmonyLib;
5+
using NeuroInscryption.Communication.Data.State;
6+
using NeuroInscryption.Utilities.Enumerators;
7+
using NeuroSdk.Messages.Outgoing;
8+
using UnityEngine;
9+
10+
namespace NeuroInscryption.Communication.ContextMessages;
11+
12+
[HarmonyPatch]
13+
file static class Attacks
14+
{
15+
private static readonly StringBuilder _contextBuilder = new();
16+
private static CardSlot? _lastSlot;
17+
18+
[HarmonyPatch(typeof(TurnManager), nameof(TurnManager.PlayerTurn), MethodType.Enumerator)]
19+
[HarmonyPrefix]
20+
public static void CapturePlayerTurn(IEnumerator __instance)
21+
{
22+
if (__instance.GetState() != 10) return;
23+
24+
_contextBuilder.Clear();
25+
_contextBuilder.Append("Combat initiated. ");
26+
}
27+
28+
[HarmonyPatch(typeof(TurnManager), nameof(TurnManager.OpponentTurn), MethodType.Enumerator)]
29+
[HarmonyPrefix]
30+
public static void CaptureOpponentTurn(IEnumerator __instance, bool __result)
31+
{
32+
if (__instance.GetState() != 2) return;
33+
34+
_contextBuilder.Append("It is your opponent's turn. ");
35+
}
36+
37+
[HarmonyPatch(typeof(TurnManager), nameof(TurnManager.OpponentTurn), MethodType.Enumerator)]
38+
[HarmonyPostfix]
39+
public static void SendContext(IEnumerator __instance)
40+
{
41+
if (__instance.GetState() is not (6 or 7)) return;
42+
43+
Context.Send(_contextBuilder.ToString(), true);
44+
}
45+
46+
[HarmonyPatch(typeof(PlayableCard), nameof(PlayableCard.QueuedSlot), MethodType.Setter)]
47+
[HarmonyPrefix]
48+
public static void CapturePlayingSlot(PlayableCard __instance, [HarmonyArgument(0)] CardSlot value)
49+
{
50+
if (__instance.QueuedSlot != null && value == null)
51+
{
52+
_lastSlot = __instance.QueuedSlot;
53+
}
54+
}
55+
56+
[HarmonyPatch(typeof(PlayableCard), nameof(PlayableCard.OnPlayedFromOpponentQueue))]
57+
[HarmonyPrefix]
58+
public static void CaptureCardPlayed(PlayableCard __instance)
59+
{
60+
if (_lastSlot == null)
61+
{
62+
Debug.LogError("Last slot not assigned when card captured, this should never happen");
63+
_contextBuilder.Append($"Their {__instance.Info.DisplayedNameEnglish} was played in a slot. ");
64+
}
65+
else
66+
{
67+
_contextBuilder.Append($"Their {__instance.Info.DisplayedNameEnglish} was played in slot {_lastSlot.Index + CombatState.LaneIndexBase}. ");
68+
}
69+
}
70+
71+
[HarmonyPatch(typeof(PlayableCard), nameof(PlayableCard.AttackIsBlocked))]
72+
[HarmonyPostfix]
73+
public static void CaptureAttackBlock(PlayableCard __instance, CardSlot opposingSlot, bool __result)
74+
{
75+
if (__result)
76+
{
77+
_contextBuilder.Append($"{P(__instance)} {__instance.Info.DisplayedNameEnglish} could not attack {Pl(opposingSlot.Card)} {opposingSlot.Card.Info.DisplayedNameEnglish}! ");
78+
}
79+
}
80+
81+
[HarmonyPatch(typeof(CombatPhaseManager3D), nameof(CombatPhaseManager3D.VisualizeCardAttackingDirectly), MethodType.Enumerator)]
82+
[HarmonyPrefix]
83+
public static void CaptureOpponentDamage(IEnumerator __instance)
84+
{
85+
CardSlot attackingSlot = __instance.GetFieldValue<CardSlot>("attackingSlot");
86+
CardSlot targetSlot = __instance.GetFieldValue<CardSlot>("targetSlot");
87+
int damage = __instance.GetFieldValue<int>("damage");
88+
89+
_contextBuilder.Append($"{P(attackingSlot.Card)} {attackingSlot.Card.Info.DisplayedNameEnglish} attacked {D(targetSlot)} directly for {damage} damage. ");
90+
}
91+
92+
[HarmonyPatch(typeof(PlayableCard), nameof(PlayableCard.TakeDamage))]
93+
[HarmonyPrefix]
94+
public static void CaptureAttack(PlayableCard __instance, int damage, PlayableCard attacker)
95+
{
96+
_contextBuilder.Append($"{P(attacker)} {attacker.Info.DisplayedNameEnglish} dealt {damage} damage to {Pl(__instance)} {__instance.Info.DisplayedNameEnglish}. ");
97+
}
98+
99+
private static string P(PlayableCard card) => card.OpponentCard ? "Their" : "Your";
100+
private static string Pl(PlayableCard card) => P(card).ToLowerInvariant();
101+
private static string D(CardSlot slot) => !slot.IsPlayerSlot ? "the opponent" : "you";
102+
}

0 commit comments

Comments
 (0)