refactor: Button_ID - Lobby_ID translation is now only stored in hasmap and not in a Json file anymore

This commit is contained in:
Jona Walpert
2026-03-13 14:54:47 +01:00
parent 59765e33bb
commit a39775bd04
4 changed files with 24 additions and 112 deletions
@@ -1,82 +1,56 @@
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui; package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
/**
* Controller for the Casono main UI lobby.
* Handles UI initialization and user actions.
*/
/**
* Controller for the Casono main UI lobby.
* Handles UI initialization and user actions.
*/
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.shape.Rectangle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
/** /**
* Controller for the Casono main UI lobby. * Controller for the Casono main UI lobby.
* Handles UI initialization and user actions. * Handles UI initialization and user actions.
* <p>
* Standardkonstruktor für den Controller.
*/ */
public class CasinomainuiController { public class CasinomainuiController {
/**
* Default constructor for the controller.
*/
public CasinomainuiController() {
// Default constructor
}
/** Root pane of the UI. */
@FXML @FXML
private AnchorPane rootPane; private AnchorPane rootPane;
/** Title label for the main UI. */
@FXML @FXML
private Label titleLabel; private Label titleLabel;
/** Subtitle label for the main UI. */
@FXML @FXML
private Label subtitleLabel; private Label subtitleLabel;
/** Logo image view. */
@FXML @FXML
private javafx.scene.image.ImageView logoView; private ImageView logoView;
/** Green box shape element. */
@FXML @FXML
private javafx.scene.shape.Rectangle greenBox; private Rectangle greenBox;
/** Exit button for closing the application. */
@FXML @FXML
private Button exitbutton; private Button exitbutton;
/** Casino table VBox for grid rendering. */
@FXML @FXML
private javafx.scene.layout.VBox casinoTable; private VBox casinoTable;
private LobbyButtonTranslationManager translationManager; private LobbyButtonTranslationManager translationManager;
private LobbyButtonGridManager gridManager; private LobbyButtonGridManager gridManager;
private int nextButtonId = 1; private int nextButtonId = 1;
public CasinomainuiController() {
// Default constructor
}
/** /**
* Initializes the UI components and sets default values. * Initializes the UI components and sets default values.
*/ */
@FXML
public void initialize() { public void initialize() {
titleLabel.setText("Casono"); titleLabel.setText("Casono");
subtitleLabel.setText("Texas Hold'em Poker"); subtitleLabel.setText("Texas Hold'em Poker");
logoView.setImage(new javafx.scene.image.Image(getClass().getResource("/images/logo.png").toExternalForm())); logoView.setImage(new Image(getClass().getResource("/images/logo.png").toExternalForm()));
translationManager = new LobbyButtonTranslationManager(); translationManager = new LobbyButtonTranslationManager();
gridManager = new LobbyButtonGridManager(new javafx.scene.layout.GridPane(), translationManager); gridManager = new LobbyButtonGridManager(new javafx.scene.layout.GridPane(), translationManager);
casinoTable.getChildren().clear();
casinoTable.getChildren().add(gridManager.getGridPane()); casinoTable.getChildren().add(gridManager.getGridPane());
gridManager.renderLobbyButtons(); gridManager.renderLobbyButtons();
} }
@@ -108,5 +82,4 @@ public class CasinomainuiController {
System.out.println("Fehler beim Hinzufügen: " + e.getMessage()); System.out.println("Fehler beim Hinzufügen: " + e.getMessage());
} }
} }
} }
@@ -2,82 +2,34 @@ package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.json.JSONObject;
/** /**
* Handles JSON translation between ButtonID and LobbyID. * Verwaltet das Mapping zwischen Button-IDs und Lobby-IDs rein im Speicher.
* <p> * Keine Dateioperationen, nur Laufzeitdatenstruktur.
* Verwaltet das Mapping zwischen Button-IDs und Lobby-IDs, speichert und lädt die Zuordnung aus einer JSON-Datei.
*/ */
public class LobbyButtonTranslationManager { public class LobbyButtonTranslationManager {
/** /** Maximale Anzahl an Buttons/Lobbys */
* Removes all lobbies from the mapping and saves the file. private static final int MAX_BUTTONS = 8;
*/ /** Zuordnung ButtonID → LobbyID */
public void clearAllLobbies() {
buttonIdToLobbyId.clear();
saveTranslation();
}
/** Pfad zur JSON-Translationsdatei. */
private final String translationFilePath = "src/main/resources/ui-structure/lobby_button_translation.json";
/** Zuordnung ButtonID → LobbyID. */
private final Map<Integer, Integer> buttonIdToLobbyId = new HashMap<>(); private final Map<Integer, Integer> buttonIdToLobbyId = new HashMap<>();
/** Maximale Anzahl an Buttons im Grid. */
private final int maxButtons = 8;
/** /**
* Konstruktor: lädt die Zuordnung aus der JSON-Datei. * Konstruktor: initialisiert die Zuordnung leer.
*/ */
public LobbyButtonTranslationManager() { public LobbyButtonTranslationManager() {
loadTranslation(); // Zuordnung bleibt leer beim Start
} }
/** /**
* Lädt die Zuordnung ButtonID → LobbyID aus der JSON-Datei. * Prüft, ob das Grid voll ist (MAX_BUTTONS erreicht).
* Falls die Datei nicht existiert, bleibt die Zuordnung leer.
*/
public void loadTranslation() {
try {
Path path = Path.of(translationFilePath);
if (!Files.exists(path)) return;
String json = Files.readString(path);
JSONObject obj = new JSONObject(json);
buttonIdToLobbyId.clear();
for (String key : obj.keySet()) {
buttonIdToLobbyId.put(Integer.parseInt(key), obj.getInt(key));
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Speichert die aktuelle Zuordnung ButtonID → LobbyID in die JSON-Datei.
*/
public void saveTranslation() {
try {
JSONObject obj = new JSONObject();
for (Map.Entry<Integer, Integer> entry : buttonIdToLobbyId.entrySet()) {
obj.put(String.valueOf(entry.getKey()), entry.getValue());
}
Files.write(Path.of(translationFilePath), obj.toString(2).getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Prüft, ob das Grid voll ist (maxButtons erreicht).
* @return true, wenn Grid voll; sonst false * @return true, wenn Grid voll; sonst false
*/ */
public boolean isFull() { public boolean isFull() {
return buttonIdToLobbyId.size() >= maxButtons; return buttonIdToLobbyId.size() >= MAX_BUTTONS;
} }
/** /**
* Fügt eine Zuordnung ButtonID → LobbyID hinzu und speichert sie. * Fügt eine Zuordnung ButtonID → LobbyID hinzu.
* @param buttonId Die ID des Buttons * @param buttonId Die ID des Buttons
* @param lobbyId Die ID der Lobby * @param lobbyId Die ID der Lobby
* @throws Exception wenn das Grid voll ist * @throws Exception wenn das Grid voll ist
@@ -85,16 +37,14 @@ public class LobbyButtonTranslationManager {
public void addLobbyButton(int buttonId, int lobbyId) throws Exception { public void addLobbyButton(int buttonId, int lobbyId) throws Exception {
if (isFull()) throw new Exception("Grid is full!"); if (isFull()) throw new Exception("Grid is full!");
buttonIdToLobbyId.put(buttonId, lobbyId); buttonIdToLobbyId.put(buttonId, lobbyId);
saveTranslation();
} }
/** /**
* Entfernt eine Zuordnung für die gegebene ButtonID und speichert. * Entfernt eine Zuordnung für die gegebene ButtonID.
* @param buttonId Die ID des zu entfernenden Buttons * @param buttonId Die ID des zu entfernenden Buttons
*/ */
public void removeLobbyButton(int buttonId) { public void removeLobbyButton(int buttonId) {
buttonIdToLobbyId.remove(buttonId); buttonIdToLobbyId.remove(buttonId);
saveTranslation();
} }
/** /**
@@ -15,7 +15,6 @@ class LobbyButtonGridManagerTest {
gridPane = new GridPane(); gridPane = new GridPane();
translationManager = new LobbyButtonTranslationManager(); translationManager = new LobbyButtonTranslationManager();
translationManager.getButtonIdToLobbyId().clear(); translationManager.getButtonIdToLobbyId().clear();
translationManager.saveTranslation();
gridManager = new LobbyButtonGridManager(gridPane, translationManager); gridManager = new LobbyButtonGridManager(gridPane, translationManager);
} }
@@ -12,7 +12,6 @@ class LobbyButtonTranslationManagerTest {
void setUp() { void setUp() {
manager = new LobbyButtonTranslationManager(); manager = new LobbyButtonTranslationManager();
manager.getButtonIdToLobbyId().clear(); manager.getButtonIdToLobbyId().clear();
manager.saveTranslation();
} }
@Test @Test
@@ -38,15 +37,6 @@ class LobbyButtonTranslationManagerTest {
assertEquals("Grid is full!", ex.getMessage()); assertEquals("Grid is full!", ex.getMessage());
} }
@Test
void testSaveAndLoadTranslation() throws Exception {
manager.addLobbyButton(3, 300);
manager.saveTranslation();
manager.getButtonIdToLobbyId().clear();
manager.loadTranslation();
assertEquals(300, manager.getLobbyIdForButton(3));
}
@Test @Test
void testGetButtonIdToLobbyId() throws Exception { void testGetButtonIdToLobbyId() throws Exception {
manager.addLobbyButton(4, 400); manager.addLobbyButton(4, 400);