From ffb37def471c7fb056ad6c76a68baab41c3e674f Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 1 Jun 2021 17:59:29 +0200 Subject: [PATCH] feat: implemented utility methods for LobbyConnection --- .../server/lobbymanager/LobbyConnection.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 e55b653..7e8b397 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 @@ -1,6 +1,7 @@ package uulm.teamname.marvelous.server.lobbymanager; import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.gamelogic.ParticipantType; import uulm.teamname.marvelous.server.lobby.Lobby; import java.util.HashSet; @@ -37,6 +38,14 @@ public class LobbyConnection { return player2 != null; } + public Participant getPlayer1() { + return player1; + } + + public Participant getPlayer2() { + return player2; + } + /** * Adds a new player into the player1 slot * @param player is the websocket to be added @@ -106,4 +115,24 @@ public class LobbyConnection { // TODO: implement MessageRelay.getInstance().broadcastEvents(this, events); } + + public void broadcastToAllExcept(Participant except, Event... events) { + // TODO: implement + var messageRelayInstance = MessageRelay.getInstance(); + if (except.type == ParticipantType.Spectator) { + spectators.stream() + .filter(spectator -> !spectator.equals(except)) + .forEach(spectator -> messageRelayInstance.sendMessage(this, spectator, events)); + messageRelayInstance.sendMessage(this, player1, events); + messageRelayInstance.sendMessage(this, player2, events); + } else { + messageRelayInstance.sendMessage(this, except.equals(player1) ? player2 : player1, events); + spectators.forEach(spectator -> messageRelayInstance.sendMessage(this, spectator, events)); + } + } + + /** Kills all connections to client, as well as the lobby */ + public void terminateConnection() { + MessageRelay.getInstance().terminate(this); + } }