In Client: added 3 new methods

GuiParameters: added 3 new Params
Lounge: added Maps, changed names and implemented methods
This commit is contained in:
Sebastian Lenzlinger 2022-04-30 22:18:15 +02:00
parent 6e00128997
commit 9007870d19
4 changed files with 200 additions and 43 deletions

View File

@ -358,6 +358,9 @@ public class Client {
updateListOfClients(data);
//TODO
break;
case GuiParameters.getMembersInLobby:
updateLobbyMembers(data);
break;
//case GuiParameters.viewChangeToGame: (commented out due to compiling error)
//TODO
//break; (commented out due to compiling error)
@ -367,6 +370,12 @@ public class Client {
//case GuiParameters.viewChangeToLobby: (commented out due to compiling error)
//TODO
//break; (commented out due to compiling error)
case GuiParameters.addNewMemberToLobby:
addPlayerToLobby(data);
break;
case GuiParameters.newLobbyCreated:
makeNewLobby(data);
break;
default:
notificationTextDisplay(data);
//TODO(Sebi,Seraina): should the gameController be in the Application just like the ChatController?
@ -378,14 +387,30 @@ public class Client {
}
private void makeNewLobby(String data) {
String[] params = data.split(":");
loungeSceneViewController.newLobby(params[0], params[1]);
}
private void addPlayerToLobby(String data) {
String[] params = data.split(":");
loungeSceneViewController.addPlayerToLobby(params[0], params[1]);
}
private void updateLobbyMembers(String data) {
String[] dataArr = data.split(":");
String lobbyID = dataArr[0];
String adminName = dataArr[1];
}
private void updateListOfLobbies(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]));
loungeSceneViewController.addLobbyToList(new SimpleStringProperty(arr[i]));
}
//TODO
}
private void updateListOfClients(String data) {

View File

@ -1,25 +1,73 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.lounge;
import java.util.Set;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ToggleButton;
public class LobbyListItem extends ListCell {
public class LobbyListItem implements Observable {
private final String lobbyID;
private final String adminName;
private ObservableSet<SimpleStringProperty> clientsInLobby;
private final ToggleButton button;
private final SimpleStringProperty lobbyID;
private final SimpleStringProperty adminName;
private ObservableList<SimpleStringProperty> clientsInLobby;
private final Button button;
private SimpleBooleanProperty ownedByClient;
private SimpleBooleanProperty isOpen;
public LobbyListItem(String lobbyID, String adminName,
ObservableSet<SimpleStringProperty> clientsInLobby, ToggleButton button) {
public LobbyListItem(SimpleStringProperty lobbyID, SimpleStringProperty adminName,
ObservableList<SimpleStringProperty> clientsInLobby, Button button,
SimpleBooleanProperty ownedByClient) {
this.lobbyID = lobbyID;
this.adminName = adminName;
this.clientsInLobby = clientsInLobby;
this.button = button;
}
/**
* Adds an {@link InvalidationListener} which will be notified whenever the {@code Observable}
* becomes invalid. If the same listener is added more than once, then it will be notified more
* than once. That is, no check is made to ensure uniqueness.
* <p>
* Note that the same actual {@code InvalidationListener} instance may be safely registered for
* different {@code Observables}.
* <p>
* The {@code Observable} stores a strong reference to the listener which will prevent the
* listener from being garbage collected and may result in a memory leak. It is recommended to
* either unregister a listener by calling {@link #removeListener(InvalidationListener)
* removeListener} after use or to use an instance of {@link WeakInvalidationListener} avoid this
* situation.
*
* @param listener The listener to register
* @throws NullPointerException if the listener is null
* @see #removeListener(InvalidationListener)
*/
@Override
public void addListener(InvalidationListener listener) {
}
/**
* Removes the given listener from the list of listeners, that are notified whenever the value of
* the {@code Observable} becomes invalid.
* <p>
* If the given listener has not been previously registered (i.e. it was never added) then this
* method call is a no-op. If it had been previously added then it will be removed. If it had been
* added more than once, then only the first occurrence will be removed.
*
* @param listener The listener to remove
* @throws NullPointerException if the listener is null
* @see #addListener(InvalidationListener)
*/
@Override
public void removeListener(InvalidationListener listener) {
}
}

View File

@ -5,16 +5,25 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.client.gui.events.ChangeNameButtonPr
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.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleMapProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
@ -31,9 +40,9 @@ public class LoungeSceneViewController implements Initializable {
public Button newGameButton;
@FXML
private ListView LobbyListView;
private ListView<HBox> LobbyListView;
@FXML
private ListView ClientListView;
private ListView<SimpleStringProperty> ClientListView;
@FXML
private Button ChangeNameButton;
@FXML
@ -49,9 +58,12 @@ public class LoungeSceneViewController implements Initializable {
public static ClientModel client;
private ObservableMap<String, ObservableList<String>> lobbyToMemberssMap;
private HashMap<String, String> clientToLobbyMap;
public LoungeSceneViewController() {
super();
lobbyToMemberssMap = FXCollections.observableHashMap();
}
@ -71,38 +83,86 @@ public class LoungeSceneViewController implements Initializable {
ClientListView.setItems(client.getAllClients());
LobbyListView.setPlaceholder(new Text("No open lobbies!"));
}
client.getAllClients().addListener(new ListChangeListener<SimpleStringProperty>() {
@Override
public void onChanged(Change<? extends SimpleStringProperty> c) {
List<SimpleStringProperty> removed = (List<SimpleStringProperty>) c.getRemoved();
for(SimpleStringProperty player: removed) {
public void updateLobbyListView() {
//TODO
}
}
});
}
public void updateClientListView(ObservableList<SimpleStringProperty> names) {
ObservableList<SimpleStringProperty> clientsLeft = ClientListView.getItems();
clientsLeft.removeAll(names);
this.ClientListView.setItems(names);
for (SimpleStringProperty gone : clientsLeft) {
//TODO
}
}
/**
* Adds a lobby to the view
* Adds players to a lobby
* "NMEMB" {@link ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters}
* @param lobbyID
* @param admin
* @param players
* @param player
*/
public void addLobby(String lobbyID, String admin, String players) {
TitledPane lobbyObject = new TitledPane();
lobbyObject.setId(lobbyID+admin);
lobbyObject.textProperty().setValue("Lobby Nr: " + lobbyID + " Admin: " + admin);
ObservableList<SimpleStringProperty> listOfPlayersInLobby = new SimpleListProperty<>();
String[] playersArr = players.split(":");
int noOfPlayers = playersArr.length;
for(int i = 0; i < noOfPlayers; i++){
listOfPlayersInLobby.add(new SimpleStringProperty(playersArr[i]));
public void addPlayerToLobby(String lobbyID, String player) {
ObservableList<String> members = lobbyToMemberssMap.get(lobbyID);
members.add(player);
}
ListView view = new ListView(listOfPlayersInLobby);
lobbyObject.contentProperty().set(view);
LobbyListView.getItems().add(lobbyObject);
/**
* Used when a new lobby shall be added to the view.
* "NLOBBY" {@link ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters}
* @param lobbyID
* @param adminName
*/
public void newLobby(String lobbyID, String adminName) {
SimpleStringProperty id = new SimpleStringProperty(lobbyID);
SimpleStringProperty admin = new SimpleStringProperty((adminName));
boolean ownedByClient = false;
Button startOrJoin;
if (adminName.equals(client.getUsername())) {
ownedByClient = true;
startOrJoin = new Button("Start");
startOrJoin.setOnAction(event -> startGame());
} else {
startOrJoin = new Button("Join");
startOrJoin.setOnAction(event -> joinGame(lobbyID));
}
HBox lobby = new HBox();
Label idLabel = new Label();
Label adminLabel = new Label();
idLabel.textProperty().bind(id);
adminLabel.textProperty().bind(admin);
lobby.getChildren().add(idLabel);
lobby.getChildren().add(adminLabel);
lobby.getChildren().add(startOrJoin);
ListView<String> members = new ListView<>();
members.setId("membersOfLobby");
if (ownedByClient) {
members.getItems().add("(you are admin) " + adminName);
} else {
members.getItems().add("(admin)" + adminName);
members.getItems().add(client.getUsername());
}
lobby.setId(lobbyID);
lobbyToMemberssMap.put(lobbyID, members.getItems());
LobbyListView.getItems().add(lobby);
}
private void joinGame(String lobbyID) {
client.getClient().sendMsgToServer(Protocol.joinLobby + "$" + lobbyID);
}
private void startGame() {
client.getClient().sendMsgToServer(Protocol.startANewGame);
}
;
public void addClientToList(String s) {
ClientListView.getItems().add(new SimpleStringProperty(s));
@ -112,6 +172,7 @@ public class LoungeSceneViewController implements Initializable {
client.getClient().sendMsgToServer(Protocol.createNewLobby);
}
public void changeName() {
TextField name = new TextField("Enter new name!");
this.NTtBToolBar.getItems().add(name);
@ -124,6 +185,17 @@ public class LoungeSceneViewController implements Initializable {
});
}
public void removePlayer(String id) {
Iterator<SimpleStringProperty> it = client.getAllClients().iterator();
while (it.hasNext()) {
String uid = it.next().getValue();
if (uid.equals(id)) {
it.remove();
break;
}
}
}
/**
* Utility to set the client model for this class
*

View File

@ -43,8 +43,20 @@ public class GuiParameters {
/**
* Tells, Gui, who the members of a specified Lobby are.
* Form: {@code LMEMBS$<lobbyID>$<member names>$..}
* Tells Gui, who the members of a specified Lobby are.
* Form: {@code LMEMBS$<lobbyID>:<ADMIN NAME>:<member names>:<..>}
*/
public static String changeToLobby = "LMEMBS";
public static final String getMembersInLobby = "LMEMBS";
/**
* Tells Gui, that a new Lobby has been created.
* Form: {@code NLOBBY$<lobbyID>:<Admin Name>}
*/
public static final String newLobbyCreated = "NLOBBY";
/**
* Tells Gui, to add a player to a lobby.
* Form: {@code NMEMB$<LobbyIS>:<PlayerNamse>}
*/
public static final String addNewMemberToLobby = "NMEMB";
}