From 383fa456fc79b7d37396b52dbdeb0631de16c48b Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Mon, 7 Jun 2021 17:47:22 +0200 Subject: [PATCH] fix: fixed bug where spectators joining later won't be able to send events --- .../server/lobbymanager/LobbyConnection.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java index 5ac3885..1eca31c 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/LobbyConnection.java @@ -11,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.messages.server.*; import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.server.Server; import uulm.teamname.marvelous.server.lobby.Lobby; +import uulm.teamname.marvelous.server.netconnector.ClientState; import uulm.teamname.marvelous.server.netconnector.SUID; import uulm.teamname.marvelous.server.netconnector.UserManager; @@ -48,6 +49,9 @@ public class LobbyConnection implements Runnable { } public void addParticipant(Participant participant) { + if (this.state == LobbyConnectionState.Started) { + participant.getClient().state = ClientState.Playing; + } if(participant.type == ParticipantType.Spectator) { spectators.add(participant); return; @@ -60,19 +64,19 @@ public class LobbyConnection implements Runnable { } } + /** Disconnects a participant from the LobbyConnection */ public void removeParticipant(Participant participant) { removeParticipant(participant, "You have been disconnected."); } + /** Disconnects a participant from the LobbyConnection */ public void removeParticipant(Participant participant, String reason) { LobbyManager.getInstance().removeParticipant(participant); UserManager.getInstance().removeClient(participant.getClient(), reason); if(participant.type == ParticipantType.Spectator) { spectators.remove(participant); - } - - if(participant.type == ParticipantType.PlayerOne) { + } else if(participant.type == ParticipantType.PlayerOne) { player1 = null; }else { player2 = null; @@ -104,6 +108,8 @@ public class LobbyConnection implements Runnable { return player2; } + public HashSet getSpectators() {return spectators;} + public boolean hasPlayer1() { return player1 != null; } @@ -246,10 +252,12 @@ public class LobbyConnection implements Runnable { public void sendEvents(Participant recipient, Event... events) { - EventMessage message = new EventMessage(); - message.messages = events; + if (recipient != null) { + EventMessage message = new EventMessage(); + message.messages = events; - recipient.sendMessage(message); + recipient.sendMessage(message); + } } public void broadcastEvents(List events) {