From d306b2b736f1347d5a98c0c1a83fc0251e0dae09 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 18 May 2021 16:57:03 +0200 Subject: [PATCH] feat: singleton MessageRelay and better prototype of LobbyManager --- .../server/LobbyManager/LobbyConnection.java | 19 ++++++++++++++++--- .../server/LobbyManager/MessageRelay.java | 12 +++++++++++- 2 files changed, 27 insertions(+), 4 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 34259c3..1b8f002 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,8 +1,9 @@ package uulm.teamname.marvelous.server.LobbyManager; +import org.java_websocket.WebSocket; +import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.server.Lobby.Lobby; -import java.net.http.WebSocket; import java.util.HashSet; /** @@ -28,14 +29,14 @@ public class LobbyConnection { * @return whether there is a player1 */ public boolean hasPlayer1() { - return player1 == null; + return player1 != null; } /** * @return whether there is a player2 */ public boolean hasPlayer2() { - return player2 == null; + return player2 != null; } /** @@ -96,4 +97,16 @@ public class LobbyConnection { public boolean contains(WebSocket webSocket) { return player1 == webSocket || player2 == webSocket || spectators.contains(webSocket); } + + // Methods to send events + + public void sendEvents(Event[] events, MessageSource target) { + // TODO: implement + MessageRelay.getInstance(); + } + + public void broadcastEvents(Event[] events) { + // TODO: implement + MessageRelay.getInstance(); + } } diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/MessageRelay.java b/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/MessageRelay.java index 9f73b41..f05a3ff 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/MessageRelay.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/MessageRelay.java @@ -6,14 +6,24 @@ import uulm.teamname.marvelous.server.Lobby.Lobby; import java.util.HashMap; public class MessageRelay { + + public static MessageRelay instance; + private final HashMap lobbies; private final HashMap sockets; - public MessageRelay() { + private MessageRelay() { this.lobbies = new HashMap<>(); this.sockets = new HashMap<>(); } + public static MessageRelay getInstance() { + if (instance == null) { + instance = new MessageRelay(); + } + return instance; + } + public void relayMessage (WebSocket conn, String message) { var targetLobby = lobbies.get(conn); // TODO: Parse JSON