Implement multiplayer extension: 2-10 players FFA/Team/Multi-Team

Core engine:
- Add SeatIndex, TeamIndex, IsEliminated to PlayerState
- Add SeatOrder, GameMode to GameState with helpers: ActiveSeats(),
  GetNextActiveSeat(), GetEnemyIds(), AreAllies()
- Add StartingZones to Board for per-seat placement validation
- Add BoardShape, PlayerCount to GameRules
- New BoardShapeGenerator: Rectangle (up to 4 seats) + Cross shapes
- N-player Initialize() overload; old 2-player overload delegates to it
- EndTurn uses GetNextActiveSeat() instead of hardcoded GetOpponentId()
- Attack finds target across all enemy players; EliminatePlayer() helper
  allows multi-player elimination without immediate game over
- PlaceArchSpirit validates via StartingZones (legacy row fallback preserved)
- AbilitySystem.Own() uses AreAllies() for team-aware ownership checks

Database:
- GameSession: +player_count, +game_mode, +additional_players_json
- CustomLobby: +game_mode, +team_count, +additional_players_json
- EF migration: AddMultiplayerSlots (applied)

Handlers:
- GameCreateCustomLobbyHandler: honours MaxPlayers/GameMode/TeamCount from request
- GameJoinCustomLobbyHandler: multi-slot join up to MaxPlayers
- GameLobbyReadyHandler: N-player all-ready check; seats/team assignment by mode
- GameReadyHandler: builds N-player list from AdditionalPlayersJson
- GameHandlerBase.LoadActiveSessionAsync: recognises additional players

Client:
- Game.gd: get_opponents() returns all non-local players;
  get_opponent() kept as backward-compat alias returning first opponent

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-25 23:57:36 -04:00
co-authored by Claude Happy
parent a023d6ff94
commit f700fd5de6
19 changed files with 1196 additions and 88 deletions
@@ -1,5 +1,6 @@
namespace SOR.Game.Handlers;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Database.Entities.Catalog;
@@ -68,7 +69,11 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
.SingleOrDefaultAsync(ct)
?? throw new GameException("SESSION_NOT_FOUND");
if (session.PlayerOneAccountId != accountId && session.PlayerTwoAccountId != accountId)
bool isP1orP2 = session.PlayerOneAccountId == accountId || session.PlayerTwoAccountId == accountId;
bool isAdditional = !isP1orP2 && session.AdditionalPlayersJson != null &&
session.AdditionalPlayersJson.Contains(accountId.ToString());
if (!isP1orP2 && !isAdditional)
throw new GameException("NOT_IN_SESSION");
if (session.Status == GameSessionStatus.Active)
@@ -106,21 +111,73 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
}
// Both ready — initialize game state.
var p1DeckCards = await LoadDeckCardsAsync(session.PlayerOneDeckId, ct);
var p2DeckCards = await LoadDeckCardsAsync(session.PlayerTwoDeckId, ct);
var p1ArchSpirit = await LoadDeckArchSpiritAsync(session.PlayerOneDeckId, ct);
var p2ArchSpirit = await LoadDeckArchSpiritAsync(session.PlayerTwoDeckId, ct);
// Deserialize rule snapshot (falls back to default if none stored).
var rules = session.RuleSnapshotJson is not null
? GameEngine.DeserializeRules(session.RuleSnapshotJson)
: null;
var state = GameEngine.Initialize(
session.Id,
session.PlayerOneAccountId, p1DeckCards, p1ArchSpirit,
session.PlayerTwoAccountId, p2DeckCards, p2ArchSpirit,
rules);
GameState state;
if (session.PlayerCount <= 2 || string.IsNullOrEmpty(session.AdditionalPlayersJson))
{
// Standard 2-player path.
var p1DeckCards = await LoadDeckCardsAsync(session.PlayerOneDeckId, ct);
var p2DeckCards = await LoadDeckCardsAsync(session.PlayerTwoDeckId, ct);
var p1ArchSpirit = await LoadDeckArchSpiritAsync(session.PlayerOneDeckId, ct);
var p2ArchSpirit = await LoadDeckArchSpiritAsync(session.PlayerTwoDeckId, ct);
state = GameEngine.Initialize(
session.Id,
session.PlayerOneAccountId, p1DeckCards, p1ArchSpirit,
session.PlayerTwoAccountId, p2DeckCards, p2ArchSpirit,
rules);
}
else
{
// N-player path: read additional players from session JSON.
var additionalEntries = JsonSerializer.Deserialize<List<SessionAdditionalPlayer>>(
session.AdditionalPlayersJson,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
?? new();
// Build player list with seat/team indices.
// P1 and P2 always have seatIndex 0 and 1; teamIndex from GameMode.
int teamCount = 1; // FFA default
static int TeamIdx(string gameMode, int seat, int tc) =>
gameMode == "FFA" ? seat : seat % tc;
var playerList = new List<(Guid, IEnumerable<(SOR.Database.Entities.Catalog.CardDefinition, int)>,
SOR.Database.Entities.Catalog.CardDefinition, int, int)>
{
(session.PlayerOneAccountId,
await LoadDeckCardsAsync(session.PlayerOneDeckId, ct),
await LoadDeckArchSpiritAsync(session.PlayerOneDeckId, ct),
0,
TeamIdx(session.GameMode, 0, teamCount)),
(session.PlayerTwoAccountId,
await LoadDeckCardsAsync(session.PlayerTwoDeckId, ct),
await LoadDeckArchSpiritAsync(session.PlayerTwoDeckId, ct),
1,
TeamIdx(session.GameMode, 1, teamCount)),
};
foreach (var entry in additionalEntries)
{
playerList.Add((
entry.AccountId,
await LoadDeckCardsAsync(entry.DeckId, ct),
await LoadDeckArchSpiritAsync(entry.DeckId, ct),
entry.SeatIndex,
entry.TeamIndex));
}
state = GameEngine.Initialize(
session.Id,
playerList.AsReadOnly(),
session.GameMode,
rules?.BoardShape ?? "Rectangle",
rules);
}
session.StateJson = GameEngine.Serialize(state);
session.Status = GameSessionStatus.Active;
@@ -138,6 +195,15 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
// ── Helpers ───────────────────────────────────────────────────────────────
/// <summary>JSON shape for AdditionalPlayersJson entries on a GameSession.</summary>
private sealed class SessionAdditionalPlayer
{
public Guid AccountId { get; set; }
public Guid DeckId { get; set; }
public int SeatIndex { get; set; }
public int TeamIndex { get; set; }
}
private async Task<IEnumerable<(CardDefinition, int)>> LoadDeckCardsAsync(Guid deckId, CancellationToken ct)
{
var deck = await _collectionDb.Decks