diff --git a/Client/origin-client/autoloads/CustomLobby.gd b/Client/origin-client/autoloads/CustomLobby.gd index 5e5c27a..06418ca 100644 --- a/Client/origin-client/autoloads/CustomLobby.gd +++ b/Client/origin-client/autoloads/CustomLobby.gd @@ -342,8 +342,8 @@ func add_ai(difficulty: int, strategy: String, ai_slots = ai_slots.filter(func(s: Dictionary) -> bool: return int(s.get("seatIndex", -1)) != seat) ai_slots.append({ - "accountId": str(d.get("aiId", "")), - "displayName": str(d.get("displayName", "AI")), + "accountId": str(d.get("aiAccountId", "")), + "displayName": str(d.get("aiDisplayName", "AI")), "difficulty": difficulty, "strategy": strategy, "seatIndex": seat, @@ -451,7 +451,7 @@ func _on_server_push(type: String, payload: Variant) -> void: "game.ai.slots_changed": var d := payload as Dictionary - ai_slots = d.get("slots", []) as Array + ai_slots = d.get("currentAiSlots", []) as Array ai_slots_changed.emit(ai_slots) "game.custom.lobby.team_reassigned": diff --git a/Client/origin-client/scenes/free_play/lobby_room_screen.gd b/Client/origin-client/scenes/free_play/lobby_room_screen.gd index 133e58b..ee6e1f3 100644 --- a/Client/origin-client/scenes/free_play/lobby_room_screen.gd +++ b/Client/origin-client/scenes/free_play/lobby_room_screen.gd @@ -51,6 +51,7 @@ const RULES_DIALOG = preload("res://scenes/free_play/components/RulesEditorDialo var _first_turn_pref : String = "random" var _current_rules : Dictionary = {} var _team_panel : VBoxContainer = null # dynamically added team-controls section +var _ai_adding : bool = false # guard against concurrent add-AI requests # ── Lifecycle ───────────────────────────────────────────────────────────────── @@ -172,10 +173,11 @@ func _refresh_display() -> void: host_name_label.text = "Host: %s%s" % [str(lobby.get("hostDisplayName", Auth.display_name)), team_suffix_0] host_deck_label.text = "Deck: (selected)" - # Guest slot — may be a human, an AI, or empty + # Guest slot — may be a human, an AI (2-player only), or empty. + # For 3+ player games the seat-1 AI is an additional player shown in the panel below. var team_suffix_1 : String = _team_label(game_mode, 1, team_count) var has_guest : bool = lobby.get("hasGuest", false) or lobby.get("guestAccountId", "") != "" - var ai_in_guest_slot : Dictionary = _ai_slot_for_seat(1) + var ai_in_guest_slot : Dictionary = _ai_slot_for_seat(1) if max_players == 2 else {} if not ai_in_guest_slot.is_empty(): guest_name_label.text = "AI: %s%s" % [str(ai_in_guest_slot.get("displayName", "AI")), team_suffix_1] guest_deck_label.text = "Difficulty %d" % int(ai_in_guest_slot.get("difficulty", 1)) @@ -188,10 +190,11 @@ func _refresh_display() -> void: guest_name_label.text = "Waiting for guest…" guest_deck_label.text = "" - # Show Add AI button only for host when guest slot is truly empty (max 2 players) - # and neither player is ready + # Show Add AI Opponent button only for 2-player lobbies (guest slot). + # 3+ player lobbies use per-seat Add AI buttons in the additional players panel. var slot_open : bool = (not has_guest and ai_in_guest_slot.is_empty() and max_players == 2) add_ai_btn.visible = CustomLobby.is_host and slot_open and not CustomLobby.i_am_ready + add_ai_btn.disabled = _ai_adding # Additional players (3+) from lobby dict. _rebuild_additional_players_panel(lobby, max_players) @@ -212,7 +215,9 @@ func _refresh_display() -> void: _update_rules_label() -## Rebuild the additional players panel (seats 3+) from the lobby dict. +## Rebuild the additional players panel (seats 2+) from the lobby dict. +## For 3+ player lobbies this covers ALL non-host seats (seats 1–N when no guest is present, +## seats 2–N when a human guest occupies seat 1). func _rebuild_additional_players_panel(lobby: Dictionary, max_players: int) -> void: for c in additional_players_container.get_children(): c.queue_free() @@ -221,32 +226,40 @@ func _rebuild_additional_players_panel(lobby: Dictionary, max_players: int) -> v additional_players_container.visible = false return - var gm : String = str(lobby.get("gameMode", "FFA")) - var tc : int = int(lobby.get("teamCount", 1)) + var gm : String = str(lobby.get("gameMode", "FFA")) + var tc : int = int(lobby.get("teamCount", 1)) - # The lobby dict may contain additionalPlayers array (from server push or join response). - var addl : Array = lobby.get("additionalPlayers", []) as Array - if addl.is_empty(): - # Show empty slots for players who haven't joined yet. - # Host always occupies slot 1; guest slot 2 if guestAccountId is present. - var has_host : bool = true # host is always present - var has_guest : bool = lobby.get("hasGuest", false) or str(lobby.get("guestAccountId", "")) != "" - var taken_slots : int = (1 if has_host else 0) + (1 if has_guest else 0) - for i in range(max_players - taken_slots): - var seat_n : int = taken_slots + i - var tsuf : String = _team_label(gm, seat_n, tc) - var row := _make_player_slot_row("Seat %d: Waiting…%s" % [seat_n + 1, tsuf], false) + # Build a seat-index → addl-player dict for O(1) lookup. + var addl_by_seat : Dictionary = {} + for ap: Dictionary in (lobby.get("additionalPlayers", []) as Array): + var si : int = int(ap.get("seatIndex", -1)) + if si >= 0: + addl_by_seat[si] = ap + + # First additional seat depends on whether a human occupies the guest slot (seat 1). + var has_guest2 : bool = lobby.get("hasGuest", false) or str(lobby.get("guestAccountId", "")) != "" + var first_addl : int = 2 if has_guest2 else 1 + + for seat_n in range(first_addl, max_players): + var tsuf : String = _team_label(gm, seat_n, tc) + var ai_slot : Dictionary = _ai_slot_for_seat(seat_n) + if not ai_slot.is_empty(): + # AI occupies this seat. + var row := _make_player_slot_row( + "AI: %s%s" % [str(ai_slot.get("displayName", "AI")), tsuf], true) additional_players_container.add_child(row) - else: - for i2: int in addl.size(): - var entry : Dictionary = addl[i2] as Dictionary + elif addl_by_seat.has(seat_n): + # Human player occupies this seat. + var entry : Dictionary = addl_by_seat[seat_n] as Dictionary var aid : String = str(entry.get("accountId", "")) var aname : String = str(entry.get("displayName", str(entry.get("accountId", "Player")).substr(0, 8))) var ready : bool = bool(entry.get("isReady", false)) - var seat_n2 : int = i2 + 2 - var tsuf2 : String = _team_label(gm, seat_n2, tc) - var who := "You" if aid == Auth.user_id else aname - var row := _make_player_slot_row("Player: %s%s" % [who, tsuf2], ready) + var who : String = "You" if aid == Auth.user_id else aname + var row := _make_player_slot_row("Player: %s%s" % [who, tsuf], ready) + additional_players_container.add_child(row) + else: + # Seat is empty — show waiting row with Add AI button for the host. + var row := _make_additional_seat_row("Seat %d: Waiting…%s" % [seat_n + 1, tsuf]) additional_players_container.add_child(row) additional_players_container.visible = true @@ -279,6 +292,24 @@ func _make_player_slot_row(label_text: String, is_ready: bool) -> HBoxContainer: return hbox +func _make_additional_seat_row(label_text: String) -> HBoxContainer: + var hbox := HBoxContainer.new() + hbox.add_theme_constant_override("separation", 8) + var name_lbl := Label.new() + name_lbl.text = label_text + name_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL + name_lbl.add_theme_font_size_override("font_size", 13) + hbox.add_child(name_lbl) + if CustomLobby.is_host and not CustomLobby.i_am_ready: + var ai_btn := Button.new() + ai_btn.text = "Add AI" + ai_btn.custom_minimum_size = Vector2(70, 0) + ai_btn.disabled = _ai_adding + ai_btn.pressed.connect(_on_add_ai_pressed) + hbox.add_child(ai_btn) + return hbox + + ## Rebuild the host-only team assignment panel (shown only in team game modes). ## Each row: "[Seat N: ] [<] T0 [>]" func _rebuild_team_controls(lobby: Dictionary, max_players: int, @@ -543,6 +574,8 @@ func _on_ai_slots_changed(_slots: Array) -> void: func _on_add_ai_pressed() -> void: + if _ai_adding: + return var dlg := AcceptDialog.new() dlg.title = "Add AI Opponent" dlg.ok_button_text = "Add" @@ -580,14 +613,16 @@ func _on_add_ai_pressed() -> void: dlg.confirmed.connect(func(): var difficulty := diff_opts.selected + 1 # 1-indexed - var strategy :Variant = strategies[strat_opts.selected] - add_ai_btn.disabled = true + var strategy : Variant = strategies[strat_opts.selected] + _ai_adding = true + _refresh_display() CustomLobby.add_ai(difficulty, strategy, func(_d: Dictionary) -> void: - add_ai_btn.disabled = false + _ai_adding = false _refresh_display(), func(_code: String, _msg: String) -> void: - add_ai_btn.disabled = false + _ai_adding = false + _refresh_display() _show_toast("Failed to add AI: " + _msg)) dlg.queue_free()) dlg.canceled.connect(func(): dlg.queue_free()) diff --git a/Client/origin-client/scenes/game/components/board/tile_target_resolver.gd b/Client/origin-client/scenes/game/components/board/tile_target_resolver.gd index c5ac3e3..5395575 100644 --- a/Client/origin-client/scenes/game/components/board/tile_target_resolver.gd +++ b/Client/origin-client/scenes/game/components/board/tile_target_resolver.gd @@ -29,14 +29,14 @@ func valid_play_tiles(card: Dictionary, tile_bodies: Dictionary) -> Array: return results var ax: int = arch_pos.get("x", 0) var ay: int = arch_pos.get("y", 0) - # Channel range is a property of the ArchSpirit. + # Channel range is validated by the server from the Spirit card being played. # dict.get(key, default) only uses the default when the key is ABSENT. # The server sends nullable ints (e.g. "minChannelRange": null), so the # key EXISTS but its value is null — GDScript returns null, which silently # coerces to 0 on a typed int variable, collapsing the range to nothing. # Read as Variant first, then substitute the default explicitly. - var raw_min: Variant = arch.get("minChannelRange") - var raw_max: Variant = arch.get("maxChannelRange") + var raw_min: Variant = card.get("minChannelRange") + var raw_max: Variant = card.get("maxChannelRange") var min_r: int = 1 if raw_min == null else int(raw_min) var max_r: int = 1 if raw_max == null else int(raw_max) var card_affinity: String = card.get("affinity", "") @@ -50,7 +50,7 @@ func valid_play_tiles(card: Dictionary, tile_bodies: Dictionary) -> Array: var tile: Dictionary = Game.get_tile(tx, ty) print("tile", tile) print("[GettingAff]", card_affinity) - if not unit_registry.tile_has_blocking_occupant(tile): + if unit_registry.tile_has_blocking_occupant(tile): continue if card_affinity != "" and not tile_matches_channel_affinity(tile, card_affinity): continue diff --git a/Client/origin-client/scenes/game/components/units/unit_registry.gd b/Client/origin-client/scenes/game/components/units/unit_registry.gd index 30e09ed..ea75229 100644 --- a/Client/origin-client/scenes/game/components/units/unit_registry.gd +++ b/Client/origin-client/scenes/game/components/units/unit_registry.gd @@ -77,7 +77,7 @@ func is_ruin_instance(iid: String) -> bool: ## Returns true when the tile has an occupant that blocks movement (i.e. not a Ruin). func tile_has_blocking_occupant(tile: Dictionary) -> bool: var occupant_id: String = str(tile.get("occupantId", "")) - if occupant_id.is_empty() or occupant_id == "null": + if occupant_id.is_empty() or occupant_id == "" or occupant_id == "null": print("[TileBlock] failing out") return false print("[TileBlock] returning this: ", not is_ruin_instance(occupant_id)) diff --git a/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs b/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs index 323fbf6..f1f5f89 100644 --- a/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameLobbyReadyHandler.cs @@ -154,16 +154,20 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler pushTasks.Add(_clientPush.PushAsync(ap.AccountId.ToString(), readyPush, ct)); await Task.WhenAll(pushTasks); - // All-ready check: host + guest must be ready; for 3+ players all must be ready. - bool allReady = lobby.GuestAccountId.HasValue && lobby.HostReady && lobby.GuestReady; - if (allReady && lobby.AdditionalPlayersJson != null) - { - var addl = JsonSerializer.Deserialize>( - lobby.AdditionalPlayersJson, - new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) - ?? new(); - allReady = addl.All(p => p.IsReady); - } + // All-ready check: host must be ready; guest must be ready if a human guest is present. + // For 3+ player lobbies filled entirely via AdditionalPlayersJson (no human guest), + // GuestAccountId is null — lobby.Status == Full already guarantees all seats are occupied. + bool guestReady = !lobby.GuestAccountId.HasValue || lobby.GuestReady; + List addlForCheck = string.IsNullOrEmpty(lobby.AdditionalPlayersJson) + ? [] + : JsonSerializer.Deserialize>( + lobby.AdditionalPlayersJson, + new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? []; + + bool allReady = lobby.Status == CustomLobbyStatus.Full + && lobby.HostReady + && guestReady + && addlForCheck.All(p => p.IsReady); if (allReady) await StartGameAsync(lobby, accountId, ct); @@ -177,32 +181,59 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler Guid triggeringAccountId, CancellationToken ct) { - var guestId = lobby.GuestAccountId!.Value; - - // Resolve first-turn: matching preferences → that player goes first; otherwise random. - Guid playerOneId; - if (lobby.HostFirstTurnPref == "host" && lobby.GuestFirstTurnPref == "host") - playerOneId = lobby.HostAccountId; - else if (lobby.HostFirstTurnPref == "guest" && lobby.GuestFirstTurnPref == "guest") - playerOneId = guestId; - else - playerOneId = Random.Shared.Next(2) == 0 ? lobby.HostAccountId : guestId; - - var playerTwoId = playerOneId == lobby.HostAccountId ? guestId : lobby.HostAccountId; - var p1DeckId = playerOneId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value; - var p2DeckId = playerTwoId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value; - - // Additional players (seats 2+). + // Deserialize additional players (seats 1+ when no human guest, 2+ when guest is present). var additionalPlayers = string.IsNullOrEmpty(lobby.AdditionalPlayersJson) ? new List() : JsonSerializer.Deserialize>( lobby.AdditionalPlayersJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new(); + // Determine the two "base" players (P1/P2) and the remaining additional players. + // • Human guest present → host + guest fill seats 0/1; additionalPlayers are seats 2+. + // • No human guest → host fills seat 0; first additional player fills seat 1; + // remaining additional players fill seats 2+. + // (3+ player games have no first-turn preference UI, so seat order = turn order.) + Guid playerOneId, playerTwoId; + Guid p1DeckId, p2DeckId; + int p1LobbySeat, p2LobbySeat; + List remainingAddl; + + if (lobby.GuestAccountId.HasValue) + { + var guestId = lobby.GuestAccountId.Value; + + // Resolve first-turn preference. + if (lobby.HostFirstTurnPref == "host" && lobby.GuestFirstTurnPref == "host") + playerOneId = lobby.HostAccountId; + else if (lobby.HostFirstTurnPref == "guest" && lobby.GuestFirstTurnPref == "guest") + playerOneId = guestId; + else + playerOneId = Random.Shared.Next(2) == 0 ? lobby.HostAccountId : guestId; + + playerTwoId = playerOneId == lobby.HostAccountId ? guestId : lobby.HostAccountId; + p1DeckId = playerOneId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value; + p2DeckId = playerTwoId == lobby.HostAccountId ? lobby.HostDeckId : lobby.GuestDeckId!.Value; + p1LobbySeat = playerOneId == lobby.HostAccountId ? 0 : 1; + p2LobbySeat = 1 - p1LobbySeat; + remainingAddl = additionalPlayers; + } + else + { + // No human guest: host is P1 (lobby seat 0), first additional player is P2 (lobby seat 1). + var p2 = additionalPlayers[0]; + playerOneId = lobby.HostAccountId; + playerTwoId = p2.AccountId; + p1DeckId = lobby.HostDeckId; + p2DeckId = p2.DeckId; + p1LobbySeat = 0; + p2LobbySeat = 1; + remainingAddl = additionalPlayers.Skip(1).ToList(); + } + // Build the full ordered player list: [P1, P2, P3, ...]. - var allPlayerIds = new List { playerOneId, playerTwoId }; - var allDeckIds = new List { p1DeckId, p2DeckId }; - foreach (var ap in additionalPlayers) + var allPlayerIds = new List { playerOneId, playerTwoId }; + var allDeckIds = new List { p1DeckId, p2DeckId }; + foreach (var ap in remainingAddl) { allPlayerIds.Add(ap.AccountId); allDeckIds.Add(ap.DeckId); @@ -221,10 +252,6 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler 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 .AsNoTracking() @@ -296,9 +323,9 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler // Build additional players JSON for the session (seats 2+). string? sessionAdditionalJson = null; - if (additionalPlayers.Count > 0) + if (remainingAddl.Count > 0) { - var sessionAddl = additionalPlayers.Select((ap, i) => new + var sessionAddl = remainingAddl.Select((ap, i) => new { accountId = ap.AccountId, deckId = ap.DeckId, @@ -370,9 +397,9 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler ServerMessage.Push("game.custom.started", new CustomLobbyGameStartedPush(session.Id, playerOneId, p1Name)), ct), }; - for (int i = 0; i < additionalPlayers.Count; i++) + for (int i = 0; i < remainingAddl.Count; i++) { - var apId = additionalPlayers[i].AccountId; + var apId = remainingAddl[i].AccountId; var apName = names.GetValueOrDefault(apId, apId.ToString()[..8]); startTasks.Add(_clientPush.PushAsync(apId.ToString(), ServerMessage.Push("game.custom.started",