Add AbilitySystem — core effect execution, trigger hooks, activate_ability handler
- AbilitySystem.cs: full rewrite — ActivateAbility, FireTrigger, FireCardEffects, ExecuteActions, ResolveTargets, ExecuteEffect (§5.1–5.10), condition evaluation, filter evaluation, trigger matching, value/affinity resolution - GameState.cs: CardInstance gains AbilitiesJson, EffectsJson, AbilityUsesThisTurn, Genus; PlayerState gains SpAccumulationBonus, DrawPerTurnBonus and 12 action- restriction flags; GameState gains ActiveEffects list; new ActiveEffect class - GameMessages.cs: GameActivateAbilityRequest/Response + AbilityTarget DTO - GameEngine.cs: ActivateAbility public method; trigger hooks in ChannelCard, MoveUnit, Attack (UnitAttack + UnitDeath), Sacrifice, EndTurn (TurnEnd/TurnStart), Ascend, Transition; AbilitiesJson/EffectsJson copied in DefinitionToInstance, BuildPlayerState, ToFieldCard; Repentance/Instinct/Ruin now fire EffectsJson inline - GameActivateAbilityHandler: game.activate_ability message handler - ServiceRegistration: register GameActivateAbilityHandler Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@ using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using SOR.Database.Entities.Catalog;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Game.Abilities;
|
||||
using SOR.Game.Combat;
|
||||
using SOR.Game.Grid;
|
||||
|
||||
@@ -230,6 +231,21 @@ public static class GameEngine
|
||||
var field = ToFieldCard(card, coord);
|
||||
player.Field.Add(field);
|
||||
board.PlaceUnit(coord, field.InstanceId, actorId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Channeled,
|
||||
ActorId = actorId,
|
||||
CardType = "Spirit",
|
||||
Data = new TriggerEventData { SourceId = field.InstanceId },
|
||||
});
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitEnterBattlefield,
|
||||
ActorId = actorId,
|
||||
UnitType = "Spirit",
|
||||
Data = new TriggerEventData { SourceId = field.InstanceId },
|
||||
});
|
||||
}
|
||||
else if (card.CardType == "Terramorpha")
|
||||
{
|
||||
@@ -264,15 +280,32 @@ public static class GameEngine
|
||||
AffinitySystem.PushAffinity(board.GetTile(t)!, affinity);
|
||||
|
||||
player.Discard.Add(card);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Channeled,
|
||||
ActorId = actorId,
|
||||
CardType = "Terramorpha",
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// Repentance, Instinct, Ruin — pay cost and discard.
|
||||
// Full effect resolution deferred to AbilitySystem (future).
|
||||
// Repentance, Instinct, Ruin — pay cost, fire EffectsJson actions, then discard.
|
||||
player.Hand.Remove(card);
|
||||
player.CurrentSp -= card.SpCost;
|
||||
player.CurrentSt -= card.StCost;
|
||||
|
||||
// Fire inline effects if the card has an EffectsJson action list.
|
||||
AbilitySystem.FireCardEffects(state, actorId, card);
|
||||
|
||||
player.Discard.Add(card);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Channeled,
|
||||
ActorId = actorId,
|
||||
CardType = card.CardType,
|
||||
});
|
||||
}
|
||||
|
||||
return state;
|
||||
@@ -333,6 +366,13 @@ public static class GameEngine
|
||||
player.ArchSpirit = newArchSpirit;
|
||||
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Ascend,
|
||||
ActorId = actorId,
|
||||
Data = new TriggerEventData { SourceId = newArchSpirit.InstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -384,6 +424,13 @@ public static class GameEngine
|
||||
player.ArchSpirit = newArchSpirit;
|
||||
board.PlaceUnit(coord, newArchSpirit.InstanceId, actorId);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.Transition,
|
||||
ActorId = actorId,
|
||||
Data = new TriggerEventData { SourceId = newArchSpirit.InstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -449,6 +496,14 @@ public static class GameEngine
|
||||
unit.Position = targetPos;
|
||||
board.MoveUnit(unitInstanceId, targetPos);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitMove,
|
||||
ActorId = actorId,
|
||||
UnitType = unit.CardType,
|
||||
Data = new TriggerEventData { SourceId = unitInstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -504,10 +559,30 @@ public static class GameEngine
|
||||
var activePlayer = state.Players[actorId];
|
||||
var opponentPlayer = state.Players[opponentId];
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitAttack,
|
||||
ActorId = actorId,
|
||||
UnitType = attacker.CardType,
|
||||
Data = new TriggerEventData
|
||||
{
|
||||
SourceId = attackerInstanceId,
|
||||
TargetId = targetInstanceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (attacker.CurrentHealth <= 0)
|
||||
{
|
||||
if (state.Board is not null) state.Board.RemoveUnit(attacker.InstanceId);
|
||||
MoveToDiscard(activePlayer, attacker);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitDeath,
|
||||
ActorId = actorId,
|
||||
UnitType = attacker.CardType,
|
||||
Data = new TriggerEventData { TargetId = attacker.InstanceId },
|
||||
});
|
||||
}
|
||||
|
||||
if (target.CurrentHealth <= 0)
|
||||
@@ -521,6 +596,14 @@ public static class GameEngine
|
||||
{
|
||||
if (state.Board is not null) state.Board.RemoveUnit(target.InstanceId);
|
||||
MoveToDiscard(opponentPlayer, target);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitDeath,
|
||||
ActorId = opponentId,
|
||||
UnitType = target.CardType,
|
||||
Data = new TriggerEventData { TargetId = target.InstanceId },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -559,6 +642,14 @@ public static class GameEngine
|
||||
player.Field.Remove(unit);
|
||||
player.Discard.Add(unit);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.SpiritSacrificed,
|
||||
ActorId = actorId,
|
||||
UnitType = unit.CardType,
|
||||
Data = new TriggerEventData { SourceId = actorId, TargetId = unitInstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -579,12 +670,24 @@ public static class GameEngine
|
||||
ValidateActivePlayer(state, actorId);
|
||||
ValidatePhase(state, GamePhase.Main);
|
||||
|
||||
// Reset exhausted for the player ending their turn.
|
||||
// Reset exhausted and ability uses for the player ending their turn.
|
||||
var active = state.Players[actorId];
|
||||
foreach (var card in active.Field.OfType<FieldCard>())
|
||||
card.IsExhausted = false;
|
||||
{
|
||||
card.IsExhausted = false;
|
||||
card.AbilityUsesThisTurn = null;
|
||||
}
|
||||
if (active.ArchSpirit is not null)
|
||||
active.ArchSpirit.IsExhausted = false;
|
||||
{
|
||||
active.ArchSpirit.IsExhausted = false;
|
||||
active.ArchSpirit.AbilityUsesThisTurn = null;
|
||||
}
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.TurnEnd,
|
||||
ActorId = actorId,
|
||||
});
|
||||
|
||||
// Switch active player.
|
||||
state.ActivePlayerAccountId = GetOpponentId(state, actorId);
|
||||
@@ -605,12 +708,18 @@ public static class GameEngine
|
||||
if (next.ArchSpirit.MaxDefense.HasValue) next.ArchSpirit.CurrentDefense = next.ArchSpirit.MaxDefense;
|
||||
}
|
||||
|
||||
next.CurrentSp += rules.SpAccumulationPerTurn;
|
||||
next.CurrentSp += rules.SpAccumulationPerTurn + next.SpAccumulationBonus;
|
||||
if (rules.SpHasCap && next.CurrentSp > rules.SpCap)
|
||||
next.CurrentSp = rules.SpCap;
|
||||
|
||||
state.Phase = GamePhase.Draw;
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.TurnStart,
|
||||
ActorId = state.ActivePlayerAccountId,
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -621,6 +730,32 @@ public static class GameEngine
|
||||
return state;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Activate an Active ability on a field unit owned by the active player.
|
||||
/// Validates turn / phase, delegates cost + effect resolution to AbilitySystem.
|
||||
/// </summary>
|
||||
public static GameState ActivateAbility(
|
||||
GameState state,
|
||||
string actorId,
|
||||
string sourceInstanceId,
|
||||
int abilityIndex,
|
||||
IReadOnlyList<AbilityTarget>? targets)
|
||||
{
|
||||
ValidateActivePlayer(state, actorId);
|
||||
ValidatePhase(state, GamePhase.Main);
|
||||
|
||||
state = AbilitySystem.ActivateAbility(state, actorId, sourceInstanceId, abilityIndex, targets);
|
||||
|
||||
AbilitySystem.FireTrigger(state, new TriggerOccurrence
|
||||
{
|
||||
Kind = TriggerEventKind.UnitAbilityActivated,
|
||||
ActorId = actorId,
|
||||
Data = new TriggerEventData { SourceId = sourceInstanceId },
|
||||
});
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
// ── DTO projection ────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
@@ -723,6 +858,8 @@ public static class GameEngine
|
||||
Position = null,
|
||||
Tier = archSpiritDef.Tier,
|
||||
PreviousTierId = archSpiritDef.PreviousTierId,
|
||||
AbilitiesJson = archSpiritDef.AbilitiesJson,
|
||||
EffectsJson = archSpiritDef.EffectsJson,
|
||||
};
|
||||
|
||||
ps.CurrentSp = 0;
|
||||
@@ -760,6 +897,8 @@ public static class GameEngine
|
||||
Tier = def.Tier,
|
||||
PreviousTierId = def.PreviousTierId,
|
||||
TerramorphaMode = def.TerramorphaMode,
|
||||
AbilitiesJson = def.AbilitiesJson,
|
||||
EffectsJson = def.EffectsJson,
|
||||
};
|
||||
|
||||
private static void DrawCards(PlayerState player, int count)
|
||||
@@ -868,6 +1007,8 @@ public static class GameEngine
|
||||
Tier = source.Tier,
|
||||
PreviousTierId = source.PreviousTierId,
|
||||
TerramorphaMode = source.TerramorphaMode,
|
||||
AbilitiesJson = source.AbilitiesJson,
|
||||
EffectsJson = source.EffectsJson,
|
||||
Position = position,
|
||||
};
|
||||
|
||||
|
||||
@@ -67,6 +67,17 @@ public sealed record GameAscendRequest(Guid SessionId, string IncomingCardInstan
|
||||
/// </summary>
|
||||
public sealed record GameTransitionRequest(Guid SessionId, string IncomingCardInstanceId);
|
||||
|
||||
/// <summary>
|
||||
/// Activate an Active ability on one of the player's field units.
|
||||
/// AbilityIndex is 0-based into the source unit's AbilitiesJson array.
|
||||
/// Targets is required when the ability needs player-chosen targets.
|
||||
/// </summary>
|
||||
public sealed record GameActivateAbilityRequest(
|
||||
Guid SessionId,
|
||||
string SourceInstanceId,
|
||||
int AbilityIndex,
|
||||
IReadOnlyList<AbilityTarget>? Targets = null);
|
||||
|
||||
// ── Custom lobby requests ─────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Create a new custom lobby. Host provides their deck and optional rule overrides.</summary>
|
||||
@@ -125,6 +136,8 @@ public sealed record GameAscendResponse(GameStateDto State);
|
||||
|
||||
public sealed record GameTransitionResponse(GameStateDto State);
|
||||
|
||||
public sealed record GameActivateAbilityResponse(GameStateDto State);
|
||||
|
||||
// ── Custom lobby responses ─────────────────────────────────────────────────────
|
||||
|
||||
public sealed record CreateCustomLobbyResponse(
|
||||
@@ -255,3 +268,15 @@ public sealed record CustomRulesDto(
|
||||
int SpCap,
|
||||
int BoardWidth,
|
||||
int BoardHeight);
|
||||
|
||||
/// <summary>
|
||||
/// A single client-provided target for one action in an ability.
|
||||
/// Exactly one of InstanceId or Coord should be set, matching the action's TargetType.
|
||||
/// </summary>
|
||||
public sealed record AbilityTarget(
|
||||
/// <summary>0-based index of the ability action this target satisfies.</summary>
|
||||
int ActionIndex,
|
||||
/// <summary>Set for unit/card targets.</summary>
|
||||
string? InstanceId = null,
|
||||
/// <summary>Set for tile targets.</summary>
|
||||
CoordDto? Coord = null);
|
||||
|
||||
@@ -34,6 +34,12 @@ public sealed class GameState
|
||||
/// Created during GameEngine.Initialize and populated during the Placement phase.
|
||||
/// </summary>
|
||||
public Board? Board { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Duration-tracked effects currently active on units or players.
|
||||
/// Decremented / expired during EndTurn processing.
|
||||
/// </summary>
|
||||
public List<ActiveEffect> ActiveEffects { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class PlayerState
|
||||
@@ -61,6 +67,35 @@ public sealed class PlayerState
|
||||
/// </summary>
|
||||
public int CurrentSt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Per-player SP accumulation bonus added on top of GameRules.SpAccumulationPerTurn.
|
||||
/// Modified by ModifySPAccumulationEffect. Resets when the effect expires.
|
||||
/// </summary>
|
||||
public int SpAccumulationBonus { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Per-player bonus to cards drawn per turn, stacked on top of GameRules.DrawPerTurn.
|
||||
/// Modified by ModifyCardsDrawnPerTurnEffect.
|
||||
/// </summary>
|
||||
public int DrawPerTurnBonus { get; set; }
|
||||
|
||||
// ── Action-restriction flags (set by ability effects) ─────────────────────
|
||||
|
||||
/// <summary>False when an effect prohibits drawing cards this turn.</summary>
|
||||
public bool CanDrawCards { get; set; } = true;
|
||||
/// <summary>False when an effect stops SP accumulating at turn start.</summary>
|
||||
public bool AccumulatesSP { get; set; } = true;
|
||||
public bool CanChannelSpirit { get; set; } = true;
|
||||
public bool CanSacrificeSpirit { get; set; } = true;
|
||||
public bool CanAscendArchSpirit { get; set; } = true;
|
||||
public bool CanTransitionArchSpirit { get; set; } = true;
|
||||
public bool CanChannelRepentance { get; set; } = true;
|
||||
public bool CanChannelInstinct { get; set; } = true;
|
||||
public bool CanChannelRuin { get; set; } = true;
|
||||
public bool CanChannelTerramorphaMono { get; set; } = true;
|
||||
public bool CanChannelTerramorphaDi { get; set; } = true;
|
||||
public bool CanChannelTerramorphaTri { get; set; } = true;
|
||||
|
||||
// ── Leader ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
@@ -151,6 +186,33 @@ public class CardInstance
|
||||
/// requires when channeled. Mirrored from CardDefinition.
|
||||
/// </summary>
|
||||
public TerramorphaMode? TerramorphaMode { get; set; }
|
||||
|
||||
// ── Abilities ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Snapshot of CardDefinition.AbilitiesJson copied at game start.
|
||||
/// Allows GameEngine to evaluate triggered/active abilities without DB access.
|
||||
/// Format: JSON array of AbilityDefinition.
|
||||
/// </summary>
|
||||
public string? AbilitiesJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Snapshot of CardDefinition.EffectsJson (for Repentance/Instinct/Ruin cards).
|
||||
/// Format: JSON array of Action (resolved once when the card is channeled).
|
||||
/// </summary>
|
||||
public string? EffectsJson { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tracks Active ability uses this turn. Key = ability index, value = use count.
|
||||
/// Reset to empty at the start of the owning player's turn (in EndTurn).
|
||||
/// </summary>
|
||||
public Dictionary<int, int>? AbilityUsesThisTurn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Genus tags on this unit (e.g. "Beast", "Plant"). May be modified by abilities.
|
||||
/// Null until at least one genus tag is added.
|
||||
/// </summary>
|
||||
public List<string>? Genus { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -165,3 +227,27 @@ public sealed class FieldCard : CardInstance
|
||||
/// <summary>Remaining movement points this turn.</summary>
|
||||
public int CurrentMp { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A duration-tracked effect currently applied to a unit or player.
|
||||
/// Stored in GameState.ActiveEffects; decremented / expired during EndTurn.
|
||||
/// </summary>
|
||||
public sealed class ActiveEffect
|
||||
{
|
||||
/// <summary>InstanceId of the unit that applied this effect.</summary>
|
||||
public string SourceInstanceId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// InstanceId of the affected unit, or AccountId of the affected player.
|
||||
/// </summary>
|
||||
public string TargetRef { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Serialized effect JSON (round-trips via AbilitySystem JSON options).</summary>
|
||||
public string EffectJson { get; set; } = "{}";
|
||||
|
||||
/// <summary>Duration model as serialized JSON.</summary>
|
||||
public string DurationJson { get; set; } = "{}";
|
||||
|
||||
/// <summary>Remaining turns for ForNTurns durations. Decremented at turn start.</summary>
|
||||
public int TurnsRemaining { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// game.activate_ability — active player fires an Active ability on one of their field units.
|
||||
///
|
||||
/// AbilityIndex is 0-based into the source unit's AbilitiesJson ability array.
|
||||
/// Targets is optional; required when the ability needs player-chosen targets.
|
||||
/// </summary>
|
||||
internal sealed class GameActivateAbilityHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
|
||||
public GameActivateAbilityHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
}
|
||||
|
||||
public string MessageType => "game.activate_ability";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GameActivateAbilityRequest>(message.Payload);
|
||||
var actorId = connection.UserId;
|
||||
|
||||
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
var newState = GameEngine.ActivateAbility(
|
||||
state, actorId, req.SourceInstanceId, req.AbilityIndex, req.Targets);
|
||||
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.activate_ability",
|
||||
JsonSerializer.Serialize(req), ct);
|
||||
|
||||
await PushStateToPlayersAsync(_clientPush, session, newState, ct);
|
||||
|
||||
return ServerMessage.Reply("game.activate_ability.ack", message.Seq,
|
||||
new GameActivateAbilityResponse(GameEngine.ToDto(newState, actorId)));
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ public static class ServiceRegistration
|
||||
services.AddTransient<IMessageHandler, GameSacrificeHandler>();
|
||||
services.AddTransient<IMessageHandler, GameAscendHandler>();
|
||||
services.AddTransient<IMessageHandler, GameTransitionHandler>();
|
||||
services.AddTransient<IMessageHandler, GameActivateAbilityHandler>();
|
||||
services.AddTransient<IMessageHandler, GameEndTurnHandler>();
|
||||
services.AddTransient<IMessageHandler, GameConcedeHandler>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user