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;
/**
* 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.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
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.VBox;
import javafx.scene.shape.Rectangle;
/**
* Controller for the Casono main UI lobby.
* Handles UI initialization and user actions.
* <p>
* Standardkonstruktor für den Controller.
*/
public class CasinomainuiController {
/**
* Default constructor for the controller.
*/
public CasinomainuiController() {
// Default constructor
}
/** Root pane of the UI. */
@FXML
private AnchorPane rootPane;
/** Title label for the main UI. */
@FXML
private Label titleLabel;
/** Subtitle label for the main UI. */
@FXML
private Label subtitleLabel;
/** Logo image view. */
@FXML
private javafx.scene.image.ImageView logoView;
/** Green box shape element. */
private ImageView logoView;
@FXML
private javafx.scene.shape.Rectangle greenBox;
/** Exit button for closing the application. */
private Rectangle greenBox;
@FXML
private Button exitbutton;
/** Casino table VBox for grid rendering. */
@FXML
private javafx.scene.layout.VBox casinoTable;
private VBox casinoTable;
private LobbyButtonTranslationManager translationManager;
private LobbyButtonGridManager gridManager;
private int nextButtonId = 1;
public CasinomainuiController() {
// Default constructor
}
/**
* Initializes the UI components and sets default values.
*/
@FXML
public void initialize() {
titleLabel.setText("Casono");
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();
gridManager = new LobbyButtonGridManager(new javafx.scene.layout.GridPane(), translationManager);
casinoTable.getChildren().clear();
casinoTable.getChildren().add(gridManager.getGridPane());
gridManager.renderLobbyButtons();
}
@@ -108,5 +82,4 @@ public class CasinomainuiController {
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.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.
* <p>
* Verwaltet das Mapping zwischen Button-IDs und Lobby-IDs, speichert und lädt die Zuordnung aus einer JSON-Datei.
* Verwaltet das Mapping zwischen Button-IDs und Lobby-IDs rein im Speicher.
* Keine Dateioperationen, nur Laufzeitdatenstruktur.
*/
public class LobbyButtonTranslationManager {
/**
* Removes all lobbies from the mapping and saves the file.
*/
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. */
/** Maximale Anzahl an Buttons/Lobbys */
private static final int MAX_BUTTONS = 8;
/** Zuordnung ButtonID → LobbyID */
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() {
loadTranslation();
// Zuordnung bleibt leer beim Start
}
/**
* Lädt die Zuordnung ButtonID → LobbyID aus der JSON-Datei.
* 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).
* Prüft, ob das Grid voll ist (MAX_BUTTONS erreicht).
* @return true, wenn Grid voll; sonst false
*/
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 lobbyId Die ID der Lobby
* @throws Exception wenn das Grid voll ist
@@ -85,16 +37,14 @@ public class LobbyButtonTranslationManager {
public void addLobbyButton(int buttonId, int lobbyId) throws Exception {
if (isFull()) throw new Exception("Grid is full!");
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
*/
public void removeLobbyButton(int buttonId) {
buttonIdToLobbyId.remove(buttonId);
saveTranslation();
}
/**