diff --git a/Client/origin-client/autoloads/CustomLobby.gd b/Client/origin-client/autoloads/CustomLobby.gd index 4c801ef..20d863d 100644 --- a/Client/origin-client/autoloads/CustomLobby.gd +++ b/Client/origin-client/autoloads/CustomLobby.gd @@ -40,6 +40,10 @@ signal preset_vote_rejected(lobby_id: String, preset_id: String) ## Emitted locally when the player has left or cancelled the lobby. signal lobby_left +## Pushed by the server when the other participant leaves a Full lobby. +## reason is "host_cancelled" or "guest_left". +signal partner_left(reason: String) + # ── State ───────────────────────────────────────────────────────────────────── var lobby_id : String = "" @@ -253,3 +257,9 @@ func _on_server_push(type: String, payload: Variant) -> void: var lid := str(d.get("lobbyId", "")) var pid := str(d.get("presetId", "")) preset_vote_rejected.emit(lid, pid) + + "game.custom.lobby.partner_left": + var d := payload as Dictionary + var reason : String = str(d.get("reason", "")) + clear_state() + partner_left.emit(reason) diff --git a/Client/origin-client/autoloads/CustomLobby.gd.uid b/Client/origin-client/autoloads/CustomLobby.gd.uid new file mode 100644 index 0000000..3fdfb7d --- /dev/null +++ b/Client/origin-client/autoloads/CustomLobby.gd.uid @@ -0,0 +1 @@ +uid://jy5lpxwyc6f7 diff --git a/Client/origin-client/scenes/free_play/free_play_screen.gd b/Client/origin-client/scenes/free_play/free_play_screen.gd index 48e1632..e8c8cee 100644 --- a/Client/origin-client/scenes/free_play/free_play_screen.gd +++ b/Client/origin-client/scenes/free_play/free_play_screen.gd @@ -73,12 +73,16 @@ func _load_decks() -> void: var d := p as Dictionary _all_decks = d.get("decks", []) _populate_create_deck_buttons() - # Auto-select first deck so _selected_deck is never empty. - if not _all_decks.is_empty() and _selected_deck.is_empty(): - _selected_deck = str(_all_decks[0].get("id", "")), + _auto_select_deck(), func(_c: String, _m: String) -> void: pass) +func _auto_select_deck() -> void: + # Auto-select the first deck so _selected_deck is never empty when joining/creating. + if not _all_decks.is_empty() and _selected_deck.is_empty(): + _selected_deck = str(_all_decks[0].get("id", "")) + + func _refresh_lobbies() -> void: if _current_tab == "open": _load_open_lobbies() diff --git a/Client/origin-client/scenes/free_play/free_play_screen.gd.uid b/Client/origin-client/scenes/free_play/free_play_screen.gd.uid new file mode 100644 index 0000000..e2b77b0 --- /dev/null +++ b/Client/origin-client/scenes/free_play/free_play_screen.gd.uid @@ -0,0 +1 @@ +uid://dw508xf153tvr 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 ca4c695..b3d23f0 100644 --- a/Client/origin-client/scenes/free_play/lobby_room_screen.gd +++ b/Client/origin-client/scenes/free_play/lobby_room_screen.gd @@ -57,6 +57,7 @@ func _exit_tree() -> void: CustomLobby.preset_vote_requested.disconnect(_on_preset_vote_requested) CustomLobby.preset_vote_rejected.disconnect(_on_preset_vote_rejected) CustomLobby.lobby_left.disconnect(_on_lobby_left) + CustomLobby.partner_left.disconnect(_on_partner_left) # ── Setup ───────────────────────────────────────────────────────────────────── @@ -113,6 +114,7 @@ func _connect_signals() -> void: CustomLobby.preset_vote_requested.connect(_on_preset_vote_requested) CustomLobby.preset_vote_rejected.connect(_on_preset_vote_rejected) CustomLobby.lobby_left.connect(_on_lobby_left) + CustomLobby.partner_left.connect(_on_partner_left) # ── Refresh ─────────────────────────────────────────────────────────────────── @@ -237,6 +239,13 @@ func _on_preset_vote_rejected(_lid: String, _pid: String) -> void: func _on_lobby_left() -> void: get_tree().change_scene_to_file("res://scenes/free_play/FreePlayScreen.tscn") + +func _on_partner_left(reason: String) -> void: + var msg := "Your partner left the lobby." if reason == "guest_left" else "The host cancelled the lobby." + _show_toast(msg) + await get_tree().create_timer(1.5).timeout + get_tree().change_scene_to_file("res://scenes/free_play/FreePlayScreen.tscn") + # ── Actions ─────────────────────────────────────────────────────────────────── func _on_leave_pressed() -> void: diff --git a/Client/origin-client/scenes/free_play/lobby_room_screen.gd.uid b/Client/origin-client/scenes/free_play/lobby_room_screen.gd.uid new file mode 100644 index 0000000..5c801bc --- /dev/null +++ b/Client/origin-client/scenes/free_play/lobby_room_screen.gd.uid @@ -0,0 +1 @@ +uid://ba1ux64fml50t diff --git a/Server/src/SOR.Game/GameMessages.cs b/Server/src/SOR.Game/GameMessages.cs index 2dc7274..4f85d41 100644 --- a/Server/src/SOR.Game/GameMessages.cs +++ b/Server/src/SOR.Game/GameMessages.cs @@ -348,6 +348,12 @@ public sealed record LobbyReadyStatusPush(Guid LobbyId, bool HostReady, bool Gue /// Pushed to both participants when a lobby participant changes their selected deck. public sealed record LobbyDeckChangedPush(Guid LobbyId, Guid AccountId, Guid DeckId); +/// +/// Pushed to the remaining participant when the other player leaves a Full lobby. +/// Reason is "host_cancelled" (host left) or "guest_left" (guest left). +/// +public sealed record LobbyPartnerLeftPush(Guid LobbyId, string Reason); + // ── DTOs ───────────────────────────────────────────────────────────────────── /// diff --git a/Server/src/SOR.Game/Handlers/GameLeaveCustomLobbyHandler.cs b/Server/src/SOR.Game/Handlers/GameLeaveCustomLobbyHandler.cs index e9f2ec9..5081332 100644 --- a/Server/src/SOR.Game/Handlers/GameLeaveCustomLobbyHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameLeaveCustomLobbyHandler.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using SOR.Database.Contexts; using SOR.Database.Enums; using SOR.Networking.Abstractions.Messaging; +using SOR.Networking.Abstractions.Push; using SOR.Networking.Abstractions.Transport; using SOR.Shared.Networking; @@ -12,16 +13,26 @@ using SOR.Shared.Networking; /// /// Host cancelling: marks the lobby Cancelled (removes it for guests). /// Guest leaving: clears their slot, resets lobby to Open. +/// +/// If the lobby is already Started (game in progress) or Cancelled, returns success silently — +/// the caller should concede the game via game.concede instead of trying to leave the lobby. +/// +/// When a Full lobby loses a participant, pushes LobbyPartnerLeftPush to the remaining player. /// internal sealed class GameLeaveCustomLobbyHandler : GameHandlerBase, IMessageHandler { private readonly IMessageSerializer _serializer; private readonly GameDbContext _gameDb; + private readonly IClientPush _clientPush; - public GameLeaveCustomLobbyHandler(IMessageSerializer serializer, GameDbContext gameDb) + public GameLeaveCustomLobbyHandler( + IMessageSerializer serializer, + GameDbContext gameDb, + IClientPush clientPush) { - _serializer = serializer; - _gameDb = gameDb; + _serializer = serializer; + _gameDb = gameDb; + _clientPush = clientPush; } public string MessageType => "game.custom.leave"; @@ -37,28 +48,49 @@ internal sealed class GameLeaveCustomLobbyHandler : GameHandlerBase, IMessageHan var lobby = await _gameDb.CustomLobbies.FindAsync([req.LobbyId], ct) ?? throw new GameException("LOBBY_NOT_FOUND"); + // If already resolved (game started or lobby cancelled), return success silently. + // Player should use game.concede for an in-progress game. if (lobby.Status == CustomLobbyStatus.Started || lobby.Status == CustomLobbyStatus.Cancelled) - throw new GameException("LOBBY_ALREADY_RESOLVED"); + return ServerMessage.Reply("game.custom.leave.ack", message.Seq, + new LeaveCustomLobbyResponse(LobbyId: lobby.Id)); if (lobby.HostAccountId == accountId) { - // Host cancels — closes the lobby entirely. + // Host cancels — close the lobby entirely. + // If a guest is present, notify them. + var guestId = lobby.GuestAccountId; lobby.Status = CustomLobbyStatus.Cancelled; + await _gameDb.SaveChangesAsync(ct); + + if (guestId.HasValue) + { + await _clientPush.PushAsync(guestId.Value.ToString(), + ServerMessage.Push("game.custom.lobby.partner_left", + new LobbyPartnerLeftPush(lobby.Id, "host_cancelled")), ct); + } } else if (lobby.GuestAccountId == accountId) { - // Guest leaves — free up the slot. - lobby.GuestAccountId = null; - lobby.GuestDeckId = null; - lobby.Status = CustomLobbyStatus.Open; + // Guest leaves — free up the slot and reset ready flags. + var hostId = lobby.HostAccountId; + lobby.GuestAccountId = null; + lobby.GuestDeckId = null; + lobby.HostReady = false; + lobby.GuestReady = false; + lobby.HostFirstTurnPref = null; + lobby.GuestFirstTurnPref = null; + lobby.Status = CustomLobbyStatus.Open; + await _gameDb.SaveChangesAsync(ct); + + await _clientPush.PushAsync(hostId.ToString(), + ServerMessage.Push("game.custom.lobby.partner_left", + new LobbyPartnerLeftPush(lobby.Id, "guest_left")), ct); } else { throw new GameException("NOT_IN_LOBBY"); } - await _gameDb.SaveChangesAsync(ct); - return ServerMessage.Reply("game.custom.leave.ack", message.Seq, new LeaveCustomLobbyResponse(LobbyId: lobby.Id)); }