Fix: Game UI to support server commands #277

Merged
j.kropff merged 3 commits from feat/game-ui-communication into main 2026-04-12 14:07:37 +02:00
Showing only changes of commit 7316ec80bf - Show all commits
@@ -48,6 +48,7 @@ public class CasinoGameController {
private PlayerId myPlayerId; private PlayerId myPlayerId;
private TaskbarController taskbarController; private TaskbarController taskbarController;
private Image dealerImage; private Image dealerImage;
private javafx.animation.Timeline timeline;
private static final int TOTAL_SLOTS = 5; private static final int TOTAL_SLOTS = 5;
private static final int PLAYER_SLOTS = 2; private static final int PLAYER_SLOTS = 2;
@@ -166,6 +167,8 @@ public class CasinoGameController {
// uiTest(); // uiTest();
// or
// empty display only (optional) // empty display only (optional)
renderCommunityCards(List.of()); renderCommunityCards(List.of());
renderPlayerCards(List.of()); renderPlayerCards(List.of());
@@ -277,29 +280,58 @@ public class CasinoGameController {
*/ */
private void startLoop() { private void startLoop() {
javafx.animation.Timeline t = timeline =
new javafx.animation.Timeline( new javafx.animation.Timeline(
new javafx.animation.KeyFrame( new javafx.animation.KeyFrame(
javafx.util.Duration.seconds(UI_UPDATE_INTERVAL_SECONDS), javafx.util.Duration.seconds(UI_UPDATE_INTERVAL_SECONDS),
e -> updateUI())); e -> updateUI()));
t.setCycleCount(javafx.animation.Animation.INDEFINITE); timeline.setCycleCount(javafx.animation.Animation.INDEFINITE);
t.play(); timeline.play();
} }
/** /**
* Update the UI by fetching the latest game state from the GameService and updating all * Stop the UI update loop, which can be called when the game ends or when the user exits the
* relevant components. * game screen to prevent unnecessary updates and resource usage.
*/
public void stop() {
if (timeline != null) {
timeline.stop();
}
}
/**
* Triggers an asynchronous UI update by fetching the current game state from the GameService.
*/ */
private void updateUI() { private void updateUI() {
java.util.concurrent.CompletableFuture.supplyAsync(
() -> {
try { try {
GameState s = gameService.refresh(); return gameService.refresh();
} catch (Exception e) {
LOGGER.severe("GameService Error: " + e.getMessage());
return null;
}
})
.thenAccept(
s -> {
if (s == null) { if (s == null) {
return; return;
} }
javafx.application.Platform.runLater(() -> applyState(s));
});
}
/**
* Updates all visual UI components with the data from the provided GameState. This includes
* community cards, pot, player information, and the taskbar.
*
* @param s The current state of the game to be rendered.
*/
private void applyState(GameState s) {
renderCommunityCards(s.communityCards); renderCommunityCards(s.communityCards);
renderPot(s.pot); renderPot(s.pot);
@@ -309,10 +341,8 @@ public class CasinoGameController {
updateGameInfo(s); updateGameInfo(s);
highlightDealer(s); highlightDealer(s);
updateTaskbar(s); if (taskbarController != null) {
taskbarController.update(s, myPlayerId);
} catch (Exception e) {
LOGGER.severe("Error: UI Update failed: " + e.getMessage());
} }
} }
@@ -323,14 +353,20 @@ public class CasinoGameController {
*/ */
private void updatePlayerCards(GameState s) { private void updatePlayerCards(GameState s) {
int active = s.activePlayer; if (s.players == null || myPlayerId == null) {
if (active >= 0 && active < s.players.size()) {
Player p = s.players.get(active);
renderPlayerCards(p.getCards());
} else {
renderPlayerCards(List.of()); renderPlayerCards(List.of());
return;
} }
for (Player p : s.players) {
if (p.getId().equals(myPlayerId)) {
renderPlayerCards(p.getCards());
return;
}
}
// fallback
renderPlayerCards(List.of());
} }
/** /**
@@ -357,9 +393,10 @@ public class CasinoGameController {
* @param s The current game state. * @param s The current game state.
*/ */
private void updateTaskbar(GameState s) { private void updateTaskbar(GameState s) {
if (taskbarController != null) {
taskbarController.update(s, myPlayerId); taskbarController.update(s, myPlayerId);
} }
}
/** /**
* Update the player status components with the latest player information from the game state. * Update the player status components with the latest player information from the game state.