From 6a520ca83ae44b73d1785dd375afeda6011acd8b Mon Sep 17 00:00:00 2001 From: Sebastian Lenzlinger Date: Sat, 30 Apr 2022 17:44:30 +0200 Subject: [PATCH] Added Method for adding clients to gui view --- .../dbis/cs108/multiplayer/client/Client.java | 46 +++++++++++++------ .../multiplayer/client/gui/ClientModel.java | 15 +++--- .../gui/lounge/LoungeSceneViewController.java | 8 +++- .../multiplayer/helpers/GuiParameters.java | 21 ++------- 4 files changed, 52 insertions(+), 38 deletions(-) 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 e8b919a..f22285e 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 @@ -20,6 +20,7 @@ import java.io.*; import java.net.UnknownHostException; import java.util.Objects; import java.util.Scanner; +import javafx.beans.property.SimpleStringProperty; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -36,6 +37,7 @@ public class Client { private ChatApp chatApp; private GUI chatGui; + private ClientModel clientModel; private GameStateModel gameStateModel; private GameController gameController; @@ -70,7 +72,7 @@ public class Client { this.chatGui = new GUI(this.chatApp); clientPinger = new ClientPinger(this, this.socket); this.gameStateModel = new GameStateModel(); - this.gameController = new GameController(ChatApp.getClientModel(),gameStateModel); + this.gameController = new GameController(ChatApp.getClientModel(), gameStateModel); } catch (IOException e) { e.printStackTrace(); } @@ -133,13 +135,13 @@ public class Client { } /** - * Extracts infromation about names and positions and roles from string and adds it to - * the GameStateModel + * Extracts infromation about names and positions and roles from string and adds it to the + * GameStateModel + * * @param msg */ public void gameStateModelSetter(String msg) { - } @@ -224,6 +226,7 @@ public class Client { /** * The main Method used for testing in IDE + * * @param args not used in this main */ public static void main(String[] args) { @@ -262,8 +265,9 @@ public class Client { /** * The main-method used for the jar build of this project - * @param address the IP address of the Server (can be localhost) - * @param port the port for the connection + * + * @param address the IP address of the Server (can be localhost) + * @param port the port for the connection * @param username the username of this client */ public static void main(InetAddress address, int port, String username) { @@ -310,11 +314,14 @@ public class Client { } /** - * funnels a message to the gui, where depending on the parameter different functions/controls/methods - * of the gui are targeted. The data contains more information the gui needs - * @param parameter a string according to {@link GuiParameters} and {@link ClientGameInfoHandler} can be empty - * @param data some information in a string, separators can be $ or : - *TODO(Seraina&Sebi): evtl. auslagern? + * funnels a message to the gui, where depending on the parameter different + * functions/controls/methods of the gui are targeted. The data contains more information the gui + * needs + * + * @param parameter a string according to {@link GuiParameters} and {@link ClientGameInfoHandler} + * can be empty + * @param data some information in a string, separators can be $ or : + * TODO(Seraina&Sebi): evtl. auslagern? */ public void sendToGUI(String parameter, String data) { try { @@ -338,9 +345,11 @@ public class Client { } break; case GuiParameters.listOfLobbies: + updateListOfLobbies(data); //TODO break; case GuiParameters.listOfPLayers: + updateListOfClients(data); //TODO break; case GuiParameters.viewChangeToGame: @@ -354,7 +363,7 @@ public class Client { break; default: notificationTextDisplay(data); - //TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController? + //TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController? } } catch (Exception e) { LOGGER.warn("Communication with GUI currently not possible: " + e.getMessage()); @@ -363,9 +372,18 @@ public class Client { } + private void updateListOfClients(String data) { + String[] arr = data.split(":"); + int n = arr.length; + for (int i = 0; i < n; i = i + 2) { + ChatController.getClient().addClientToList(new SimpleStringProperty(arr[i])); + } + } + /** - * Starts a new thread, thad adds a message to notificationText in the gameController, - * waits 3 seconds and deletes it again. + * Starts a new thread, thad adds a message to notificationText in the gameController, waits 3 + * seconds and deletes it again. + * * @param data the message to be added */ public void notificationTextDisplay(String data) { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ClientModel.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ClientModel.java index 7afc932..9b2f674 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ClientModel.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/gui/ClientModel.java @@ -27,7 +27,7 @@ public class ClientModel { private Client client; private String incomingChatMsg; - private ObservableList allClients; + private ObservableList allClients; private ObservableMap idToNameMap; @@ -58,19 +58,20 @@ public class ClientModel { this.username = username; } - public ObservableList getAllClients() { + public ObservableList getAllClients() { return allClients; } - public void addClientToList(ClientListItem nameAndId) { + public void addClientToList(SimpleStringProperty nameAndId) { + if(!allClients.contains(nameAndId)) this.allClients.add(nameAndId); } - public void removeClientFromList(int id){ - Iterator it = allClients.iterator(); + public void removeClientFromList(String id){ + Iterator it = allClients.iterator(); while(it.hasNext()){ - int uid = it.next().getId(); - if(uid == id){ + String uid = it.next().getValue(); + if(uid.equals(id)){ it.remove(); break; } 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 53cbf57..c9abea2 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 @@ -20,6 +20,7 @@ import javafx.scene.layout.HBox; public class LoungeSceneViewController implements Initializable { + public Button newGameButton; @FXML private TreeView LobbyTreeView; @FXML @@ -51,6 +52,8 @@ public class LoungeSceneViewController implements Initializable { public void initialize(URL location, ResourceBundle resources) { ChangeNameButton.setOnAction(new ChangeNameButtonPressedEventHandler()); LeaveServerButton.setOnAction(new LeaveServerButtonPressedEventHandler()); + + ClientListView.setItems(client.getAllClients()); } public void updateLobbyListView(){ @@ -61,9 +64,12 @@ public class LoungeSceneViewController implements Initializable { } + public void addLobby(LobbyListItem lobby) { + LobbyTreeView. + } + public void addClientToList(ClientListItem player) { ListCell playerCell = new ListCell<>(); - ClientListView.ite } /** 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 2b5e0c2..11605d2 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 @@ -1,7 +1,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.helpers; /** - * This class contains parameters for the PTGUI protocol message + * This class contains all parameters for the PTGUI protocol message */ public class GuiParameters { @@ -15,7 +15,7 @@ public class GuiParameters { public static final String listOfLobbies = "LOBBIES"; /** - * Tells Gui, that what follows is a list of players per Lobby + * Tells Gui, that what follows is a list of players (per Lobby?) */ public static final String listOfPLayers = "PLAYERS"; @@ -25,21 +25,10 @@ public class GuiParameters { */ public static final String noiseHeardAtPosition = "NOISE"; - /** - * Tells Gui, that the start view should be displayed - */ - public static final String viewChangeToStart = "VCSTART"; /** - * Tells Gui, that the lobby view should be displayed + * Tells, Gui, who the members of a specified Lobby are. + * Form: {@code LMEMBS$$$..} */ - public static final String viewChangeToLobby = "VCLOBBY"; - - /** - * Tells Gui, that the game view should be displayed - */ - public static final String viewChangeToGame = "VCGAME"; - - - + public static String viewChangeToLobby = "LMEMBS"; }