diff --git a/Client/Client.cs b/Client/Client.cs index 5346d92..88278ad 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -8,7 +8,7 @@ using static SharedClientServer.JSONConvert; namespace Client { - public delegate void OnLobbyCreated(int id); + public delegate void LobbyCallback(int id); class Client : ObservableObject { private TcpClient tcpClient; @@ -23,7 +23,8 @@ namespace Client public Callback OnLobbiesListReceived; public Callback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; - public OnLobbyCreated OnLobbyCreated; + public LobbyCallback OnLobbyCreated; + public LobbyCallback OnLobbyLeave; public Lobby[] Lobbies { get; set; } public Client(string username) @@ -118,6 +119,10 @@ namespace Client case LobbyIdentifier.JOIN_SUCCESS: OnLobbyJoinSuccess?.Invoke(); break; + case LobbyIdentifier.LEAVE: + int lobbyLeaveID = JSONConvert.GetLobbyID(payload); + OnLobbyLeave?.Invoke(lobbyLeaveID); + break; } //TODO fill lobby with the data received break; diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index bbb9843..37d4ed9 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -34,6 +34,7 @@ namespace Client _lobbies = new ObservableCollection(); client = ClientData.Instance.Client; client.OnLobbiesListReceived = updateLobbies; + client.OnLobbyLeave = leaveLobby; OnHostButtonClick = new RelayCommand(hostGame); @@ -41,6 +42,13 @@ namespace Client JoinSelectedLobby = new RelayCommand(joinLobby, true); } + private void leaveLobby(int id) + { + _model.CanStartGame = true; + ClientData.Instance.Lobby = null; + SelectedLobby = null; + } + private void hostGame() { Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username); @@ -96,19 +104,6 @@ namespace Client Lobby[] lobbiesArr = client.Lobbies; Application.Current.Dispatcher.Invoke(delegate { - - //for (int i = 0; i < lobbiesArr.Length; i++) - //{ - // Lobby lobby = lobbiesArr[i]; - // Debug.WriteLine(lobby.PlayersIn); - // if (i < _lobbies.Count && _lobbies[i].ID == lobby.ID) - // { - // _lobbies[i].Set(lobby); - // } else - // { - // _lobbies.Add(lobbiesArr[i]); - // } - //} _lobbies.Clear(); diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index c6090da..843db9c 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -169,6 +169,7 @@ namespace Server.Models case LobbyIdentifier.LEAVE: id = JSONConvert.GetLobbyID(payload); ServerCommunication.INSTANCE.LeaveLobby(User, id); + sendMessage(JSONConvert.ConstructLobbyLeaveMessage(id)); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; }