From 110526d836d39778f23fcd31b7aaeddd567c53f3 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Wed, 13 May 2026 16:55:12 +0200 Subject: [PATCH] Added:(Network) Totem Choice first implementation --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 3 ++- .../gc14/Controller/ClientController.java | 13 ++++++++++++ .../ingsw/gc14/Controller/GameController.java | 11 +++++++++- .../java/it/polimi/ingsw/gc14/Model/Game.java | 2 +- .../it/polimi/ingsw/gc14/Model/MiniModel.java | 7 ++++--- .../it/polimi/ingsw/gc14/Network/IClient.java | 1 + .../Network/NetworkEvents/TotemChoice.java | 20 ++++++++----------- .../gc14/Network/RMI/Client/RMIClient.java | 4 ++++ .../gc14/Network/RMI/Common/IGameServer.java | 2 +- .../gc14/Network/RMI/Server/RMIServer.java | 8 +++++--- .../gc14/Network/TCP/Client/TCPClient.java | 5 +++++ .../gc14/Network/TCP/Server/TCPServer.java | 4 ++-- .../it/polimi/ingsw/gc14/ServerLauncher.java | 3 ++- 13 files changed, 58 insertions(+), 25 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 45a1da6..2394445 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -137,7 +137,7 @@ public class ClientLauncherTUI { if(admissibleChar.contains(action)) { - 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")) { + 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")) { try { System.out.println("Insert the required position:"); pos = scanner.nextInt(); @@ -156,6 +156,7 @@ public class ClientLauncherTUI { case "6" -> controller.pickOptionalBuildingCard(username, pos); case "7" -> controller.noOptionalCard(username); case "8" -> controller.skipTurn(username); + case "9" -> controller.totemChoice(username,pos); case "A", "a" -> view.fullRender(); case "B", "b" -> view.renderBoard(); case "C", "c" -> view.renderPlayer(); diff --git a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java index 4184710..c429758 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -239,4 +239,17 @@ public class ClientController { } } + //TODO + public void totemChoice(String playerUsername,int pos) { + if(!Objects.equals(playerUsername, miniModel.currentState.getCurrentPlayer().getUserName())) + view.showError("It's not your turn!"); + else { + try { + client.totemChoice(playerUsername, String.valueOf(miniModel.availableTotems.get(pos))); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + } + } + } diff --git a/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java b/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java index 3fbc4c8..84cd82d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java @@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.Controller; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Player; +import it.polimi.ingsw.gc14.Model.Totems; /** * Controller class that manages interactions between the client-side logic @@ -200,8 +201,16 @@ public class GameController { */ public boolean slotChoice(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); - if(player==null)//playerIndex>=model.) + if(player==null) return false; return model.SlotChoiceByIndex(model.getPlayerByUsername(playerUsername),pos); } + + //TODO + public boolean TotemChoice(String playerUsername,String totem) { + Player player= model.getPlayerByUsername(playerUsername); + if(player==null) + return false; + return model.TotemChoice(player, Totems.valueOf(totem)); + } } 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 ff23bf9..c5053a1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -50,7 +50,7 @@ public class Game implements Serializable { private Queue totemChoiceQueue = new LinkedList<>(); //TODO - private boolean TotemChoice(Player player,Totems totem) { + public boolean TotemChoice(Player player,Totems totem) { if(!currentState.getGameStage().equals(GameStages.TOTEM_CHOICE)) return false; if(!getCurrentState().getCurrentPlayer().equals(player)) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/MiniModel.java b/src/main/java/it/polimi/ingsw/gc14/Model/MiniModel.java index 154f057..830175c 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/MiniModel.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/MiniModel.java @@ -16,22 +16,23 @@ public class MiniModel implements Serializable { public OrderLogicCard orderLogicCard; public CurrentState currentState; public Map players; - public ListavailableTotems; + public ListavailableTotems; public MiniModel(Board board) { this.board = board; } - public MiniModel(Board board, Map slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List players, ListavailableTotems) { + public MiniModel(Board board, Map slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List players, ListavailableTotems) { this.board = board; this.slotPlayerMap = slotPlayerMap; this.orderLogicCard = orderLogicCard; this.currentState = currentState; this.players=new HashMap<>(); + this.availableTotems=availableTotems; setPlayers(players); } public void setBoard(Board board) { this.board = board; } - public void setAvailableTotems(ListavailableTotems) { + public void setAvailableTotems(ListavailableTotems) { this.availableTotems = availableTotems; } public void setSlotPlayerMap(Map slotPlayerMap) { diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java index 1c90ea6..6392d5b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java @@ -29,4 +29,5 @@ public interface IClient { public void noOptionalCard(String playerUsername) throws RemoteException; public void slotChoice(String playerUsername,int pos) throws RemoteException; + public void totemChoice(String playerUsername,String totem) throws RemoteException; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/TotemChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/TotemChoice.java index 136600d..e911423 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/TotemChoice.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/TotemChoice.java @@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; import it.polimi.ingsw.gc14.Model.MiniModel; +import it.polimi.ingsw.gc14.Model.Totems; import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; @@ -15,20 +16,15 @@ import java.util.List; public class TotemChoice extends NetworkEvent implements Serializable { /** Index of the card to draw */ - private int pos; - private ListavailableTotems; - public void setAvailableTotems(List availableTotems) { + private String totem; + private ListavailableTotems; + public void setAvailableTotems(List availableTotems) { this.availableTotems = availableTotems; } - /** - * Class constructor. - * Initializes all the attributes. - * @param username the name of the player requesting the event - * @param pos the index of the card to draw - */ - public TotemChoice(String username, int pos) { + //TODO + public TotemChoice(String username, String totem) { super(username, EventType.TOTEM_CHOICE, false); - this.pos = pos; + this.totem = totem; } /** @@ -37,7 +33,7 @@ public class TotemChoice extends NetworkEvent implements Serializable { */ @Override public boolean apply(GameController gameController) { - return gameController.slotChoice(username, pos); + return gameController.TotemChoice(username, totem); } //TODO diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java index f35065b..f9d683a 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java @@ -224,6 +224,10 @@ public class RMIClient implements IClient { public void slotChoice(String playerUsername,int pos) throws RemoteException { stub.slotChoice(playerUsername,pos); } + //TODO + public void totemChoice(String playerUsername,String totem) throws RemoteException { + stub.totemChoice(playerUsername,totem); + } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java index edb8fc2..af931e6 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java @@ -22,7 +22,7 @@ public interface IGameServer extends Remote { void pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException; void noOptionalCard(String playerUsername) throws RemoteException; void slotChoice(String playerUsername, int pos) throws RemoteException; - + void totemChoice(String playerUsername, String totems) throws RemoteException; /** * Heartbeat: called periodically by the client to signal it is still alive. * Mirrors the PING/PONG mechanism used in the TCP heartbeat channel. diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java index 099d391..e74f777 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java @@ -91,7 +91,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer { System.out.println("Reconnected player: " + username); startWatchdog(username); Game game = controller.getModel(); - callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); + callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems())); System.out.println("Model sent: " + username); actionQueue.add(new ReconnectPlayer(username)); return true; @@ -117,7 +117,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer { System.out.println("Reconnected player: " + username); startWatchdog(username); Game game = controller.getModel(); - callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); + callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems())); System.out.println("Model sent: " + username); actionQueue.add(new ReconnectPlayer(username)); return true; @@ -285,7 +285,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer { } - + public void totemChoice(String playerUsername, String totems) throws RemoteException { + actionQueue.offer(new TotemChoice(playerUsername,totems)); + } // Metodi per avviare server RMI diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java index 17f922b..ffd5d99 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java @@ -292,4 +292,9 @@ public class TCPClient implements IClient { e.printStackTrace(); } } + + //TODO + public void totemChoice(String playerUsername,String totems) { + doEvent(new TotemChoice(playerUsername,totems)); + } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java index 9de535c..84253ab 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java @@ -126,7 +126,7 @@ public class TCPServer { clientSocket.getOutputStream().write(1); pendingHeartbeat.put(username, handler); Game game = controller.getModel(); - handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); + handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems())); Thread thread = new Thread(handler); thread.start(); clientHandlers.add(handler); @@ -175,7 +175,7 @@ public class TCPServer { clientSocket.getOutputStream().write(1); pendingHeartbeat.put(username, handler); Game game = controller.getModel(); - handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); + handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems())); Thread thread = new Thread(handler); thread.start(); clientHandlers.add(handler); diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 800145b..72fa817 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -6,6 +6,7 @@ import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Model.Player; +import it.polimi.ingsw.gc14.Model.Totems; import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound; @@ -215,7 +216,7 @@ public class ServerLauncher { MiniModel miniModel; synchronized (gameController) { Game game = gameController.getModel(); - miniModel= new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers()); + miniModel= new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(), new ArrayList<>(List.of(Totems.values()))); } serverRMI.notifyAll(miniModel); serverTCP.notifyAll(miniModel);