Merge branch 'master' into feature/jsonForWords

This commit is contained in:
SemvdH
2020-10-22 17:01:28 +02:00
committed by GitHub
5 changed files with 27 additions and 8 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;
@@ -128,7 +129,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

@@ -183,6 +183,10 @@ namespace Server.Models
int id = JSONConvert.GetLobbyID(payload); int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User, id); ServerCommunication.INSTANCE.JoinLobby(this.User, id);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage()); 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())); ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
//Task.Run(SendLobbyData); //Task.Run(SendLobbyData);

View File

@@ -202,12 +202,18 @@ 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)
{ {
if (l.Users.Count == 0)
{
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}");
break; break;

View File

@@ -136,9 +136,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