Added Method for adding clients to gui view

This commit is contained in:
Sebastian Lenzlinger 2022-04-30 17:44:30 +02:00
parent 2996f7aca7
commit 6a520ca83a
4 changed files with 52 additions and 38 deletions

View File

@ -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) {

View File

@ -27,7 +27,7 @@ public class ClientModel {
private Client client;
private String incomingChatMsg;
private ObservableList<ClientListItem> allClients;
private ObservableList<SimpleStringProperty> allClients;
private ObservableMap<Integer, SimpleStringProperty> idToNameMap;
@ -58,19 +58,20 @@ public class ClientModel {
this.username = username;
}
public ObservableList<ClientListItem> getAllClients() {
public ObservableList<SimpleStringProperty> 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<ClientListItem> it = allClients.iterator();
public void removeClientFromList(String id){
Iterator<SimpleStringProperty> 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;
}

View File

@ -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<StringProperty> playerCell = new ListCell<>();
ClientListView.ite
}
/**

View File

@ -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$<lobbyID>$<member names>$..}
*/
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";
}