Files
sor/Server/src/SOR.Game/Grid/AffinitySystem.cs
T
71947f785e Phase 1: Custom game system — DB schema extensions
- Extend CustomLobby with 12 new rule override columns (Password,
  HandSizeCap, DiscardOnMaxHand, MaxDeckSize, MinDeckSize, MaxArchTier1,
  TurnTimerSeconds, TimeLimitMinutes, TurnLimit, MpBuff, BoardWidth/Height)
- Extend GameRuleSet with matching non-nullable rule fields (same set)
- Extend GameRules record with 9 new rule fields (all default to 0/false)
- Add LobbyPreset entity (owner-scoped named rule presets)
- Register LobbyPreset in GameDbContext with index on OwnerAccountId
- Migration: AddCustomGameExtensions (applied to pg-forge-01)
- Add custom-game-system-plan.md with full 8-phase architecture plan
  and deferred features tracking list
- Also includes: AbilityBuilder ValueExpr dropdowns + help panel,
  Affinity enum/system fixes (from prior sessions)

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>
2026-03-18 22:42:59 -04:00

108 lines
5.5 KiB
C#

namespace SOR.Game.Grid;
using SOR.Database.Enums;
/// <summary>
/// Affinity resolution, push operations, and channeling validity.
///
/// Combination rules (see affinity.md §4):
/// Basic affinities: Void, Aether, Lake, Hill, Forest, Field.
/// Non-basic affinities: determined by (Base1, Base2) pair.
/// Gateway affinities are directional — require Base2 == Void.
/// Gateway/UnstableAether are tile-only and cannot appear on cards.
/// </summary>
public static class AffinitySystem
{
private static readonly HashSet<Affinity> BasicAffinities =
[
Affinity.Void, Affinity.Aether,
Affinity.Lake, Affinity.Hill, Affinity.Forest, Affinity.Field,
];
/// <summary>
/// Resolve the effective affinity of a tile from its two base slots.
/// Base1 = older slot, Base2 = newer slot.
/// </summary>
public static Affinity Resolve(Affinity base1, Affinity base2)
=> (base1, base2) switch
{
// ── Void + Void ───────────────────────────────────────────────────
(Affinity.Void, Affinity.Void) => Affinity.PureVoid,
// ── Void + basic → tile reads as that basic affinity (§8) ─────────
(Affinity.Void, Affinity.Lake) => Affinity.Lake,
(Affinity.Void, Affinity.Hill) => Affinity.Hill,
(Affinity.Void, Affinity.Forest) => Affinity.Forest,
(Affinity.Void, Affinity.Field) => Affinity.Field,
(Affinity.Void, Affinity.Aether) => Affinity.UnstableAether,
// ── Gateway: basic + Void (directional, Base2 must be Void) ──────
(Affinity.Lake, Affinity.Void) => Affinity.GatewayLake,
(Affinity.Hill, Affinity.Void) => Affinity.GatewayHill,
(Affinity.Forest, Affinity.Void) => Affinity.GatewayForest,
(Affinity.Field, Affinity.Void) => Affinity.GatewayField,
// ── Aether combinations ──────────────────────────────────────────
(Affinity.Aether, Affinity.Aether) => Affinity.PerfectAether,
(Affinity.Aether, _) => Affinity.UnstableAether,
(_, Affinity.Aether) => Affinity.UnstableAether,
// ── Lake pairings ─────────────────────────────────────────────────
(Affinity.Lake, Affinity.Lake) => Affinity.Ocean,
(Affinity.Lake, Affinity.Hill) or (Affinity.Hill, Affinity.Lake) => Affinity.Island,
(Affinity.Lake, Affinity.Field) or (Affinity.Field, Affinity.Lake) => Affinity.Marsh,
(Affinity.Lake, Affinity.Forest) or (Affinity.Forest, Affinity.Lake) => Affinity.Swamp,
// ── Hill pairings ─────────────────────────────────────────────────
(Affinity.Hill, Affinity.Hill) => Affinity.Mountain,
(Affinity.Hill, Affinity.Field) or (Affinity.Field, Affinity.Hill) => Affinity.Steppe,
(Affinity.Hill, Affinity.Forest) or (Affinity.Forest, Affinity.Hill) => Affinity.Fort,
// ── Field pairings ────────────────────────────────────────────────
(Affinity.Field, Affinity.Field) => Affinity.Plains,
(Affinity.Field, Affinity.Forest) or (Affinity.Forest, Affinity.Field) => Affinity.Grove,
// ── Forest pairings ───────────────────────────────────────────────
(Affinity.Forest, Affinity.Forest) => Affinity.Jungle,
// ── Fallback (should not occur in a valid game state) ─────────────
_ => Affinity.PureVoid,
};
/// <summary>
/// Push a new basic affinity onto a tile: Base1 = old Base2, Base2 = newAffinity.
/// </summary>
public static void PushAffinity(Tile tile, Affinity newAffinity)
{
tile.Base1 = tile.Base2;
tile.Base2 = newAffinity;
}
/// <summary>
/// Apply ArchSpirit placement affinity push (§3):
/// pushes the affinity to the placement tile AND all in-bounds orthogonal neighbors.
/// </summary>
public static void ApplyPlacementAffinity(Board board, Coord placementCoord, Affinity affinity)
{
var tile = board.GetTile(placementCoord);
if (tile is not null) PushAffinity(tile, affinity);
foreach (var adjacent in board.GetAdjacentTiles(placementCoord))
PushAffinity(adjacent, affinity);
}
/// <summary>
/// Validate whether a card with the given affinity may be channeled to a tile (§6.3).
///
/// Basic affinity card: tile.Base1 or tile.Base2 must match the card's affinity.
/// Non-basic affinity card: the resolved tile affinity must match exactly.
/// </summary>
public static bool IsAffinityValid(Tile tile, Affinity cardAffinity)
{
if (BasicAffinities.Contains(cardAffinity))
return tile.Base1 == cardAffinity || tile.Base2 == cardAffinity;
return Resolve(tile.Base1, tile.Base2) == cardAffinity;
}
}