Feat: Reuse shared ClientService in CasinomainuiController

Make the controller use ClientApp.getSharedClientService() if available; otherwise fall back to creating a new ClientService. Add/cleanup Javadoc.
This commit is contained in:
Jona Walpert
2026-04-12 10:52:55 +02:00
parent c3e0e740ed
commit 65a93a8c82
@@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui; package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
import ch.unibas.dmi.dbis.cs108.casono.client.ClientApp;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService; import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.network.LobbyClient; import ch.unibas.dmi.dbis.cs108.casono.client.network.LobbyClient;
import javafx.application.Platform; import javafx.application.Platform;
@@ -36,12 +37,16 @@ public class CasinomainuiController {
private int nextButtonId = 1; private int nextButtonId = 1;
private LobbyClient lobbyClient; private LobbyClient lobbyClient;
/** Default constructor for dependency injection by FXMLLoader. */ /** Default constructor used by FXMLLoader. */
public CasinomainuiController() { public CasinomainuiController() {
// Default constructor // Default constructor
} }
/** Initializes the UI components and sets default values. */ /**
* Initializes the UI components and sets default values. If a shared {@link
* ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService} exists (created at application
* start), the controller reuses it so the connection remains open and already-logged-in.
*/
@FXML @FXML
public void initialize() { public void initialize() {
titleLabel.setText("Casono"); titleLabel.setText("Casono");
@@ -51,7 +56,8 @@ public class CasinomainuiController {
translationManager = LobbyButtonTranslationManager.getInstance(); translationManager = LobbyButtonTranslationManager.getInstance();
String host = System.getProperty("casono.server.host"); String host = System.getProperty("casono.server.host");
int port = Integer.parseInt(System.getProperty("casono.server.port")); int port = Integer.parseInt(System.getProperty("casono.server.port"));
ClientService clientService; ClientService clientService = ClientApp.getSharedClientService();
if (clientService == null) {
try { try {
clientService = new ClientService(host, port); clientService = new ClientService(host, port);
} catch (RuntimeException e) { } catch (RuntimeException e) {
@@ -62,6 +68,7 @@ public class CasinomainuiController {
e.getMessage()); e.getMessage());
clientService = new ClientService(true); // offline mode clientService = new ClientService(true); // offline mode
} }
}
gridManager = gridManager =
new LobbyButtonGridManager( new LobbyButtonGridManager(
new javafx.scene.layout.GridPane(), translationManager, clientService); new javafx.scene.layout.GridPane(), translationManager, clientService);
@@ -117,7 +124,11 @@ public class CasinomainuiController {
Platform.exit(); Platform.exit();
} }
/** Handles creation of a new lobby button. */ /**
* Handles creation of a new lobby button. Attempts to create a lobby on the server via the
* {@link LobbyButtonGridManager} and registers the new button in the local translation manager.
* Errors are logged and displayed as an informational alert.
*/
@FXML @FXML
public void handleCreateLobbyButton() { public void handleCreateLobbyButton() {
if (translationManager.isFull()) { if (translationManager.isFull()) {