From c3095ae69de5b02ba30d02110124aeda676cc1b3 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Wed, 13 May 2026 17:43:46 +0200 Subject: [PATCH 1/3] Update: TUI totem choice impl --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 3 +- .../java/it/polimi/ingsw/gc14/Model/Game.java | 32 +++++++++++++++- .../it/polimi/ingsw/gc14/ServerLauncher.java | 11 ++++-- .../it/polimi/ingsw/gc14/View/TUI/TUI.java | 37 ++++++++++++++++--- 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 2394445..f9e17b7 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -47,6 +47,7 @@ public class ClientLauncherTUI { admissibleChar.add("6"); admissibleChar.add("7"); admissibleChar.add("8"); + admissibleChar.add("9"); admissibleChar.add("A"); admissibleChar.add("B"); admissibleChar.add("C"); @@ -137,7 +138,7 @@ public class ClientLauncherTUI { if(admissibleChar.contains(action)) { - if (!action.equals("7") && !action.equals("8")&& !action.equals("9") && !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) { + if (!action.equals("7") && !action.equals("8")&& !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) { try { System.out.println("Insert the required position:"); pos = scanner.nextInt(); diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java index c5053a1..1b59d3d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -19,6 +19,7 @@ import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; import java.io.Serializable; import java.util.*; +import java.util.random.RandomGenerator; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -61,11 +62,27 @@ public class Game implements Serializable { } player.totem=totem; Player nextPlayer= totemChoiceQueue.poll(); + if(nextPlayer==null) { currentState.GameStageUpdate(GameStages.SLOT_CHOICE); currentState.PlayerUpdate(orderLogicCard.pull(),null); } + else + { + while(disconnetedPlayers.containsKey(nextPlayer)&& disconnetedPlayers.get(nextPlayer)) + { + nextPlayer.totem=getAvailableTotems().get(new Random().nextInt(getAvailableTotems().size()-1)); + nextPlayer=totemChoiceQueue.poll(); + if(nextPlayer==null) + { + currentState.GameStageUpdate(GameStages.SLOT_CHOICE); + currentState.PlayerUpdate(orderLogicCard.pull(),null); + return true; + } + } + currentState.PlayerUpdate(nextPlayer,null); + } return true; } @@ -80,7 +97,20 @@ public class Game implements Serializable { } disconnetedPlayers.put(player,true); if(currentState.getCurrentPlayer().equals(player)) - nextPlayerSetup(); + if(!currentState.getGameStage().equals(GameStages.TOTEM_CHOICE)) + nextPlayerSetup(); + else + { + if(totemChoiceQueue.isEmpty()) + { + player.totem=getAvailableTotems().get(new Random().nextInt(getAvailableTotems().size()-1)); + } + else { + totemChoiceQueue.add(player); + } + currentState.PlayerUpdate(totemChoiceQueue.poll(),null); + } + return true; } diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 72fa817..1fe83c8 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -11,6 +11,7 @@ import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound; import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer; +import it.polimi.ingsw.gc14.Network.NetworkEvents.TotemChoice; import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; import it.polimi.ingsw.gc14.View.TUI.TUI; @@ -116,21 +117,23 @@ public class ServerLauncher { int roundPrec=gameController.getModel().getCurrentState().getRound(); synchronized(gameController){ event.setIsError(!event.apply(gameController)); + Game game=gameController.getModel(); if(!event.getIsError()) { - event.setData(gameController.getModel().getSlotMap(),gameController.getModel().orderLogicCard,gameController.getModel().getCurrentState(),gameController.getModel().getPlayerByUsername(event.getUsername())); + event.setData(game.getSlotMap(),game.orderLogicCard,game.getCurrentState(),game.getPlayerByUsername(event.getUsername())); + if(event.getEventType().equals(EventType.TOTEM_CHOICE)) + ((TotemChoice)event).setAvailableTotems(gameController.getModel().getAvailableTotems()); } serverRMI.notifyAll(event); serverTCP.notifyAll(event); - if(gameController.getModel().getCurrentState().getRound()!=roundPrec) + if(game.getCurrentState().getRound()!=roundPrec) { - Game game=gameController.getModel(); ApplyNextRound nextRound=new ApplyNextRound(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(),game.getPlayers()); serverRMI.notifyAll(nextRound); serverTCP.notifyAll(nextRound); } if(!event.getIsError()){ - if(this.gameController.getModel().getCurrentState().getGameStage() == GameStages.ENDED){ + if(game.getCurrentState().getGameStage() == GameStages.ENDED){ if(!this.deleteSave()){ System.out.println("\n!!! Couldn't delete save !!!\n"); } diff --git a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java index d78577b..4a93894 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java @@ -1,8 +1,6 @@ package it.polimi.ingsw.gc14.View.TUI; -import it.polimi.ingsw.gc14.Model.Game; -import it.polimi.ingsw.gc14.Model.MiniModel; -import it.polimi.ingsw.gc14.Model.Player; -import it.polimi.ingsw.gc14.Model.Slot; +import it.polimi.ingsw.gc14.Model.*; +import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; import it.polimi.ingsw.gc14.View.IView; import java.util.ArrayList; @@ -92,7 +90,20 @@ public class TUI implements IView { */ @Override public void render() { - renderBoard(); + if(model.currentState.getGameStage().equals(GameStages.TOTEM_CHOICE)) + { + renderTotems(); + } + else if(model.currentState.getGameStage().equals(GameStages.ENDED)) + { + //renderStanding + renderBoard(); + } + else + { + renderBoard(); + } + System.out.println("\nYOUR ACTION:"); } /** @@ -141,10 +152,23 @@ public class TUI implements IView { System.out.println(AsciiTable.sideBySide(lines, lines2, 3)); } + public void renderTotems() { + clearTerminal(); + var table = new AsciiTable(BorderStyle.ROUNDED, model.availableTotems.size()); + List lines = model.availableTotems.stream().map(Totems::toString).toList(); + int i=0; + for(String line : lines) + { + line = (i++) +". "+ line; + } + table.addRow(lines); + System.out.println(model.currentState+"\nTOTEMS AVAILABLE:\n"+table.build()); + } + /** * Renders the player status table only. * - *

Layout: + *

Layout:SkipTurn *