feat: singleton MessageRelay and better prototype of LobbyManager

This commit is contained in:
Yannik Bretschneider 2021-05-18 16:57:03 +02:00
parent a4fd7c1ef5
commit d306b2b736
2 changed files with 27 additions and 4 deletions

View File

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

View File

@ -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<WebSocket, Lobby> lobbies;
private final HashMap<Lobby, WebSocket> 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