From 9e829c47ffff4abde3f2109726d49cf43e4f4323 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 23 Oct 2020 12:26:31 +0200 Subject: [PATCH] [FIX] lobbies now get correctly updated when someone leaves or joins --- Client/Client.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 00357e0..50dcb4c 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -28,7 +28,7 @@ namespace Client public LobbyJoinCallback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; public Callback OnServerDisconnect; - public Callback OnClientJoinLobby; + public Callback OnLobbyUpdate; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; public RandomWord RandomWord; @@ -51,6 +51,7 @@ namespace Client this.tcpClient.EndConnect(ar); this.stream = tcpClient.GetStream(); OnSuccessfullConnect?.Invoke(); + OnLobbyUpdate = updateGameLobby; SendMessage(JSONConvert.ConstructUsernameMessage(username)); this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); @@ -142,7 +143,7 @@ namespace Client Lobbies = JSONConvert.GetLobbiesFromMessage(payload); OnLobbiesListReceived?.Invoke(); OnLobbiesReceivedAndWaitingForHost?.Invoke(); - OnClientJoinLobby?.Invoke(); + OnLobbyUpdate?.Invoke(); break; case LobbyIdentifier.HOST: // we receive this when the server has made us a host of a new lobby @@ -150,21 +151,10 @@ namespace Client Debug.WriteLine("[CLIENT] got lobby object"); int lobbyCreatedID = JSONConvert.GetLobbyID(payload); OnLobbyCreated?.Invoke(lobbyCreatedID); + OnLobbyUpdate?.Invoke(); break; case LobbyIdentifier.JOIN_SUCCESS: OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload)); - - OnClientJoinLobby = () => - { - foreach (var item in Lobbies) - { - Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count); - if (item.ID == data.Lobby.ID) - ViewModels.ViewModelGame.HandleIncomingPlayer(item); - } - - }; - break; case LobbyIdentifier.LEAVE: int lobbyLeaveID = JSONConvert.GetLobbyID(payload); @@ -195,6 +185,16 @@ namespace Client } + private void updateGameLobby() + { + foreach (var item in Lobbies) + { + Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count); + if (data.Lobby != null && item.ID == data.Lobby.ID) + ViewModels.ViewModelGame.HandleIncomingPlayer(item); + } + } + public void SendMessage(byte[] message) { Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));