From 7a8857da0cf62fc38a3fbdc4c1f2fc7fa28d5199 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Wed, 19 May 2021 13:12:14 +0200 Subject: [PATCH] feat: changed WebSockets to Participants, and created those --- .../server/LobbyManager/LobbyConnection.java | 17 ++++----- .../server/LobbyManager/MessageRelay.java | 2 +- .../server/LobbyManager/Participant.java | 38 +++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/Participant.java 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 0768ff4..7ee0699 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 @@ -13,9 +13,8 @@ import java.util.HashSet; */ public class LobbyConnection { private Lobby lobby; - private WebSocket player1; - private WebSocket player2; - private HashSet spectators; + private Participant player1, player2; + private HashSet spectators; /** * Creates a new LobbyConnection from a given lobby @@ -45,7 +44,7 @@ public class LobbyConnection { * @param player is the websocket to be added * @return true if added successfully, and false otherwise */ - public boolean addPlayer1(WebSocket player) { + public boolean addPlayer1(Participant player) { if (this.contains(player)) return false; if (player1 == null) { player1 = player; @@ -60,7 +59,7 @@ public class LobbyConnection { * @param player is the websocket to be added * @return true if added successfully, and false otherwise */ - public boolean addPlayer2(WebSocket player) { + public boolean addPlayer2(Participant player) { if (this.contains(player)) return false; if (player2 == null) { player2 = player; @@ -75,14 +74,14 @@ public class LobbyConnection { * @param player is the websocket to be added * @return true if added successfully, and false otherwise */ - public boolean addPlayer(WebSocket player) { + public boolean addPlayer(Participant player) { if (!addPlayer1(player)) { return addPlayer2(player); } return true; } - public boolean removePlayer(WebSocket player) { + public boolean removePlayer(Participant player) { if (player1 == player) { player1 = null; return true; @@ -101,8 +100,8 @@ public class LobbyConnection { // Methods to send events - public void sendEvents(ParticipantType target, Event... events) { - MessageRelay.getInstance().sendMessage(this, target, events); + public void sendEvents(Participant recipient, Event... events) { + MessageRelay.getInstance().sendMessage(this, recipient, events); } public void broadcastEvents(Event... events) { 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 d55c6e1..a2a3584 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 @@ -30,7 +30,7 @@ public class MessageRelay { // TODO: send to target lobby } - public void sendMessage (LobbyConnection origin, ParticipantType target, Event[] events) { + public void sendMessage (LobbyConnection origin, Participant recipient, Event[] events) { switch (target) { case Player1 -> { diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/Participant.java b/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/Participant.java new file mode 100644 index 0000000..bd21e27 --- /dev/null +++ b/Server/src/main/java/uulm/teamname/marvelous/server/LobbyManager/Participant.java @@ -0,0 +1,38 @@ +package uulm.teamname.marvelous.server.LobbyManager; + +import org.java_websocket.WebSocket; +import uulm.teamname.marvelous.gamelibrary.gamelogic.ParticipantType; + +import java.util.Objects; + +public class Participant { + + /** The Websocket to contact the participant with */ + public final WebSocket connection; + + /** The type (as in role) of participant */ + public final ParticipantType type; + + /** Whether the participant is an AI */ + public final boolean AI; + + /** Creates a new {@link Participant} */ + public Participant (WebSocket connection, ParticipantType type, boolean AI) { + this.connection = connection; + this.type = type; + this.AI = AI; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Participant that = (Participant) o; + return AI == that.AI && Objects.equals(connection, that.connection) && type == that.type; + } + + @Override + public int hashCode() { + return Objects.hash(connection, type, AI); + } +}