From ba1b71d8705e4e5dfd7667f473885f22f08c7ff1 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 20 Oct 2020 19:55:36 +0200 Subject: [PATCH 1/8] [AD D] added lobby add methods --- Server/Models/ServerClient.cs | 2 ++ SharedClientServer/JSONConvert.cs | 49 ++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index b10639b..d5846cf 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Net.Sockets; using System.Text; +using static SharedClientServer.JSONConvert; namespace Server.Models { @@ -121,6 +122,7 @@ namespace Server.Models case JSONConvert.LOBBY: // lobby data + LobbyIdentifier l = JSONConvert.GetLobbyIdentifier(payload); break; case JSONConvert.CANVAS: // canvas data diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index b0d83cb..6620e06 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -14,11 +14,12 @@ namespace SharedClientServer public const byte LOBBY = 0x03; public const byte CANVAS = 0x04; - enum LobbyIdentifier + public enum LobbyIdentifier { HOST, - ADD, + JOIN, LEAVE, + LIST, REQUEST } public static (string,string) GetUsernameAndMessage(byte[] json) @@ -43,12 +44,46 @@ namespace SharedClientServer }); } - public static byte[] ConstructLobbyDataMessage(Lobby lobby) + public static byte[] ConstructLobbyRequestMessage() { - return null; + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.REQUEST + }); } + public static byte[] ConstructLobbyListMessage(Lobby[] lobbiesList) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.LIST, + lobbies = lobbiesList + }); + } + public static byte[] ConstructLobbyJoinMessage(int lobbyID) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.JOIN, + id = lobbyID + }); + } + + public static byte[] ConstructLobbyLeaveMessage(int lobbyID) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.LEAVE, + id = lobbyID + }); + } + + public static int GetLobbyID(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.id; + } /// /// constructs a message that can be sent to the clients or server @@ -70,5 +105,11 @@ namespace SharedClientServer Array.Copy(BitConverter.GetBytes(payloadBytes.Length+5),0,res,0,4); return res; } + + public static LobbyIdentifier GetLobbyIdentifier(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.identifier; + } } } From 671951c35b317da3fb8911b6a8d2b0e7a5250fe0 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 20 Oct 2020 21:12:31 +0200 Subject: [PATCH 2/8] [ADD] added sending the lobbies data from the server to the clients on startup --- Client/Client.cs | 17 ++++++++++++++--- Client/ViewModels/ViewModel.cs | 26 ++++++++++++++++++++++---- Client/Views/LoginScreen.xaml.cs | 1 + Server/Models/ServerClient.cs | 8 +++++++- Server/Models/ServerCommunication.cs | 3 +++ SharedClientServer/JSONConvert.cs | 10 ++++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 6ba9b16..b14eaf8 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Net.Sockets; using System.Text; +using static SharedClientServer.JSONConvert; namespace Client { @@ -18,6 +19,8 @@ namespace Client public bool Connected = false; private string username; public Callback OnSuccessfullConnect; + public Callback OnLobbiesListReceived; + public Lobby[] Lobbies { get; set; } public Client(string username) { @@ -70,9 +73,9 @@ namespace Client private void handleData(byte[] message) { - byte id = message[0]; - byte[] payload = new byte[message.Length - 1]; - Array.Copy(message, 1, payload, 0, message.Length - 1); + byte id = message[4]; + byte[] payload = new byte[message.Length - 5]; + Array.Copy(message, 5, payload, 0, message.Length - 5); switch (id) { case JSONConvert.LOGIN: @@ -89,6 +92,14 @@ namespace Client case JSONConvert.LOBBY: // lobby data + LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(payload); + switch (lobbyIdentifier) + { + case LobbyIdentifier.LIST: + Lobbies = JSONConvert.GetLobbiesFromMessage(payload); + OnLobbiesListReceived?.Invoke(); + break; + } //TODO fill lobby with the data received break; case JSONConvert.CANVAS: diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index be55690..aeea9f0 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -9,6 +9,7 @@ using System.Diagnostics; using System.Windows; using System.Collections.ObjectModel; using Client.Views; +using System.Linq; namespace Client { @@ -21,14 +22,17 @@ namespace Client public Lobby SelectedLobby { get; set; } + private Client client; + public ViewModel() { _model = new Model(); _lobbies = new ObservableCollection(); - - _lobbies.Add(new Lobby(50, 3, 8)); - _lobbies.Add(new Lobby(69, 1, 9)); - _lobbies.Add(new Lobby(420, 7, 7)); + client = ClientData.Instance.Client; + client.OnLobbiesListReceived = updateLobbies; + //_lobbies.Add(new Lobby(50, 3, 8)); + //_lobbies.Add(new Lobby(69, 1, 9)); + //_lobbies.Add(new Lobby(420, 7, 7)); OnHostButtonClick = new RelayCommand(() => { @@ -38,6 +42,20 @@ namespace Client JoinSelectedLobby = new RelayCommand(startGameInLobby, true); } + private void updateLobbies() + { + Lobby[] lobbiesArr = client.Lobbies; + Application.Current.Dispatcher.Invoke(delegate + { + foreach (Lobby lobby in lobbiesArr) + { + _lobbies.Add(lobby); + } + }); + } + + + private void startGameInLobby() { if (SelectedLobby != null) diff --git a/Client/Views/LoginScreen.xaml.cs b/Client/Views/LoginScreen.xaml.cs index 780b619..5b742d2 100644 --- a/Client/Views/LoginScreen.xaml.cs +++ b/Client/Views/LoginScreen.xaml.cs @@ -34,6 +34,7 @@ namespace Client.Views Application.Current.Dispatcher.Invoke(delegate { data.User = user; data.Client = client; + client.SendMessage(JSONConvert.ConstructLobbyRequestMessage()); MainWindow startWindow = new MainWindow(); startWindow.Show(); this.Close(); diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index d5846cf..987db9c 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -123,6 +123,13 @@ 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; + } break; case JSONConvert.CANVAS: // canvas data @@ -132,7 +139,6 @@ namespace Server.Models Debug.WriteLine("[SERVER] Received weird identifier: " + id); break; } - //TODO implement ways to handle the message } /// diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 5507103..a4bfb96 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -33,6 +33,9 @@ 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)); serverClientsInlobbies = new Dictionary>(); } diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 6620e06..7ca1d3f 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -1,8 +1,10 @@ using Client; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Text; namespace SharedClientServer @@ -79,6 +81,14 @@ namespace SharedClientServer }); } + public static Lobby[] GetLobbiesFromMessage(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + JArray lobbiesArray = payload.lobbies; + Debug.WriteLine(lobbiesArray.ToString()); + return lobbiesArray.ToObject(); + } + public static int GetLobbyID(byte[] json) { dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); From 7b30911cd7c85bd339edb2e805a92d0d0f7ac2fc Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 20 Oct 2020 22:16:19 +0200 Subject: [PATCH 3/8] [ADD] added hosting a lobby --- Client/Client.cs | 10 +++++++ Client/ViewModels/ViewModel.cs | 43 +++++++++++++++++++++------- Server/Models/ServerClient.cs | 7 +++++ Server/Models/ServerCommunication.cs | 9 ++++++ SharedClientServer/JSONConvert.cs | 39 +++++++++++++++++++++---- SharedClientServer/Lobby.cs | 2 +- 6 files changed, 93 insertions(+), 17 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index b14eaf8..dd6aaff 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -8,6 +8,7 @@ using static SharedClientServer.JSONConvert; namespace Client { + public delegate void OnLobbyCreated(int id, int playersIn, int playersMax); class Client : ObservableObject { private TcpClient tcpClient; @@ -20,6 +21,7 @@ namespace Client private string username; public Callback OnSuccessfullConnect; public Callback OnLobbiesListReceived; + public OnLobbyCreated OnLobbyCreated; public Lobby[] Lobbies { get; set; } public Client(string username) @@ -98,6 +100,14 @@ namespace Client case LobbyIdentifier.LIST: Lobbies = JSONConvert.GetLobbiesFromMessage(payload); OnLobbiesListReceived?.Invoke(); + break; + case LobbyIdentifier.HOST: + // we receive this when the server has made us a host of a new lobby + // TODO get the new lobby and the id + Debug.WriteLine("[CLIENT] got lobby object"); + Lobby lobby = JSONConvert.GetLobby(payload); + OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers); + break; } //TODO fill lobby with the data received diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index aeea9f0..7b954d2 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -30,18 +30,35 @@ namespace Client _lobbies = new ObservableCollection(); client = ClientData.Instance.Client; client.OnLobbiesListReceived = updateLobbies; - //_lobbies.Add(new Lobby(50, 3, 8)); - //_lobbies.Add(new Lobby(69, 1, 9)); - //_lobbies.Add(new Lobby(420, 7, 7)); + - OnHostButtonClick = new RelayCommand(() => - { - Debug.WriteLine("Host button clicked"); - }); + OnHostButtonClick = new RelayCommand(hostGame); JoinSelectedLobby = new RelayCommand(startGameInLobby, true); } + private void hostGame() + { + Debug.WriteLine("attempting to host game for " + ClientData.Instance.User.Username); + client.SendMessage(JSONConvert.ConstructLobbyHostMessage()); + client.OnLobbyCreated = becomeHostForLobby; + } + + private void becomeHostForLobby(int id, int players, int maxplayers) + { + + Debug.WriteLine($"got host succes with data {id} {players} {maxplayers} "); + Lobby newLobby = new Lobby(id, players, maxplayers); + SelectedLobby = newLobby; + Application.Current.Dispatcher.Invoke(delegate + { + _lobbies.Add(newLobby); + startGameInLobby(); + }); + } + + + private void updateLobbies() { Lobby[] lobbiesArr = client.Lobbies; @@ -61,13 +78,17 @@ namespace Client if (SelectedLobby != null) { ClientData.Instance.Lobby = SelectedLobby; - - _model.CanStartGame = false; - GameWindow window = new GameWindow(); - window.Show(); + startGameWindow(); } } + private void startGameWindow() + { + _model.CanStartGame = false; + GameWindow window = new GameWindow(); + window.Show(); + } + private void ClickCheck() { if(!(_model.Status)) diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 987db9c..0b118e4 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -1,4 +1,5 @@  +using Client; using SharedClientServer; using System; using System.Collections.Generic; @@ -129,6 +130,12 @@ namespace Server.Models 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; } break; case JSONConvert.CANVAS: diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index a4bfb96..b95f25d 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -148,5 +148,14 @@ namespace Server.Models } } } + + public Lobby HostForLobby(User user) + { + Lobby lobby = new Lobby( lobbies.Count + 1,0, 8); + lobbies.Add(lobby); + serverClientsInlobbies.Add(lobby, new List()); + AddToLobby(lobby, user); + return lobby; + } } } diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 7ca1d3f..1a00a0d 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -46,6 +46,25 @@ namespace SharedClientServer }); } + #region lobby messages + + public static byte[] ConstructLobbyHostMessage() + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.HOST + }); + } + + public static byte[] ConstructLobbyHostCreatedMessage(Lobby l) + { + return GetMessageToSend(LOBBY, new + { + identifier = LobbyIdentifier.HOST, + lobby = l + }) ; + } + public static byte[] ConstructLobbyRequestMessage() { return GetMessageToSend(LOBBY, new @@ -80,6 +99,11 @@ namespace SharedClientServer id = lobbyID }); } + public static LobbyIdentifier GetLobbyIdentifier(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.identifier; + } public static Lobby[] GetLobbiesFromMessage(byte[] json) { @@ -95,6 +119,15 @@ namespace SharedClientServer return payload.id; } + public static Lobby GetLobby(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + JObject dynamicAsObject = payload.lobby; + return dynamicAsObject.ToObject(); + } + + #endregion + /// /// constructs a message that can be sent to the clients or server /// @@ -116,10 +149,6 @@ namespace SharedClientServer return res; } - public static LobbyIdentifier GetLobbyIdentifier(byte[] json) - { - dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); - return payload.identifier; - } + } } diff --git a/SharedClientServer/Lobby.cs b/SharedClientServer/Lobby.cs index 5a69627..fc58731 100644 --- a/SharedClientServer/Lobby.cs +++ b/SharedClientServer/Lobby.cs @@ -64,7 +64,7 @@ namespace Client public int PlayersIn { - get { return _playersIn; } + get { return _users.Count; } set { _playersIn = value; } } 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 4/8] [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 /// From 24701f8bb351a7fe447a5d5dcd1318457c14dfba Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 19:35:52 +0200 Subject: [PATCH 5/8] [FIX] fix users not being sent with lobbies --- Client/Client.cs | 1 + Client/ViewModels/ViewModel.cs | 16 ++++++++++++++-- Server/Models/ServerClient.cs | 2 ++ Server/Models/ServerCommunication.cs | 1 + SharedClientServer/JSONConvert.cs | 10 ++++++++-- SharedClientServer/Lobby.cs | 13 +++++++++++++ SharedClientServer/User.cs | 4 +++- 7 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index f150349..6287824 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -99,6 +99,7 @@ namespace Client switch (lobbyIdentifier) { case LobbyIdentifier.LIST: + Debug.WriteLine("got lobbies list"); Lobbies = JSONConvert.GetLobbiesFromMessage(payload); OnLobbiesListReceived?.Invoke(); break; diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 52b091e..bc0589b 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -72,13 +72,25 @@ namespace Client private void updateLobbies() { + Debug.WriteLine("updating lobbies..."); Lobby[] lobbiesArr = client.Lobbies; Application.Current.Dispatcher.Invoke(delegate { - foreach (Lobby lobby in lobbiesArr) + + for (int i = 0; i < lobbiesArr.Length; i++) { - _lobbies.Add(lobby); + 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]); + } } + + }); } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 571a775..291b9eb 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -149,11 +149,13 @@ namespace Server.Models Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User); Debug.WriteLine("[SERVERCLIENT] created lobby"); sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created)); + sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; case LobbyIdentifier.JOIN: int id = JSONConvert.GetLobbyID(payload); ServerCommunication.INSTANCE.JoinLobby(this.User,id); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); + sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; } } diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 03335fa..cfc9b8d 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -129,6 +129,7 @@ namespace Server.Models { bool succ; l.AddUser(user, out succ); + Debug.WriteLine("[SERVERCOMM] added user to lobby, now contains " + l.PlayersIn); if (!succ) { // TODO send lobby full message diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 137892f..4f7812f 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -110,8 +110,14 @@ namespace SharedClientServer { dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); JArray lobbiesArray = payload.lobbies; - Debug.WriteLine(lobbiesArray.ToString()); - return lobbiesArray.ToObject(); + Debug.WriteLine("[JSONCONVERT] got lobbies from message" + lobbiesArray.ToString()); + Lobby[] lobbiesTemp = lobbiesArray.ToObject(); + Debug.WriteLine("lobbies in array: "); + foreach (Lobby l in lobbiesTemp) + { + Debug.WriteLine("players: " + l.PlayersIn); + } + return lobbiesTemp; } public static int GetLobbyID(byte[] json) diff --git a/SharedClientServer/Lobby.cs b/SharedClientServer/Lobby.cs index fc58731..2ddf501 100644 --- a/SharedClientServer/Lobby.cs +++ b/SharedClientServer/Lobby.cs @@ -68,12 +68,25 @@ namespace Client set { _playersIn = value; } } + public void Set(Lobby lobby) + { + this._id = lobby._id; + this._users = lobby._users; + this._maxPlayers = lobby._maxPlayers; + } + public int MaxPlayers { get { return _maxPlayers; } set { _maxPlayers = value; } } + public List Users + { + get { return _users; } + set { _users = value; } + } + } } diff --git a/SharedClientServer/User.cs b/SharedClientServer/User.cs index 6e95f64..35ec378 100644 --- a/SharedClientServer/User.cs +++ b/SharedClientServer/User.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Text; @@ -10,6 +11,7 @@ namespace SharedClientServer private int _score; private bool _host; + [JsonConstructor] public User(string username, int score, bool host) { _username = username; From fc5d51a876ed6b728e52abbfa5b8d53fe4379508 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 19:38:40 +0200 Subject: [PATCH 6/8] [FIX] made user host when hosting a lobby --- Server/Models/ServerCommunication.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index cfc9b8d..7b7de44 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -155,6 +155,7 @@ namespace Server.Models Lobby lobby = new Lobby( lobbies.Count + 1,0, 8); lobbies.Add(lobby); serverClientsInlobbies.Add(lobby, new List()); + user.Host = true; AddToLobby(lobby, user); return lobby; } From 3a133145199bcba9677fe4a59c6be8182091eb1d Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 20:34:11 +0200 Subject: [PATCH 7/8] [FIX] fixed showing of players in lobbies when joining and hosting --- Client/Client.cs | 10 ++-- Client/ViewModels/ViewModel.cs | 71 +++++++++++++++++++--------- Server/Models/ServerClient.cs | 8 ++-- Server/Models/ServerCommunication.cs | 6 +-- SharedClientServer/JSONConvert.cs | 4 +- 5 files changed, 64 insertions(+), 35 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index 6287824..71e8c43 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, int playersIn, int playersMax); + public delegate void OnLobbyCreated(int id); class Client : ObservableObject { private TcpClient tcpClient; @@ -22,6 +22,7 @@ namespace Client public Callback OnSuccessfullConnect; public Callback OnLobbiesListReceived; public Callback OnLobbyJoinSuccess; + public Callback OnLobbiesReceivedAndWaitingForHost; public OnLobbyCreated OnLobbyCreated; public Lobby[] Lobbies { get; set; } @@ -102,13 +103,14 @@ namespace Client Debug.WriteLine("got lobbies list"); Lobbies = JSONConvert.GetLobbiesFromMessage(payload); OnLobbiesListReceived?.Invoke(); + OnLobbiesReceivedAndWaitingForHost?.Invoke(); break; case LobbyIdentifier.HOST: // we receive this when the server has made us a host of a new lobby - // TODO get the new lobby and the id + // TODO get lobby id Debug.WriteLine("[CLIENT] got lobby object"); - Lobby lobby = JSONConvert.GetLobby(payload); - OnLobbyCreated?.Invoke(lobby.ID,lobby.PlayersIn,lobby.MaxPlayers); + int lobbyCreatedID = JSONConvert.GetLobbyID(payload); + OnLobbyCreated?.Invoke(lobbyCreatedID); break; case LobbyIdentifier.JOIN_SUCCESS: OnLobbyJoinSuccess?.Invoke(); diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index bc0589b..1ebcc7e 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -10,6 +10,7 @@ using System.Windows; using System.Collections.ObjectModel; using Client.Views; using System.Linq; +using System.Windows.Data; namespace Client { @@ -24,6 +25,9 @@ namespace Client private Client client; + private bool wantToBeHost = false; + private int wantToBeHostId = 0; + public ViewModel() { _model = new Model(); @@ -44,21 +48,40 @@ namespace Client client.OnLobbyCreated = becomeHostForLobby; } - private void becomeHostForLobby(int id, int players, int maxplayers) + private void becomeHostForLobby(int id) { - Debug.WriteLine($"got host succes with data {id} {players} {maxplayers} "); - Lobby newLobby = new Lobby(id, players, maxplayers); - SelectedLobby = newLobby; - Application.Current.Dispatcher.Invoke(delegate - { - _lobbies.Add(newLobby); - startGameInLobby(); - }); + Debug.WriteLine($"got host succes with data {id} "); + wantToBeHost = true; + wantToBeHostId = id; + client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived; + // lobby id krijgen waarvan je host bent + // wachten totdat list van lobbies is gekomen + // uit die list de id zoeken en die joinen + + } + + private void hostLobbiesReceived() + { + if (wantToBeHost) + foreach (Lobby l in Lobbies) + { + if (l.ID == wantToBeHostId) + { + Debug.WriteLine("found lobby that we want to be host of: " + l.ID + ", joining.."); + SelectedLobby = l; + startGameInLobby(); + wantToBeHost = false; + client.OnLobbiesReceivedAndWaitingForHost = null; + break; + } + } } private void joinLobby() { + // lobby die je wilt joinen verwijderen + // nieuwe binnengekregen lobby toevoegen client.OnLobbyJoinSuccess = OnLobbyJoinSuccess; client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID)); } @@ -77,25 +100,29 @@ namespace Client Application.Current.Dispatcher.Invoke(delegate { - for (int i = 0; i < lobbiesArr.Length; i++) + //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(); + + foreach (Lobby l in lobbiesArr) { - 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.Add(l); } - }); } - - private void startGameInLobby() { if (SelectedLobby != null) diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index 291b9eb..24a7a6f 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -146,16 +146,16 @@ namespace Server.Models break; case LobbyIdentifier.HOST: // add new lobby and add this serverclient to it - Lobby created = ServerCommunication.INSTANCE.HostForLobby(this.User); + int createdLobbyID = ServerCommunication.INSTANCE.HostForLobby(this.User); Debug.WriteLine("[SERVERCLIENT] created lobby"); - sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(created)); - sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); + sendMessage(JSONConvert.ConstructLobbyHostCreatedMessage(createdLobbyID)); + ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; case LobbyIdentifier.JOIN: int id = JSONConvert.GetLobbyID(payload); ServerCommunication.INSTANCE.JoinLobby(this.User,id); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); - sendMessage(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); + ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); break; } } diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index 7b7de44..f0eb010 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -33,7 +33,7 @@ namespace Server.Models listener = new TcpListener(IPAddress.Any, port); serverClients = new List(); lobbies = new List(); - Lobby temp = new Lobby(1, 1, 8); + Lobby temp = new Lobby(1, 7, 8); lobbies.Add(temp); serverClientsInlobbies = new Dictionary>(); serverClientsInlobbies.Add(temp, new List()); @@ -150,14 +150,14 @@ namespace Server.Models } } - public Lobby HostForLobby(User user) + public int HostForLobby(User user) { Lobby lobby = new Lobby( lobbies.Count + 1,0, 8); lobbies.Add(lobby); serverClientsInlobbies.Add(lobby, new List()); user.Host = true; AddToLobby(lobby, user); - return lobby; + return lobby.ID; } public void JoinLobby(User user, int id) diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 4f7812f..dd967e8 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -57,12 +57,12 @@ namespace SharedClientServer }); } - public static byte[] ConstructLobbyHostCreatedMessage(Lobby l) + public static byte[] ConstructLobbyHostCreatedMessage(int lobbyID) { return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.HOST, - lobby = l + id = lobbyID }) ; } From 95448832c3d317bcc8640dc0e948205d72098eaf Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 21 Oct 2020 20:42:00 +0200 Subject: [PATCH 8/8] [ADD] added get lobby for users method --- Client/ViewModels/ViewModel.cs | 3 --- Server/Models/ServerCommunication.cs | 12 ++++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 1ebcc7e..bbb9843 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -55,9 +55,6 @@ namespace Client wantToBeHost = true; wantToBeHostId = id; client.OnLobbiesReceivedAndWaitingForHost = hostLobbiesReceived; - // lobby id krijgen waarvan je host bent - // wachten totdat list van lobbies is gekomen - // uit die list de id zoeken en die joinen } diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index f0eb010..a069725 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -121,6 +121,18 @@ namespace Server.Models } } + public Lobby GetLobbyForUser(User user) + { + foreach (Lobby l in lobbies) + { + if (l.Users.Contains(user)) + { + return l; + } + } + return null; + } + public void AddToLobby(Lobby lobby, User user) { foreach (Lobby l in lobbies)