From ace3fb59166cac7eb34d355d49411819782199fd Mon Sep 17 00:00:00 2001 From: Gabriele Caliandro Date: Fri, 1 Jul 2022 23:59:26 +0200 Subject: [PATCH] Cli: BugFix related to chracter cards --- README.md | 2 +- .../polimi/ingsw/eriantys/cli/menus/Menu.java | 3 +- .../eriantys/cli/menus/game/MenuEffect.java | 29 ++++++++++++--- .../eriantys/cli/menus/game/MenuGame.java | 36 ++++++++++++++----- .../eriantys/cli/menus/game/MenuMoving.java | 3 +- .../cli/menus/game/MenuPickAssistantCard.java | 3 +- .../cli/menus/game/MenuPickingCloud.java | 3 +- .../eriantys/cli/menus/game/MenuPlacing.java | 17 +++++---- 8 files changed, 69 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 1ebfc11..f67e98e 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ connected clients and multiple games. * `4 players`: a 4 players' game can be played * `Multiple games`: every game has an associated game code (e.g. `ABCD`). The player must supply one when trying to join a game, and it is returned one after the creation of a new game. A list of available games to join is provided in UIs. -* `Player reconnections`: the server keep a heartbeat running with all clients (`PING` and `PONG` messages are used). +* `Player reconnections`: the server keeps a heartbeat running with all clients (`PING` and `PONG` messages are used). Once the heartbeat fails, the client is marked as disconnected in the game lobby, and the game continues skipping disconnected players. A player may also disconnect voluntarily from a game. Once it is reconnected, a copy of the game state gets sent to that player. diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/Menu.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/Menu.java index 8efd5fb..b52abf3 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/Menu.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/Menu.java @@ -94,7 +94,8 @@ final protected int getNumber() { while (true) { try { line = getKeyboardInput(); - return Integer.parseInt(line); + int number = Integer.parseInt(line); + return number; } catch (NumberFormatException e) { out.print("Must insert a number, insert again: "); } diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuEffect.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuEffect.java index 3617164..8580f8a 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuEffect.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuEffect.java @@ -5,17 +5,28 @@ import it.polimi.ingsw.eriantys.controller.Controller; import it.polimi.ingsw.eriantys.model.GameState; import it.polimi.ingsw.eriantys.model.entities.character_cards.CharacterCard; +import it.polimi.ingsw.eriantys.model.entities.character_cards.CharacterCardEnum; import it.polimi.ingsw.eriantys.model.entities.character_cards.ColorInputCards; import it.polimi.ingsw.eriantys.model.entities.character_cards.IslandInputCards; import java.beans.PropertyChangeEvent; import java.util.List; +import static it.polimi.ingsw.eriantys.controller.EventType.INPUT_ENTERED; import static it.polimi.ingsw.eriantys.model.enums.HouseColor.*; public class MenuEffect extends MenuGame { public MenuEffect() { super(); + addListeningEvents(); + } + + private void addListeningEvents() { + eventsToBeListening.forEach(eventType -> controller.addListener(this, eventType.tag)); + } + + private void removeListeningEvents() { + eventsToBeListening.forEach(eventType -> controller.removeListener(this, eventType.tag)); } @Override @@ -60,10 +71,10 @@ public MenuEnum show() { waitForGreenLight(); out.println("Card activated. ", GREEN); new CharacterCardsView(List.of(cc)).draw(out); - if (studentsLeftToMove() == 0) + if (studentsLeftToMove() == 0) { + removeListeningEvents(); return MenuEnum.MOVING; - else - return MenuEnum.PLACING; + } } // Print reasons why action is invalid @@ -108,19 +119,29 @@ private boolean chooseCharacterCard() { if (choice.equalsIgnoreCase("n")) return false; + // Purchasable check + CharacterCardEnum chosenCard = ccs().get(ccIndex).getCardEnum(); + boolean isUsed = game().getPlayingField().isCharacterCardUsed(chosenCard); + if (me().getCoins() < ccs().get(ccIndex).getCost(isUsed)) { + out.println("Not enough coins", RED); + return false; + } + // Send the action if (controller.sender().sendChooseCharacterCard(ccIndex)) { waitForGreenLight(); out.println("Card chosen.", GREEN); return true; } - out.println("Choose a valid card", RED); + out.println("Choose a valid card or not enough coins", RED); } } @Override public void propertyChange(PropertyChangeEvent evt) { super.propertyChange(evt); + if (evt.getPropertyName().equals(INPUT_ENTERED.tag)) + inputGreenLight = true; greenLight = true; } } diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuGame.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuGame.java index e89a06d..e8df216 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuGame.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuGame.java @@ -134,7 +134,7 @@ final protected void showViewOptions() { .append("7 - Show turn orders").append(System.lineSeparator()) .append("8 - Show players"); if (rules().gameMode.equals(GameMode.EXPERT)) - options.append("10 - CharacterCards"); + options.append("\n10 - CharacterCards"); out.println(options); // Optional closing row @@ -159,22 +159,37 @@ final protected void handleViewOptions(String choice) { if (rules().gameMode.equals(GameMode.EXPERT)) viewAll.addView(characterCardsView()); viewAll.draw(out); + return; } // View all islands - case "2" -> islandsView().draw(out); + case "2" -> { + islandsView().draw(out); + return; + } // View all dashboards - case "3" -> dashboardsView().draw(out); + case "3" -> { + dashboardsView().draw(out); + return; + } // View all character cards - case "4" -> cloudsView().draw(out); - + case "4" -> { + cloudsView().draw(out); + return; + } // View my assistant cards - case "5" -> new AssistantCardsView(me()).draw(out); + case "5" -> { + new AssistantCardsView(me()).draw(out); + return; + } // View my dashboard - case "6" -> new DashboardView(me(), rules(), professorHolder()).draw(out); + case "6" -> { + new DashboardView(me(), rules(), professorHolder()).draw(out); + return; + } // Show turn orders case "7" -> { @@ -190,15 +205,20 @@ final protected void handleViewOptions(String choice) { .forEach(player -> out.print(player + " -> ")); } out.println(); + return; } - case "8" -> playersView().draw(out); + case "8" -> { + playersView().draw(out); + return; + } // View all character cards case "10" -> { if (rules().gameMode.equals(GameMode.EXPERT)) characterCardsView().draw(out); } + default -> refreshMenu = false; } if (refreshMenu) diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuMoving.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuMoving.java index e96f4c6..505685d 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuMoving.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuMoving.java @@ -82,8 +82,7 @@ public MenuEnum show() { showOptions(); } - default -> { - } + default -> showOptions(); } } } diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickAssistantCard.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickAssistantCard.java index 833f693..2ca93e7 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickAssistantCard.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickAssistantCard.java @@ -75,8 +75,7 @@ public MenuEnum show() { out.println("Someone else already played this card.", RED); showOptions(); } - default -> { - } + default -> showOptions(); } } } diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickingCloud.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickingCloud.java index a1dbb77..03053cd 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickingCloud.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPickingCloud.java @@ -72,8 +72,7 @@ public MenuEnum show() { out.println("You're in the wrong phase.", RED); showOptions(); } - default -> { - } + default -> showOptions(); } } } diff --git a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPlacing.java b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPlacing.java index 0893fba..12e72e8 100644 --- a/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPlacing.java +++ b/src/main/java/it/polimi/ingsw/eriantys/cli/menus/game/MenuPlacing.java @@ -83,7 +83,7 @@ public MenuEnum show() { islandsView().draw(out); // Take island index input - out.println("Choose an island: "); + out.print("Choose an island: "); int islandIndex = getNumber() - 1; // Index correction // Send actions @@ -95,7 +95,9 @@ public MenuEnum show() { waitForGreenLight(); // Advance game condition - return escapeCondition(); + if(escapeCondition().equals(MenuEnum.EFFECT)) + new MenuEffect().show(); + return MenuEnum.MOVING; } // Move Students from entrance to dining @@ -116,7 +118,10 @@ public MenuEnum show() { waitForGreenLight(); // Advance game condition - return escapeCondition(); + // Advance game condition + if(escapeCondition().equals(MenuEnum.EFFECT)) + new MenuEffect().show(); + return MenuEnum.MOVING; } // Choose a character card from those in playing field @@ -127,9 +132,7 @@ public MenuEnum show() { } out.println("\nA card was already played", RED); } - - default -> { - } + default -> showOptions(); } } } @@ -139,7 +142,7 @@ private MenuEnum escapeCondition() { // Condition to continue the game if (studentsLeftToMove() == 0) { // Ask the player if he wants to play and effect before going on with the game - if (rules().gameMode.equals(GameMode.EXPERT) && !isCharacterCardUnplayed()) { + if (rules().gameMode.equals(GameMode.EXPERT) && isCharacterCardUnplayed()) { out.println("\nDo you want to play a character card?"); out.println("1 - YES"); out.println("ANY_KEY - NO");