[FIX] lobbies now get correctly updated when someone leaves or joins

This commit is contained in:
Sem van der Hoeven
2020-10-23 12:26:31 +02:00
parent f5d55ef405
commit 9e829c47ff

View File

@@ -28,7 +28,7 @@ namespace Client
public LobbyJoinCallback OnLobbyJoinSuccess;
public Callback OnLobbiesReceivedAndWaitingForHost;
public Callback OnServerDisconnect;
public Callback OnClientJoinLobby;
public Callback OnLobbyUpdate;
public LobbyCallback OnLobbyCreated;
public LobbyCallback OnLobbyLeave;
public RandomWord RandomWord;
@@ -51,6 +51,7 @@ namespace Client
this.tcpClient.EndConnect(ar);
this.stream = tcpClient.GetStream();
OnSuccessfullConnect?.Invoke();
OnLobbyUpdate = updateGameLobby;
SendMessage(JSONConvert.ConstructUsernameMessage(username));
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
@@ -142,7 +143,7 @@ namespace Client
Lobbies = JSONConvert.GetLobbiesFromMessage(payload);
OnLobbiesListReceived?.Invoke();
OnLobbiesReceivedAndWaitingForHost?.Invoke();
OnClientJoinLobby?.Invoke();
OnLobbyUpdate?.Invoke();
break;
case LobbyIdentifier.HOST:
// we receive this when the server has made us a host of a new lobby
@@ -150,21 +151,10 @@ namespace Client
Debug.WriteLine("[CLIENT] got lobby object");
int lobbyCreatedID = JSONConvert.GetLobbyID(payload);
OnLobbyCreated?.Invoke(lobbyCreatedID);
OnLobbyUpdate?.Invoke();
break;
case LobbyIdentifier.JOIN_SUCCESS:
OnLobbyJoinSuccess?.Invoke(JSONConvert.GetLobbyJoinIsHost(payload));
OnClientJoinLobby = () =>
{
foreach (var item in Lobbies)
{
Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count);
if (item.ID == data.Lobby.ID)
ViewModels.ViewModelGame.HandleIncomingPlayer(item);
}
};
break;
case LobbyIdentifier.LEAVE:
int lobbyLeaveID = JSONConvert.GetLobbyID(payload);
@@ -195,6 +185,16 @@ namespace Client
}
private void updateGameLobby()
{
foreach (var item in Lobbies)
{
Debug.WriteLine("[CLIENT] lobby data: {0}", item.Users.Count);
if (data.Lobby != null && item.ID == data.Lobby.ID)
ViewModels.ViewModelGame.HandleIncomingPlayer(item);
}
}
public void SendMessage(byte[] message)
{
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));