feat: changed WebSockets to Participants, and created those

This commit is contained in:
Yannik Bretschneider 2021-05-19 13:12:14 +02:00
parent 2b00b1520b
commit 7a8857da0c
3 changed files with 47 additions and 10 deletions

View File

@ -13,9 +13,8 @@ import java.util.HashSet;
*/ */
public class LobbyConnection { public class LobbyConnection {
private Lobby lobby; private Lobby lobby;
private WebSocket player1; private Participant player1, player2;
private WebSocket player2; private HashSet<Participant> spectators;
private HashSet<WebSocket> spectators;
/** /**
* Creates a new LobbyConnection from a given lobby * Creates a new LobbyConnection from a given lobby
@ -45,7 +44,7 @@ public class LobbyConnection {
* @param player is the websocket to be added * @param player is the websocket to be added
* @return true if added successfully, and false otherwise * @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 (this.contains(player)) return false;
if (player1 == null) { if (player1 == null) {
player1 = player; player1 = player;
@ -60,7 +59,7 @@ public class LobbyConnection {
* @param player is the websocket to be added * @param player is the websocket to be added
* @return true if added successfully, and false otherwise * @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 (this.contains(player)) return false;
if (player2 == null) { if (player2 == null) {
player2 = player; player2 = player;
@ -75,14 +74,14 @@ public class LobbyConnection {
* @param player is the websocket to be added * @param player is the websocket to be added
* @return true if added successfully, and false otherwise * @return true if added successfully, and false otherwise
*/ */
public boolean addPlayer(WebSocket player) { public boolean addPlayer(Participant player) {
if (!addPlayer1(player)) { if (!addPlayer1(player)) {
return addPlayer2(player); return addPlayer2(player);
} }
return true; return true;
} }
public boolean removePlayer(WebSocket player) { public boolean removePlayer(Participant player) {
if (player1 == player) { if (player1 == player) {
player1 = null; player1 = null;
return true; return true;
@ -101,8 +100,8 @@ public class LobbyConnection {
// Methods to send events // Methods to send events
public void sendEvents(ParticipantType target, Event... events) { public void sendEvents(Participant recipient, Event... events) {
MessageRelay.getInstance().sendMessage(this, target, events); MessageRelay.getInstance().sendMessage(this, recipient, events);
} }
public void broadcastEvents(Event... events) { public void broadcastEvents(Event... events) {

View File

@ -30,7 +30,7 @@ public class MessageRelay {
// TODO: send to target lobby // 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) { switch (target) {
case Player1 -> { case Player1 -> {

View File

@ -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);
}
}