Implement phase-2 board template engine integration

- Add BoardTemplateJson (nullable string) to GameRules so admin-defined
  board layouts are snapshotted at session creation; in-flight games are
  unaffected by subsequent template edits.
- Introduce BoardTemplateSnapshot + SnapshotTile types in SOR.Game.Grid
  (integer-enum serialisation matching the admin builder).
- BoardShapeGenerator.FromTemplate / FromTemplateJson: build a Board from
  a snapshot, respecting AffinityOverrideable + StartingRandomTilePct.
- GameEngine.Initialize: use FromTemplateJson when BoardTemplateJson is set,
  falling back to algorithmic Generate.
- GameLobbyReadyHandler: load BoardTemplateIdOverride template from DB and
  embed as BoardTemplateJson in the rule snapshot at lobby start.
- MatchmakerService: load BoardTemplateId template from DB and embed via
  JsonElement in the ranked-rule snapshot at match creation.

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:
dingo
2026-03-27 21:03:05 -04:00
co-authored by Claude Happy
parent 4924f61483
commit 1368083405
6 changed files with 200 additions and 2 deletions
@@ -230,6 +230,30 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
? lobby.BoardShapeOverride
: BoardShapeGenerator.AutoShape(playerCount);
// If the lobby specifies a board template, load it and build a snapshot JSON string
// that will be embedded in the rule snapshot so in-flight games are unaffected by later edits.
string? boardTemplateJson = null;
if (lobby.BoardTemplateIdOverride.HasValue)
{
var tmpl = await _gameDb.BoardTemplates
.FindAsync(new object[] { lobby.BoardTemplateIdOverride.Value }, ct);
if (tmpl is not null)
{
var tileOpts = new JsonSerializerOptions(JsonSerializerDefaults.Web);
var tiles = JsonSerializer.Deserialize<List<SnapshotTile>>(tmpl.TilesJson, tileOpts) ?? new();
var zones = JsonSerializer.Deserialize<Dictionary<int, Dictionary<int, List<string>>>>(
tmpl.StartingZonesJson, tileOpts) ?? new();
boardTemplateJson = JsonSerializer.Serialize(
new BoardTemplateSnapshot
{
Width = tmpl.Width,
Height = tmpl.Height,
Tiles = tiles,
StartingZones = zones,
}, tileOpts);
}
}
// Build effective rules: lobby override → admin custom-default → hard-coded default.
var rules = new GameRules
{
@@ -252,6 +276,7 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
MpBuff = lobby.MpBuffOverride ?? dbDefaults?.MpBuff ?? GameRules.Default.MpBuff,
StartingRandomTilePct = lobby.StartingRandomTilePctOverride ?? dbDefaults?.StartingRandomTilePct ?? GameRules.Default.StartingRandomTilePct,
BoardShape = boardShape,
BoardTemplateJson = boardTemplateJson,
PlayerCount = playerCount,
};