diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java index 36fe472..12ed944 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java @@ -8,6 +8,7 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.GameStateModel; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.GameController; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge.LoungeApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge.LoungeSceneViewController; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger; @@ -40,10 +41,15 @@ public class Client { public ClientPinger clientPinger; private ChatApp chatApp; - private GUI chatGui; + //private GUI chatGui; private ClientModel clientModel; private GameStateModel gameStateModel; private GameController gameController; + + private GUI gui; + + private LoungeApp loungeApp; + //private GUI loungeGui; private LoungeSceneViewController loungeSceneViewController; /** @@ -73,12 +79,17 @@ public class Client { systemName = username; } sendMsgToServer(Protocol.clientLogin + "$" + systemName); + this.chatApp = new ChatApp(new ClientModel(systemName, this)); + //this.chatGui = new GUI(this.chatApp); clientPinger = new ClientPinger(this, this.socket); this.gameStateModel = new GameStateModel(); this.chatApp = new ChatApp(new ClientModel(systemName, this)); ChatApp.setGameController(new GameController(ChatApp.getClientModel(), gameStateModel)); this.chatGui = new GUI(this.chatApp); this.gameController = new GameController(ChatApp.getClientModel(), gameStateModel); + this.loungeApp = new LoungeApp(ChatApp.getClientModel()); + //this.loungeGui = new GUI(this.loungeApp); + this.gui = new GUI(this.chatApp,this.loungeApp); this.loungeSceneViewController = new LoungeSceneViewController(); LoungeSceneViewController.setClient(ChatApp.getClientModel()); } catch (IOException e) { @@ -262,7 +273,7 @@ public class Client { cP.start(); client.userInputListener(); //this one blocks. //Start the GUI - GUI gui = new GUI(client.chatApp); + GUI gui = new GUI(client.chatApp,client.loungeApp); Thread guiThread = new Thread(gui); guiThread.start(); LOGGER.info("7"); @@ -292,7 +303,7 @@ public class Client { cP.start(); client.userInputListener(); //this one blocks. LOGGER.info("7.1"); - Thread guiThread = new Thread(client.chatGui); + Thread guiThread = new Thread(client.gui); LOGGER.info("8"); guiThread.start(); LOGGER.info("9"); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ChatApp.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ChatApp.java index 2aa522c..a89f608 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ChatApp.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ChatApp.java @@ -1,9 +1,7 @@ -package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui; +package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel; -import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController; -import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.game.GameController; import java.net.URL; import java.util.Objects; import javafx.application.Application; @@ -26,6 +24,8 @@ public class ChatApp extends Application { private ClientModel cModel; private GameController gameC; + private static LoungeSceneViewController loungeSceneViewController; + public ChatApp() { super(); LOGGER.info("Empty ChatApp constructor got called: "); @@ -88,6 +88,10 @@ public class ChatApp extends Application { return chatController; } + public static void setLoungeSceneViewController(LoungeSceneViewController controller) { + loungeSceneViewController = controller; + } + /** * The main entry point for all JavaFX applications. The start method is called after the init @@ -127,6 +131,7 @@ public class ChatApp extends Application { Objects.requireNonNull(chatResource)); primaryStage.setTitle("Night Train To Budapest"); primaryStage.setResizable(true); + primaryStage.setTitle("Chat"); primaryStage.show(); diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/GUI.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/GUI.java index e06bd0b..f3049be 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/GUI.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/GUI.java @@ -1,6 +1,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui; import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatApp; import javafx.application.Application; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -11,12 +12,21 @@ public class GUI implements Runnable { public static final BudaLogConfig l = new BudaLogConfig(LOGGER); private ChatApp chatApp; - + private LoungeApp loungeApp; public GUI(ChatApp chatApp) { this.chatApp = chatApp; } + public GUI(LoungeApp loungeApp) { + this.loungeApp = loungeApp; + } + + public GUI(ChatApp chatApp, + LoungeApp loungeApp) { + this.chatApp = chatApp; + this.loungeApp = loungeApp; + } /** * When an object implementing interface {@code Runnable} is used to create a thread, starting the @@ -31,6 +41,7 @@ public class GUI implements Runnable { @Override public void run() { LOGGER.info("here"); - Application.launch(this.chatApp.getClass()); + //Application.launch(this.chatApp.getClass()); + Application.launch(this.loungeApp.getClass()); } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeApp.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeApp.java new file mode 100644 index 0000000..ce2df98 --- /dev/null +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeApp.java @@ -0,0 +1,128 @@ +package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge; + +import ch.unibas.dmi.dbis.cs108.BudaLogConfig; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatApp; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatController; +import java.net.URL; +import java.util.Objects; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Class for debugging the lounge gui scene + */ +public class LoungeApp extends Application { + + public static final Logger LOGGER = LogManager.getLogger(ChatApp.class); + public static final BudaLogConfig l = new BudaLogConfig(LOGGER); + + private static ClientModel clientModel; + private static ChatController chatController; + private ClientModel cModel; + + private static LoungeSceneViewController loungeSceneViewController; + + public LoungeApp() { + super(); + LOGGER.info("Empty ChatApp constructor got called: "); + } + + public LoungeApp(ClientModel clientM) { + clientModel = clientM; + chatController = new ChatController(clientM); + } + + /** + * Sets the ChatController for the Application, needs to be static, but only one application can + * be launched per programm + * + * @param chatC the ChatController to be linked to this chatApp + */ + public static void setChatController(ChatController chatC) { + chatController = chatC; + } + + /** + * Sets the non-static ClientModel field of this class + * + * @param cModel the non static ClientModel to be added + */ + public void setcModel(ClientModel cModel) { + this.cModel = cModel; + } + + public ClientModel getcModel() { + return cModel; + } + + public static void setClientModel(ClientModel clientM) { + clientModel = clientM; + } + + public static ClientModel getClientModel() { + return clientModel; + } + + public ChatController getChatController() { + return chatController; + } + + public static void setLoungeSceneViewController(LoungeSceneViewController controller) { + loungeSceneViewController = controller; + } + + + /** + * The main entry point for all JavaFX applications. The start method is called after the init + * method has returned, and after the system is ready for the application to begin running. + * + *

+ * NOTE: This method is called on the JavaFX Application Thread. + *

+ * + * @param primaryStage the primary stage for this application, onto which the application scene + * can be set. Applications may create other stages, if needed, but they will + * not be primary stages. + * @throws Exception if something goes wrong + */ + @Override + public void start(Stage primaryStage) throws Exception { + this.setcModel(clientModel); + URL resource = ChatApp.class.getResource( + "LoungeSceneView.fxml"); + if (resource == null) { + LOGGER.info("File wasnt found. Name: LoungeSceneView.fxml"); + } + //ChatApp chatApp = new ChatApp(new ClientModel()); + try { + Parent root = FXMLLoader.load( + Objects.requireNonNull(LoungeApp.class.getResource( + "LoungeSceneView.fxml"))); + // TODO bin chatController.getChatPaneRoot() border to root border for rezising +// loungeSceneViewController.getChatAreaHBox().getChildren() +// .add(FXMLLoader.load(Objects.requireNonNull(ChatApp.class.getResource("ChatView.fxml")))); + Scene scene = new Scene(root); + scene.setRoot(root); + primaryStage.setScene(scene); + } catch (Exception e) { + e.printStackTrace(); + } + primaryStage.setResizable(true); + primaryStage.setTitle("Lounge"); + primaryStage.show(); + + + } + + public static void main(String[] args) { + launch(args); + } + +} + diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneViewController.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneViewController.java index 7f3b1c7..479d0a1 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneViewController.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneViewController.java @@ -1,6 +1,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.ClientModel; +import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.chat.ChatApp; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.ChangeNameButtonPressedEventHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.LeaveServerButtonPressedEventHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; @@ -9,6 +10,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ResourceBundle; +import javafx.application.Application; import javafx.beans.binding.StringBinding; import javafx.beans.property.SimpleListProperty; import javafx.beans.property.SimpleMapProperty; @@ -57,6 +59,7 @@ public class LoungeSceneViewController implements Initializable { private ToolBar NTtBToolBar; public static ClientModel client; + public static ChatApp chatApp; private ObservableMap> lobbyToMemberssMap; private HashMap clientToLobbyMap; @@ -76,11 +79,13 @@ public class LoungeSceneViewController implements Initializable { */ @Override public void initialize(URL location, ResourceBundle resources) { + ChatApp.setLoungeSceneViewController(this); this.protocol = new Protocol(); ChangeNameButton.setOnAction(event -> changeName()); LeaveServerButton.setOnAction(new LeaveServerButtonPressedEventHandler()); newGameButton.setOnAction(event -> newGame()); - + LobbyListView.setVisible(true); + ClientListView.setVisible(true); ClientListView.setItems(client.getAllClients()); LobbyListView.setPlaceholder(new Text("No open lobbies!")); client.getAllClients().addListener(new ListChangeListener() { @@ -136,8 +141,9 @@ public class LoungeSceneViewController implements Initializable { HBox lobby = new HBox(); Label idLabel = new Label(); Label adminLabel = new Label(); - idLabel.textProperty().bind(id); - adminLabel.textProperty().bind(admin); + idLabel.setText(lobbyID); + adminLabel.setText(adminName); + startOrJoin.setVisible(true); lobby.getChildren().add(idLabel); lobby.getChildren().add(adminLabel); lobby.getChildren().add(startOrJoin); @@ -151,6 +157,7 @@ public class LoungeSceneViewController implements Initializable { } lobby.setId(lobbyID); lobbyToMemberssMap.put(lobbyID, members.getItems()); + lobby.setVisible(true); LobbyListView.getItems().add(lobby); } @@ -164,6 +171,11 @@ public class LoungeSceneViewController implements Initializable { ; + /** + * Used to add a new player to the list of players. + * "NPLOS" {@link ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters} + * @param s + */ public void addClientToList(String s) { ClientListView.getItems().add(new SimpleStringProperty(s)); } @@ -174,13 +186,14 @@ public class LoungeSceneViewController implements Initializable { public void changeName() { - TextField name = new TextField("Enter new name!"); + TextField name = new TextField(); + name.setPromptText("Enter new Nickname!"); this.NTtBToolBar.getItems().add(name); name.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { client.getClient().sendMsgToServer(Protocol.nameChange + "$" + name.getText()); - NTtBToolBar.getItems().remove(NTtBToolBar.getItems().size()); + NTtBToolBar.getItems().remove(name); } }); } @@ -204,4 +217,12 @@ public class LoungeSceneViewController implements Initializable { public static void setClient(ClientModel client) { LoungeSceneViewController.client = client; } + + public HBox getChatAreaHBox() { + return ChatAreaHBox; + } + + public void setChatAreaHBox(HBox chatAreaHBox) { + ChatAreaHBox = chatAreaHBox; + } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java index c0bd603..3186285 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java @@ -6,7 +6,8 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.helpers; public class GuiParameters { /** - * Tells GUI to update the gameStateModel, in the form {@code UPDATE$name:role:kickedOff$name:role:kickedOff} ... usw. + * Tells GUI to update the gameStateModel, in the form {@code UPDATE$name:role:kickedOff$name:role:kickedOff} + * ... usw. */ public static final String updateGameState = "UPDATE"; /** @@ -20,8 +21,8 @@ public class GuiParameters { public static final String listOfPLayers = "PLAYERS"; /** - * Tells Gui, that the passenger at position {@code position} has heard some noise - * Form: {@code NOISE$position$} + * Tells Gui, that the passenger at position {@code position} has heard some noise Form: {@code + * NOISE$position$} */ public static final String noiseHeardAtPosition = "NOISE"; @@ -40,15 +41,15 @@ public class GuiParameters { */ public static final String viewChangeToGame = "VCGAME"; + /** - * Tells Gui, who the members of a specified Lobby are. - * Form: {@code LMEMBS$:::<..>} + * Tells Gui, who the members of a specified Lobby are. Form: {@code LMEMBS$:::<..>} */ public static final String getMembersInLobby = "LMEMBS"; /** - * Tells Gui, that a new Lobby has been created. - * Form: {@code NLOBBY$:} + * Tells Gui, that a new Lobby has been created. Form: {@code NLOBBY$:} */ public static final String changeToLobby = "LMEMBS"; @@ -69,8 +70,17 @@ public class GuiParameters { public static final String newLobbyCreated = "NLOBBY"; /** - * Tells Gui, to add a player to a lobby. - * Form: {@code NMEMB$:} + * Tells Gui, to add a player to a lobby. Form: {@code NMEMB$:} */ public static final String addNewMemberToLobby = "NMEMB"; + + /** + * Indicates a player changed their username. Form: {@code NCHANG$:} + */ + public static final String nameChanged = "NCHANG"; + + /** + * Indicates a player has joined the server. Form: {@code NPLOS$} + */ + public static final String newPlayerOnServer = "NPLOS"; } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java index 0024bed..cfdf602 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java @@ -5,6 +5,7 @@ import ch.unibas.dmi.dbis.cs108.gamelogic.Game; import ch.unibas.dmi.dbis.cs108.gamelogic.TrainOverflow; import ch.unibas.dmi.dbis.cs108.gamelogic.VoteHandler; import ch.unibas.dmi.dbis.cs108.highscore.OgGhostHighScore; +import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; @@ -126,8 +127,12 @@ public class ClientHandler implements Runnable { */ public void changeUsername(String newName) { String helper = this.getClientUserName(); + String oldName = getClientUserName(); this.clientUserName = nameDuplicateChecker.checkName(newName); + guiUpdateAll(Protocol.printToGUI + "$" + GuiParameters.nameChanged + "$" + oldName + ":" + + getClientUserName()); sendMsgToClient(Protocol.changedUserName + "$" + newName); + broadcastAnnouncementToAll(helper + " has changed their nickname to " + clientUserName); try { getLobby().getGame().getGameState().changeUsername(helper, newName); @@ -245,6 +250,13 @@ public class ClientHandler implements Runnable { } } + public static void guiUpdateAll(String msg) { + System.out.println(msg); + for (ClientHandler client : connectedClients) { + client.sendMsgToClient(msg); + } + } + /** * Broadcasts a non-chat Message to all clients in the same lobby. This can be used for server * messages / announcements rather than chat messages. The message will be printed to the user @@ -306,7 +318,8 @@ public class ClientHandler implements Runnable { } /** - * Sends a given message to all connected client. The message has to already be protocol-formatted. + * Sends a given message to all connected client. The message has to already be + * protocol-formatted. * * @param msg the given message. Should already be protocol-formatted. */ @@ -317,7 +330,9 @@ public class ClientHandler implements Runnable { } /** - * Sends a Message to all clients in the same lobby. The message has to already be protocol-formatted. + * Sends a Message to all clients in the same lobby. The message has to already be + * protocol-formatted. + * * @param msg the given message. Should already be protocol-formatted. */ public void sendMsgToClientsInLobby(String msg) { @@ -460,6 +475,8 @@ public class ClientHandler implements Runnable { public void createNewLobby() { if (Lobby.clientIsInLobby(this) == -1) { Lobby newGame = new Lobby(this); + guiUpdateAll(Protocol.printToGUI + "$" + GuiParameters.newLobbyCreated + "$" + getLobby() + .getLobbyID() + ":" + getClientUserName()); } else { sendAnnouncementToClient("You are already in lobby nr. " + Lobby.clientIsInLobby(this)); } @@ -476,6 +493,8 @@ public class ClientHandler implements Runnable { if (l != null) { if (l.getLobbyIsOpen()) { l.addPlayer(this); + guiUpdateAll(Protocol.printToGUI + "$" + GuiParameters.addNewMemberToLobby + "$" + i + ":" + + getClientUserName()); } else { sendAnnouncementToClient("The game in Lobby " + l.getLobbyID() + " has already started, or the lobby is already full."); @@ -571,11 +590,11 @@ public class ClientHandler implements Runnable { sendAnnouncementToClient("No Games"); } else { sendAnnouncementToClient("Open Games (i.e. open Lobbies):"); - for (Lobby l : Lobby.lobbies) { - if (l.getLobbyIsOpen()) { - sendAnnouncementToClient(" - Lobby Nr. " + l.getLobbyID()); - } + for (Lobby l : Lobby.lobbies) { + if (l.getLobbyIsOpen()) { + sendAnnouncementToClient(" - Lobby Nr. " + l.getLobbyID()); } + } sendAnnouncementToClient("Running Games:"); try { for (Game runningGame : Lobby.runningGames) { @@ -631,7 +650,7 @@ public class ClientHandler implements Runnable { public void sendHighScoreList() { String list = OgGhostHighScore.formatGhostHighscoreList(); String[] listarray = list.split("\\R"); - for (String s: listarray) { + for (String s : listarray) { sendAnnouncementToClient(s); } } diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java index 1ee67ab..b5d499e 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/JServerProtocolParser.java @@ -82,7 +82,6 @@ public class JServerProtocolParser { try { int i = Integer.parseInt(msg.substring(6, 7)); h.joinLobby(i); - h.sendMsgToClient(Protocol.printToGUI + "$" + GuiParameters.viewChangeToLobby + "$"); } catch (Exception e) { h.sendMsgToClient(Protocol.printToClientConsole + "$Invalid input. Please use JOINL$1 to join Lobby 1, for example."); @@ -90,7 +89,12 @@ public class JServerProtocolParser { break; case Protocol.createNewLobby: h.createNewLobby(); - h.sendMsgToClient(Protocol.printToGUI + "$" + GuiParameters.viewChangeToLobby+ "$"); + h.sendMsgToClient( + Protocol.printToGUI + "$" + GuiParameters.newLobbyCreated + "$" + h.getLobby() + .getLobbyID() + ":" + h.getClientUserName()); + h.guiUpdateAll(Protocol.printToGUI + "$" + GuiParameters.newLobbyCreated + "$" + h.getLobby() + .getLobbyID() + ":" + h.getClientUserName()); + LOGGER.info("Here"); break; case Protocol.listLobbies: h.listLobbies(); diff --git a/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneView.fxml b/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneView.fxml index 52c4b4e..0de7e45 100644 --- a/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneView.fxml +++ b/src/main/resources/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/lounge/LoungeSceneView.fxml @@ -3,12 +3,10 @@ - - @@ -23,7 +21,7 @@
-