This commit is contained in:
Sebastian Lenzlinger 2022-04-30 18:51:10 +02:00
parent c066763f97
commit 017a8a1c99
4 changed files with 59 additions and 13 deletions

View File

@ -20,7 +20,9 @@ import java.io.*;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -372,14 +374,26 @@ public class Client {
} }
private void updateListOfClients(String data) { private void updateListOfLobbies(String data) {
String[] arr = data.split(":"); String[] arr = data.split(":");
ObservableList<SimpleStringProperty> list = new SimpleListProperty<>();
int n = arr.length; int n = arr.length;
for (int i = 0; i < n; i = i + 2) { for (int i = 0; i < n; i = i + 2) {
ChatController.getClient().addClientToList(new SimpleStringProperty(arr[i])); list.add(new SimpleStringProperty(arr[i]));
ChatController.getClient().addLobbyToList(new SimpleStringProperty(arr[i]));
} }
} }
private void updateListOfClients(String data) {
String[] arr = data.split(":");
ObservableList<SimpleStringProperty> list = new SimpleListProperty<>();
int n = arr.length;
for (int i = 0; i < n; i = i + 2) {
list.add(new SimpleStringProperty(arr[i]));
}
ChatController.getClient().getAllClients().setAll();
}
/** /**
* Starts a new thread, thad adds a message to notificationText in the gameController, waits 3 * Starts a new thread, thad adds a message to notificationText in the gameController, waits 3
* seconds and deletes it again. * seconds and deletes it again.

View File

@ -30,6 +30,8 @@ public class ClientModel {
private ObservableList<SimpleStringProperty> allClients; private ObservableList<SimpleStringProperty> allClients;
private ObservableMap<Integer, SimpleStringProperty> idToNameMap; private ObservableMap<Integer, SimpleStringProperty> idToNameMap;
private ObservableList<SimpleStringProperty> lobbies;
private HashSet<String> clientsOnServer; private HashSet<String> clientsOnServer;
@ -38,6 +40,7 @@ public class ClientModel {
this.client = client; this.client = client;
this.allClients = FXCollections.observableArrayList(); this.allClients = FXCollections.observableArrayList();
this.idToNameMap = FXCollections.observableHashMap(); this.idToNameMap = FXCollections.observableHashMap();
this.lobbies = FXCollections.observableArrayList();
} }
//private Number; //private Number;

View File

@ -3,26 +3,33 @@ 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.ClientModel;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.ChangeNameButtonPressedEventHandler; 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.client.gui.events.LeaveServerButtonPressedEventHandler;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import java.net.URL; import java.net.URL;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.ListCell; import javafx.scene.control.ListCell;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar; import javafx.scene.control.ToolBar;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
public class LoungeSceneViewController implements Initializable { public class LoungeSceneViewController implements Initializable {
Protocol protocol;
public Button newGameButton; public Button newGameButton;
@FXML @FXML
private TreeView LobbyTreeView; private ListView LobbyListView;
@FXML @FXML
private ListView ClientListView; private ListView ClientListView;
@FXML @FXML
@ -50,30 +57,52 @@ public class LoungeSceneViewController implements Initializable {
*/ */
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
ChangeNameButton.setOnAction(new ChangeNameButtonPressedEventHandler()); this.protocol = new Protocol();
ChangeNameButton.setOnAction(event -> changeName());
LeaveServerButton.setOnAction(new LeaveServerButtonPressedEventHandler()); LeaveServerButton.setOnAction(new LeaveServerButtonPressedEventHandler());
newGameButton.setOnAction(event -> newGame());
ClientListView.se
ClientListView.setItems(client.getAllClients()); ClientListView.setItems(client.getAllClients());
LobbyListView.setPlaceholder(new Text("No open lobbies!"));
} }
public void updateLobbyListView() { public void updateLobbyListView() {
//TODO
} }
public void updateClientListView(){ public void updateClientListView(ObservableList<SimpleStringProperty> names) {
this.ClientListView.setItems(names);
} }
public void addLobby(LobbyListItem lobby) { public void addLobby(LobbyListItem lobby) {
//TODO fix/complete following code: LobbyTreeView. (commented out due to compiling error) //TODO
} }
public void addClientToList(ClientListItem player) { public void addClientToList(String s) {
ListCell<StringProperty> playerCell = new ListCell<>(); ClientListView.getItems().add(new SimpleStringProperty(s));
}
public void newGame() {
client.getClient().sendMsgToServer(Protocol.createNewLobby);
}
public void changeName() {
TextField name = new TextField("Enter new name!");
this.NTtBToolBar.getItems().add(name);
name.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
client.getClient().sendMsgToServer(Protocol.nameChange + "$" + name.getText());
NTtBToolBar.getItems().remove(NTtBToolBar.getItems().size());
}
});
} }
/** /**
* Utility to set the client model for this class * Utility to set the client model for this class
*
* @param client, the client model * @param client, the client model
*/ */
public static void setClient(ClientModel client) { public static void setClient(ClientModel client) {

View File

@ -38,7 +38,7 @@
<ListView fx:id="ClientListView" prefHeight="681.0" prefWidth="661.0" BorderPane.alignment="CENTER" /> <ListView fx:id="ClientListView" prefHeight="681.0" prefWidth="661.0" BorderPane.alignment="CENTER" />
</left> </left>
<right> <right>
<TreeView fx:id="LobbyTreeView" prefHeight="681.0" prefWidth="641.0" BorderPane.alignment="CENTER" /> <ListView fx:id="LobbyListView" prefHeight="681.0" prefWidth="641.0" BorderPane.alignment="CENTER" />
</right> </right>
</BorderPane> </BorderPane>
</children> </children>