Merge 'feat/lobby-ui' back into 'main' #172
+13
-40
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-61
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
-1
@@ -15,7 +15,6 @@ class LobbyButtonGridManagerTest {
|
||||
gridPane = new GridPane();
|
||||
translationManager = new LobbyButtonTranslationManager();
|
||||
translationManager.getButtonIdToLobbyId().clear();
|
||||
translationManager.saveTranslation();
|
||||
gridManager = new LobbyButtonGridManager(gridPane, translationManager);
|
||||
}
|
||||
|
||||
|
||||
-10
@@ -12,7 +12,6 @@ class LobbyButtonTranslationManagerTest {
|
||||
void setUp() {
|
||||
manager = new LobbyButtonTranslationManager();
|
||||
manager.getButtonIdToLobbyId().clear();
|
||||
manager.saveTranslation();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -38,15 +37,6 @@ class LobbyButtonTranslationManagerTest {
|
||||
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
|
||||
void testGetButtonIdToLobbyId() throws Exception {
|
||||
manager.addLobbyButton(4, 400);
|
||||
|
||||
Reference in New Issue
Block a user