From a46f251ef2070af2a0a10aac802c7364ec041129 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 1 Apr 2026 22:36:57 -0400 Subject: [PATCH] Separate polygon sides from board radius for hex templates; fix template IsInBounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - HexSides now stores radius+1 (for hex coordinate math); HexPolygonSides stores the corner count (3–10) - Board.IsTemplateBased: template boards use tile existence for IsInBounds/GetNeighbors instead of the hexagonal formula, so N-gon and other custom shapes work correctly - HexGrid.AllNeighbors: unfiltered 6-neighbor generator for template board use - ApplyHexConfig: generates a regular N-gon on the hex canvas — tiles outside the polygon are marked absent; N corner zones auto-generated for each player count 2..N - Admin builder UI: separate "Polygon Sides" (3–10 corners) and "Radius (tiles)" inputs - BoardTemplate entity and migration: HexPolygonSides column added to board_templates - BoardTemplateDto exposes HexPolygonSides to client Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .claude/worktrees/agent-ac72c5f4 | 1 + .../Pages/Game/BoardTemplateBuilderPage.razor | 211 +++++-- Server/src/SOR.Content/ContentMessages.cs | 5 +- .../Handlers/ContentBoardTemplatesHandler.cs | 3 +- .../Entities/Game/BoardTemplate.cs | 7 +- ...ddBoardTemplateHexPolygonSides.Designer.cs | 541 ++++++++++++++++++ ...2023622_AddBoardTemplateHexPolygonSides.cs | 31 + .../Game/GameDbContextModelSnapshot.cs | 3 + Server/src/SOR.Game/Grid/Board.cs | 35 +- .../src/SOR.Game/Grid/BoardShapeGenerator.cs | 11 +- Server/src/SOR.Game/Grid/HexGrid.cs | 12 + 11 files changed, 794 insertions(+), 66 deletions(-) create mode 160000 .claude/worktrees/agent-ac72c5f4 create mode 100644 Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.Designer.cs create mode 100644 Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.cs diff --git a/.claude/worktrees/agent-ac72c5f4 b/.claude/worktrees/agent-ac72c5f4 new file mode 160000 index 0000000..c756b9c --- /dev/null +++ b/.claude/worktrees/agent-ac72c5f4 @@ -0,0 +1 @@ +Subproject commit c756b9cffa5a357b6d35af523562b02ec6948760 diff --git a/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor b/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor index d68a610..ffa215e 100644 --- a/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor +++ b/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor @@ -102,13 +102,19 @@ else @if (_template.GridType == "Hexagon") {
- + -
Diameter = 2N−1. Tiles: 3N²−3N+1.
+ @bind="_pendingHexPolySides" /> +
Number of corners / player slots. 3 = triangle, 6 = hexagon, 10 = decagon.
- } else @@ -260,8 +266,9 @@ else
@if (_template.GridType == "Hexagon") { - int _hn = _template.HexSides > 0 ? _template.HexSides : _pendingHexSides; - Canvas N=@_hn hex (@(2*_hn-1)×@(2*_hn-1)) + int _hpoly = _template.HexPolygonSides > 0 ? _template.HexPolygonSides : _pendingHexPolySides; + int _hradius = _template.HexSides > 1 ? _template.HexSides - 1 : _pendingHexRadius; + Canvas @_hpoly-gon, R=@_hradius (@(2*_hradius+1)×@(2*_hradius+1)) } else { @@ -276,7 +283,8 @@ else { const double HexR = 16.0; const double HexPad = 12.0; - int hexN = _template.HexSides > 0 ? _template.HexSides : Math.Max(3, _pendingHexSides); + // hexN = radius + 1 (for HexGrid coordinate math) + int hexN = _template.HexSides > 0 ? _template.HexSides : (_pendingHexRadius + 1); int hexDim = 2 * hexN - 1; double svgW = HexR * Math.Sqrt(3) * (hexDim - 1) + 2 * HexR + 2 * HexPad; double svgH = HexR * 1.5 * (hexDim - 1) + 2 * HexR + 2 * HexPad; @@ -393,9 +401,10 @@ else // ── Template + in-memory state ──────────────────────────────────────────── private BoardTemplate _template = new(); - private int _pendingWidth = 8; - private int _pendingHeight = 8; - private int _pendingHexSides = 5; + private int _pendingWidth = 8; + private int _pendingHeight = 8; + private int _pendingHexPolySides = 6; // polygon corner count (3–10) + private int _pendingHexRadius = 5; // board radius in tiles (2–15) // Only non-default tiles stored; key = "x,y" private Dictionary _tiles = new(); @@ -439,10 +448,11 @@ else if (_isNew) { - _template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow }; - _pendingWidth = _template.Width; - _pendingHeight = _template.Height; - _pendingHexSides = 5; + _template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow }; + _pendingWidth = _template.Width; + _pendingHeight = _template.Height; + _pendingHexPolySides = 6; + _pendingHexRadius = 5; } else { @@ -453,10 +463,11 @@ else _loading = false; return; } - _template = t; - _pendingWidth = t.Width; - _pendingHeight = t.Height; - _pendingHexSides = t.HexSides > 0 ? t.HexSides : 5; + _template = t; + _pendingWidth = t.Width; + _pendingHeight = t.Height; + _pendingHexPolySides = t.HexPolygonSides > 0 ? t.HexPolygonSides : 6; + _pendingHexRadius = t.HexSides > 1 ? t.HexSides - 1 : 5; DeserializeTiles(t.TilesJson); DeserializeZones(t.StartingZonesJson); } @@ -525,27 +536,87 @@ else }); } - private void ApplyHexSides() + /// + /// Generate a regular N-gon of hex tiles and N corner starting zones. + /// HexSides is stored as radius+1 (for HexGrid coordinate math). + /// HexPolygonSides is stored as N (the corner count / polygon shape). + /// + private void ApplyHexConfig() { - var n = Math.Clamp(_pendingHexSides, 3, 10); - _pendingHexSides = n; - _template.HexSides = n; - int dim = 2 * n - 1; - _template.Width = dim; - _template.Height = dim; + int N = Math.Clamp(_pendingHexPolySides, 3, 10); + int R = Math.Clamp(_pendingHexRadius, 2, 15); + int sides = R + 1; // HexGrid "sides" parameter = radius + 1 + int dim = 2 * R + 1; - var toRemoveTiles = _tiles.Keys - .Where(k => { var p = k.Split(','); return !IsHexInBounds(int.Parse(p[0]), int.Parse(p[1]), n); }) - .ToList(); - foreach (var k in toRemoveTiles) _tiles.Remove(k); + _pendingHexPolySides = N; + _pendingHexRadius = R; + _template.HexSides = sides; + _template.HexPolygonSides = N; + _template.Width = dim; + _template.Height = dim; - foreach (var seatMap in _zones.Values) - foreach (var tileSet in seatMap.Values) - tileSet.RemoveWhere(k => + _tiles.Clear(); + _zones.Clear(); + + // Mark every tile that is inside the hexagonal arrangement but outside the N-gon as absent. + // Tiles inside the N-gon are left as default (no entry = void tile = present). + for (int x = 0; x < dim; x++) + for (int y = 0; y < dim; y++) + { + int q = x - R, r = y - R; + if (Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) > R) + continue; // outside hex bounding circle — not rendered + + double cx = q + r * 0.5; + double cy = r * Math.Sqrt(3.0) / 2.0; + if (!NPolyInside(cx, cy, N, R)) + { + var tile = GetOrCreateTile(x, y); + tile.IsAbsent = true; + } + } + + // N vertex positions (scaled hex Cartesian) + var verts = new (double vx, double vy)[N]; + for (int k = 0; k < N; k++) + { + double angle = Math.PI / 2.0 + 2.0 * Math.PI * k / N; + verts[k] = (R * Math.Cos(angle), R * Math.Sin(angle)); + } + + // For each player count 2..N, pick evenly-spaced corners. + int half = Math.Max(1, R / 4); + for (int playerCount = 2; playerCount <= N; playerCount++) + { + var seatMap = new Dictionary>(); + for (int seat = 0; seat < playerCount; seat++) + { + int ci = (int)Math.Round((double)seat * N / playerCount) % N; + var (vx, vy) = verts[ci]; + var zone = new HashSet(); + + // Corner tile + var corner = FindNearestNgonTile(vx, vy, R); + zone.Add(TileKey(corner.x, corner.y)); + + // Edge tiles toward adjacent vertices + for (int step = 1; step <= half; step++) { - var p = k.Split(','); - return !IsHexInBounds(int.Parse(p[0]), int.Parse(p[1]), n); - }); + double t = (double)step / (half + 1); + int nextCi = (ci + 1) % N; + int prevCi = (ci - 1 + N) % N; + var n1 = FindNearestNgonTile(vx + (verts[nextCi].vx - vx) * t, + vy + (verts[nextCi].vy - vy) * t, R); + var n2 = FindNearestNgonTile(vx + (verts[prevCi].vx - vx) * t, + vy + (verts[prevCi].vy - vy) * t, R); + zone.Add(TileKey(n1.x, n1.y)); + zone.Add(TileKey(n2.x, n2.y)); + } + + seatMap[seat] = zone; + } + _zones[playerCount] = seatMap; + } } private void OnGridTypeChanged() @@ -554,27 +625,71 @@ else _zones.Clear(); if (_template.GridType == "Hexagon") { - _template.HexSides = _pendingHexSides; - int dim = 2 * _pendingHexSides - 1; - _template.Width = dim; - _template.Height = dim; + _template.HexSides = _pendingHexRadius + 1; + _template.HexPolygonSides = _pendingHexPolySides; + int dim = 2 * _pendingHexRadius + 1; + _template.Width = dim; + _template.Height = dim; } else { - _template.HexSides = 0; - _template.Width = _pendingWidth; - _template.Height = _pendingHeight; + _template.HexSides = 0; + _template.HexPolygonSides = 6; + _template.Width = _pendingWidth; + _template.Height = _pendingHeight; } } // ── Hex geometry helpers ────────────────────────────────────────────────── - private static bool IsHexInBounds(int x, int y, int N) + /// + /// True when offset coordinate (x,y) is within the hex grid of radius R=sides−1. + /// Used for the SVG canvas to skip cells outside the bounding circle. + /// + private static bool IsHexInBounds(int x, int y, int sides) { - int q = x - (N - 1); - int r = y - (N - 1); - int s = -q - r; - return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(s))) <= N - 1; + int R = sides - 1; + int q = x - R, r = y - R; + return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) <= R; + } + + /// + /// True when scaled hex-Cartesian point (cx,cy) is inside a regular N-gon with + /// circumradius R. Uses cross-product test on each CCW edge (slightly expanded to + /// include boundary tiles). + /// + private static bool NPolyInside(double cx, double cy, int N, double R) + { + double Rexp = R * 1.02; // slight expansion to include perimeter tiles + for (int k = 0; k < N; k++) + { + double a1 = Math.PI / 2.0 + 2.0 * Math.PI * k / N; + double a2 = Math.PI / 2.0 + 2.0 * Math.PI * (k + 1) / N; + double v1x = Rexp * Math.Cos(a1), v1y = Rexp * Math.Sin(a1); + double v2x = Rexp * Math.Cos(a2), v2y = Rexp * Math.Sin(a2); + double cross = (v2x - v1x) * (cy - v1y) - (v2y - v1y) * (cx - v1x); + if (cross < -1e-9) return false; + } + return true; + } + + /// Find the nearest non-absent hex tile (in offset coords) to scaled-Cartesian (vx,vy). + private (int x, int y) FindNearestNgonTile(double vx, double vy, int R) + { + (int x, int y) best = (R, R); + double bestDist = double.MaxValue; + for (int q = -R; q <= R; q++) + for (int r = -R; r <= R; r++) + { + if (Math.Abs(q + r) > R) continue; + int ox = q + R, oy = r + R; + if (_tiles.TryGetValue(TileKey(ox, oy), out var td) && td.IsAbsent) continue; + double tcx = q + r * 0.5; + double tcy = r * Math.Sqrt(3.0) / 2.0; + double d = (tcx - vx) * (tcx - vx) + (tcy - vy) * (tcy - vy); + if (d < bestDist) { bestDist = d; best = (ox, oy); } + } + return best; } private static double HexCellX(int x, int y, int N, double R, double pad) diff --git a/Server/src/SOR.Content/ContentMessages.cs b/Server/src/SOR.Content/ContentMessages.cs index 5827386..2ff9e09 100644 --- a/Server/src/SOR.Content/ContentMessages.cs +++ b/Server/src/SOR.Content/ContentMessages.cs @@ -209,8 +209,9 @@ public sealed record BoardTemplateDto( bool IsAvailableForCustom, string TilesJson, string StartingZonesJson, - string GridType = "Square", - int HexSides = 0); + string GridType = "Square", + int HexSides = 0, + int HexPolygonSides = 6); // ── BGM config ─────────────────────────────────────────────────────────────── diff --git a/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs b/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs index 9e2f773..b2f187e 100644 --- a/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs +++ b/Server/src/SOR.Content/Handlers/ContentBoardTemplatesHandler.cs @@ -42,7 +42,8 @@ internal sealed class ContentBoardTemplatesHandler : IMessageHandler t.TilesJson, t.StartingZonesJson, t.GridType, - t.HexSides)) + t.HexSides, + t.HexPolygonSides)) .ToListAsync(ct); return ServerMessage.Reply( diff --git a/Server/src/SOR.Database/Entities/Game/BoardTemplate.cs b/Server/src/SOR.Database/Entities/Game/BoardTemplate.cs index 4dbc996..1f829bc 100644 --- a/Server/src/SOR.Database/Entities/Game/BoardTemplate.cs +++ b/Server/src/SOR.Database/Entities/Game/BoardTemplate.cs @@ -32,8 +32,11 @@ public sealed class BoardTemplate public int MinPlayers { get; set; } = 2; public int MaxPlayers { get; set; } = 2; - public string GridType { get; set; } = "Square"; - public int HexSides { get; set; } = 0; + public string GridType { get; set; } = "Square"; + /// Radius + 1: used for hex coordinate math. 0 = square grid. + public int HexSides { get; set; } = 0; + /// Number of polygon sides/corners (3–10). 6 = regular hexagon. Only meaningful when GridType = "Hexagon". + public int HexPolygonSides { get; set; } = 6; public DateTimeOffset CreatedAt { get; set; } public DateTimeOffset UpdatedAt { get; set; } diff --git a/Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.Designer.cs b/Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.Designer.cs new file mode 100644 index 0000000..b1ccffa --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.Designer.cs @@ -0,0 +1,541 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using SOR.Database.Contexts; + +#nullable disable + +namespace SOR.Database.Migrations.Game +{ + [DbContext(typeof(GameDbContext))] + [Migration("20260402023622_AddBoardTemplateHexPolygonSides")] + partial class AddBoardTemplateHexPolygonSides + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("game") + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("SOR.Database.Entities.Game.BoardTemplate", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("GridType") + .IsRequired() + .HasColumnType("text"); + + b.Property("Height") + .HasColumnType("integer"); + + b.Property("HexPolygonSides") + .HasColumnType("integer"); + + b.Property("HexSides") + .HasColumnType("integer"); + + b.Property("IsAvailableForCustom") + .HasColumnType("boolean"); + + b.Property("IsAvailableForRanked") + .HasColumnType("boolean"); + + b.Property("IsEnabled") + .HasColumnType("boolean"); + + b.Property("MaxPlayers") + .HasColumnType("integer"); + + b.Property("MinPlayers") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("StartingZonesJson") + .IsRequired() + .HasColumnType("jsonb"); + + b.Property("TilesJson") + .IsRequired() + .HasColumnType("jsonb"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Width") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("IsEnabled"); + + b.ToTable("board_templates", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.CustomLobby", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AdditionalPlayersJson") + .HasColumnType("text") + .HasColumnName("additional_players_json"); + + b.Property("BoardHeightOverride") + .HasColumnType("integer"); + + b.Property("BoardShapeOverride") + .HasColumnType("text"); + + b.Property("BoardTemplateIdOverride") + .HasColumnType("uuid"); + + b.Property("BoardWidthOverride") + .HasColumnType("integer"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DiscardOnMaxHandOverride") + .HasColumnType("boolean"); + + b.Property("DrawPerTurnOverride") + .HasColumnType("integer"); + + b.Property("FirstPlayerSkipsDrawOverride") + .HasColumnType("boolean"); + + b.Property("GameMode") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("text") + .HasDefaultValue("FFA") + .HasColumnName("game_mode"); + + b.Property("GameSessionId") + .HasColumnType("uuid"); + + b.Property("GuestAccountId") + .HasColumnType("uuid"); + + b.Property("GuestDeckId") + .HasColumnType("uuid"); + + b.Property("GuestFirstTurnPref") + .HasColumnType("text"); + + b.Property("GuestReady") + .HasColumnType("boolean"); + + b.Property("HandSizeCapOverride") + .HasColumnType("integer"); + + b.Property("HexSidesOverride") + .HasColumnType("integer"); + + b.Property("HostAccountId") + .HasColumnType("uuid"); + + b.Property("HostDeckId") + .HasColumnType("uuid"); + + b.Property("HostFirstTurnPref") + .HasColumnType("text"); + + b.Property("HostReady") + .HasColumnType("boolean"); + + b.Property("MaxArchTier1Override") + .HasColumnType("integer"); + + b.Property("MaxDeckSizeOverride") + .HasColumnType("integer"); + + b.Property("MaxPlayers") + .HasColumnType("integer"); + + b.Property("MinDeckSizeOverride") + .HasColumnType("integer"); + + b.Property("MpBuffOverride") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OpeningHandSizeOverride") + .HasColumnType("integer"); + + b.Property("Password") + .HasColumnType("text"); + + b.Property("SpAccumulationPerTurnOverride") + .HasColumnType("integer"); + + b.Property("SpCapOverride") + .HasColumnType("integer"); + + b.Property("SpHasCapOverride") + .HasColumnType("boolean"); + + b.Property("StartingRandomTilePctOverride") + .HasColumnType("integer"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.Property("TeamAssignmentsJson") + .HasColumnType("text"); + + b.Property("TeamCount") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(1) + .HasColumnName("team_count"); + + b.Property("TimeLimitMinutesOverride") + .HasColumnType("integer"); + + b.Property("TurnLimitOverride") + .HasColumnType("integer"); + + b.Property("TurnTimerSecondsOverride") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("HostAccountId"); + + b.HasIndex("Status", "CreatedAt"); + + b.ToTable("custom_lobbies", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameActionLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ActionType") + .IsRequired() + .HasColumnType("text"); + + b.Property("ActorAccountId") + .HasColumnType("uuid"); + + b.Property("GameSessionId") + .HasColumnType("uuid"); + + b.Property("OccurredAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PayloadJson") + .HasColumnType("jsonb") + .HasColumnName("payload"); + + b.Property("ResultJson") + .HasColumnType("jsonb") + .HasColumnName("result"); + + b.Property("SequenceNumber") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ActorAccountId"); + + b.HasIndex("GameSessionId", "SequenceNumber") + .IsUnique(); + + b.ToTable("action_logs", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameRuleSet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("BoardHeight") + .HasColumnType("integer"); + + b.Property("BoardTemplateId") + .HasColumnType("uuid"); + + b.Property("BoardWidth") + .HasColumnType("integer"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("DiscardOnMaxHand") + .HasColumnType("boolean"); + + b.Property("DrawPerTurn") + .HasColumnType("integer"); + + b.Property("FirstPlayerSkipsDraw") + .HasColumnType("boolean"); + + b.Property("GridType") + .IsRequired() + .HasColumnType("text"); + + b.Property("HandSizeCap") + .HasColumnType("integer"); + + b.Property("HexSides") + .HasColumnType("integer"); + + b.Property("IsCustomDefault") + .HasColumnType("boolean"); + + b.Property("IsRankedDefault") + .HasColumnType("boolean"); + + b.Property("LoserOpReward") + .HasColumnType("integer"); + + b.Property("MaxArchTier1") + .HasColumnType("integer"); + + b.Property("MaxDeckSize") + .HasColumnType("integer"); + + b.Property("MinDeckSize") + .HasColumnType("integer"); + + b.Property("MpBuff") + .HasColumnType("integer"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OpeningHandSize") + .HasColumnType("integer"); + + b.Property("RankDiffOpMultiplierPct") + .HasColumnType("integer"); + + b.Property("SpAccumulationPerTurn") + .HasColumnType("integer"); + + b.Property("SpCap") + .HasColumnType("integer"); + + b.Property("SpHasCap") + .HasColumnType("boolean"); + + b.Property("StartingRandomTilePct") + .HasColumnType("integer"); + + b.Property("TimeLimitMinutes") + .HasColumnType("integer"); + + b.Property("TurnLimit") + .HasColumnType("integer"); + + b.Property("TurnTimerSeconds") + .HasColumnType("integer"); + + b.Property("UpdatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("WinnerOpReward") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("IsCustomDefault"); + + b.HasIndex("IsRankedDefault"); + + b.ToTable("game_rule_sets", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameSession", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AdditionalPlayersJson") + .HasColumnType("text") + .HasColumnName("additional_players_json"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("EndedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("GameMode") + .IsRequired() + .ValueGeneratedOnAdd() + .HasColumnType("text") + .HasDefaultValue("FFA") + .HasColumnName("game_mode"); + + b.Property("LastActionAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PlayerCount") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasDefaultValue(2) + .HasColumnName("player_count"); + + b.Property("PlayerOneAccountId") + .HasColumnType("uuid"); + + b.Property("PlayerOneDeckId") + .HasColumnType("uuid"); + + b.Property("PlayerOneReady") + .HasColumnType("boolean"); + + b.Property("PlayerTwoAccountId") + .HasColumnType("uuid"); + + b.Property("PlayerTwoDeckId") + .HasColumnType("uuid"); + + b.Property("PlayerTwoReady") + .HasColumnType("boolean"); + + b.Property("RuleSnapshotJson") + .HasColumnType("jsonb") + .HasColumnName("rule_snapshot"); + + b.Property("StartedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("StateJson") + .HasColumnType("jsonb") + .HasColumnName("state"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.Property("WinCondition") + .HasColumnType("text"); + + b.Property("WinnerAccountId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("PlayerOneAccountId"); + + b.HasIndex("PlayerTwoAccountId"); + + b.HasIndex("Status", "CreatedAt"); + + b.ToTable("game_sessions", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.LobbyPreset", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("OwnerAccountId") + .HasColumnType("uuid"); + + b.Property("RulesJson") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("OwnerAccountId"); + + b.ToTable("lobby_presets", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.MatchmakingEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccountId") + .HasColumnType("uuid"); + + b.Property("DeckId") + .HasColumnType("uuid"); + + b.Property("JoinedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("MatchedGameSessionId") + .HasColumnType("uuid"); + + b.Property("RankScore") + .HasColumnType("integer"); + + b.Property("ResolvedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("AccountId", "Status"); + + b.HasIndex("Status", "RankScore"); + + b.ToTable("matchmaking_entries", "game"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameActionLog", b => + { + b.HasOne("SOR.Database.Entities.Game.GameSession", "Session") + .WithMany("ActionLog") + .HasForeignKey("GameSessionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Session"); + }); + + modelBuilder.Entity("SOR.Database.Entities.Game.GameSession", b => + { + b.Navigation("ActionLog"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.cs b/Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.cs new file mode 100644 index 0000000..7e8e24f --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Game/20260402023622_AddBoardTemplateHexPolygonSides.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SOR.Database.Migrations.Game +{ + /// + public partial class AddBoardTemplateHexPolygonSides : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "HexPolygonSides", + schema: "game", + table: "board_templates", + type: "integer", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "HexPolygonSides", + schema: "game", + table: "board_templates"); + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs b/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs index 17794bd..32e0694 100644 --- a/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs +++ b/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs @@ -39,6 +39,9 @@ namespace SOR.Database.Migrations.Game b.Property("Height") .HasColumnType("integer"); + b.Property("HexPolygonSides") + .HasColumnType("integer"); + b.Property("HexSides") .HasColumnType("integer"); diff --git a/Server/src/SOR.Game/Grid/Board.cs b/Server/src/SOR.Game/Grid/Board.cs index cdcd716..355bf35 100644 --- a/Server/src/SOR.Game/Grid/Board.cs +++ b/Server/src/SOR.Game/Grid/Board.cs @@ -22,11 +22,18 @@ public sealed class Board public Dictionary> StartingZones { get; init; } = new(); /// - /// When > 0, this board uses hex grid rules with the given number of sides (N). - /// Diameter = 2N−1. 0 = square grid. + /// When > 0, this board uses hex grid rules. Value = radius + 1, used for + /// axial-coordinate math (ToAxial center offset). 0 = square grid. /// public int HexSides { get; init; } = 0; + /// + /// True when this board was built from a stored template whose tile set defines + /// the exact board shape (which may be a non-hexagonal polygon). + /// IsInBounds and GetNeighbors use tile existence rather than the hex formula. + /// + public bool IsTemplateBased { get; init; } = false; + // Base1 pool: elemental basics + Aether (never Void — Base1 always carries a real affinity). private static readonly Affinity[] _base1Pool = [Affinity.Lake, Affinity.Hill, Affinity.Forest, Affinity.Field, Affinity.Aether]; @@ -81,9 +88,13 @@ public sealed class Board } public bool IsInBounds(Coord c) - => HexSides > 0 - ? HexGrid.IsInBounds(c, HexSides) - : c.X >= 0 && c.X < Width && c.Y >= 0 && c.Y < Height; + { + if (IsTemplateBased) + return Tiles.ContainsKey(c.ToKey()); + if (HexSides > 0) + return HexGrid.IsInBounds(c, HexSides); + return c.X >= 0 && c.X < Width && c.Y >= 0 && c.Y < Height; + } public Tile? GetTile(Coord c) => Tiles.TryGetValue(c.ToKey(), out var t) ? t : null; @@ -100,9 +111,17 @@ public sealed class Board /// For hex boards uses the six axial directions; for square boards uses four orthogonal directions. /// public IEnumerable GetNeighbors(Coord c) - => HexSides > 0 - ? HexGrid.Neighbors(c, HexSides) - : c.OrthogonalNeighbors().Where(IsInBounds); + { + if (HexSides > 0) + { + // Template boards: all 6 hex neighbours filtered by tile existence. + // Procedural boards: filtered by the hexagonal formula in HexGrid.Neighbors. + return IsTemplateBased + ? HexGrid.AllNeighbors(c, HexSides).Where(IsInBounds) + : HexGrid.Neighbors(c, HexSides); + } + return c.OrthogonalNeighbors().Where(IsInBounds); + } public Coord? FindOccupant(string instanceId) { diff --git a/Server/src/SOR.Game/Grid/BoardShapeGenerator.cs b/Server/src/SOR.Game/Grid/BoardShapeGenerator.cs index 5bade7b..e0a64ad 100644 --- a/Server/src/SOR.Game/Grid/BoardShapeGenerator.cs +++ b/Server/src/SOR.Game/Grid/BoardShapeGenerator.cs @@ -531,11 +531,12 @@ public static class BoardShapeGenerator return new Board { - Width = snapshot.Width, - Height = snapshot.Height, - HexSides = snapshot.HexSides, - Tiles = tiles, - StartingZones = zones, + Width = snapshot.Width, + Height = snapshot.Height, + HexSides = snapshot.HexSides, + IsTemplateBased = snapshot.HexSides > 0, // use tile-existence bounds for non-square templates + Tiles = tiles, + StartingZones = zones, }; } diff --git a/Server/src/SOR.Game/Grid/HexGrid.cs b/Server/src/SOR.Game/Grid/HexGrid.cs index 30edf0b..fb60894 100644 --- a/Server/src/SOR.Game/Grid/HexGrid.cs +++ b/Server/src/SOR.Game/Grid/HexGrid.cs @@ -56,6 +56,18 @@ public static class HexGrid } } + /// + /// All six hex neighbours of c without any bounds filter. + /// Use when the caller supplies its own in-bounds check (e.g. template boards + /// that rely on tile existence rather than the hexagonal formula). + /// + public static IEnumerable AllNeighbors(Coord c, int sides) + { + var (q, r) = ToAxial(c, sides); + foreach (var (dq, dr) in Directions) + yield return FromAxial(q + dq, r + dr, sides); + } + /// /// Returns true if there is unobstructed LOS from to /// on a hex board. Uses cube-coordinate linear interpolation