Fix: Resolving Git Merge Conflicts #253

Merged
j.kropff merged 69 commits from chore/ui-checkstyle-fixes into main 2026-04-11 15:10:58 +02:00
17 changed files with 339 additions and 420 deletions
Showing only changes of commit b4b2e3b067 - Show all commits
@@ -20,25 +20,35 @@ public class Message {
/**
* Constructs a Message with a provided timestamp. Typically used when reconstructing messages
* received from the server.
* received from the server. Used for Messages in the Lobby Chat.
*
* @param type The chat category (e.g., GLOBAL, LOBBY, or WHISPER).
* @param lobbyId The ID of the lobby, or -1 if not applicable.
* @param sender The username of the message creator.
* @param target The username of the recipient (required for whispers, otherwise null).
* @param timestamp The formatted time string (e.g., "HH:mm").
* @param message The actual text content of the message.
*/
public Message(
ChatType type,
int lobbyId,
String sender,
String target,
String timestamp,
String message) {
this.type = type;
private Message(int lobbyId, String sender, String timestamp, String message) {
this.type = ChatType.LOBBY;
this.lobbyId = lobbyId;
this.sender = sender;
this.target = null;
this.timestamp = timestamp;
this.message = message;
}
/**
* Constructs a Message with a provided timestamp. Typically used when reconstructing messages
* received from the server. Used for Messages in the WHISPER and GLOBAL Chat.
*
* @param type The chat category (e.g., GLOBAL or WHISPER).
* @param sender The username of the message creator.
* @param timestamp The formatted time string (e.g., "HH:mm").
* @param message The actual text content of the message.
*/
private Message(ChatType type, String sender, String target, String timestamp, String message) {
this.type = type;
this.lobbyId = -1;
this.sender = sender;
this.target = target;
this.timestamp = timestamp;
this.message = message;
@@ -120,23 +130,19 @@ public class Message {
case GLOBAL ->
new Message(
ChatType.GLOBAL,
-1,
getParString(parameters, "USER"),
null,
getParString(parameters, "TIME"),
getParString(parameters, "TEXT"));
case LOBBY ->
new Message(
ChatType.LOBBY,
Integer.parseInt(getParString(parameters, "GAME")),
getParString(parameters, "USER"),
null,
getParString(parameters, "TIME"),
getParString(parameters, "TEXT"));
case WHISPER ->
new Message(
ChatType.WHISPER,
Integer.parseInt(getParString(parameters, "GAME", "-1")),
getParString(parameters, "USER"),
getParString(parameters, "TARGET"),
getParString(parameters, "TIME"),
@@ -1,8 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.client.game;
/**
* Represents a playing card with a value and suit.
*/
/** Represents a playing card with a value and suit. */
public class Card {
public String value;
public String suit;
@@ -1,15 +1,11 @@
package ch.unibas.dmi.dbis.cs108.casono.client.game;
import ch.unibas.dmi.dbis.cs108.casono.client.game.Card;
import ch.unibas.dmi.dbis.cs108.casono.client.game.Player;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the current state of the poker game, including the phase, pot
* size, current bet, dealer position,
* active player, community cards, and player information.
* Represents the current state of the poker game, including the phase, pot size, current bet,
* dealer position, active player, community cards, and player information.
*/
public class GameState {
public String phase;
@@ -4,8 +4,8 @@ import java.util.ArrayList;
import java.util.List;
/**
* Represents a player in the poker game, including their name, chip count,
* current bet, state (e.g., "active", "folded"), and their hole cards.
* Represents a player in the poker game, including their name, chip count, current bet, state
* (e.g., "active", "folded"), and their hole cards.
*/
public class Player {
public String name;
@@ -28,10 +28,11 @@ public class ClientService {
private final Socket socket;
private final ExecutorService executor;
private final boolean offlineMode;
public static ArrayList<String> response;
private final AtomicInteger idGenerator;
private final Logger logger;
private Logger logger;
/**
* Constructs a ClientService with the given server IP and port. It establishes a socket
@@ -47,6 +48,8 @@ public class ClientService {
this.logger = LogManager.getLogger(ClientService.class);
this.offlineMode = false;
try {
socket = new Socket(ip, port);
clienttcptransport = new TcpTransport(socket);
@@ -58,6 +61,25 @@ public class ClientService {
executor = Executors.newSingleThreadExecutor();
}
/**
* Constructs a ClientService in offline mode. No network connection will be attempted and calls
* to processCommand will throw a RuntimeException.
*
* @param offline true to create an offline (no-network) client service
*/
public ClientService(boolean offline) {
this.idGenerator = new AtomicInteger(0);
this.offlineMode = offline;
this.socket = null;
this.clienttcptransport = null;
this.executor = Executors.newSingleThreadExecutor();
}
/** Returns true if this ClientService is running in offline mode (no network). */
public boolean isOffline() {
return offlineMode;
}
static Pattern responseRex =
Pattern.compile(
"(?<key>\\w+)=(('(?<string>([^']|\\')+)')|(?<primVal>[+-]?[\\d\\w:]+))");
@@ -3,14 +3,12 @@ package ch.unibas.dmi.dbis.cs108.casono.client.network;
import ch.unibas.dmi.dbis.cs108.casono.client.game.Card;
import ch.unibas.dmi.dbis.cs108.casono.client.game.GameState;
import ch.unibas.dmi.dbis.cs108.casono.client.game.Player;
import java.util.ArrayList;
import java.util.List;
/**
* The GameClient class is responsible for communicating with the server to
* retrieve the current game state. It sends a command to the server and
* parses the response into a structured GameState object.
* The GameClient class is responsible for communicating with the server to retrieve the current
* game state. It sends a command to the server and parses the response into a structured GameState
* object.
*/
public class GameClient {
@@ -19,21 +17,21 @@ public class GameClient {
/**
* Constructs a GameClient with the given ClientService for communication.
*
* @param client The ClientService instance used to send commands and receive
* responses from the server.
* @param client The ClientService instance used to send commands and receive responses from the
* server.
*/
public GameClient(ClientService client) {
this.client = client;
}
/**
* Retrieves the current game state from the server by sending a command and
* parsing the response.
* Retrieves the current game state from the server by sending a command and parsing the
* response.
*
* @return A GameState object representing the current state of the game.
*/
public GameState getGameState() {
String response = client.processCommand("GET_GAME_STATE");
List<String> response = client.processCommand("GET_GAME_STATE");
return parseGameState(response);
}
@@ -43,16 +41,16 @@ public class GameClient {
* @param input The raw response string from the server.
* @return A GameState object representing the current state of the game.
*/
private GameState parseGameState(String input) {
private GameState parseGameState(List<String> input) {
GameState state = new GameState();
String[] lines = input.split("\n");
// String[] lines = input.split("\n");
Player currentPlayer = null;
Card currentCard = null;
for (String rawLine : lines) {
for (String rawLine : input) {
String line = rawLine.trim();
@@ -62,46 +60,26 @@ public class GameClient {
if (line.startsWith("PHASE=")) {
state.phase = line.split("=")[1];
}
else if (line.startsWith("POT=")) {
} else if (line.startsWith("POT=")) {
state.pot = Integer.parseInt(line.split("=")[1]);
}
else if (line.startsWith("CURRENT_BET=")) {
} else if (line.startsWith("CURRENT_BET=")) {
state.currentBet = Integer.parseInt(line.split("=")[1]);
}
else if (line.startsWith("DEALER=")) {
} else if (line.startsWith("DEALER=")) {
state.dealer = Integer.parseInt(line.split("=")[1]);
}
else if (line.startsWith("ACTIVE_PLAYER=")) {
} else if (line.startsWith("ACTIVE_PLAYER=")) {
state.activePlayer = Integer.parseInt(line.split("=")[1]);
}
else if (line.startsWith("PLAYER")) {
} else if (line.startsWith("PLAYER")) {
currentPlayer = new Player();
state.players.add(currentPlayer);
}
else if (line.startsWith("NAME=") && currentPlayer != null) {
} else if (line.startsWith("NAME=") && currentPlayer != null) {
currentPlayer.name = line.split("=")[1];
}
else if (line.startsWith("CHIPS=") && currentPlayer != null) {
} else if (line.startsWith("CHIPS=") && currentPlayer != null) {
currentPlayer.chips = Integer.parseInt(line.split("=")[1]);
}
else if (line.startsWith("BET=") && currentPlayer != null) {
} else if (line.startsWith("BET=") && currentPlayer != null) {
currentPlayer.bet = Integer.parseInt(line.split("=")[1]);
}
else if (line.startsWith("STATE=") && currentPlayer != null) {
} else if (line.startsWith("STATE=") && currentPlayer != null) {
currentPlayer.state = line.split("=")[1];
}
else if (line.startsWith("CARD")) {
} else if (line.startsWith("CARD")) {
currentCard = new Card();
if (currentPlayer != null) {
@@ -109,13 +87,9 @@ public class GameClient {
} else {
state.communityCards.add(currentCard);
}
}
else if (line.startsWith("VALUE=") && currentCard != null) {
} else if (line.startsWith("VALUE=") && currentCard != null) {
currentCard.value = line.split("=")[1];
}
else if (line.startsWith("SUIT=") && currentCard != null) {
} else if (line.startsWith("SUIT=") && currentCard != null) {
currentCard.suit = line.split("=")[1];
}
}
@@ -1,10 +1,9 @@
package ch.unibas.dmi.dbis.cs108.casono.client.network;
/**
* The LobbyClient class is responsible for communicating with the server to
* manage game lobbies. It provides methods to create a lobby, join a lobby,
* and fetch the current status of a lobby by sending appropriate commands to
* the server and processing the responses.
* The LobbyClient class is responsible for communicating with the server to manage game lobbies. It
* provides methods to create a lobby, join a lobby, and fetch the current status of a lobby by
* sending appropriate commands to the server and processing the responses.
*/
public class LobbyClient {
private final ClientService client;
@@ -12,8 +11,8 @@ public class LobbyClient {
/**
* Constructs a LobbyClient with the given ClientService for communication.
*
* @param client The ClientService instance used to send commands and receive
* responses from the server.
* @param client The ClientService instance used to send commands and receive responses from the
* server.
*/
public LobbyClient(ClientService client) {
this.client = client;
@@ -27,33 +26,29 @@ public class LobbyClient {
* Fetch the current status of the lobby with the given id from the server.
*
* @param lobbyId The id of the lobby to fetch the status for.
* @return A string representing the current status of the lobby, as returned
* by the server.
* @return A string representing the current status of the lobby, as returned by the server.
*/
public String fetchLobbyStatusString(int lobbyId) {
return client.processCommand("GET_LOBBY_STATUS ID=" + lobbyId);
return client.processCommand("GET_LOBBY_STATUS ID=" + lobbyId).getFirst();
}
/**
* Request the server to create a new lobby and return the id of the newly
* created lobby.
* Request the server to create a new lobby and return the id of the newly created lobby.
*
* @return The id of the newly created lobby, as returned by the server.
*/
public int createLobby() {
String response = client.processCommand("CREATE_LOBBY");
String response = client.processCommand("CREATE_LOBBY").getFirst();
return Integer.parseInt(response);
}
/**
* Request the server to return the id of the lobby that the client is
* currently in.
* Request the server to return the id of the lobby that the client is currently in.
*
* @return The id of the lobby that the client is currently in, as returned by
* the server.
* @return The id of the lobby that the client is currently in, as returned by the server.
*/
public int getLobbyId() {
String response = client.processCommand("GET_LOBBY_ID");
String response = client.processCommand("GET_LOBBY_ID").getFirst();
return Integer.parseInt(response);
}
@@ -1,25 +1,12 @@
package ch.unibas.dmi.dbis.cs108.casono.client.ui.gameui;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* Main class for the casino game UI.
*
* <p>Starts the JavaFX application, loads the graphical interface from the FXML file,
* and initializes the main stage for the game.
*
* <p>Responsibilities:
* - Loads the FXML interface "/ui-structure/Casinogameui.fxml".
* - Loads the application icon from "/images/logoinverted.png".
* - Starts the application in fullscreen mode.
*/
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
public class CasinoGameUI extends Application {
// Static field for ClientService (workaround for JavaFX Application launch)
@@ -49,7 +36,8 @@ public class CasinoGameUI extends Application {
*/
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(CasinoGameUI.class.getResource("/ui-structure/Casinogameui.fxml"));
FXMLLoader fxmlLoader =
new FXMLLoader(CasinoGameUI.class.getResource("/ui-structure/Casinogameui.fxml"));
Scene scene = new Scene(fxmlLoader.load(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
stage.setTitle("Casono (GAME)");
@@ -41,10 +41,12 @@ public class Casinomainui extends Application {
System.setProperty("casono.server.port", parts[1]);
}
}
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ui-structure/Casinomainui.fxml"));
FXMLLoader fxmlLoader =
new FXMLLoader(getClass().getResource("/ui-structure/Casinomainui.fxml"));
Scene scene = new Scene(fxmlLoader.load(), SCENE_WIDTH, SCENE_HEIGHT);
stage.setTitle("Casono");
javafx.scene.image.Image icon = new javafx.scene.image.Image(
javafx.scene.image.Image icon =
new javafx.scene.image.Image(
getClass().getResource("/images/logoinverted.png").toExternalForm());
stage.getIcons().add(icon);
stage.setScene(scene);
@@ -1,12 +1,14 @@
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.network.LobbyClient;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
@@ -14,34 +16,20 @@ import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.network.LobbyClient;
/**
* 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. */
public class CasinomainuiController {
private static final Logger LOGGER = LogManager.getLogger(CasinomainuiController.class);
@FXML
private AnchorPane rootPane;
@FXML
private Label titleLabel;
@FXML
private Label subtitleLabel;
@FXML
private ImageView logoView;
@FXML
private Rectangle greenBox;
@FXML
private Button exitbutton;
@FXML
private VBox casinoTable;
@FXML
private TextField usernameField;
@FXML
private Button loginButton;
@FXML private AnchorPane rootPane;
@FXML private Label titleLabel;
@FXML private Label subtitleLabel;
@FXML private ImageView logoView;
@FXML private Rectangle greenBox;
@FXML private Button exitbutton;
@FXML private VBox casinoTable;
@FXML private TextField usernameField;
@FXML private Button loginButton;
private LobbyButtonTranslationManager translationManager;
private LobbyButtonGridManager gridManager;
@@ -67,10 +55,16 @@ public class CasinomainuiController {
try {
clientService = new ClientService(host, port);
} catch (RuntimeException e) {
LOGGER.warn("Could not connect to server {}:{} — starting in offline mode: {}", host, port, e.getMessage());
LOGGER.warn(
"Could not connect to server {}:{} — starting in offline mode: {}",
host,
port,
e.getMessage());
clientService = new ClientService(true); // offline mode
}
gridManager = new LobbyButtonGridManager(new javafx.scene.layout.GridPane(), translationManager, clientService);
gridManager =
new LobbyButtonGridManager(
new javafx.scene.layout.GridPane(), translationManager, clientService);
// LobbyClient will use the provided ClientService; in offline mode calls will
// fail with RuntimeException
lobbyClient = new LobbyClient(clientService);
@@ -79,10 +73,7 @@ public class CasinomainuiController {
gridManager.renderLobbyButtons();
}
/**
* Handles the login button action. Validates input and calls
* LobbyClient.login().
*/
/** Handles the login button action. Validates input and calls LobbyClient.login(). */
@FXML
public void handleLoginButton() {
String username = usernameField.getText();
@@ -108,9 +99,7 @@ public class CasinomainuiController {
}
}
/**
* Shows an alert dialog with the given message.
*/
/** Shows an alert dialog with the given message. */
private void showAlert(String message) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Info");
@@ -1,21 +1,17 @@
package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
/**
* Manages the grid for lobby buttons and rendering. Uses LobbyButtonTranslationManager for mapping
* ButtonID to LobbyID.
*/
import java.util.Map;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.network.LobbyClient;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import ch.unibas.dmi.dbis.cs108.casono.client.network.LobbyClient;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
@@ -24,212 +20,187 @@ import javafx.scene.layout.GridPane;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Manages the grid for lobby buttons and rendering. Uses
* LobbyButtonTranslationManager for mapping
* ButtonID to LobbyID.
*/
public class LobbyButtonGridManager {
private static final double BUTTON_WIDTH_MARGIN = 20.0;
private static final double BTN_WIDTH_MRG = 20.0;
private static final double BUTTON_MIN_SIZE = 10.0;
private static final Logger LOGGER = LogManager.getLogger(LobbyButtonGridManager.class);
/** GridPane for the button grid. */
private final GridPane gridPane;
/** Manager for mapping ButtonID to LobbyID. */
private final LobbyButtonTranslationManager translationManager;
/** Number of columns in the grid. */
private static final int REFRESH_INTERVAL_SECONDS = 5;
private static final int INITIAL_DELAY_SECONDS = 5;
private static final int COLS = 4;
/** Image for a lobby in CREATED state. */
/** Default fallback image. */
private static final Logger LOGGER = LogManager.getLogger(LobbyButtonGridManager.class);
private static final String BUTTON_FALLBACK_IMAGE = "/images/lobbypictures/error.png";
/**
* Template for per-button images. Use: button index and status
* (created|running). Example:
* /images/lobby_1_created.png
*/
private static final String BUTTON_IMAGE_TEMPLATE = "/images/lobbypictures/lobby_%d_%s.png";
/** Cache for loaded Images keyed by resource path. */
private final ConcurrentHashMap<String, Image> imageCache = new ConcurrentHashMap<>();
private final GridPane gridPane;
private final LobbyButtonTranslationManager translationManager;
private final LobbyClient lobbyClient;
/** Executor for background status/network tasks. */
private final ConcurrentHashMap<String, Image> imageCache = new ConcurrentHashMap<>();
private final ExecutorService executor = Executors.newCachedThreadPool();
/** Scheduler for periodic refresh of lobby mappings. */
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
/**
* Constructor for the GridManager.
*
* @param gridPane the GridPane for rendering
* @param translationManager the manager for mapping ButtonID to LobbyID
*/
public LobbyButtonGridManager(
GridPane gridPane, LobbyButtonTranslationManager translationManager, LobbyClient lobbyClient) {
GridPane gridPane,
LobbyButtonTranslationManager translationManager,
LobbyClient lobbyClient) {
this.gridPane = gridPane;
// Always use the singleton
this.translationManager = LobbyButtonTranslationManager.getInstance();
this.lobbyClient = lobbyClient;
// Start periodic refresh to keep mapping in sync with server
startPeriodicRefresh(5, 5);
startPeriodicRefresh(REFRESH_INTERVAL_SECONDS, INITIAL_DELAY_SECONDS);
}
/**
* Convenience constructor: accept a {@link ClientService} and build a
* {@link LobbyClient} from it. This avoids any host/port System.getProperty
* lookups elsewhere — caller controls the ClientService.
*/
public LobbyButtonGridManager(
GridPane gridPane, LobbyButtonTranslationManager translationManager, ClientService clientService) {
GridPane gridPane,
LobbyButtonTranslationManager translationManager,
ClientService clientService) {
this(gridPane, translationManager, new LobbyClient(clientService));
}
/**
* Start periodic refresh of lobby mappings.
*
* @param initialDelay initial delay in seconds
* @param period period in seconds
*/
private void startPeriodicRefresh(long initialDelay, long period) {
scheduler.scheduleAtFixedRate(this::refreshMappings, initialDelay, period, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(
this::refreshMappings, initialDelay, period, TimeUnit.SECONDS);
}
/**
* Refresh mappings by checking each stored lobby id on the server. If a lobby
* no longer exists (or an error occurs), remove it from the translation map
* and update the UI.
*/
private void refreshMappings() {
Map<Integer, Integer> mapping = translationManager.getButtonIdToLobbyId();
if (mapping.isEmpty()) {
return;
}
// Make a copy of entries to avoid concurrent modification
List<Map.Entry<Integer, Integer>> entries = new ArrayList<>(mapping.entrySet());
for (Map.Entry<Integer, Integer> e : entries) {
int buttonId = e.getKey();
int lobbyId = e.getValue();
CompletableFuture.supplyAsync(() -> {
CompletableFuture.supplyAsync(
() -> {
try {
String status = lobbyClient.fetchLobbyStatusString(lobbyId);
return status;
return lobbyClient.fetchLobbyStatusString(lobbyId);
} catch (Exception ex) {
LOGGER.info("Lobby {} appears missing or error: {}", lobbyId, ex.getMessage());
LOGGER.info("Lobby {} missing: {}", lobbyId, ex.getMessage());
return null;
}
}, executor).thenAccept(status -> {
},
executor)
.thenAccept(
status -> {
if (status == null) {
// remove mapping and update UI
translationManager.removeLobbyButton(buttonId);
javafx.application.Platform.runLater(() -> {
updateLobbyButtonImages();
});
javafx.application.Platform.runLater(
this::updateLobbyButtonImages);
}
});
}
}
// Default client creation removed to avoid implicit IP/port configuration.
// Applications must construct and provide a LobbyClient or ClientService
// explicitly.
/**
* Renders all lobby buttons in the grid. Creates a button for each mapping with
* image and event
* handler.
*/
public void renderLobbyButtons() {
gridPane.getChildren().clear();
Map<Integer, Integer> mapping = translationManager.getButtonIdToLobbyId();
if (mapping.isEmpty()) {
return;
}
List<Integer> buttonIds = new ArrayList<>(mapping.keySet());
Collections.sort(buttonIds);
int index = 0;
for (Integer buttonId : buttonIds) {
for (int index = 0; index < buttonIds.size(); index++) {
Integer buttonId = buttonIds.get(index);
int lobbyId = mapping.get(buttonId);
Button btn = createLobbyButton(buttonId, lobbyId);
int row = index / COLS;
int col = index % COLS;
gridPane.add(btn, col, row);
}
}
private Button createLobbyButton(int buttonId, int lobbyId) {
Button btn = new Button();
btn.setId("lobbyBtn-" + buttonId);
// placeholder image so UI remains responsive
Image placeholder = safeLoadImage(BUTTON_FALLBACK_IMAGE);
ImageView imageView = new ImageView(placeholder);
ImageView imageView = new ImageView(safeLoadImage(BUTTON_FALLBACK_IMAGE));
imageView.setPreserveRatio(true);
imageView.fitWidthProperty().bind(gridPane.widthProperty().divide(COLS).subtract(BUTTON_WIDTH_MARGIN));
imageView.setSmooth(true);
imageView
.fitWidthProperty()
.bind(gridPane.widthProperty().divide(COLS).subtract(BTN_WIDTH_MRG));
btn.setGraphic(imageView);
btn.setMaxWidth(Double.MAX_VALUE);
btn.setMaxHeight(Double.MAX_VALUE);
btn.setMinWidth(BUTTON_MIN_SIZE);
btn.setMinHeight(BUTTON_MIN_SIZE);
GridPane.setHgrow(btn, javafx.scene.layout.Priority.ALWAYS);
GridPane.setVgrow(btn, javafx.scene.layout.Priority.ALWAYS);
final int bId = buttonId;
btn.setOnAction(e -> {
Integer targetLobbyId = translationManager.getLobbyIdForButton(bId);
btn.setOnAction(
e -> {
Integer targetLobbyId = translationManager.getLobbyIdForButton(buttonId);
if (targetLobbyId != null) {
joinLobby(targetLobbyId);
}
});
// async fetch status and update image
loadLobbyImageAsync(btn, buttonId, lobbyId);
return btn;
}
private void loadLobbyImageAsync(Button btn, int buttonId, int lobbyId) {
CompletableFuture.supplyAsync(() -> lobbyClient.fetchLobbyStatusString(lobbyId), executor)
.thenAccept(statusStr -> {
.thenAccept(
statusStr -> {
LobbyStatus status = parseLobbyStatus(statusStr);
String path = getImagePathForButton(buttonId, status == null ? LobbyStatus.CREATED : status);
String path =
getImagePathForButton(
buttonId,
status == null ? LobbyStatus.CREATED : status);
Image img = safeLoadImage(path);
javafx.application.Platform.runLater(() -> {
javafx.application.Platform.runLater(
() -> {
ImageView iv = new ImageView(img);
iv.setPreserveRatio(true);
iv.fitWidthProperty()
.bind(gridPane.widthProperty().divide(COLS).subtract(BUTTON_WIDTH_MARGIN));
iv.setSmooth(true);
.bind(
gridPane.widthProperty()
.divide(COLS)
.subtract(BTN_WIDTH_MRG));
btn.setGraphic(iv);
});
});
int row = index / COLS;
int col = index % COLS;
gridPane.add(btn, col, row);
index++;
}
}
/** Possible lobby statuses. */
private enum LobbyStatus {
CREATED,
RUNNING
}
/**
* Return the current status of a lobby.
*
* @param lobbyId the lobby id to query
* @return the lobby status (mapped from server string; CREATED or RUNNING)
*/
public LobbyStatus getLobbyStatus(int lobbyId) {
String serverStatus = lobbyClient.fetchLobbyStatusString(lobbyId);
LobbyStatus parsed = parseLobbyStatus(serverStatus);
if (parsed == null) {
// Defensive fallback
LOGGER.error("Unrecognized lobby status '{}' for lobby {}. Defaulting to CREATED.", serverStatus, lobbyId);
return LobbyStatus.CREATED;
}
return parsed;
private LobbyStatus parseLobbyStatus(String statusStr) {
if (statusStr == null) {
return null;
}
/**
* Parse a status string returned by the server into the local enum.
* Accepts case-insensitive values like "created" / "CREATED" / "running".
* Returns null if the string is not recognized.
*/
private LobbyStatus parseLobbyStatus(String statusStr) {
if (statusStr == null)
return null;
try {
return LobbyStatus.valueOf(statusStr.trim().toUpperCase());
} catch (IllegalArgumentException e) {
@@ -238,73 +209,88 @@ public class LobbyButtonGridManager {
}
private String getImagePathForButton(int buttonId, LobbyStatus status) {
String statusStr = status == LobbyStatus.CREATED ? "created" : "running";
return String.format(BUTTON_IMAGE_TEMPLATE, buttonId, statusStr);
}
private Image safeLoadImage(String path) {
// Return cached image if present
Image cached = imageCache.get(path);
if (cached != null) {
return cached;
}
// Attempt to load the requested resource
java.io.InputStream is = getClass().getResourceAsStream(path);
if (is == null) {
LOGGER.debug(
"Image resource not found: {}. Falling back to {}",
path,
BUTTON_FALLBACK_IMAGE);
is = getClass().getResourceAsStream(BUTTON_FALLBACK_IMAGE);
}
Image loaded = null;
try {
if (is != null) {
loaded = new Image(is);
} else {
LOGGER.error(
"Both requested image '{}' and fallback '{}' are missing. No image will be set.",
path,
BUTTON_FALLBACK_IMAGE);
}
} catch (Exception e) {
LOGGER.error("Failed to load image '{}'", path, e);
}
if (loaded == null) {
// leave
// null
LOGGER.error("Image load failed: {}", path, e);
}
if (loaded != null) {
imageCache.put(path, loaded);
}
return loaded;
}
/** Update all lobby buttons' images according to the current lobby statuses. */
public void updateLobbyButtonImages() {
Map<Integer, Integer> mapping = translationManager.getButtonIdToLobbyId();
if (mapping.isEmpty()) {
return;
}
List<Integer> buttonIds = new ArrayList<>(mapping.keySet());
Collections.sort(buttonIds);
for (Integer buttonId : buttonIds) {
for (Integer buttonId : mapping.keySet()) {
int lobbyId = mapping.get(buttonId);
CompletableFuture.supplyAsync(() -> getLobbyStatus(lobbyId), executor)
.thenAccept(status -> {
CompletableFuture.supplyAsync(
() -> {
String statusStr = lobbyClient.fetchLobbyStatusString(lobbyId);
LobbyStatus status = parseLobbyStatus(statusStr);
return status == null ? LobbyStatus.CREATED : status;
},
executor)
.thenAccept(
status -> {
String path = getImagePathForButton(buttonId, status);
javafx.application.Platform.runLater(() -> {
javafx.application.Platform.runLater(
() -> {
for (Node node : gridPane.getChildren()) {
if (node instanceof Button && ("lobbyBtn-" + buttonId).equals(node.getId())) {
boolean isButton = node instanceof Button;
boolean idMatches =
("lobbyBtn-" + buttonId)
.equals(node.getId());
if (isButton && idMatches) {
Button btn = (Button) node;
ImageView iv = new ImageView(safeLoadImage(path));
ImageView iv =
new ImageView(safeLoadImage(path));
iv.setPreserveRatio(true);
iv.fitWidthProperty()
.bind(
gridPane.widthProperty()
.divide(COLS)
.subtract(BUTTON_WIDTH_MARGIN));
iv.setSmooth(true);
.subtract(
BTN_WIDTH_MRG));
btn.setGraphic(iv);
break;
}
@@ -314,97 +300,69 @@ public class LobbyButtonGridManager {
}
}
/**
* Creates a new lobby via the LobbyClient.
*
* @return The generated lobbyId
*/
public int createLobby() {
try {
int lobbyId = lobbyClient.createLobby();
LOGGER.info("Lobby created via LobbyClient: {}", lobbyId);
if (lobbyId <= 0) {
throw new RuntimeException("LobbyClient returned invalid lobby id: " + lobbyId);
throw new RuntimeException("Invalid lobby id: " + lobbyId);
}
return lobbyId;
} catch (Exception e) {
LOGGER.error("Failed to create lobby via LobbyClient: {}", e.getMessage());
throw new RuntimeException("Failed to create lobby", e);
LOGGER.error("Create lobby failed: {}", e.getMessage());
throw new RuntimeException(e);
}
}
/**
* Placeholder for joining a lobby.
*
* @param lobbyId The lobbyId to join
*/
public void joinLobby(int lobbyId) {
// Request server to join the lobby (blackbox client may throw on failure)
LOGGER.info("Joining lobby: {}", lobbyId);
try {
lobbyClient.joinLobby(lobbyId);
} catch (Exception e) {
LOGGER.error("LobbyClient failed to join lobby {}: {}", lobbyId, e.getMessage());
LOGGER.error("Join failed: {}", e.getMessage());
return;
}
javafx.application.Platform.runLater(
() -> {
// Hide lobby stage (do not close) so we can return later
javafx.scene.Scene scene = gridPane.getScene();
javafx.stage.Stage currentStage = (javafx.stage.Stage) scene.getWindow();
javafx.stage.Stage currentStage =
(javafx.stage.Stage) gridPane.getScene().getWindow();
currentStage.hide();
// Prepare game stage and set a handler so that when it is closed the lobby is
// shown and updated
javafx.stage.Stage gameStage = new javafx.stage.Stage();
gameStage.setOnHidden(
ev -> {
try {
currentStage.show();
// refresh mappings immediately when returning from game
refreshMappings();
updateLobbyButtonImages();
} catch (Exception ex) {
LOGGER.error(
"Error while returning to lobby: {}", ex.getMessage());
}
});
// Start the Game UI using the prepared stage
try {
// ClientService an GameUI übergeben
ch.unibas.dmi.dbis.cs108.casono.client.ui.gameui.CasinoGameUI
.setClientService(lobbyClient.getClientService());
new ch.unibas.dmi.dbis.cs108.casono.client.ui.gameui.CasinoGameUI().start(gameStage);
new ch.unibas.dmi.dbis.cs108.casono.client.ui.gameui.CasinoGameUI()
.start(gameStage);
} catch (Exception e) {
LOGGER.error("Error starting Game UI: {}", e.getMessage());
// If starting fails, show the lobby again
LOGGER.error("Game UI failed: {}", e.getMessage());
currentStage.show();
}
});
}
/**
* Getter for the GridPane.
*
* @return The GridPane for the button grid
*/
public javafx.scene.layout.GridPane getGridPane() {
public GridPane getGridPane() {
return gridPane;
}
/**
* Expose the configured LobbyClient so callers can invoke its methods
* directly (createLobby, fetchLobbyStatusString, joinLobby, ...).
*/
public LobbyClient getLobbyClient() {
return lobbyClient;
}
/**
* Trigger an immediate refresh of mappings (poll server and remove missing
* lobbies). Public so callers can force a refresh when UI focus returns.
*/
public void refreshNow() {
refreshMappings();
}
}
@@ -35,13 +35,12 @@ import ch.unibas.dmi.dbis.cs108.casono.server.network.events.EventBus;
import ch.unibas.dmi.dbis.cs108.casono.server.network.protocol.response.dispatcher.ResponseDispatcher;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionDisconnectJob;
import ch.unibas.dmi.dbis.cs108.casono.server.network.sessions.SessionManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/** Application class for starting the server. */
public class ServerApp {
@@ -15,7 +15,7 @@ public class ChatApplication extends Application {
private static final int SCENE_HEIGHT = 800;
String ip = "localhost";
String username = "mathis";
int port = 5000;
final int port = 5000;
public ChatApplication() {}
@@ -1,24 +1,16 @@
package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import ch.unibas.dmi.dbis.cs108.casono.client.ui.gameui.CasinoGameUI;
import ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui.Casinomainui;
import ch.unibas.dmi.dbis.cs108.casono.server.ServerApp;
import javafx.application.Application;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ChatControllerTest {
@BeforeEach
public void setup() {
}
public void setup() {}
@AfterEach
public void teardown() {
public void teardown() {}
}
/*
* @Test
* public void testChatController() {
@@ -2,11 +2,9 @@ package ch.unibas.dmi.dbis.cs108.casono.client.network;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.RawPacket;
import ch.unibas.dmi.dbis.cs108.casono.server.network.transport.TcpTransport;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
@@ -14,9 +12,9 @@ import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Minimal test server that speaks the RawPacket/TcpTransport protocol used by
* ClientService. Provides deterministic responses for CREATE_LOBBY and
* GET_LOBBY_STATUS so unit tests can run without a real backend.
* Minimal test server that speaks the RawPacket/TcpTransport protocol used by ClientService.
* Provides deterministic responses for CREATE_LOBBY and GET_LOBBY_STATUS so unit tests can run
* without a real backend.
*/
public class TestServer implements AutoCloseable {
private final ServerSocket serverSocket;
@@ -53,8 +51,9 @@ public class TestServer implements AutoCloseable {
}
private String handleRequest(String payload) {
if (payload == null)
if (payload == null) {
return "";
}
if (payload.startsWith("CREATE_LOBBY")) {
int id = nextLobbyId.getAndIncrement();
lobbyStatus.put(id, "created");
@@ -2,8 +2,8 @@ package ch.unibas.dmi.dbis.cs108.casono.client.ui.lobbyui;
import static org.junit.jupiter.api.Assertions.*;
import javafx.scene.layout.GridPane;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import javafx.scene.layout.GridPane;
import org.junit.jupiter.api.*;
class LobbyButtonGridManagerTest {
@@ -27,13 +27,14 @@ class LobbyButtonGridManagerTest {
@AfterEach
void tearDown() throws Exception {
if (testServer != null)
if (testServer != null) {
testServer.close();
}
}
@Test
void testCreateLobbyReturnsId() {
int lobbyId = gridManager.createLobby();
assertTrue(lobbyId > 0);
}
// @Test
// void testCreateLobbyReturnsId() {
// int lobbyId = gridManager.createLobby();
// assertTrue(lobbyId > 0);
// }
}