Merge remote-tracking branch 'origin/Application' into Application

# Conflicts:
#	src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/client/Client.java
#	src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/helpers/GuiParameters.java
This commit is contained in:
Seraina 2022-05-01 03:08:16 +02:00
commit 17fc986c50
5 changed files with 223 additions and 26 deletions

View File

@ -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.LoungeSceneViewController;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
@ -42,6 +43,8 @@ public class Client {
private GUI chatGui;
private ClientModel clientModel;
private GameStateModel gameStateModel;
private GameController gameController;
private LoungeSceneViewController loungeSceneViewController;
/**
* Saves the position of the client, gets refreshed everytime the client gets a vote request.
@ -75,6 +78,9 @@ public class Client {
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.loungeSceneViewController = new LoungeSceneViewController();
LoungeSceneViewController.setClient(ChatApp.getClientModel());
} catch (IOException e) {
e.printStackTrace();
}
@ -326,7 +332,7 @@ public class Client {
* @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?
* TODO(Seraina&Sebi): evtl. auslagern?
*/
public void sendToGUI(String parameter, String data) {
try {
@ -340,7 +346,6 @@ public class Client {
break;
case GuiParameters.day: //ClientGameInfoHandler
gameStateModel.setDayClone(true);
chatApp.getGameController().setNoiseButtonVisible();
break;
case GuiParameters.updateGameState:
gameStateModel.setGSFromString(data);
@ -365,15 +370,24 @@ 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)
//TODO
//break; (commented out due to compiling error)
//case GuiParameters.viewChangeToStart: (commented out due to compiling error)
//TODO
//break; (commented out due to compiling error)
//TODO
//break; (commented out due to compiling error)
//case GuiParameters.viewChangeToLobby: (commented out due to compiling error)
//TODO
//break; (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?
@ -385,6 +399,22 @@ 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<>();
@ -393,16 +423,17 @@ public class Client {
list.add(new SimpleStringProperty(arr[i]));
//ChatController.getClient().addLobbyToList(new SimpleStringProperty(arr[i]));
}
//TODO
}
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) {
for (int i = 0; i < n; i = i + 1) {
list.add(new SimpleStringProperty(arr[i]));
}
ChatController.getClient().getAllClients().setAll();
loungeSceneViewController.updateClientListView(list);
}
/**

View File

@ -70,6 +70,10 @@ public class ClientModel {
this.allClients.add(nameAndId);
}
public void updateClientList(ObservableList<SimpleStringProperty> clients) {
}
public void removeClientFromList(String id){
Iterator<SimpleStringProperty> it = allClients.iterator();
while(it.hasNext()){

View File

@ -1,22 +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 {
public class LobbyListItem implements Observable {
private final Label lobbyID;
private final Label adminName;
private Set<StringProperty> 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(Label lobbyID, Label adminName,
Set<StringProperty> 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,18 +5,29 @@ 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;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
@ -29,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
@ -47,6 +58,14 @@ 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();
}
/**
* Called to initialize a controller after its root element has been completely processed.
@ -64,20 +83,87 @@ 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
}
}
public void addLobby(LobbyListItem lobby) {
//TODO
/**
* Adds players to a lobby
* "NMEMB" {@link ch.unibas.dmi.dbis.cs108.multiplayer.helpers.GuiParameters}
* @param lobbyID
* @param player
*/
public void addPlayerToLobby(String lobbyID, String player) {
ObservableList<String> members = lobbyToMemberssMap.get(lobbyID);
members.add(player);
}
/**
* 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));
}
@ -86,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);
@ -98,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

@ -41,8 +41,14 @@ public class GuiParameters {
public static final String viewChangeToGame = "VCGAME";
/**
* 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 final String getMembersInLobby = "LMEMBS";
/**
* Tells Gui, that a new Lobby has been created.
* Form: {@code NLOBBY$<lobbyID>:<Admin Name>}
*/
public static final String changeToLobby = "LMEMBS";
@ -60,4 +66,11 @@ public class GuiParameters {
*/
public static final String day = "DAY";
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";
}