diff --git a/Client/Client.cs b/Client/Client.cs index 50c60fc..77fc98b 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -13,9 +13,14 @@ using static SharedClientServer.JSONConvert; namespace Client { public delegate void LobbyJoinCallback(bool isHost); + + public delegate void RandomWord(string word); + public delegate void HandleIncomingMsg(string username, string msg); + internal delegate void HandleIncomingPlayer(Lobby lobby); public delegate void CanvasDataReceived(double[][] coordinates, Color color); public delegate void CanvasReset(); public delegate void LobbyCallback(int id); + class Client : ObservableObject { @@ -34,8 +39,12 @@ namespace Client public LobbyJoinCallback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; public Callback OnServerDisconnect; + public Callback OnLobbyUpdate; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; + public RandomWord RandomWord; + public HandleIncomingMsg IncomingMsg; + public HandleIncomingPlayer IncomingPlayer; private ClientData data = ClientData.Instance; public CanvasDataReceived CanvasDataReceived; public CanvasReset CReset; @@ -57,6 +66,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); @@ -72,6 +82,7 @@ namespace Client if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected) return; + try { int amountReceived = stream.EndRead(ar); @@ -129,7 +140,7 @@ namespace Client if(textUsername != data.User.Username) { - ViewModels.ViewModelGame.HandleIncomingMsg(textUsername, textMsg); + IncomingMsg?.Invoke(textUsername, textMsg); } //TODO display username and message in chat window @@ -147,6 +158,7 @@ namespace Client Lobbies = JSONConvert.GetLobbiesFromMessage(payload); OnLobbiesListReceived?.Invoke(); OnLobbiesReceivedAndWaitingForHost?.Invoke(); + OnLobbyUpdate?.Invoke(); break; case LobbyIdentifier.HOST: // we receive this when the server has made us a host of a new lobby @@ -156,7 +168,6 @@ namespace Client OnLobbyCreated?.Invoke(lobbyCreatedID); break; case LobbyIdentifier.JOIN_SUCCESS: - OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload)); break; case LobbyIdentifier.LEAVE: @@ -189,9 +200,11 @@ namespace Client case JSONConvert.RANDOMWORD: //Flag byte for receiving the random word. int lobbyId = JSONConvert.GetLobbyID(payload); + string randomWord = JSONConvert.GetRandomWord(payload); - if(data.Lobby?.ID == lobbyId) - ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload)); + if (data.Lobby?.ID == lobbyId) + RandomWord?.Invoke(randomWord); + break; default: Debug.WriteLine("[CLIENT] Received weird identifier: " + id); @@ -201,11 +214,20 @@ namespace Client } + private void updateGameLobby() + { + foreach (var item in Lobbies) + { + Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count); + if (item.ID == data.Lobby?.ID) + IncomingPlayer?.Invoke(item); + } + } + public void SendMessage(byte[] message) { 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 1ae0063..212584e 100644 --- a/Client/ViewModels/ViewModelGame.cs +++ b/Client/ViewModels/ViewModelGame.cs @@ -1,4 +1,3 @@ - using Client.Views; using GalaSoft.MvvmLight.Command; using SharedClientServer; @@ -29,6 +28,7 @@ namespace Client.ViewModels private Timer queueTimer; public static ObservableCollection Messages { get; } = new ObservableCollection(); + public ObservableCollection Players { get; } = new ObservableCollection(); private dynamic _payload; @@ -47,6 +47,14 @@ namespace Client.ViewModels } } + private string _randomWord; + + public string RandomWord + { + get { return _randomWord; } + set { _randomWord = value; } + } + public static string Word { get; @@ -80,6 +88,9 @@ namespace Client.ViewModels ButtonResetCanvas = new RelayCommand(CanvasResetLocal); data.Client.CanvasDataReceived = UpdateCanvasWithNewData; data.Client.CReset = CanvasResetData; + data.Client.RandomWord = HandleRandomWord; + data.Client.IncomingMsg = HandleIncomingMsg; + data.Client.IncomingPlayer = HandleIncomingPlayer; } public ICommand OnKeyDown { get; set; } @@ -237,6 +248,16 @@ namespace Client.ViewModels Word = randomWord; }); } + public 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 22bdd4c..dde8efc 100644 --- a/Client/Views/GameWindow.xaml +++ b/Client/Views/GameWindow.xaml @@ -22,16 +22,9 @@ - - - - - - - - + @@ -44,13 +37,15 @@