diff --git a/Client/Client.cs b/Client/Client.cs index 3a6e12f..4799d2e 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -9,6 +9,7 @@ using static SharedClientServer.JSONConvert; namespace Client { public delegate void LobbyCallback(int id); + public delegate void LobbyJoinCallback(bool isHost); class Client : ObservableObject { private TcpClient tcpClient; @@ -21,7 +22,7 @@ namespace Client private string username; public Callback OnSuccessfullConnect; public Callback OnLobbiesListReceived; - public Callback OnLobbyJoinSuccess; + public LobbyJoinCallback OnLobbyJoinSuccess; public Callback OnLobbiesReceivedAndWaitingForHost; public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyLeave; @@ -128,7 +129,8 @@ namespace Client OnLobbyCreated?.Invoke(lobbyCreatedID); break; case LobbyIdentifier.JOIN_SUCCESS: - OnLobbyJoinSuccess?.Invoke(); + + OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload)); break; case LobbyIdentifier.LEAVE: int lobbyLeaveID = JSONConvert.GetLobbyID(payload); diff --git a/Client/ViewModels/ViewModel.cs b/Client/ViewModels/ViewModel.cs index 37d4ed9..32e65d8 100644 --- a/Client/ViewModels/ViewModel.cs +++ b/Client/ViewModels/ViewModel.cs @@ -85,14 +85,14 @@ namespace Client private void joinLobby() { - // lobby die je wilt joinen verwijderen - // nieuwe binnengekregen lobby toevoegen + client.OnLobbyJoinSuccess = OnLobbyJoinSuccess; client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID)); } - private void OnLobbyJoinSuccess() + private void OnLobbyJoinSuccess(bool isHost) { + ClientData.Instance.User.Host = isHost; startGameInLobby(); } diff --git a/Server/Models/ServerClient.cs b/Server/Models/ServerClient.cs index ce7c023..755f28a 100644 --- a/Server/Models/ServerClient.cs +++ b/Server/Models/ServerClient.cs @@ -183,6 +183,10 @@ namespace Server.Models int id = JSONConvert.GetLobbyID(payload); ServerCommunication.INSTANCE.JoinLobby(this.User, id); sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); + + bool isHost; + ServerCommunication.INSTANCE.JoinLobby(this.User,id, out isHost); + sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage(isHost)); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); //Task.Run(SendLobbyData); diff --git a/Server/Models/ServerCommunication.cs b/Server/Models/ServerCommunication.cs index c1ff0ee..2f35e42 100644 --- a/Server/Models/ServerCommunication.cs +++ b/Server/Models/ServerCommunication.cs @@ -202,12 +202,18 @@ namespace Server.Models return lobby.ID; } - public void JoinLobby(User user, int id) + public void JoinLobby(User user, int id, out bool isHost) { + isHost = false; foreach (Lobby l in lobbies) { if (l.ID == id) { + if (l.Users.Count == 0) + { + user.Host = true; + isHost = true; + } AddToLobby(l, user); Debug.WriteLine($"{user.Username} joined lobby with id {id}"); break; diff --git a/SharedClientServer/JSONConvert.cs b/SharedClientServer/JSONConvert.cs index 1c7527b..f9af510 100644 --- a/SharedClientServer/JSONConvert.cs +++ b/SharedClientServer/JSONConvert.cs @@ -136,9 +136,16 @@ namespace SharedClientServer return dynamicAsObject.ToObject(); } - public static byte[] ConstructLobbyJoinSuccessMessage() + public static byte[] ConstructLobbyJoinSuccessMessage(bool isHost) { - return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS}); + return GetMessageToSend(LOBBY, new { identifier = LobbyIdentifier.JOIN_SUCCESS, + host = isHost}); + } + + public static bool GetLobbyJoinIsHost(byte[] json) + { + dynamic payload = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json)); + return payload.host; } #endregion