[ADD] added that when you join an empty lobby you become the host

This commit is contained in:
Sem van der Hoeven
2020-10-22 13:22:52 +02:00
parent 471247827b
commit 0b72c4dc9b
5 changed files with 22 additions and 10 deletions

View File

@@ -9,6 +9,7 @@ using static SharedClientServer.JSONConvert;
namespace Client namespace Client
{ {
public delegate void LobbyCallback(int id); public delegate void LobbyCallback(int id);
public delegate void LobbyJoinCallback(bool isHost);
class Client : ObservableObject class Client : ObservableObject
{ {
private TcpClient tcpClient; private TcpClient tcpClient;
@@ -21,7 +22,7 @@ namespace Client
private string username; private string username;
public Callback OnSuccessfullConnect; public Callback OnSuccessfullConnect;
public Callback OnLobbiesListReceived; public Callback OnLobbiesListReceived;
public Callback OnLobbyJoinSuccess; public LobbyJoinCallback OnLobbyJoinSuccess;
public Callback OnLobbiesReceivedAndWaitingForHost; public Callback OnLobbiesReceivedAndWaitingForHost;
public LobbyCallback OnLobbyCreated; public LobbyCallback OnLobbyCreated;
public LobbyCallback OnLobbyLeave; public LobbyCallback OnLobbyLeave;
@@ -124,7 +125,8 @@ namespace Client
OnLobbyCreated?.Invoke(lobbyCreatedID); OnLobbyCreated?.Invoke(lobbyCreatedID);
break; break;
case LobbyIdentifier.JOIN_SUCCESS: case LobbyIdentifier.JOIN_SUCCESS:
OnLobbyJoinSuccess?.Invoke();
OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload));
break; break;
case LobbyIdentifier.LEAVE: case LobbyIdentifier.LEAVE:
int lobbyLeaveID = JSONConvert.GetLobbyID(payload); int lobbyLeaveID = JSONConvert.GetLobbyID(payload);

View File

@@ -85,14 +85,14 @@ namespace Client
private void joinLobby() private void joinLobby()
{ {
// lobby die je wilt joinen verwijderen
// nieuwe binnengekregen lobby toevoegen
client.OnLobbyJoinSuccess = OnLobbyJoinSuccess; client.OnLobbyJoinSuccess = OnLobbyJoinSuccess;
client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID)); client.SendMessage(JSONConvert.ConstructLobbyJoinMessage(SelectedLobby.ID));
} }
private void OnLobbyJoinSuccess() private void OnLobbyJoinSuccess(bool isHost)
{ {
ClientData.Instance.User.Host = isHost;
startGameInLobby(); startGameInLobby();
} }

View File

@@ -175,8 +175,9 @@ namespace Server.Models
break; break;
case LobbyIdentifier.JOIN: case LobbyIdentifier.JOIN:
int id = JSONConvert.GetLobbyID(payload); int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User,id); bool isHost;
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); ServerCommunication.INSTANCE.JoinLobby(this.User,id, out isHost);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage(isHost));
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray())); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
break; break;
case LobbyIdentifier.LEAVE: case LobbyIdentifier.LEAVE:

View File

@@ -201,8 +201,9 @@ namespace Server.Models
return lobby.ID; 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) foreach (Lobby l in lobbies)
{ {
if (l.ID == id) if (l.ID == id)
@@ -210,6 +211,7 @@ namespace Server.Models
if (l.Users.Count == 0) if (l.Users.Count == 0)
{ {
user.Host = true; user.Host = true;
isHost = true;
} }
AddToLobby(l, user); AddToLobby(l, user);
Debug.WriteLine($"{user.Username} joined lobby with id {id}"); Debug.WriteLine($"{user.Username} joined lobby with id {id}");

View File

@@ -133,9 +133,16 @@ namespace SharedClientServer
return dynamicAsObject.ToObject<Lobby>(); return dynamicAsObject.ToObject<Lobby>();
} }
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 #endregion