diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index 93dfbdb..2ebc17c 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -1163,9 +1163,19 @@ func _rebuild_ownership(state: Dictionary) -> void: _enemy_unit_ids.clear() _friendly_unit_ids.clear() var my_id := Auth.user_id + # Find my team index so allies are treated as friendly too. + var my_team_idx : int = -1 for p: Dictionary in state.get("players", []): - var is_mine : bool = str(p.get("accountId", "")).to_lower() == my_id.to_lower() - var target : Dictionary = _enemy_unit_ids if not is_mine else _friendly_unit_ids + if str(p.get("accountId", "")).to_lower() == my_id.to_lower(): + my_team_idx = int(p.get("teamIndex", -1)) + break + for p: Dictionary in state.get("players", []): + var pid : String = str(p.get("accountId", "")).to_lower() + var is_mine : bool = pid == my_id.to_lower() + var team_idx : int = int(p.get("teamIndex", -1)) + # In team games (teamIndex matches), treat allies as friendly. + var is_ally : bool = (not is_mine) and (my_team_idx >= 0) and (team_idx == my_team_idx) + var target : Dictionary = _friendly_unit_ids if (is_mine or is_ally) else _enemy_unit_ids for u: Dictionary in p.get("field", []): var iid : String = u.get("instanceId", "") if not iid.is_empty(): target[iid] = true @@ -1446,17 +1456,36 @@ func _rebuild_units(state: Dictionary) -> void: # causing a one-frame bind-pose flash on every state echo. # 1. Collect the desired unit set from the new state. - var my_id := Auth.user_id - var desired : Dictionary = {} # iid → {unit, color, is_enemy} + var my_id := Auth.user_id + var team_colors : Dictionary = state.get("teamColors", {}) + var desired : Dictionary = {} # iid → {unit, color, is_enemy} + # Find my team index for ally detection. + var my_team_idx : int = -1 for player: Dictionary in state.get("players", []): - var is_mine : bool = str(player.get("accountId", "")).to_lower() == my_id.to_lower() - var color : Color = Color(0.3, 0.5, 1.0) if is_mine else Color(1.0, 0.35, 0.35) + if str(player.get("accountId", "")).to_lower() == my_id.to_lower(): + my_team_idx = int(player.get("teamIndex", -1)) + break + for player: Dictionary in state.get("players", []): + var pid : String = str(player.get("accountId", "")).to_lower() + var is_mine : bool = pid == my_id.to_lower() + var team_idx : int = int(player.get("teamIndex", -1)) + var is_ally : bool = (not is_mine) and (my_team_idx >= 0) and (team_idx == my_team_idx) + var is_enemy : bool = not (is_mine or is_ally) + # Use the team's palette color; fall back to blue (friendly) or red (enemy). + var hex_str : String = str(team_colors.get(str(team_idx), "")) + var color : Color + if not hex_str.is_empty(): + color = Color.html("#" + hex_str) + elif is_enemy: + color = Color(1.0, 0.35, 0.35) + else: + color = Color(0.3, 0.5, 1.0) var arch : Dictionary = _safe_arch(player) for unit: Dictionary in ([arch] + player.get("field", [])): if unit.is_empty() or unit.get("position") == null: continue var iid : String = str(unit.get("instanceId", "")) if iid.is_empty(): continue - desired[iid] = {"unit": unit, "color": color, "is_enemy": not is_mine} + desired[iid] = {"unit": unit, "color": color, "is_enemy": is_enemy} # 2. Remove units that are no longer on the board. var to_remove : Array[String] = [] @@ -4219,7 +4248,19 @@ func _on_hold_timer_fired() -> void: func _on_game_over(winner_id: String, win_condition: String, winner_delta: int, loser_delta: int, player_outcomes: Array = []) -> void: - var i_won : bool = (winner_id == Auth.user_id) + # In N-player / team games, derive win from player_outcomes so a teammate + # winning also counts as a local victory. + var i_won : bool + if not player_outcomes.is_empty(): + var my_outcome : String = "" + for po: Dictionary in player_outcomes: + if str(po.get("accountId", "")) == Auth.user_id: + my_outcome = str(po.get("outcome", "")) + break + i_won = (my_outcome == "Winner") + else: + i_won = (winner_id == Auth.user_id) + _go_result_lbl.text = "Victory!" if i_won else "Defeat" _go_result_lbl.modulate = Color(0.4, 1.0, 0.5) if i_won else Color(1.0, 0.4, 0.4) @@ -4233,9 +4274,7 @@ func _on_game_over(winner_id: String, win_condition: String, var sign : String = "+" if rd >= 0 else "" var label: String = "You" if pid == Auth.user_id else Game.get_display_name(pid) outcome_lines.append("%s: %s (%s%d)" % [label, oc, sign, rd]) - _go_rank_lbl.text = "[%s] -" % win_condition + " -".join(outcome_lines) + _go_rank_lbl.text = "[%s]\n" % win_condition + "\n".join(outcome_lines) else: var my_delta : int = winner_delta if i_won else loser_delta _go_rank_lbl.text = "Win by %s | Rank %s%d" % [ diff --git a/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor b/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor index ffa215e..e3fd77e 100644 --- a/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor +++ b/Server/src/SOR.Admin/Components/Pages/Game/BoardTemplateBuilderPage.razor @@ -297,6 +297,9 @@ else @for (int col = 0; col < hexDim; col++) { if (!IsHexInBounds(col, row, hexN)) continue; + // Absent tiles are outside the polygon shape — don't render them. + // (CSS repeating-linear-gradient is not a valid SVG fill value.) + if (_tiles.TryGetValue(TileKey(col, row), out var svgTd) && svgTd.IsAbsent) continue; var hr = row; var hc = col; double cx = HexCellX(hc, hr, hexN, HexR, HexPad); double cy = HexCellY(hr, hexN, HexR, HexPad); diff --git a/Server/src/SOR.Database/Entities/Game/GameSession.cs b/Server/src/SOR.Database/Entities/Game/GameSession.cs index 10970e3..cf48570 100644 --- a/Server/src/SOR.Database/Entities/Game/GameSession.cs +++ b/Server/src/SOR.Database/Entities/Game/GameSession.cs @@ -56,6 +56,17 @@ public sealed class GameSession /// "FFA" | "Teams" | "MultiTeam". public string GameMode { get; set; } = "FFA"; + /// + /// Number of distinct teams. 0 = unset (treated as 2 for Teams/MultiTeam, or PlayerCount for FFA). + /// + public int TeamCount { get; set; } = 0; + + /// Team index for the player in seat 0 (PlayerOne in game turn order). + public int PlayerOneTeamIndex { get; set; } = 0; + + /// Team index for the player in seat 1 (PlayerTwo in game turn order). + public int PlayerTwoTeamIndex { get; set; } = 1; + /// /// JSON array of additional player slots beyond P1/P2. /// Each element: { "accountId": "...", "deckId": "...", "seatIndex": N, "teamIndex": N }. diff --git a/Server/src/SOR.Database/Migrations/Game/20260402121607_AddGameSessionTeamFields.Designer.cs b/Server/src/SOR.Database/Migrations/Game/20260402121607_AddGameSessionTeamFields.Designer.cs new file mode 100644 index 0000000..cc7d730 --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Game/20260402121607_AddGameSessionTeamFields.Designer.cs @@ -0,0 +1,550 @@ +// +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("20260402121607_AddGameSessionTeamFields")] + partial class AddGameSessionTeamFields + { + /// + 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("PlayerOneTeamIndex") + .HasColumnType("integer"); + + b.Property("PlayerTwoAccountId") + .HasColumnType("uuid"); + + b.Property("PlayerTwoDeckId") + .HasColumnType("uuid"); + + b.Property("PlayerTwoReady") + .HasColumnType("boolean"); + + b.Property("PlayerTwoTeamIndex") + .HasColumnType("integer"); + + 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("TeamCount") + .HasColumnType("integer"); + + 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/20260402121607_AddGameSessionTeamFields.cs b/Server/src/SOR.Database/Migrations/Game/20260402121607_AddGameSessionTeamFields.cs new file mode 100644 index 0000000..5ba6ae9 --- /dev/null +++ b/Server/src/SOR.Database/Migrations/Game/20260402121607_AddGameSessionTeamFields.cs @@ -0,0 +1,57 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SOR.Database.Migrations.Game +{ + /// + public partial class AddGameSessionTeamFields : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "PlayerOneTeamIndex", + schema: "game", + table: "game_sessions", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "PlayerTwoTeamIndex", + schema: "game", + table: "game_sessions", + type: "integer", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "TeamCount", + schema: "game", + table: "game_sessions", + type: "integer", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "PlayerOneTeamIndex", + schema: "game", + table: "game_sessions"); + + migrationBuilder.DropColumn( + name: "PlayerTwoTeamIndex", + schema: "game", + table: "game_sessions"); + + migrationBuilder.DropColumn( + name: "TeamCount", + schema: "game", + table: "game_sessions"); + } + } +} diff --git a/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs b/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs index 32e0694..29ed195 100644 --- a/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs +++ b/Server/src/SOR.Database/Migrations/Game/GameDbContextModelSnapshot.cs @@ -412,6 +412,9 @@ namespace SOR.Database.Migrations.Game b.Property("PlayerOneReady") .HasColumnType("boolean"); + b.Property("PlayerOneTeamIndex") + .HasColumnType("integer"); + b.Property("PlayerTwoAccountId") .HasColumnType("uuid"); @@ -421,6 +424,9 @@ namespace SOR.Database.Migrations.Game b.Property("PlayerTwoReady") .HasColumnType("boolean"); + b.Property("PlayerTwoTeamIndex") + .HasColumnType("integer"); + b.Property("RuleSnapshotJson") .HasColumnType("jsonb") .HasColumnName("rule_snapshot"); @@ -436,6 +442,9 @@ namespace SOR.Database.Migrations.Game .IsRequired() .HasColumnType("text"); + b.Property("TeamCount") + .HasColumnType("integer"); + b.Property("WinCondition") .HasColumnType("text"); diff --git a/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs b/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs index 651d8f7..b58ce4b 100644 --- a/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs @@ -211,11 +211,19 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler int playerCount = allPlayerIds.Count; int teamCount = Math.Max(1, lobby.TeamCount); - // Compute team indices based on game mode: - // FFA → teamIndex = seatIndex (everyone on own team) - // Teams (≥2) → teamIndex = seatIndex % teamCount (alternating assignment) - static int ComputeTeamIndex(string gameMode, int seatIndex, int teamCount) => - gameMode == "FFA" ? seatIndex : seatIndex % teamCount; + // Resolve the full seat → team mapping. + // Uses host's custom reassignments if any, otherwise defaults to seat % teamCount. + var seatTeams = GameReassignTeamHandler.DeserializeAssignments( + lobby.TeamAssignmentsJson, lobby.MaxPlayers, teamCount); + + // FFA: every seat is its own team (ignores seatTeams). + // Teams/MultiTeam: use seatTeams keyed by lobby seat index. + int ComputeTeamIndex(int gameSeat, int lobbySeat) => + lobby.GameMode == "FFA" ? gameSeat : seatTeams.GetValueOrDefault(lobbySeat, lobbySeat % teamCount); + + // P1/P2 lobby seats (before the turn-order coin flip). + int p1LobbySeat = playerOneId == lobby.HostAccountId ? 0 : 1; + int p2LobbySeat = 1 - p1LobbySeat; // Load the admin-configured custom-default ruleset. var dbDefaults = await _gameDb.GameRuleSets @@ -293,7 +301,7 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler accountId = ap.AccountId, deckId = ap.DeckId, seatIndex = i + 2, - teamIndex = ComputeTeamIndex(lobby.GameMode, i + 2, teamCount), + teamIndex = ComputeTeamIndex(i + 2, i + 2), // lobby seat == game seat for additional }); sessionAdditionalJson = JsonSerializer.Serialize(sessionAddl); } @@ -306,6 +314,9 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler PlayerTwoDeckId = p2DeckId, PlayerCount = playerCount, GameMode = lobby.GameMode, + TeamCount = teamCount, + PlayerOneTeamIndex = ComputeTeamIndex(0, p1LobbySeat), + PlayerTwoTeamIndex = ComputeTeamIndex(1, p2LobbySeat), AdditionalPlayersJson = sessionAdditionalJson, Status = GameSessionStatus.Initializing, CreatedAt = DateTimeOffset.UtcNow, diff --git a/Server/src/SOR.Game/Handlers/GameReadyHandler.cs b/Server/src/SOR.Game/Handlers/GameReadyHandler.cs index 6cf9d9b..10a0b8b 100644 --- a/Server/src/SOR.Game/Handlers/GameReadyHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameReadyHandler.cs @@ -165,11 +165,8 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler ?? 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; - + // P1 (seat 0) and P2 (seat 1) have their team indices stored directly on the session + // (set by GameLobbyReadyHandler when the lobby starts, respecting TeamAssignmentsJson). var playerList = new List<(Guid, IEnumerable<(SOR.Database.Entities.Catalog.CardDefinition, int)>, SOR.Database.Entities.Catalog.CardDefinition, int, int)> { @@ -177,12 +174,12 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler await LoadDeckCardsAsync(session.PlayerOneDeckId, ct), await LoadDeckArchSpiritAsync(session.PlayerOneDeckId, ct), 0, - TeamIdx(session.GameMode, 0, teamCount)), + session.PlayerOneTeamIndex), (session.PlayerTwoAccountId, await LoadDeckCardsAsync(session.PlayerTwoDeckId, ct), await LoadDeckArchSpiritAsync(session.PlayerTwoDeckId, ct), 1, - TeamIdx(session.GameMode, 1, teamCount)), + session.PlayerTwoTeamIndex), }; foreach (var entry in additionalEntries)