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] [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; } }