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 93873e0..975699c 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 @@ -18,6 +18,7 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; +import java.util.stream.Collectors; /** * A class that handles the connection to the lobby. It contains the participants inside of the lobby. The class @@ -105,7 +106,7 @@ public class LobbyConnection implements Runnable { Logger.info("Lobby '{}' is terminating. Exiting...", gameID); return; } else if (currentMessage == null) { - Logger.trace("Message was null, continuing"); // TODO: remove for production + // Logger.trace("Message was null, continuing"); // TODO: remove for production continue; } else if (currentMessage.item2 instanceof CharacterSelectionMessage) { var origin = currentMessage.item1; @@ -115,10 +116,6 @@ public class LobbyConnection implements Runnable { origin.name); characterSelectionActive = receiveCharacterSelection(origin, message); - Logger.trace("Sending confirmSelectionMessage to player1"); - var replyMessage = new ConfirmSelectionMessage(); - replyMessage.selectionComplete = !characterSelectionActive; - sendMessage(origin, replyMessage); } else if (currentMessage.item2 instanceof RequestMessage) { var origin = currentMessage.item1; // var message = (RequestMessage) currentMessage.item2; this is ignored here @@ -131,8 +128,7 @@ public class LobbyConnection implements Runnable { } else { Logger.warn("Message that isn't of type RequestMessage or CharacacterSelectionMessage" + "received in Lobby. This is probably a bug."); - sendError( - currentMessage.item1, + sendError(currentMessage.item1, "Message couldn't be processed by the lobby, as its type was invalid"); } } @@ -227,7 +223,7 @@ public class LobbyConnection implements Runnable { } private void receiveRequests(Participant origin, RequestMessage message) { - System.out.println("REMOVE THIS PRINTLN: received request message " + message.toString()); + // TODO: implement } /** @@ -237,8 +233,8 @@ public class LobbyConnection implements Runnable { * * @param origin is the participant that sent the message * @param message is the message triggering the CharacterSelection method - * @return true if CharacterSelection is now completed, and false otherwise. If the method is called outside of the - * CharacterSelection phase, true is returned, as the characterSelection is after all done + * @return false if CharacterSelection is now completed, and true otherwise. If the method is called outside of the + * CharacterSelection phase, false is returned, as the characterSelection is after all done */ private boolean receiveCharacterSelection(Participant origin, CharacterSelectionMessage message) { if (this.characterSelectionActive) { @@ -253,6 +249,12 @@ public class LobbyConnection implements Runnable { selectionPossibilities.item1, message.characters); Logger.info("Player 1 has selected their characters"); + Logger.trace("Sending selection confirmation message to Player1"); + + Logger.trace("Sending confirmSelectionMessage to player1"); + var replyMessage = new ConfirmSelectionMessage(); + replyMessage.selectionComplete = !characterSelectionActive; + sendMessage(origin, replyMessage); } else { Logger.debug("Player 1 tried to select characters twice, sending error"); sendError(origin, @@ -262,13 +264,17 @@ public class LobbyConnection implements Runnable { } case PlayerTwo -> { if (playerTwoSelection == null) { - Logger.trace("Checking whether there are actually 6 choices"); playerTwoSelection = getChosenCharacters( selectionPossibilities.item1, message.characters); - Logger.info("Player 1 has selected their characters"); + Logger.info("Player 2 has selected their characters"); + + Logger.trace("Sending confirmSelectionMessage to player2"); + var replyMessage = new ConfirmSelectionMessage(); + replyMessage.selectionComplete = !characterSelectionActive; + sendMessage(origin, replyMessage); } else { - Logger.debug("Player 1 tried to select characters twice, sending error"); + Logger.debug("Player 2 tried to select characters twice, sending error"); sendError(origin, "Cannot select characters as characters were already selected"); } @@ -284,7 +290,7 @@ public class LobbyConnection implements Runnable { "the CharacterSelectionPhase, sending error", origin.name); sendError(origin, "The character selection phase is already over"); } - return !characterSelectionActive || (playerOneSelection != null && playerTwoSelection != null); + return (playerOneSelection == null || playerTwoSelection == null) && characterSelectionActive; } private CharacterProperties[] getChosenCharacters(CharacterProperties[] possibleChoices, Boolean[] choices) { @@ -309,13 +315,24 @@ public class LobbyConnection implements Runnable { } private void initializeLobby() { - Logger.trace("Initializing lobby..."); + Logger.trace("Transforming chosen characters into integer array of IDs"); + var player1CharacterChoiceIDs = Arrays.stream(playerOneSelection) + .map(properties -> properties.characterID) + .collect(Collectors.toList()); + + var player2CharacterChoiceIDs = Arrays.stream(playerOneSelection) + .map(properties -> properties.characterID) + .collect(Collectors.toList()); + + Logger.info("Initializing lobby..."); this.lobby = new Lobby( gameID, this, Server.getPartyConfig(), Server.getCharacterConfig(), - Server.getScenarioConfig()); + Server.getScenarioConfig(), + player1CharacterChoiceIDs, + player2CharacterChoiceIDs); } /** @@ -466,6 +483,10 @@ public class LobbyConnection implements Runnable { broadcast(message); } + public void broadcastEvents(List events) { + broadcastEvents(events.toArray(new Event[0])); + } + public void broadcastToAllExcept(Participant except, Event... events) { var message = new EventMessage(); message.messages = events;