From 1c04ab95c08a0edf6f1a65925e77abfd2c1cece5 Mon Sep 17 00:00:00 2001 From: Dogukan Date: Thu, 22 Oct 2020 23:54:07 +0200 Subject: [PATCH] [ADDED] player list in lobby updates on join! Not on disconnect yet. Tried to set the word label doesn't work yet. --- Client/Client.cs | 17 +++++++++++++++-- Client/ViewModels/ViewModelGame.cs | 26 ++++++++++++++++++++++++-- Client/Views/GameWindow.xaml | 15 ++++----------- Client/Views/LoginScreen.xaml | 4 +++- Client/Views/LoginScreen.xaml.cs | 3 ++- Server/Models/ServerClient.cs | 4 +++- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 15e58a9..fc73ac8 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -27,6 +27,7 @@ namespace Client public LobbyJoinCallback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; public Callback OnServerDisconnect; + public Callback OnClientJoinLobby; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; private ClientData data = ClientData.Instance; @@ -62,6 +63,7 @@ namespace Client { if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) return; + try { int amountReceived = stream.EndRead(ar); @@ -138,6 +140,7 @@ namespace Client Lobbies = JSONConvert.GetLobbiesFromMessage(payload); OnLobbiesListReceived?.Invoke(); OnLobbiesReceivedAndWaitingForHost?.Invoke(); + OnClientJoinLobby?.Invoke(); break; case LobbyIdentifier.HOST: // we receive this when the server has made us a host of a new lobby @@ -147,8 +150,19 @@ namespace Client OnLobbyCreated?.Invoke(lobbyCreatedID); 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); @@ -181,7 +195,6 @@ namespace Client { Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message)); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null); - } private void OnWriteComplete(IAsyncResult ar) diff --git a/Client/ViewModels/ViewModelGame.cs b/Client/ViewModels/ViewModelGame.cs index cbc6a49..224a1c8 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -2,6 +2,7 @@ using Client.Views; using GalaSoft.MvvmLight.Command; using SharedClientServer; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; @@ -24,6 +25,8 @@ namespace Client.ViewModels public static ObservableCollection Messages { get; } = new ObservableCollection(); + public static ObservableCollection Players { get; } = new ObservableCollection(); + private dynamic _payload; public static string Word @@ -46,6 +49,7 @@ namespace Client.ViewModels _message = value; } } + public ICommand OnKeyDown { get; set; } public void Canvas_MouseDown(MouseButtonEventArgs e, GameWindow window) @@ -106,7 +110,6 @@ namespace Client.ViewModels internal void AddMessage(string message) { Messages.Add($"{data.User.Username}: {message}"); - _payload = new { username = data.User.Username, @@ -131,6 +134,10 @@ namespace Client.ViewModels public void LeaveGame(object sender, CancelEventArgs e) { Debug.WriteLine("Leaving..."); + Application.Current.Dispatcher.Invoke(delegate + { + Players.Remove(data.User.Username); + }); data.Client.SendMessage(JSONConvert.ConstructLobbyLeaveMessage(data.Lobby.ID)); } @@ -141,7 +148,22 @@ namespace Client.ViewModels public static void HandleRandomWord(string randomWord) { Debug.WriteLine("[CLIENT] Reached the handle random word method!"); - Word = "NegerPik"; + Application.Current.Dispatcher.Invoke(delegate + { + Word = randomWord; + }); + } + + public static void HandleIncomingPlayer(Lobby lobby) + { + Application.Current.Dispatcher.Invoke(delegate + { + Players.Clear(); + foreach (var item in lobby.Users) + { + Players.Add(item.Username); + } + }); } } } diff --git a/Client/Views/GameWindow.xaml b/Client/Views/GameWindow.xaml index dff9a66..3fad8dd 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -22,21 +22,14 @@ - - - - - - - - +