namespace SOR.Game; using System.Text.Json.Serialization; using SOR.Database.Enums; using SOR.Game.Grid; /// /// Full live game state — serialized as JSONB in GameSession.StateJson. /// The GameEngine owns all mutations; handlers call into it exclusively. /// public sealed class GameState { public Guid SessionId { get; set; } public GamePhase Phase { get; set; } = GamePhase.Setup; public int TurnNumber { get; set; } = 1; /// AccountId (as string) of the player whose turn it currently is. public string ActivePlayerAccountId { get; set; } = string.Empty; /// AccountId (as string) of the first player (P1). Determines placement row. public string PlayerOneAccountId { get; set; } = string.Empty; /// Per-player state, keyed by AccountId.ToString(). public Dictionary Players { get; set; } = new(); /// Running sequence number for action log / StateVersion. public int NextActionSequence { get; set; } = 1; /// Rules snapshot applied to this game session. public GameRules Rules { get; set; } = GameRules.Default; /// /// The battlefield grid. Null only before the game session is initialized. /// Created during GameEngine.Initialize and populated during the Placement phase. /// public Board? Board { get; set; } /// /// Duration-tracked effects currently active on units or players. /// Decremented / expired during EndTurn processing. /// public List ActiveEffects { get; set; } = new(); /// /// Deferred effects (from ApplyOnEventEffect) waiting to fire when their trigger occurs. /// Consumed on firing; cancelled when source unit leaves the battlefield. /// public List PendingDeferredEffects { get; set; } = new(); } public sealed class PlayerState { public string AccountId { get; set; } = string.Empty; // ── Zones ──────────────────────────────────────────────────────────────── public List Hand { get; set; } = new(); public List Deck { get; set; } = new(); public List Field { get; set; } = new(); public List Discard { get; set; } = new(); // ── Resources ──────────────────────────────────────────────────────────── /// /// Spirit Points — starts at 0, gains SpAccumulationPerTurn each turn. /// Accumulates indefinitely; never resets between turns. /// public int CurrentSp { get; set; } /// /// Sacrifice Tokens — earned by sacrificing Spirits (§6.15). /// Starts at 0; never granted per turn. /// public int CurrentSt { get; set; } /// /// Per-player SP accumulation bonus added on top of GameRules.SpAccumulationPerTurn. /// Modified by ModifySPAccumulationEffect. Resets when the effect expires. /// public int SpAccumulationBonus { get; set; } /// /// Per-player bonus to cards drawn per turn, stacked on top of GameRules.DrawPerTurn. /// Modified by ModifyCardsDrawnPerTurnEffect. /// public int DrawPerTurnBonus { get; set; } // ── Action-restriction flags (set by ability effects) ───────────────────── /// False when an effect prohibits drawing cards this turn. public bool CanDrawCards { get; set; } = true; /// False when an effect stops SP accumulating at turn start. 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 ─────────────────────────────────────────────────────────────── /// /// The player's ArchSpirit — placed during the Placement phase. /// If destroyed the game ends (this player loses). /// public FieldCard? ArchSpirit { get; set; } } /// /// A card instance in any zone. Carries mutable combat and positional state. /// /// [JsonPolymorphic] is required so that FieldCard sub-type properties /// survive the GameState JSONB serialize/deserialize round-trip. /// [JsonPolymorphic(TypeDiscriminatorPropertyName = "$t")] [JsonDerivedType(typeof(CardInstance), "c")] [JsonDerivedType(typeof(FieldCard), "f")] public class CardInstance { /// Unique instance ID within the game session (not a DB id). public string InstanceId { get; set; } = Guid.NewGuid().ToString("N")[..8]; // ── Definition mirror ───────────────────────────────────────────────────── public Guid CardDefinitionId { get; set; } public string Name { get; set; } = string.Empty; public string CardType { get; set; } = string.Empty; public string Affinity { get; set; } = string.Empty; public string Rarity { get; set; } = string.Empty; public int SpCost { get; set; } public int StCost { get; set; } // ── Combat stats ────────────────────────────────────────────────────────── public int? MaxHealth { get; set; } public int? CurrentHealth { get; set; } public int? MaxDefense { get; set; } public int? CurrentDefense { get; set; } public int? Damage { get; set; } // ── Movement ────────────────────────────────────────────────────────────── public MovementType? MovementType { get; set; } public int? MaxMp { get; set; } public bool CanMove { get; set; } // ── Attack ──────────────────────────────────────────────────────────────── public int? MinAttackRange { get; set; } public int? MaxAttackRange { get; set; } public bool RequiresLos { get; set; } // ── Channeling ──────────────────────────────────────────────────────────── public int? MinChannelRange { get; set; } public int? MaxChannelRange { get; set; } // ── State flags ─────────────────────────────────────────────────────────── /// True if this unit has already attacked or used an ability this turn. public bool IsExhausted { get; set; } /// True for the player's ArchSpirit (the win-condition target). public bool IsLeader { get; set; } public bool CanAttack { get; set; } public bool CanDefend { get; set; } // ── Ascend / Transition ─────────────────────────────────────────────────── /// /// For ArchSpirit cards: tier number (1 = base). Mirrored from CardDefinition /// to enable ascension chain validation in the engine without a DB round-trip. /// public int? Tier { get; set; } /// /// For Tier 2+ ArchSpirits: the CardDefinitionId of the Tier (N-1) predecessor. /// Null for Tier 1 and all non-ArchSpirit cards. /// public Guid? PreviousTierId { get; set; } // ── Terramorpha ──────────────────────────────────────────────────────────── /// /// For Terramorpha cards only: how many tiles (and adjacency chain) this card /// requires when channeled. Mirrored from CardDefinition. /// public TerramorphaMode? TerramorphaMode { get; set; } // ── Abilities ───────────────────────────────────────────────────────────── /// /// Snapshot of CardDefinition.AbilitiesJson copied at game start. /// Allows GameEngine to evaluate triggered/active abilities without DB access. /// Format: JSON array of AbilityDefinition. /// public string? AbilitiesJson { get; set; } /// /// Snapshot of CardDefinition.EffectsJson (for Repentance/Instinct/Ruin cards). /// Format: JSON array of Action (resolved once when the card is channeled). /// public string? EffectsJson { get; set; } /// /// 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). /// public Dictionary? AbilityUsesThisTurn { get; set; } /// /// Genus tags on this unit (e.g. "Beast", "Plant"). May be modified by abilities. /// Null until at least one genus tag is added. /// public List? Genus { get; set; } // ── Unit restriction flags (set by ability effects) ─────────────────────── /// False when an effect prevents this unit from being targeted by attacks. public bool CanBeAttacked { get; set; } = true; /// False when an effect prevents this unit from being blocked in combat. public bool CanBeBlocked { get; set; } = true; /// False when an effect prevents this unit from being path-obstructed. public bool CanBeObstructed { get; set; } = true; /// False when an effect prevents this unit from using abilities. public bool CanUseAbility { get; set; } = true; /// False when this unit cannot be sacrificed. public bool CanBeSacrificed { get; set; } = true; // ── Extra attacks ───────────────────────────────────────────────────────── /// /// Additional attacks this unit may declare this turn before becoming exhausted. /// Granted by GrantAdditionalAttackEffect. Decremented on each extra attack; reset at turn start. /// public int ExtraAttacksRemaining { get; set; } // ── Sacrifice override ──────────────────────────────────────────────────── /// /// When non-null and ≥ 0, overrides the normal ST sacrifice formula for this unit. /// Null = use normal formula. -1 = sentinel "no override" for reversal tracking. /// public int? FixedSacrificeReturn { get; set; } // ── Disabled abilities ──────────────────────────────────────────────────── /// /// Ability names that are temporarily disabled on this unit. /// Null until at least one ability is disabled. /// public List? DisabledAbilities { get; set; } // ── Reveal ──────────────────────────────────────────────────────────────── /// True if this card has been revealed to all players. public bool IsRevealed { get; set; } } /// /// A CardInstance that is on the field (placed by ChannelCard or PlaceArchSpirit). /// Carries positional state (tile coordinate) and remaining movement points. /// public sealed class FieldCard : CardInstance { /// Current tile position on the board. Null until placed. public Coord? Position { get; set; } /// Remaining movement points this turn. public int CurrentMp { get; set; } } /// /// A deferred effect (from ApplyOnEventEffect) scheduled to fire once when its trigger event occurs. /// Consumed after firing; cancelled if the source unit leaves the battlefield before the event fires. /// public sealed class PendingDeferredEffect { /// InstanceId of the unit that created this deferred effect. public string SourceInstanceId { get; set; } = string.Empty; /// AccountId of the player who owns the source unit. public string OwnerAccountId { get; set; } = string.Empty; /// Serialized TriggerEvent JSON — the event to watch for. public string TriggerJson { get; set; } = "{}"; /// InstanceId (card) or AccountId (player) of the entity to apply effects to. public string TargetRef { get; set; } = string.Empty; /// Serialized Effect[] JSON — effects applied to TargetRef when the trigger fires. public string EffectsJson { get; set; } = "[]"; } /// /// A duration-tracked effect currently applied to a unit or player. /// Stored in GameState.ActiveEffects; decremented / expired during EndTurn. /// public sealed class ActiveEffect { /// InstanceId of the unit that applied this effect. public string SourceInstanceId { get; set; } = string.Empty; /// /// InstanceId of the affected unit, or AccountId of the affected player. /// public string TargetRef { get; set; } = string.Empty; /// Serialized effect JSON (round-trips via AbilitySystem JSON options). public string EffectJson { get; set; } = "{}"; /// Duration model as serialized JSON. public string DurationJson { get; set; } = "{}"; /// Remaining turns for ForNTurns durations. Decremented at turn start. public int TurnsRemaining { get; set; } }