From 76cd392525f959318f03130f091b13d27805ac54 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 20 Oct 2020 22:49:25 +0200 Subject: [PATCH] [ADD] added joining lobbies --- Client/Client.cs | 5 +++- Client/ViewModels/ViewModel.cs | 20 +++++++++++++--- Client/Views/GameWindow.xaml.cs | 5 ++++ Client/Views/MainWindow.xaml.cs | 13 +--------- Server/Models/ServerClient.cs | 36 ++++++++++++++++++---------- Server/Models/ServerCommunication.cs | 19 ++++++++++++--- SharedClientServer/JSONConvert.cs | 6 +++++ 7 files changed, 72 insertions(+), 32 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index dd6aaff..f150349 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -21,6 +21,7 @@ namespace Client private string username; public Callback OnSuccessfullConnect; public Callback OnLobbiesListReceived; + public Callback OnLobbyJoinSuccess; public OnLobbyCreated OnLobbyCreated; public Lobby[] Lobbies { get; set; } @@ -107,7 +108,9 @@ namespace Client Debug.WriteLine("[CLIENT] got lobby object"); Lobby lobby = JSONConvert.GetLobby(payload); OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers); - + break; + case LobbyIdentifier.JOIN_SUCCESS: + OnLobbyJoinSuccess?.Invoke(); break; } //TODO fill lobby with the data received diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 7b954d2..52b091e 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -34,7 +34,7 @@ namespace Client OnHostButtonClick = new RelayCommand(hostGame); - JoinSelectedLobby = new RelayCommand(startGameInLobby, true); + JoinSelectedLobby = new RelayCommand(joinLobby, true); } private void hostGame() @@ -57,6 +57,17 @@ namespace Client }); } + private void joinLobby() + { + client.OnLobbyJoinSuccess = OnLobbyJoinSuccess; + client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID)); + } + + private void OnLobbyJoinSuccess() + { + startGameInLobby(); + } + private void updateLobbies() @@ -85,8 +96,11 @@ namespace Client private void startGameWindow() { _model.CanStartGame = false; - GameWindow window = new GameWindow(); - window.Show(); + Application.Current.Dispatcher.Invoke(delegate + { + GameWindow window = new GameWindow(); + window.Show(); + }); } private void ClickCheck() diff --git a/Client/Views/GameWindow.xaml.cs b/Client/Views/GameWindow.xaml.cs index aacc67e..f527b39 100644 --- a/Client/Views/GameWindow.xaml.cs +++ b/Client/Views/GameWindow.xaml.cs @@ -113,5 +113,10 @@ namespace Client.Views string user = data.User.Username; SentMessage.AppendText($"{user}: {message}\n"); } + + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + + } } } diff --git a/Client/Views/MainWindow.xaml.cs b/Client/Views/MainWindow.xaml.cs index aa9cf2f..058bbc8 100644 --- a/Client/Views/MainWindow.xaml.cs +++ b/Client/Views/MainWindow.xaml.cs @@ -33,18 +33,7 @@ namespace Client private void Button_Click(object sender, RoutedEventArgs e) { - Lobby lobbySelected = LobbyList.SelectedItem as Lobby; - if(lobbySelected != null) - { - testLabel.Content = lobbySelected.ID; - colorSelection.IsEnabled = false; - joinButton.IsEnabled = false; - hostButton.IsEnabled = false; - - GameWindow window = new GameWindow(); - window.Show(); - Close(); - } + } } } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 0b118e4..571a775 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -124,19 +124,7 @@ namespace Server.Models case JSONConvert.LOBBY: // lobby data LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload); - switch(l) - { - case LobbyIdentifier.REQUEST: - Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies..."); - sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); - break; - case LobbyIdentifier.HOST: - // add new lobby and add this serverclient to it - Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User); - Debug.WriteLine("[SERVERCLIENT] created lobby"); - sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created)); - break; - } + handleLobbyMessage(payload,l); break; case JSONConvert.CANVAS: // canvas data @@ -148,6 +136,28 @@ namespace Server.Models } } + private void handleLobbyMessage(byte[] payload, LobbyIdentifier l) + { + switch (l) + { + case LobbyIdentifier.REQUEST: + Debug.WriteLine("[SERVERCLIENT] got lobby request message, sending lobbies..."); + sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); + break; + case LobbyIdentifier.HOST: + // add new lobby and add this serverclient to it + Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User); + Debug.WriteLine("[SERVERCLIENT] created lobby"); + sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created)); + break; + case LobbyIdentifier.JOIN: + int id = JSONConvert.GetLobbyID(payload); + ServerCommunication.INSTANCE.JoinLobby(this.User,id); + sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); + break; + } + } + /// /// sends a message to the tcp client /// diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index b95f25d..03335fa 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -33,10 +33,10 @@ namespace Server.Models listener = new TcpListener(IPAddress.Any, port); serverClients = new List(); lobbies = new List(); - lobbies.Add(new Lobby(1,1,1)); - lobbies.Add(new Lobby(2, 2, 2)); - lobbies.Add(new Lobby(3, 3, 3)); + Lobby temp = new Lobby(1, 1, 8); + lobbies.Add(temp); serverClientsInlobbies = new Dictionary>(); + serverClientsInlobbies.Add(temp, new List()); } /// @@ -157,5 +157,18 @@ namespace Server.Models AddToLobby(lobby, user); return lobby; } + + public void JoinLobby(User user, int id) + { + foreach (Lobby l in lobbies) + { + if (l.ID == id) + { + AddToLobby(l, user); + Debug.WriteLine($"{user.Username} joined lobby with id {id}"); + break; + } + } + } } } diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 1a00a0d..137892f 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -20,6 +20,7 @@ namespace SharedClientServer { HOST, JOIN, + JOIN_SUCCESS, LEAVE, LIST, REQUEST @@ -126,6 +127,11 @@ namespace SharedClientServer return dynamicAsObject.ToObject(); } + public static byte[] ConstructLobbyJoinSuccessMessage() + { + return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS}); + } + #endregion ///