From d6583a40c2405b1bda5e479144d9b60d77a003a3 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Fri, 17 Apr 2026 22:52:27 +0200 Subject: [PATCH 01/28] Add: GameControllerTest --- .../gc14/Controller/GameControllerTest.java | 228 ++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 src/test/java/it/polimi/ingsw/gc14/Controller/GameControllerTest.java diff --git a/src/test/java/it/polimi/ingsw/gc14/Controller/GameControllerTest.java b/src/test/java/it/polimi/ingsw/gc14/Controller/GameControllerTest.java new file mode 100644 index 0000000..19a845e --- /dev/null +++ b/src/test/java/it/polimi/ingsw/gc14/Controller/GameControllerTest.java @@ -0,0 +1,228 @@ +package it.polimi.ingsw.gc14.Controller; + +import it.polimi.ingsw.gc14.Model.Cards.TribeCard; +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; +import it.polimi.ingsw.gc14.Model.Player; +import org.junit.jupiter.api.Test; + +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +import static org.junit.jupiter.api.Assertions.*; + +class GameControllerTest { + + @Test + void addPlayer() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giorgio")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + assertNotNull(game.getPlayerByUsername("Giorgio")); + assertNotNull(game.getPlayerByUsername("Marco")); + assertNotNull(game.getPlayerByUsername("Luca")); + + assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage()); + assertFalse(controller.addPlayer("Extra")); + } + + @Test + void allMethodsShouldReturnFalseForUnknownUsername() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertFalse(controller.slotChoice("ghost", 0)); + assertFalse(controller.drawUpperTribeCard("ghost", 0)); + assertFalse(controller.drawLowerTribeCard("ghost", 0)); + assertFalse(controller.drawUpperBuildingCard("ghost", 0)); + assertFalse(controller.drawLowerBuildingCard("ghost", 0)); + assertFalse(controller.pickOptionalTribeCard("ghost", 0)); + assertFalse(controller.pickOptionalBuildingCard("ghost", 0)); + } + + @Test + void slotChoice() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giorgio")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + String cur = game.getCurrentState().getCurrentPlayer().getUserName(); + String other = cur.equals("Giorgio") ? "Marco" : "Giorgio"; + assertFalse(controller.slotChoice(other, 0)); + + Queue order = new LinkedList<>(); + for (int i = 0; i < 3; i++) { + Player p = game.getCurrentState().getCurrentPlayer(); + order.add(p); + assertTrue(controller.slotChoice(p.getUserName(), i)); + } + + assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage()); + assertEquals(order.poll(), game.getCurrentState().getCurrentPlayer()); + } + + @Test + void drawLowerTribeCard() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giorgio")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + Queue order = new LinkedList<>(); + for (int i = 0; i < 3; i++) { + Player p = game.getCurrentState().getCurrentPlayer(); + order.add(p); + assertTrue(controller.slotChoice(p.getUserName(), i)); + } + + Player first = order.poll(); + assertEquals(first, game.getCurrentState().getCurrentPlayer()); + + List cards = game.getLowerListTribeCards(); + int idx = cards.indexOf( + cards.stream() + .filter(c -> !c.IsEventCard()) + .findFirst() + .orElseThrow() + ); + + Player wrongPlayer = order.peek(); + assertNotNull(wrongPlayer); + + int before = first.getTotCharacters(); + assertTrue(controller.drawLowerTribeCard(first.getUserName(), idx)); + assertEquals(before + 1, first.getTotCharacters()); + } + + @Test + void drawUpperTribeCard() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giorgio")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + for (int i = 0; i < 3; i++) { + Player p = game.getCurrentState().getCurrentPlayer(); + assertTrue(controller.slotChoice(p.getUserName(), i)); + } + + assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage()); + + while (game.getCurrentState().getNUpper() == 0) { + assertTrue(game.getCurrentState().getNLower() > 0); + + Player current = game.getCurrentState().getCurrentPlayer(); + List lower = game.getLowerListTribeCards(); + int lowerIdx = lower.indexOf( + lower.stream() + .filter(c -> !c.IsEventCard()) + .findFirst() + .orElseThrow() + ); + + assertTrue(controller.drawLowerTribeCard(current.getUserName(), lowerIdx)); + } + + Player current = game.getCurrentState().getCurrentPlayer(); + int beforeTot = current.getTotCharacters(); + + List upper = game.getUpperListTribeCards(); + int upperIdx = upper.indexOf( + upper.stream() + .filter(c -> !c.IsEventCard()) + .findFirst() + .orElseThrow() + ); + + assertTrue(controller.drawUpperTribeCard(current.getUserName(), upperIdx)); + assertEquals(beforeTot + 1, current.getTotCharacters()); + } + + @Test + void drawUpperBuildingCard() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giacomo")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + for (int i = 0; i < 3; i++) { + Player p = game.getCurrentState().getCurrentPlayer(); + assertTrue(controller.slotChoice(p.getUserName(), i)); + } + + assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage()); + + while (game.getCurrentState().getNUpper() == 0) { + assertTrue(game.getCurrentState().getNLower() > 0); + + Player current = game.getCurrentState().getCurrentPlayer(); + List lower = game.getLowerListTribeCards(); + int lowerIdx = lower.indexOf( + lower.stream() + .filter(c -> !c.IsEventCard()) + .findFirst() + .orElseThrow() + ); + + assertTrue(controller.drawLowerTribeCard(current.getUserName(), lowerIdx)); + } + + Player current = game.getCurrentState().getCurrentPlayer(); + + assertFalse(controller.drawUpperBuildingCard(current.getUserName(), 0)); + + current.addFood(100); + assertTrue(controller.drawUpperBuildingCard(current.getUserName(), 0)); + } + + @Test + void drawLowerBuildingCard() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giacomo")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + for (int i = 0; i < 3; i++) { + Player p = game.getCurrentState().getCurrentPlayer(); + assertTrue(controller.slotChoice(p.getUserName(), i)); + } + + String cur = game.getCurrentState().getCurrentPlayer().getUserName(); + assertFalse(controller.drawLowerBuildingCard(cur, 0)); + } + + @Test + void pickOptionalCards() { + Game game = new Game(3); + GameController controller = new GameController(game); + + assertTrue(controller.addPlayer("Giacomo")); + assertTrue(controller.addPlayer("Marco")); + assertTrue(controller.addPlayer("Luca")); + + for (int i = 0; i < 3; i++) { + Player p = game.getCurrentState().getCurrentPlayer(); + assertTrue(controller.slotChoice(p.getUserName(), i)); + } + + String cur = game.getCurrentState().getCurrentPlayer().getUserName(); + assertFalse(controller.pickOptionalTribeCard(cur, 0)); + assertFalse(controller.pickOptionalBuildingCard(cur, 0)); + } +} From 339e606504518a783fad07f9e84d332a7e368e49 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sat, 18 Apr 2026 17:41:54 +0200 Subject: [PATCH 02/28] Add: Javadoc for BuildingCard --- .../ingsw/gc14/Model/Cards/BuildingCard.java | 96 ++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java index 9b632f7..5c16e8e 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java @@ -8,24 +8,76 @@ import it.polimi.ingsw.gc14.Model.Player; import java.util.ArrayList; public class BuildingCard extends PlayableCard implements Cloneable , BuildingEffect { + + /** + * The price of this building card. + */ private int price; + + /** + * Returns the price of this building card. + * + * @return the price of this building card + */ public int getPrice() { return price; } + + /** + * Indicates whether this building card has already been bought. + */ private boolean bought; + + /** + * The type of effect associated with this building card. + */ protected EffectType effectType; + + /** + * The identifier of the effect associated with this building card. + */ protected int effectId; + + /** + * The prestige value of this building card. + */ private int prestigeValue; + + /** + * Returns the prestige value of this building card. + * + * @return the prestige value of this building card + */ public int getPrestigeValue() { return prestigeValue; } + + /** + * Returns the identifier of the effect associated with this building card. + * + * @return the effect identifier of this building card + */ public int getEffectId() { return effectId; } + + /** + * Returns the type of effect associated with this building card. + * + * @return the effect type of this building card + */ public EffectType getEffectType() { return effectType; } + /** + * Creates a building card with the specified era, price, and prestige value. + * + * @param era the era of the building card + * @param price the price of the building card + * @param prestigeValue the prestige value of the building card + * @throws IllegalArgumentException if {@code price <= 0} or {@code prestigeValue < 0} + */ public BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{ super(era); if (price > 0) { @@ -41,6 +93,20 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf bought = false; } + + + /** + * Creates a building card with the specified effect identifier, era, price, + * and prestige value. + * The effect type is determined from the given effect identifier. + * + * @param effectId the identifier of the effect associated with the building card + * @param era the era of the building card + * @param price the price of the building card + * @param prestigeValue the prestige value of the building card + * @throws IllegalArgumentException if the effect identifier is not valid, + * if {@code price <= 0}, or if {@code prestigeValue < 0} + */ public BuildingCard(int effectId ,int era,int price,int prestigeValue) throws IllegalArgumentException{ this.effectId = effectId; switch (effectId){ @@ -72,12 +138,28 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf this(era,price,prestigeValue); } - + /** + * Creates and returns a copy of this building card. + * + * @return a clone of this building card + */ @Override public BuildingCard clone() { return new BuildingCard(effectId,getEra(),getPrice(),getPrestigeValue()); } + + /** + * Attempts to buy this building card for the specified player. + * The purchase succeeds only if the card has not already been bought + * and the player can pay its price in Food. + * If the purchase succeeds, the card is added to the player's building cards + * and marked as bought. + * + * @param player the player attempting to buy the building card + * @return {@code true} if the building card is successfully bought, + * {@code false} otherwise + */ public boolean buy(Player player) { if( bought || !player.removeFood(getPrice())) return false; @@ -85,8 +167,20 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf bought=true; return true; } + + /** + * Applies the effect of this building card to the specified player. + * + * @param player the player to whom the effect is applied + */ + @Override public void applyEffect(Player player){}; + /** + * Returns the string representation of this building card. + * + * @return the string representation of this building card + */ @Override public String toString() { return "Era:"+String.valueOf(getEra())+" Price:"+String.valueOf(getPrice())+" Prestige:"+String.valueOf(getPrestigeValue()); From 2aca523bf0a21786113d76289854a51a2564ae6f Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sat, 18 Apr 2026 17:56:40 +0200 Subject: [PATCH 03/28] Add: Javadoc for Character --- .../Model/Cards/TribeCards/Character.java | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java index 9717f5b..ea4a22d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java @@ -3,29 +3,75 @@ package it.polimi.ingsw.gc14.Model.Cards.TribeCards; import it.polimi.ingsw.gc14.Model.Cards.TribeCard; import it.polimi.ingsw.gc14.Model.Player; - +/** + * Abstract base class for all character cards. + * A Character is a {@link TribeCard} that is not an event card and is associated + * with a specific {@link CharacterType}. + */ public abstract class Character extends TribeCard implements Cloneable { + + /** + * The specific type of this character card. + */ private CharacterType type; + + /** + * Returns the type of this character card. + * + * @return the type of this character card + */ public CharacterType getType() { return type; } + /** + * Creates a character card with the specified era and character type. + * + * @param Era the era of the character card + * @param type the type of the character card + */ public Character(int Era, CharacterType type){ super(Era,false ); this.type = type; } + + /** + * Creates a character card with the specified era, character type, + * and minimum number of players. + * + * @param Era the era of the character card + * @param type the type of the character card + * @param nMin the minimum number of players required for the card + */ public Character(int Era, CharacterType type,int nMin){ super(Era,false ,nMin); this.type = type; } + /** + * Returns the string representation of this character card. + * The returned string includes the string representation of the superclass + * and the string representation of the character type. + * + * @return the string representation of this character card + */ @Override public String toString() { return super.toString()+" "+type.toString(); } + /** + * Creates and returns a copy of this character card. + * + * @return a clone of this character card + */ @Override public abstract Character clone(); + /** + * Inserts this character card into the appropriate collection of the specified player. + * + * @param player the player who receives the character card + */ public abstract void insert(Player player); } From 31796aabdb5d03fc3b70315e379ffdcd8a8853e1 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Sat, 18 Apr 2026 18:00:49 +0200 Subject: [PATCH 04/28] Updated: GameController --- .../java/it/polimi/ingsw/gc14/Controller/GameController.java | 3 +++ 1 file changed, 3 insertions(+) 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 077a9b7..bdc462e 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java @@ -10,6 +10,9 @@ public class GameController { } public GameController() { } + public Game getModel() { + return model; + } public void setModel(Game model) { this.model = model; } From 9f8413fdc211377099857d61d9908f68c66e370f Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sat, 18 Apr 2026 18:11:48 +0200 Subject: [PATCH 05/28] Add: Partial Implementation Of TCPServer.java. --- .../gc14/Network/Server/TCP/TCPServer.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/Server/TCP/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/Server/TCP/TCPServer.java index 4403d84..a2e6e80 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/Server/TCP/TCPServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/Server/TCP/TCPServer.java @@ -9,11 +9,16 @@ import java.util.List; public class TCPServer { int port = -1; + int ConnectedPlayers = 0; ServerSocket serverTCP = null; GameController gameController; private List clientHandlers; + private int getConnectedPlayers(){ + return ConnectedPlayers; + } + public void start(String args[]){ clientHandlers = new ArrayList<>(); @@ -25,19 +30,26 @@ public class TCPServer { e.printStackTrace(); return; } - System.out.println("Listening on port " + port); + System.out.println("Listening on port: " + port); while(true){ Socket clientSocket = null; - try { + try{ clientSocket = serverTCP.accept(); - } catch (IOException e) { + if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers >= clientHandlers.size()){ + clientSocket.close(); + System.out.println("Player already present or username is invalid. Connection terminated.\n"); + } + } + catch (IOException e){ e.printStackTrace(); } + System.out.println("Accepted"); - ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, this.gameController); + ConnectedPlayers++; + ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController); clientHandlers.add(clientHandler); Thread t = new Thread(clientHandler); t.start(); From 3abc0e14b89d40c36fd34c01f65a1f989e72ff93 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sat, 18 Apr 2026 18:20:46 +0200 Subject: [PATCH 06/28] Add: Javadoc for EventCard --- .../Model/Cards/TribeCards/EventCard.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java index 549d5fb..a555aa2 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java @@ -6,13 +6,37 @@ import it.polimi.ingsw.gc14.Model.Player; import java.util.ArrayList; import java.lang.reflect.Array; +/** + * Abstract base class for all event cards. + * An EventCard is a {@link TribeCard} marked as an event card and associated + * with a specific {@link EventType}. + */ public abstract class EventCard extends TribeCard { + // Getters + + /** + * The specific type of this event card. + */ private EventType type; + + /** + * Returns the type of this event card. + * + * @return the type of this event card + */ public EventType getType() { return type; } + // End getters // Constructors + + /** + * Creates an event card with the specified era and event type. + * + * @param Era the era of the event card + * @param type the type of the event card + */ public EventCard(int Era, EventType type) { super(Era,true); this.type = type; @@ -20,15 +44,34 @@ public abstract class EventCard extends TribeCard { // End Constructors // Function + + /** + * Creates and returns a copy of this event card. + * + * @return a clone of this event card + */ @Override public abstract TribeCard clone(); + /** + * Returns the string representation of this event card. + * The returned string includes the string representation of the superclass + * and the string representation of the event type. + * + * @return the string representation of this event card + */ @Override public String toString() { return super.toString()+" "+type.toString(); } + /** + * Activates the effect of this event card on the specified list of players. + * + * @param playerList the list of players affected by the event + */ public abstract void activateEvent (ArrayList playerList); + // End Functions } From 2e806d3987d9627382580d79ec50a387e17be022 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sat, 18 Apr 2026 18:21:01 +0200 Subject: [PATCH 07/28] Add: Javadoc for TribeCard --- .../ingsw/gc14/Model/Cards/TribeCard.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java index 00260f9..38e3364 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java @@ -3,24 +3,71 @@ package it.polimi.ingsw.gc14.Model.Cards; import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character; import it.polimi.ingsw.gc14.Model.PlayableCard; +/** + * Abstract base class for all tribe cards. + * A TribeCard is a {@link PlayableCard} that may either be an event card + * or a non-event card, and may optionally specify a minimum number of players. + */ public abstract class TribeCard extends PlayableCard { + + /** + * Indicates whether this tribe card is an event card. + */ private boolean isEventCard; + + /** + * Returns whether this tribe card is an event card. + * + * @return {@code true} if this card is an event card, {@code false} otherwise + */ public boolean IsEventCard() { return isEventCard; } + /** + * The minimum number of players required for this tribe card. + */ private int nMin=0; + + /** + * Returns the minimum number of players required for this tribe card. + * + * @return the minimum number of players required for this tribe card + */ public int getNMin() { return nMin; } + + /** + * Creates a tribe card with the specified era and event-card flag. + * The minimum number of players is set to 0. + * + * @param Era the era of the tribe card + * @param isEventCard whether the card is an event card + */ public TribeCard(int Era,boolean isEventCard) { this(Era,isEventCard,0); } + + /** + * Creates a tribe card with the specified era, event-card flag, + * and minimum number of players. + * + * @param Era the era of the tribe card + * @param isEventCard whether the card is an event card + * @param nMin the minimum number of players required for the card + */ public TribeCard(int Era,boolean isEventCard,int nMin) { super(Era); this.nMin=nMin; this.isEventCard=isEventCard; } + + /** + * Creates and returns a copy of this tribe card. + * + * @return a clone of this tribe card + */ @Override public abstract TribeCard clone(); } From c1e9602b35b174f9d244437fcdd886158fcc67d1 Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sat, 18 Apr 2026 19:17:55 +0200 Subject: [PATCH 08/28] Add: Added Partial Logic For Client Connection Handling And Termination In TCPServer.java. --- .../Network/TCP/Server/ClientHandler.java | 25 ++++++++++++------- .../gc14/Network/TCP/Server/TCPServer.java | 11 +++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java index a599d19..7000c31 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java @@ -12,37 +12,44 @@ public class ClientHandler implements Runnable { PrintWriter out = null; List clientHandlers; GameController gameController; + public ClientHandler(Socket clientSocket, List clientHandlers, GameController gameController) { this.clientSocket = clientSocket; this.clientHandlers = clientHandlers; this.gameController = gameController; } + @Override - public void run() { + public void run(){ clientLoop(); } - private void clientLoop() { - try { + + private void clientLoop(){ + try{ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - synchronized (out) { + synchronized(out){ out = new PrintWriter(clientSocket.getOutputStream(), true); } - } catch (IOException e) { + } + catch(IOException e){ e.printStackTrace(); } String s = ""; try{ while ((s = in.readLine()) != null) { - System.out.println(s); - out.println(s.toUpperCase()); + synchronized(out){ + System.out.println(s); + out.println(s.toUpperCase()); + } } } catch (IOException e) { e.printStackTrace(); } } - private void notifyEvent(Object event) { - synchronized (out) { + + public void notifyEvent(Object event) { + synchronized(out){ out.println(event.toString()); } } 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 8a4fe3b..7cafb9e 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 @@ -37,16 +37,15 @@ public class TCPServer { try{ clientSocket = serverTCP.accept(); - if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers >= clientHandlers.size()){ + if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers > gameController.getModel().getNPlayers()){ clientSocket.close(); - System.out.println("Player already present or username is invalid. Connection terminated.\n"); + System.out.println("Invalid parameters. Connection terminated.\n"); } } catch (IOException e){ e.printStackTrace(); } - System.out.println("Accepted"); ConnectedPlayers++; ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController); @@ -56,8 +55,12 @@ public class TCPServer { } } - private TCPServer(GameController gameController, int port) { + private TCPServer(GameController gameController, int port){ this.port = port; this.gameController = gameController; } + + public void broadcastUpdate(Object event){ + clientHandlers.forEach((x) -> x.notifyEvent(event)); + } } From 2302671a5ac9ddea9fe6adf6e1175ed8ef8ab504 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 12:15:14 +0200 Subject: [PATCH 09/28] Fixed: Javadoc in BuildingCard.java, Character.java, EventCard.java, and TribeCard.java. --- .../ingsw/gc14/Model/Cards/BuildingCard.java | 32 +++++++++---------- .../ingsw/gc14/Model/Cards/TribeCard.java | 16 +++++----- .../Model/Cards/TribeCards/Character.java | 20 ++++++------ .../Model/Cards/TribeCards/EventCard.java | 14 ++++---- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java index 5c16e8e..ba35960 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java @@ -17,7 +17,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Returns the price of this building card. * - * @return the price of this building card + * @return the price of this building card. */ public int getPrice() { return price; @@ -46,7 +46,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Returns the prestige value of this building card. * - * @return the prestige value of this building card + * @return the prestige value of this building card. */ public int getPrestigeValue() { return prestigeValue; @@ -55,7 +55,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Returns the identifier of the effect associated with this building card. * - * @return the effect identifier of this building card + * @return the effect identifier of this building card. */ public int getEffectId() { return effectId; @@ -64,7 +64,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Returns the type of effect associated with this building card. * - * @return the effect type of this building card + * @return the effect type of this building card. */ public EffectType getEffectType() { return effectType; @@ -73,9 +73,9 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Creates a building card with the specified era, price, and prestige value. * - * @param era the era of the building card - * @param price the price of the building card - * @param prestigeValue the prestige value of the building card + * @param era the era of the building card. + * @param price the price of the building card. + * @param prestigeValue the prestige value of the building card. * @throws IllegalArgumentException if {@code price <= 0} or {@code prestigeValue < 0} */ public BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{ @@ -100,10 +100,10 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf * and prestige value. * The effect type is determined from the given effect identifier. * - * @param effectId the identifier of the effect associated with the building card - * @param era the era of the building card - * @param price the price of the building card - * @param prestigeValue the prestige value of the building card + * @param effectId the identifier of the effect associated with the building card. + * @param era the era of the building card. + * @param price the price of the building card. + * @param prestigeValue the prestige value of the building card. * @throws IllegalArgumentException if the effect identifier is not valid, * if {@code price <= 0}, or if {@code prestigeValue < 0} */ @@ -141,7 +141,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Creates and returns a copy of this building card. * - * @return a clone of this building card + * @return a clone of this building card. */ @Override public BuildingCard clone() @@ -156,9 +156,9 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf * If the purchase succeeds, the card is added to the player's building cards * and marked as bought. * - * @param player the player attempting to buy the building card + * @param player the player attempting to buy the building card. * @return {@code true} if the building card is successfully bought, - * {@code false} otherwise + * {@code false} otherwise. */ public boolean buy(Player player) { if( bought || !player.removeFood(getPrice())) @@ -171,7 +171,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Applies the effect of this building card to the specified player. * - * @param player the player to whom the effect is applied + * @param player the player to whom the effect is applied. */ @Override public void applyEffect(Player player){}; @@ -179,7 +179,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf /** * Returns the string representation of this building card. * - * @return the string representation of this building card + * @return the string representation of this building card. */ @Override public String toString() { diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java index 38e3364..845f68b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java @@ -18,7 +18,7 @@ public abstract class TribeCard extends PlayableCard { /** * Returns whether this tribe card is an event card. * - * @return {@code true} if this card is an event card, {@code false} otherwise + * @return {@code true} if this card is an event card, {@code false} otherwise. */ public boolean IsEventCard() { return isEventCard; @@ -32,7 +32,7 @@ public abstract class TribeCard extends PlayableCard { /** * Returns the minimum number of players required for this tribe card. * - * @return the minimum number of players required for this tribe card + * @return the minimum number of players required for this tribe card. */ public int getNMin() { return nMin; @@ -42,8 +42,8 @@ public abstract class TribeCard extends PlayableCard { * Creates a tribe card with the specified era and event-card flag. * The minimum number of players is set to 0. * - * @param Era the era of the tribe card - * @param isEventCard whether the card is an event card + * @param Era the era of the tribe card. + * @param isEventCard whether the card is an event card. */ public TribeCard(int Era,boolean isEventCard) { this(Era,isEventCard,0); @@ -53,9 +53,9 @@ public abstract class TribeCard extends PlayableCard { * Creates a tribe card with the specified era, event-card flag, * and minimum number of players. * - * @param Era the era of the tribe card - * @param isEventCard whether the card is an event card - * @param nMin the minimum number of players required for the card + * @param Era the era of the tribe card. + * @param isEventCard whether the card is an event card. + * @param nMin the minimum number of players required for the card. */ public TribeCard(int Era,boolean isEventCard,int nMin) { super(Era); @@ -66,7 +66,7 @@ public abstract class TribeCard extends PlayableCard { /** * Creates and returns a copy of this tribe card. * - * @return a clone of this tribe card + * @return a clone of this tribe card. */ @Override public abstract TribeCard clone(); diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java index ea4a22d..9cdb9ae 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Character.java @@ -5,7 +5,7 @@ import it.polimi.ingsw.gc14.Model.Player; /** * Abstract base class for all character cards. - * A Character is a {@link TribeCard} that is not an event card and is associated + * A Character is a {@link TribeCard} that is not an event card and is associated. * with a specific {@link CharacterType}. */ public abstract class Character extends TribeCard implements Cloneable { @@ -18,7 +18,7 @@ public abstract class Character extends TribeCard implements Cloneable { /** * Returns the type of this character card. * - * @return the type of this character card + * @return the type of this character card. */ public CharacterType getType() { return type; @@ -27,8 +27,8 @@ public abstract class Character extends TribeCard implements Cloneable { /** * Creates a character card with the specified era and character type. * - * @param Era the era of the character card - * @param type the type of the character card + * @param Era the era of the character card. + * @param type the type of the character card. */ public Character(int Era, CharacterType type){ super(Era,false ); @@ -39,9 +39,9 @@ public abstract class Character extends TribeCard implements Cloneable { * Creates a character card with the specified era, character type, * and minimum number of players. * - * @param Era the era of the character card - * @param type the type of the character card - * @param nMin the minimum number of players required for the card + * @param Era the era of the character card. + * @param type the type of the character card. + * @param nMin the minimum number of players required for the card. */ public Character(int Era, CharacterType type,int nMin){ super(Era,false ,nMin); @@ -53,7 +53,7 @@ public abstract class Character extends TribeCard implements Cloneable { * The returned string includes the string representation of the superclass * and the string representation of the character type. * - * @return the string representation of this character card + * @return the string representation of this character card. */ @Override public String toString() { @@ -63,7 +63,7 @@ public abstract class Character extends TribeCard implements Cloneable { /** * Creates and returns a copy of this character card. * - * @return a clone of this character card + * @return a clone of this character card. */ @Override public abstract Character clone(); @@ -71,7 +71,7 @@ public abstract class Character extends TribeCard implements Cloneable { /** * Inserts this character card into the appropriate collection of the specified player. * - * @param player the player who receives the character card + * @param player the player who receives the character card. */ public abstract void insert(Player player); } diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java index a555aa2..a85898b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/EventCard.java @@ -8,7 +8,7 @@ import java.lang.reflect.Array; /** * Abstract base class for all event cards. - * An EventCard is a {@link TribeCard} marked as an event card and associated + * An EventCard is a {@link TribeCard} marked as an event card and associated. * with a specific {@link EventType}. */ public abstract class EventCard extends TribeCard { @@ -23,7 +23,7 @@ public abstract class EventCard extends TribeCard { /** * Returns the type of this event card. * - * @return the type of this event card + * @return the type of this event card. */ public EventType getType() { return type; } @@ -34,8 +34,8 @@ public abstract class EventCard extends TribeCard { /** * Creates an event card with the specified era and event type. * - * @param Era the era of the event card - * @param type the type of the event card + * @param Era the era of the event card. + * @param type the type of the event card. */ public EventCard(int Era, EventType type) { super(Era,true); @@ -48,7 +48,7 @@ public abstract class EventCard extends TribeCard { /** * Creates and returns a copy of this event card. * - * @return a clone of this event card + * @return a clone of this event card. */ @Override public abstract TribeCard clone(); @@ -58,7 +58,7 @@ public abstract class EventCard extends TribeCard { * The returned string includes the string representation of the superclass * and the string representation of the event type. * - * @return the string representation of this event card + * @return the string representation of this event card. */ @Override public String toString() { @@ -68,7 +68,7 @@ public abstract class EventCard extends TribeCard { /** * Activates the effect of this event card on the specified list of players. * - * @param playerList the list of players affected by the event + * @param playerList the list of players affected by the event. */ public abstract void activateEvent (ArrayList playerList); From c2500292f08d5c00a237b67539930b6ee11da6f6 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 15:29:28 +0200 Subject: [PATCH 10/28] Add: Javadoc for Slot class --- .../java/it/polimi/ingsw/gc14/Model/Slot.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java b/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java index 083e9f9..4f41da4 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java @@ -1,26 +1,79 @@ package it.polimi.ingsw.gc14.Model; +/** + * Represents a slot with a specific identifier and associated values + * for upper cards, lower cards, food, and minimum number of players. + * The slot configuration depends on the specified slot identifier. + */ public class Slot { // Getters + + /** + * The identifier of this slot. + */ private char slotId; + + /** + * Returns the identifier of this slot. + * + * @return the identifier of this slot. + */ public char getSlotId() { return slotId; } + + /** + * The number of upper cards associated with this slot. + */ private int NUpper; + /** + * Returns the number of upper cards associated with this slot. + * + * @return the number of upper cards associated with this slot. + */ public int getNUpper(){ return NUpper; } + + /** + * The minimum number of players required for this slot. + */ private int nMinPlayer; + + /** + * Returns the minimum number of players required for this slot. + * + * @return the minimum number of players required for this slot. + */ public int getNMinPlayer() { return nMinPlayer; } + + /** + * The number of lower cards associated with this slot. + */ private int NLower; + + /** + * Returns the number of lower cards associated with this slot. + * + * @return the number of lower cards associated with this slot. + */ public int getNLower(){ return NLower; } + /** + * The amount of Food associated with this slot. + */ private int Food; + + /** + * Returns the amount of Food associated with this slot. + * + * @return the amount of Food associated with this slot. + */ public int getFood(){ return Food; } @@ -28,7 +81,17 @@ public class Slot { // Setters // End setters + // Constructors + + /** + * Creates a slot with the specified identifier. + * The slot values for Food, NLower, NUpper, and minimum number of players + * are determined by the given slot identifier. + * + * @param slotId the identifier of the slot. + * @throws IllegalArgumentException if the specified slot identifier is not valid. + */ public Slot(char slotId) throws IllegalArgumentException { this.slotId = slotId; @@ -78,6 +141,14 @@ public class Slot { } } + + /** + * Returns the string representation of this slot. + * The returned string includes the slot identifier, number of upper cards, + * number of lower cards, food value, and minimum number of players. + * + * @return the string representation of this slot. + */ @Override public String toString() { return ("SlotID: "+this.getSlotId()+"\nNUpper: "+this.getNUpper()+"\nNLower: "+this.getNLower()+"\nFood: "+this.getFood()+"\nNMinPlayer: "+this.getNMinPlayer()+"\n"); From ec9c236932e85153a166a9e61adfa1acbf216013 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 15:34:49 +0200 Subject: [PATCH 11/28] Add: Javadoc for PlayableCard class --- .../polimi/ingsw/gc14/Model/PlayableCard.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java index 7d3b418..6df2330 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java @@ -1,10 +1,31 @@ package it.polimi.ingsw.gc14.Model; +/** + * Abstract base class for all playable cards. + * A PlayableCard is characterized by an era value. + */ public abstract class PlayableCard { + + /** + * The era associated with this playable card. + */ private int Era; + + /** + * Returns the era of this playable card. + * + * @return the era of this playable card. + */ public int getEra(){ return Era; } + + /** + * Creates a playable card with the specified era. + * + * @param Era the era of the playable card. + * @throws IllegalArgumentException if {@code Era <= 0} or {@code Era >= 4}. + */ public PlayableCard (int Era) throws IllegalArgumentException{ if (Era>0 && Era<4) { this.Era = Era; @@ -13,6 +34,11 @@ public abstract class PlayableCard { } } + /** + * Returns the string representation of this playable card. + * + * @return the string representation of this playable card. + */ @Override public String toString() { return "Era:"+String.valueOf(Era); From 9809a409a4bd099cee668901a36015aa90df533c Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 15:45:14 +0200 Subject: [PATCH 12/28] Add: Javadoc for OrderLogicCard class --- .../ingsw/gc14/Model/OrderLogicCard.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java index cfb298d..52fbf2b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java @@ -4,25 +4,75 @@ import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; import java.util.*; +/** + * Abstract base class for all order logic cards. + * An OrderLogicCard manages a queue of players and defines the effects + * applied when players are pushed back into the queue. + */ public abstract class OrderLogicCard { + + /** + * The queue of players associated with this order logic card. + */ private Queue players; + + /** + * Creates an order logic card with the specified list of players. + * The input list is shuffled before being inserted into the queue. + * + * @param players the list of players associated with this order logic card. + */ public OrderLogicCard(ArrayList players) { Collections.shuffle(players); this.players = new LinkedList<>(players); } + + /** + * Applies the effect associated with the current queue position of the player + * and then adds the player to the end of the queue. + * + * @param player the player to be pushed into the queue. + */ public void push(Player player){ effect(player,players.size()); players.add(player); } + + /** + * Removes and returns the first player in the queue. + * + * @return the first player in the queue, or {@code null} if the queue is empty. + */ public Player pull(){ return players.poll(); } + + /** + * Returns the first player in the queue without removing it. + * + * @return the first player in the queue, or {@code null} if the queue is empty. + */ public Player getFirst() { return players.peek(); } + + /** + * Applies the effect associated with the specified player and queue position. + * + * @param player the player to whom the effect is applied. + * @param index the queue position index associated with the effect. + * @throws IndexOutOfBoundsException if the specified index is not valid. + */ protected abstract void effect(Player player, int index) throws IndexOutOfBoundsException; + /** + * Applies the building-related effect to the specified player. + * For each building card owned by the player with effect id equal to 3, + * the player gains 1 Food. + * + * @param player the player to whom the building effect is applied. + */ protected void buildingEffect(Player player) { for(BuildingCard b : player.buildingCards.stream().filter(x->x.getEffectId()==3).toList()) From e9e0bc7405c16fb37e8589934e523d3f5a40efcc Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 16:02:03 +0200 Subject: [PATCH 13/28] Add: Javadoc for CurrentState class --- .../gc14/Model/GamePackage/CurrentState.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java index c1039b7..0d171f0 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java @@ -4,39 +4,108 @@ import it.polimi.ingsw.gc14.Model.Player; import it.polimi.ingsw.gc14.Model.Slot; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; +/** + * Represents the current state of the game. + * A CurrentState object stores the current player, slot, era, round, + * remaining upper and lower cards, and the current game stage. + */ public class CurrentState { // region Getters + + /** + * The current player associated with the game state. + */ private Player player; + + /** + * Returns the current player. + * + * @return the current player. + */ public Player getCurrentPlayer(){ return player; } + /** + * The current slot associated with the game state. + */ private Slot slot; + + /** + * Returns the current slot. + * + * @return the current slot. + */ public Slot getSlot(){ return slot; } + /** + * The current era of the game. + */ private int Era; + + /** + * Returns the current era of the game. + * + * @return the current era of the game. + */ public int getEra(){ return Era; } + /** + * The current round of the game. + */ private int round; + + /** + * Returns the current round of the game. + * + * @return the current round of the game. + */ public int getRound(){ return round; } + /** + * The number of upper cards currently available. + */ private int NUpper; + + /** + * Returns the number of upper cards currently available. + * + * @return the number of upper cards currently available. + */ public int getNUpper(){ return NUpper; } + /** + * The number of lower cards currently available. + */ private int NLower; + + /** + * Returns the number of lower cards currently available. + * + * @return the number of lower cards currently available. + */ public int getNLower(){ return NLower; } + /** + * The current stage of the game. + */ private GameStages GameStage; + + /** + * Returns the current stage of the game. + * + * @return the current stage of the game. + */ public GameStages getGameStage(){ return GameStage; } @@ -44,28 +113,52 @@ public class CurrentState { // endregion getters // region Setters + + /** + * Increments the current era by 1. + */ public void EraUpdate(){ Era++; } + /** + * Increments the current round by 1. + */ public void RoundUpdate(){ round++; } + /** + * Decrements the number of upper cards by 1. + */ public void UpperDrawn(){ NUpper--; } + /** + * Decrements the number of lower cards by 1. + */ public void LowerDrawn(){ NLower--; } + /** + * Updates the current game stage. + * + * @param GameStage the new game stage. + */ public void GameStageUpdate(GameStages GameStage){ this.GameStage = GameStage; } // endregion setters // region Constructors + + /** + * Creates a new CurrentState object with default initial values. + * The initial player and slot are {@code null}, the era and round are set to 1, + * the number of upper and lower cards is set to 0, and the game stage is set to {@code WAITING}. + */ public CurrentState(){ this.player = null; this.slot = null; @@ -78,6 +171,15 @@ public class CurrentState { // endregion constructors // region Functions + + /** + * Updates the current player and slot. + * If the specified slot is {@code null}, the numbers of upper and lower cards are both set to 0. + * Otherwise, the numbers of upper and lower cards are updated using the values of the specified slot. + * + * @param player the new current player. + * @param slot the new current slot. + */ public void PlayerUpdate(Player player, Slot slot){ this.player = player; this.slot = slot; From 4b732139ea62d7056fb7d5c6ae2d455542296dd5 Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:07:05 +0200 Subject: [PATCH 14/28] Add: Added Partial Logic For Client-Server Serialized Message Handling. --- .../java/it/polimi/ingsw/gc14/Model/Game.java | 3 +- .../Network/TCP/Server/ClientHandler.java | 32 ++++++++++--------- .../gc14/Network/TCP/Server/TCPServer.java | 23 +++++++++++-- 3 files changed, 40 insertions(+), 18 deletions(-) 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 4091090..98507e0 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -14,10 +14,11 @@ import it.polimi.ingsw.gc14.Model.Orders.Order4; import it.polimi.ingsw.gc14.Model.Orders.Order5; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; +import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; -public class Game { +public class Game implements Serializable { private ArrayList playersList; private CurrentState currentState; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java index 7000c31..6ac06de 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java @@ -8,11 +8,15 @@ import java.util.List; public class ClientHandler implements Runnable { private Socket clientSocket; - BufferedReader in = null; - PrintWriter out = null; + public ObjectInputStream in = null; + public ObjectOutputStream out = null; List clientHandlers; GameController gameController; + public Socket getClientSocket() { + return clientSocket; + } + public ClientHandler(Socket clientSocket, List clientHandlers, GameController gameController) { this.clientSocket = clientSocket; this.clientHandlers = clientHandlers; @@ -25,21 +29,11 @@ public class ClientHandler implements Runnable { } private void clientLoop(){ - try{ - in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - synchronized(out){ - out = new PrintWriter(clientSocket.getOutputStream(), true); - } - } - catch(IOException e){ - e.printStackTrace(); - } String s = ""; try{ while ((s = in.readLine()) != null) { synchronized(out){ - System.out.println(s); - out.println(s.toUpperCase()); + //do something } } } @@ -48,9 +42,17 @@ public class ClientHandler implements Runnable { } } - public void notifyEvent(Object event) { + public void notifyEvent(Object event){ synchronized(out){ - out.println(event.toString()); + try{ + out = new ObjectOutputStream(clientSocket.getOutputStream()); + out.writeObject(gameController.getModel()); + } + catch(IOException e){ + e.printStackTrace(); + } } } } + + 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 7cafb9e..0614f7b 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 @@ -2,7 +2,7 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; -import java.io.IOException; +import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.List; @@ -15,6 +15,8 @@ public class TCPServer { private List clientHandlers; + + private int getConnectedPlayers(){ return ConnectedPlayers; } @@ -46,10 +48,27 @@ public class TCPServer { e.printStackTrace(); } - System.out.println("Accepted"); + System.out.println("Accepted player: " + gameController.getModel().getPlayerByUsername(clientSocket.getInetAddress().toString())); + ConnectedPlayers++; ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController); clientHandlers.add(clientHandler); + + //Sending model to clients + if(ConnectedPlayers == gameController.getModel().getNPlayers()){ + for (ClientHandler handler : clientHandlers) { + try { + synchronized(handler.out){ + ObjectOutputStream socketTx = new ObjectOutputStream(handler.getClientSocket().getOutputStream()); + socketTx.writeObject(gameController.getModel()); + } + } + catch(IOException e){ + e.printStackTrace(); + } + } + } + Thread t = new Thread(clientHandler); t.start(); } From 2caad47737c33b319cc5629bfd15a89e94c8eda8 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 16:34:42 +0200 Subject: [PATCH 15/28] Add: Javadoc for Sustenance class --- .../Cards/TribeCards/Events/Sustenance.java | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/Sustenance.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/Sustenance.java index 00b2a9e..59be90f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/Sustenance.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/Sustenance.java @@ -9,17 +9,45 @@ import it.polimi.ingsw.gc14.Model.Player; import java.util.ArrayList; public class Sustenance extends EventCard { + + /** + * The prestige penalty multiplier applied for each unpaid Food unit. + */ private int PrestigeDebt; + + /** + * Returns the prestige penalty multiplier associated with this Sustenance event. + * + * @return the prestige penalty multiplier associated with this Sustenance event. + */ public int getPrestigeDebt() { return PrestigeDebt; } + /** + * Creates a Sustenance event card with the specified era and prestige debt value. + * + * @param Era the era of the event card. + * @param PrestigeDebt the prestige penalty multiplier for unpaid Food units. + */ public Sustenance(int Era, int PrestigeDebt) { super(Era, EventType.SUSTENANCE); this.PrestigeDebt = PrestigeDebt; } - // Sustenence va eseguito per ultimo tra gli eventi + /** + * Activates the Sustenance event for the specified list of players. + * For each player, the required Food is computed from the total number of characters, + * reduced by the contribution of Gatherers and by any applicable character discounts + * granted by owned building cards with effect id equal to 1. + * If the resulting Food debt is positive, the player must pay it with available Food. + * If the player does not have enough Food, all remaining Food is removed and the player + * loses Prestige equal to the unpaid Food debt multiplied by {@code PrestigeDebt}. + * This event is intended to be executed last among event effects. + * + * @param playerList the list of players affected by the event. + * @throws NullPointerException if {@code playerList} or one of its required elements is {@code null}. + */ @Override public void activateEvent (ArrayList playerList) throws NullPointerException { for(Player player : playerList){ @@ -51,6 +79,12 @@ public class Sustenance extends EventCard { } } } + + /** + * Creates and returns a copy of this Sustenance event card. + * + * @return a clone of this Sustenance event card. + */ @Override public EventCard clone() { return new Sustenance(getEra(), PrestigeDebt); From d14c97c1d4308f252a5d1a4d187bfeefba05865b Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:40:25 +0200 Subject: [PATCH 16/28] Add: Finished Implementation For TCP Server. --- .../Network/TCP/Server/ClientHandler.java | 28 +++++++++++++++---- .../gc14/Network/TCP/Server/TCPServer.java | 4 ++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java index 6ac06de..1a3b94f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java @@ -1,6 +1,8 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; import java.io.*; import java.net.*; @@ -8,10 +10,12 @@ import java.util.List; public class ClientHandler implements Runnable { private Socket clientSocket; + private TCPServer server; public ObjectInputStream in = null; public ObjectOutputStream out = null; List clientHandlers; GameController gameController; + private EventType eventType; public Socket getClientSocket() { return clientSocket; @@ -29,12 +33,26 @@ public class ClientHandler implements Runnable { } private void clientLoop(){ - String s = ""; try{ - while ((s = in.readLine()) != null) { - synchronized(out){ - //do something + NetworkEvent input = null; + synchronized(in){ + in = new ObjectInputStream(clientSocket.getInputStream()); + } + while(true){ + try{ + input = (NetworkEvent)(in.readObject()); + if(input.apply(gameController)){ + server.broadcastUpdate(input); + } } + catch(java.io.IOException e){ + e.printStackTrace(); + } + catch (ClassNotFoundException e){ + throw new RuntimeException(e); + } + + } } catch (IOException e) { @@ -42,7 +60,7 @@ public class ClientHandler implements Runnable { } } - public void notifyEvent(Object event){ + public void notifyEvent(NetworkEvent event){ synchronized(out){ try{ out = new ObjectOutputStream(clientSocket.getOutputStream()); 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 0614f7b..38fafe1 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 @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; import java.io.*; import java.net.*; @@ -43,6 +44,7 @@ public class TCPServer { clientSocket.close(); System.out.println("Invalid parameters. Connection terminated.\n"); } + // gestione di ADD_PLAYER } catch (IOException e){ e.printStackTrace(); @@ -79,7 +81,7 @@ public class TCPServer { this.gameController = gameController; } - public void broadcastUpdate(Object event){ + public void broadcastUpdate(NetworkEvent event){ clientHandlers.forEach((x) -> x.notifyEvent(event)); } } From f0a4a9e1bda65163558ede0a09425b911bba2f7d Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 16:42:01 +0200 Subject: [PATCH 17/28] Add: Javadoc for CavePaintings class --- .../TribeCards/Events/CavePaintings.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java index f89ca6c..ef6b8b8 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java @@ -11,9 +11,30 @@ import it.polimi.ingsw.gc14.Model.Player; import java.util.ArrayList; public class CavePaintings extends EventCard { + + /** + * The minimum number of Artist cards required to avoid the prestige penalty. + */ private int NLower; + + /** + * The amount of Prestige removed if the player has fewer Artist cards than {@code NLower}. + */ private int NPrestigeRem; // NPrestigeLower + + /** + * The Prestige multiplier applied if the player has at least {@code NLower} Artist cards. + */ private int NPrestigeMul; // NPrestigeUpper + + /** + * Creates a CavePaintings event card with the specified era and effect parameters. + * + * @param Era the era of the event card. + * @param NLower the minimum number of Artist cards required to avoid the prestige penalty. + * @param NPrestigeRem the amount of Prestige removed if the player has fewer Artist cards than {@code NLower}. + * @param NPrestigeMul the Prestige multiplier applied if the player has at least {@code NLower} Artist cards. + */ public CavePaintings(int Era, int NLower, int NPrestigeRem, int NPrestigeMul) { super(Era, EventType.CAVE_PAINTINGS); this.NLower = NLower; @@ -21,6 +42,16 @@ public class CavePaintings extends EventCard { this.NPrestigeMul = NPrestigeMul; } + /** + * Activates the CavePaintings event for the specified list of players. + * For each player, the number of Artist cards is computed together with the number + * of owned building cards having effect id equal to 9. + * The player gains Food equal to the number of such buildings multiplied by the number of Artist cards. + * If the player has fewer Artist cards than {@code NLower}, the player loses {@code NPrestigeRem} Prestige. + * Otherwise, the player gains Prestige equal to {@code NPrestigeMul} multiplied by the number of Artist cards. + * + * @param playerList the list of players affected by the event. + */ @Override public void activateEvent (ArrayList playerList){ for (Player player : playerList){ @@ -42,6 +73,12 @@ public class CavePaintings extends EventCard { } } } + + /** + * Creates and returns a copy of this CavePaintings event card. + * + * @return a clone of this CavePaintings event card. + */ @Override public EventCard clone() { From 47ef913a432ce8e5b4ee16dd701026d8d4ac529c Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:57:51 +0200 Subject: [PATCH 18/28] Add: Added Event Methods For Client-Server Event Execution. --- .../NetworkEvents/DrawLowerBuildingCard.java | 29 +++++++++++++++++++ .../TCP/NetworkEvents/DrawLowerTribeCard.java | 29 +++++++++++++++++++ .../NetworkEvents/DrawUpperBuildingCard.java | 29 +++++++++++++++++++ .../TCP/NetworkEvents/DrawUpperTribeCard.java | 29 +++++++++++++++++++ .../PickOptionalBuildingCard.java | 29 +++++++++++++++++++ .../NetworkEvents/PickOptionalTribeCard.java | 29 +++++++++++++++++++ .../Network/TCP/NetworkEvents/SlotChoice.java | 29 +++++++++++++++++++ 7 files changed, 203 insertions(+) create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java new file mode 100644 index 0000000..8e7fc80 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public DrawLowerBuildingCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_LOWER_BUILD; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawLowerBuildingCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java new file mode 100644 index 0000000..17848d1 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawLowerTribeCard extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public DrawLowerTribeCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_LOWER_TRIBE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawLowerTribeCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java new file mode 100644 index 0000000..a39bbb1 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public DrawUpperBuildingCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_UPPER_BUILD; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawUpperBuildingCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java new file mode 100644 index 0000000..6f261ed --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawUpperTribeCard extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public DrawUpperTribeCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_UPPER_TRIBE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawUpperTribeCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java new file mode 100644 index 0000000..1946be3 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public PickOptionalBuildingCard(String username, int pos){ + this.username = username; + this.eventType = EventType.PICK_OPTIONAL_BUILD; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.pickOptionalBuildingCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java new file mode 100644 index 0000000..5620976 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class PickOptionalTribeCard extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public PickOptionalTribeCard(String username, int pos){ + this.username = username; + this.eventType = EventType.PICK_OPTIONAL_TRIBE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.pickOptionalTribeCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java new file mode 100644 index 0000000..2c6caa1 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class SlotChoice extends NetworkEvent implements Serializable{ + private String username; + private EventType eventType; + private int pos; + + public SlotChoice(String username, int pos){ + this.username = username; + this.eventType = EventType.SLOT_CHOICE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.slotChoice(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} From a4e0b31534285695227ce03c87a265d45feb286b Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 16:58:55 +0200 Subject: [PATCH 19/28] Add: Javadoc for GameController class --- .../ingsw/gc14/Controller/GameController.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) 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 bdc462e..ca65067 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/GameController.java @@ -3,40 +3,114 @@ package it.polimi.ingsw.gc14.Controller; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Player; +/** + * Controller class that manages interactions between the client-side logic + * and the {@link Game} model. + * It provides methods to add players and to perform game actions by delegating them to the model. + */ public class GameController { + + /** + * The game model managed by this controller. + */ private Game model; + + /** + * Creates a GameController associated with the specified game model. + * + * @param model the game model managed by this controller. + */ public GameController(Game model) { this.model = model; } + + /** + * Creates a GameController without an associated game model. + */ public GameController() { } + + /** + * Returns the game model managed by this controller. + * + * @return the game model managed by this controller. + */ public Game getModel() { return model; } + + /** + * Updates the game model managed by this controller. + * + * @param model the new game model managed by this controller. + */ public void setModel(Game model) { this.model = model; } + + /** + * Attempts to add a new player with the specified username to the game model. + * + * @param username the username of the player to add. + * @return {@code true} if the player is successfully added, {@code false} otherwise. + */ public boolean addPlayer(String username) { return model.addPlayer(new Player(username)); } + + /** + * Attempts to draw an upper tribe card for the specified player from the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the upper tribe card to draw. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the draw operation fails. + */ public boolean drawUpperTribeCard(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null) return false; return model.DrawUpperTribeCardByIndex(model.getPlayerByUsername(playerUsername),pos); } + + /** + * Attempts to draw a lower tribe card for the specified player from the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the lower tribe card to draw. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the draw operation fails. + */ public boolean drawLowerTribeCard(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null) return false; return model.DrawLowerTribeCardByIndex(model.getPlayerByUsername(playerUsername), pos); } + + /** + * Attempts to draw an upper building card for the specified player from the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the upper building card to draw. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the draw operation fails. + */ public boolean drawUpperBuildingCard(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null) return false; return model.DrawUpperBuildingCardByIndex(model.getPlayerByUsername(playerUsername), pos); } + + /** + * Attempts to draw a lower building card for the specified player from the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the lower building card to draw. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the draw operation fails. + */ public boolean drawLowerBuildingCard(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null) @@ -44,18 +118,44 @@ public class GameController { return model.DrawLowerBuildingCardByIndex(model.getPlayerByUsername(playerUsername), pos); } + /** + * Attempts to pick an optional tribe card for the specified player from the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the optional tribe card to pick. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the pick operation fails. + */ public boolean pickOptionalTribeCard(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null) return false; return model.PickOptionalTribeCardByIndex(model.getPlayerByUsername(playerUsername), pos); } + + /** + * Attempts to pick an optional building card for the specified player from the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the optional building card to pick. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the pick operation fails. + */ public boolean pickOptionalBuildingCard(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null) return false; return model.PickOptionalBuildingCard(model.getPlayerByUsername(playerUsername), pos); } + + /** + * Attempts to perform the slot choice action for the specified player at the specified position. + * + * @param playerUsername the username of the player performing the action. + * @param pos the position of the chosen slot. + * @return {@code true} if the action succeeds, {@code false} if the player does not exist + * or if the slot choice operation fails. + */ public boolean slotChoice(String playerUsername,int pos) { Player player= model.getPlayerByUsername(playerUsername); if(player==null)//playerIndex>=model.) From d86d926306fef44e7222bee1967dbfa5a37be8d8 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Sun, 19 Apr 2026 17:02:34 +0200 Subject: [PATCH 20/28] Add: RMI --- .../ingsw/gc14/Model/Cards/BuildingCard.java | 5 +- .../ingsw/gc14/Model/Cards/TribeCard.java | 4 +- .../java/it/polimi/ingsw/gc14/Model/Game.java | 3 +- .../ingsw/gc14/Model/GamePackage/Board.java | 3 +- .../gc14/Model/GamePackage/CurrentState.java | 4 +- .../ingsw/gc14/Model/OrderLogicCard.java | 3 +- .../polimi/ingsw/gc14/Model/PlayableCard.java | 4 +- .../it/polimi/ingsw/gc14/Model/Player.java | 3 +- .../java/it/polimi/ingsw/gc14/Model/Slot.java | 4 +- .../Network/RMI/Client/IClientCallback.java | 13 +++++ .../gc14/Network/RMI/Client/RMIClient.java | 9 ++++ .../gc14/Network/RMI/Server/IGameServer.java | 20 ++++++++ .../gc14/Network/RMI/Server/IRMIServer.java | 9 ++++ .../Network/RMI/Server/RMIGameController.java | 51 +++++++++++++++++++ .../gc14/Network/RMI/Server/RMIServer.java | 33 ++++++++++++ .../java/it/polimi/ingsw/gc14/View/IView.java | 17 +++++++ 16 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java index 9b632f7..4a53828 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/BuildingCard.java @@ -5,9 +5,10 @@ import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType; import it.polimi.ingsw.gc14.Model.PlayableCard; import it.polimi.ingsw.gc14.Model.Player; +import java.io.Serializable; import java.util.ArrayList; -public class BuildingCard extends PlayableCard implements Cloneable , BuildingEffect { +public class BuildingCard extends PlayableCard implements Cloneable , BuildingEffect, Serializable { private int price; public int getPrice() { return price; @@ -26,7 +27,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf return effectType; } - public BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{ + protected BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{ super(era); if (price > 0) { this.price = price; diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java index 00260f9..5d8daa1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCard.java @@ -3,7 +3,9 @@ package it.polimi.ingsw.gc14.Model.Cards; import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character; import it.polimi.ingsw.gc14.Model.PlayableCard; -public abstract class TribeCard extends PlayableCard { +import java.io.Serializable; + +public abstract class TribeCard extends PlayableCard implements Serializable { private boolean isEventCard; public boolean IsEventCard() { return isEventCard; 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 4091090..98507e0 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -14,10 +14,11 @@ import it.polimi.ingsw.gc14.Model.Orders.Order4; import it.polimi.ingsw.gc14.Model.Orders.Order5; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; +import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; -public class Game { +public class Game implements Serializable { private ArrayList playersList; private CurrentState currentState; diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java index 7f989b5..c495c12 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java @@ -8,13 +8,14 @@ import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance; import it.polimi.ingsw.gc14.Model.DecksCreator; import it.polimi.ingsw.gc14.Model.Slot; +import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; /** * Board manages all the elements during the game such as decks, upper and lower rows, totems, tiles... */ -public class Board { +public class Board implements Serializable { /** * slotList contains the ordered list of slots (tiles). The slots changes based on the number of players. * Each slot (tile) has special action as drawing from the upper/lower row or taking food. diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java index c1039b7..f56c5bb 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/CurrentState.java @@ -4,7 +4,9 @@ import it.polimi.ingsw.gc14.Model.Player; import it.polimi.ingsw.gc14.Model.Slot; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; -public class CurrentState { +import java.io.Serializable; + +public class CurrentState implements Serializable { // region Getters private Player player; public Player getCurrentPlayer(){ diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java index cfb298d..2f55aab 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/OrderLogicCard.java @@ -2,9 +2,10 @@ package it.polimi.ingsw.gc14.Model; import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; +import java.io.Serializable; import java.util.*; -public abstract class OrderLogicCard { +public abstract class OrderLogicCard implements Serializable { private Queue players; public OrderLogicCard(ArrayList players) { Collections.shuffle(players); diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java b/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java index 7d3b418..bf2d41e 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/PlayableCard.java @@ -1,6 +1,8 @@ package it.polimi.ingsw.gc14.Model; -public abstract class PlayableCard { +import java.io.Serializable; + +public abstract class PlayableCard implements Serializable { private int Era; public int getEra(){ return Era; diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Player.java b/src/main/java/it/polimi/ingsw/gc14/Model/Player.java index 345bc04..bef8cbd 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Player.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Player.java @@ -4,13 +4,14 @@ import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType; import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.*; +import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; /** * Default Player class; contains all identifiers and methods needed. */ -public class Player { +public class Player implements Serializable { /** * The maximum length allowed for the username string. */ diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java b/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java index 083e9f9..46e7f94 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Slot.java @@ -1,6 +1,8 @@ package it.polimi.ingsw.gc14.Model; -public class Slot { +import java.io.Serializable; + +public class Slot implements Serializable { // Getters private char slotId; public char getSlotId() { diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java new file mode 100644 index 0000000..6b840e7 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java @@ -0,0 +1,13 @@ +package it.polimi.ingsw.gc14.Network.RMI.Client; + +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; + +import java.rmi.*; + +public interface IClientCallback extends Remote { + void onGameInit(Game model) throws RemoteException; + void onAction(NetworkEvent action) throws RemoteException; + void onError(String message) throws RemoteException; + void onGameOver(String winner) throws RemoteException; +} \ No newline at end of file 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 new file mode 100644 index 0000000..e31222e --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java @@ -0,0 +1,9 @@ +package it.polimi.ingsw.gc14.Network.RMI.Client; +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; + +public class RMIClient { + +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java new file mode 100644 index 0000000..a8beffd --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java @@ -0,0 +1,20 @@ +package it.polimi.ingsw.gc14.Network.RMI.Server; + +import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; + +import java.rmi.*; + +public interface IGameServer extends Remote { + // Connessione + String joinGame(String username, IClientCallback callback) throws RemoteException; + + // Azioni di gioco — specchio dei tuoi metodi GameController + boolean drawUpperTribeCard(String playerUsername, int pos) throws RemoteException; + boolean drawLowerTribeCard(String playerUsername, int pos) throws RemoteException; + boolean drawUpperBuildingCard(String playerUsername, int pos) throws RemoteException; + boolean drawLowerBuildingCard(String playerUsername, int pos) throws RemoteException; + boolean pickOptionalTribeCard(String playerUsername, int pos) throws RemoteException; + boolean pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException; + boolean slotChoice(String playerUsername, int pos) throws RemoteException; +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java new file mode 100644 index 0000000..2fddf40 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java @@ -0,0 +1,9 @@ +package it.polimi.ingsw.gc14.Network.RMI.Server; + +import java.rmi.RemoteException; + +public interface IRMIServer { + void connect(VirtualView client) throws RemoteException; + void add(Integer number) throws RemoteException; + void reset() throws RemoteException; +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java new file mode 100644 index 0000000..3ad851e --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java @@ -0,0 +1,51 @@ +package it.polimi.ingsw.gc14.Network.RMI.Server; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; + +import java.rmi.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.*; + +public class RMIGameController implements IGameServer { + + private final GameController controller; // delega tutto qui + private final Map clients = new ConcurrentHashMap<>(); + + public RMIGameController(GameController controller) { + this.controller = controller; + } + + @Override + public String joinGame(String username, IClientCallback callback) throws RemoteException { + controller.addPlayer(username); + clients.put(username, callback); + callback.onGameInit(controller.getModel()); + return username; + } + + + @Override + public boolean drawUpperTribeCard(String username, int pos) throws RemoteException { + boolean result = controller.drawUpperTribeCard(username, pos); + if (!result) { + notifyError(username, "Mossa non valida"); + } else { + notifyAll(new NetworkEvent(Action.ActionType.DRAW_UPPER_TRIBE, username, pos)); + } + return result; + } + + + + private void notifyAll(Action action) throws RemoteException { + for (IClientCallback cb : clients.values()) { + cb.onAction(action); + } + } + + private void notifyError(String username, String message) throws RemoteException { + IClientCallback cb = clients.get(username); + if (cb != null) cb.onError(message); + } \ No newline at end of file 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 new file mode 100644 index 0000000..ecc802a --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java @@ -0,0 +1,33 @@ +package it.polimi.ingsw.gc14.Network.RMI.Server; + +import it.polimi.ingsw.gc14.Controller.GameController; + +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; + +public class RMIServer { + + private final RMIGameController controller; + private Registry registry; + + public RMIServer(RMIGameController controller) { + this.controller = controller; + } + + @Override + public void start() throws Exception { + IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); + registry = LocateRegistry.createRegistry(1099); + registry.rebind("GameServer", stub); + System.out.println("RMI Server avviato sulla porta 1099"); + } + + @Override + public void stop() throws Exception { + registry.unbind("GameServer"); + UnicastRemoteObject.unexportObject(controller, true); + System.out.println("RMI Server fermato"); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/View/IView.java b/src/main/java/it/polimi/ingsw/gc14/View/IView.java index a30be8b..042191d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -1,4 +1,21 @@ package it.polimi.ingsw.gc14.View; +import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; + public interface IView { + // --- Setup --- + void showWelcome(); + String askPlayerName(); + + // --- Rendering --- + void render(GameState state); + void showMessage(String message); + void showError(String message); + void showWinner(String winner); + + // --- Input giocatore --- + NetworkEvent askMove(GameState state); + + // --- Lifecycle --- + void close(); } From 8f1562a83d3916bf9aeb822e40218bb90b8f9faf Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sun, 19 Apr 2026 17:07:29 +0200 Subject: [PATCH 21/28] Fix: Minor Changes In CavePaintings.java. --- .../ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java index ef6b8b8..cbb1458 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Cards/TribeCards/Events/CavePaintings.java @@ -14,6 +14,7 @@ public class CavePaintings extends EventCard { /** * The minimum number of Artist cards required to avoid the prestige penalty. + * Also, the bottom number on the card. */ private int NLower; From f83a979519ee08333b933ca669161937342b286a Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 17:44:43 +0200 Subject: [PATCH 22/28] Add: JavaDoc for Game getters and fields --- .../java/it/polimi/ingsw/gc14/Model/Game.java | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) 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 98507e0..c03c38f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -18,41 +18,107 @@ import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; +/** + * Represents the main game model. + * A Game object stores the players, the current state of the match, + * the slot assignments, the board, and the logic required to manage the game flow. + */ public class Game implements Serializable { + /** + * The list of players participating in the game. + */ private ArrayList playersList; + + /** + * The current state of the game. + */ private CurrentState currentState; + + /** + * The mapping between slots and the players assigned to them. + */ private HashMap slotMap; + + /** + * The configured number of players for this game. + */ private int nPlayers; + + /** + * The queue of players involved in optional card resolution. + */ private Queue OptionalCardQueue; + + /** + * The order logic card associated with this game. + */ private OrderLogicCard orderLogicCard; + + /** + * The board associated with this game. + */ private Board board; - + /** + * Returns clones of the upper tribe cards currently available on the board. + * + * @return a list containing clones of the upper tribe cards currently available on the board. + */ public ListgetUpperListTribeCards() { List cards = new ArrayList<>(); board.upperListTribe.forEach(x->cards.add(x.clone())); return cards; } + + /** + * Returns clones of the lower tribe cards currently available on the board. + * + * @return a list containing clones of the lower tribe cards currently available on the board. + */ public ListgetLowerListTribeCards() { List cards = new ArrayList<>(); board.lowerListTribe.forEach(x->cards.add(x.clone())); return cards; } + + /** + * Returns clones of the upper building cards currently available on the board. + * + * @return a list containing clones of the upper building cards currently available on the board. + */ public ListgetUpperListBuilding() { List cards = new ArrayList<>(); board.upperListBuilding.forEach(x->cards.add(x.clone())); return cards; } + + /** + * Returns clones of the lower building cards currently available on the board. + * + * @return a list containing clones of the lower building cards currently available on the board. + */ public ListgetLowerListBuilding() { List cards = new ArrayList<>(); board.lowerListBuilding.forEach(x->cards.add(x.clone())); return cards; } + + /** + * Returns the current state of the game. + * + * @return the current state of the game. + */ public CurrentState getCurrentState() { return currentState; }; + /** + * Returns the player with the specified username, if present. + * + * @param Username the username of the player to search for. + * @return the player with the specified username, or {@code null} if no such player exists. + */ public Player getPlayerByUsername(String Username) throws IndexOutOfBoundsException { return playersList.stream().filter(x->x.getUserName().equals(Username)).findFirst().orElse(null); } From b45a0df082506b16959ec0a0f2a7b79635843e1b Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Sun, 19 Apr 2026 17:56:45 +0200 Subject: [PATCH 23/28] Refactor: Network Type --- .../ingsw/gc14/Network/NetworkEvent.java | 20 +++++++++++++ .../gc14/Network/NetworkEvents/AddPlayer.java | 25 ++++++++++++++++ .../NetworkEvents/DrawLowerBuildingCard.java | 29 +++++++++++++++++++ .../NetworkEvents/DrawLowerTribeCard.java | 28 ++++++++++++++++++ .../NetworkEvents/DrawUpperBuildingCard.java | 28 ++++++++++++++++++ .../NetworkEvents/DrawUpperTribeCard.java | 28 ++++++++++++++++++ .../PickOptionalBuildingCard.java | 28 ++++++++++++++++++ .../NetworkEvents/PickOptionalTribeCard.java | 28 ++++++++++++++++++ .../Network/NetworkEvents/SlotChoice.java | 28 ++++++++++++++++++ .../Network/RMI/Client/IClientCallback.java | 2 +- .../gc14/Network/RMI/Server/IGameServer.java | 15 +++------- .../Network/RMI/Server/RMIGameController.java | 20 ++++++------- 12 files changed, 257 insertions(+), 22 deletions(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java new file mode 100644 index 0000000..6e0f683 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java @@ -0,0 +1,20 @@ +package it.polimi.ingsw.gc14.Network; + +import it.polimi.ingsw.gc14.Controller.GameController; + +import java.io.Serializable; + +public abstract class NetworkEvent implements Serializable { + protected String username; + public String getUsername() { + return username; + } + public NetworkEvent() + { + + } + public NetworkEvent(String username) { + this.username = username; + } + public abstract boolean apply(GameController gameController); +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java new file mode 100644 index 0000000..3032dfb --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java @@ -0,0 +1,25 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class AddPlayer extends NetworkEvent implements Serializable { + private EventType eventType; + public AddPlayer(String username) { + this.username = username; + this.eventType = EventType.ADD_PLAYER; + } + @Override + public boolean apply(GameController gameController) + { + return gameController.addPlayer(username); + } + public String apply(IView gameController) + { + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java new file mode 100644 index 0000000..3cfcadd --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{ + + private EventType eventType; + private int pos; + + public DrawLowerBuildingCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_LOWER_BUILD; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawLowerBuildingCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java new file mode 100644 index 0000000..0884279 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawLowerTribeCard extends NetworkEvent implements Serializable{ + private EventType eventType; + private int pos; + + public DrawLowerTribeCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_LOWER_TRIBE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawLowerTribeCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java new file mode 100644 index 0000000..d77d759 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{ + private EventType eventType; + private int pos; + + public DrawUpperBuildingCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_UPPER_BUILD; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawUpperBuildingCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java new file mode 100644 index 0000000..92cc9c0 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class DrawUpperTribeCard extends NetworkEvent implements Serializable{ + private EventType eventType; + private int pos; + + public DrawUpperTribeCard(String username, int pos){ + this.username = username; + this.eventType = EventType.DRAW_UPPER_TRIBE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.drawUpperTribeCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java new file mode 100644 index 0000000..1b3b89c --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{ + private EventType eventType; + private int pos; + + public PickOptionalBuildingCard(String username, int pos){ + this.username = username; + this.eventType = EventType.PICK_OPTIONAL_BUILD; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.pickOptionalBuildingCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java new file mode 100644 index 0000000..886db9f --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class PickOptionalTribeCard extends NetworkEvent implements Serializable{ + private EventType eventType; + private int pos; + + public PickOptionalTribeCard(String username, int pos){ + this.username = username; + this.eventType = EventType.PICK_OPTIONAL_TRIBE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController){ + return gameController.pickOptionalTribeCard(username, pos); + } + + public String apply(IView gameController){ + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java new file mode 100644 index 0000000..035dfdf --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14.Network.NetworkEvents; + +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +import java.io.Serializable; + +public class SlotChoice extends NetworkEvent implements Serializable { + private EventType eventType; + private int pos; + + public SlotChoice(String username, int pos) { + this.username = username; + this.eventType = EventType.SLOT_CHOICE; + this.pos = pos; + } + + @Override + public boolean apply(GameController gameController) { + return gameController.slotChoice(username, pos); + } + + public String apply(IView gameController) { + return gameController.toString(); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java index 6b840e7..d70ddcc 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Client; import it.polimi.ingsw.gc14.Model.Game; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvent; import java.rmi.*; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java index a8beffd..e966683 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java @@ -1,20 +1,13 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvent; import java.rmi.*; public interface IGameServer extends Remote { - // Connessione - String joinGame(String username, IClientCallback callback) throws RemoteException; - // Azioni di gioco — specchio dei tuoi metodi GameController - boolean drawUpperTribeCard(String playerUsername, int pos) throws RemoteException; - boolean drawLowerTribeCard(String playerUsername, int pos) throws RemoteException; - boolean drawUpperBuildingCard(String playerUsername, int pos) throws RemoteException; - boolean drawLowerBuildingCard(String playerUsername, int pos) throws RemoteException; - boolean pickOptionalTribeCard(String playerUsername, int pos) throws RemoteException; - boolean pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException; - boolean slotChoice(String playerUsername, int pos) throws RemoteException; + String joinGame(String username, IClientCallback callback) throws RemoteException; + boolean doEvent(NetworkEvent event) throws RemoteException; + } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java index 3ad851e..b32bc2f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java @@ -2,7 +2,8 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import it.polimi.ingsw.gc14.Controller.GameController; import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvents.DrawUpperBuildingCard; import java.rmi.*; import java.util.concurrent.ConcurrentHashMap; @@ -10,7 +11,7 @@ import java.util.*; public class RMIGameController implements IGameServer { - private final GameController controller; // delega tutto qui + private final GameController controller; private final Map clients = new ConcurrentHashMap<>(); public RMIGameController(GameController controller) { @@ -27,19 +28,17 @@ public class RMIGameController implements IGameServer { @Override - public boolean drawUpperTribeCard(String username, int pos) throws RemoteException { - boolean result = controller.drawUpperTribeCard(username, pos); + public void doEvent(NetworkEvent event) throws RemoteException { + boolean result = event.apply(controller); if (!result) { - notifyError(username, "Mossa non valida"); + notifyError("","Mossa non valida"); } else { - notifyAll(new NetworkEvent(Action.ActionType.DRAW_UPPER_TRIBE, username, pos)); + notifyAll(new DrawUpperBuildingCard(username, pos)); } return result; } - - - private void notifyAll(Action action) throws RemoteException { + private void notifyAll(NetworkEvent action) throws RemoteException { for (IClientCallback cb : clients.values()) { cb.onAction(action); } @@ -48,4 +47,5 @@ public class RMIGameController implements IGameServer { private void notifyError(String username, String message) throws RemoteException { IClientCallback cb = clients.get(username); if (cb != null) cb.onError(message); - } \ No newline at end of file + } +} \ No newline at end of file From 9b24ddfd7eb5e2bcf4a856971eb06d45fd373b38 Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 18:10:25 +0200 Subject: [PATCH 24/28] Add: JavaDoc for Game constructors and game action methods --- .../java/it/polimi/ingsw/gc14/Model/Game.java | 98 ++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) 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 c03c38f..72842ff 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -123,10 +123,21 @@ public class Game implements Serializable { return playersList.stream().filter(x->x.getUserName().equals(Username)).findFirst().orElse(null); } - + /** + * Returns the configured number of players for this game. + * + * @return the configured number of players for this game. + */ public int getNPlayers() { return nPlayers; } + + /** + * Creates a game with the specified number of players. + * + * @param nPlayers the configured number of players for the game. + * @throws IllegalArgumentException if {@code nPlayers < 0} or {@code nPlayers > 5}. + */ public Game(int nPlayers) throws IllegalArgumentException{ if(nPlayers < 0||nPlayers > 5) throw new IllegalArgumentException(); @@ -141,11 +152,24 @@ public class Game implements Serializable { playersList = new ArrayList<>(); OptionalCardQueue = new LinkedList<>(); } + + /** + * Creates a game with 0 configured players. + */ public Game() { this(0); } + /** + * Attempts to add the specified player to the game. + * The operation succeeds only if the configured number of players is not 0, + * the current game stage is {@code WAITING}, and the player is not already present. + * If the number of players reaches the configured maximum, the game is initialized. + * + * @param player the player to add to the game. + * @return {@code true} if the player is successfully added, {@code false} otherwise. + */ public boolean addPlayer(Player player) { if(this.nPlayers==0) { @@ -162,6 +186,12 @@ public class Game implements Serializable { } return true; } + + /** + * Initializes the game after all required players have been added. + * The method creates the appropriate order logic card according to the number of players, + * selects the first current player, and updates the game stage to {@code SLOT_CHOICE}. + */ public void init() { switch (nPlayers) { @@ -182,7 +212,18 @@ public class Game implements Serializable { currentState.GameStageUpdate(GameStages.SLOT_CHOICE); } - //region Cotroller Methods + //region Controller Methods + + /** + * Attempts to assign the slot at the specified index to the specified player. + * The operation succeeds only if the index is valid, the current game stage is {@code SLOT_CHOICE}, + * the specified player is the current player, and the selected slot is not already assigned. + * If the slot is successfully assigned, the next player setup is triggered. + * + * @param player the player performing the slot choice. + * @param slotIndex the index of the selected slot. + * @return {@code true} if the slot choice succeeds, {@code false} otherwise. + */ public boolean SlotChoiceByIndex(Player player, int slotIndex) { if(slotIndex<0 || slotIndex>=slotMap.size()) return false; @@ -206,6 +247,20 @@ public class Game implements Serializable { } //region Drawing Methods + + /** + * Attempts to draw the upper tribe card at the specified index for the specified player. + * The operation succeeds only if the index is valid, the game stage is {@code RESOLVING_ACTIONS}, + * the specified player is the current player, at least one upper card draw is still available, + * and the selected tribe card is not an event card. + * If successful, the card is inserted into the player's collection, removed from the board, + * and the number of remaining upper draws is decremented. + * If both upper and lower draws become zero, the next player setup is triggered. + * + * @param player the player performing the draw. + * @param cardIndex the index of the upper tribe card to draw. + * @return {@code true} if the draw succeeds, {@code false} otherwise. + */ public boolean DrawUpperTribeCardByIndex(Player player,int cardIndex) { if( cardIndex<0 || cardIndex >=board.upperListTribe.size()) return false; @@ -232,6 +287,20 @@ public class Game implements Serializable { return true; } + + /** + * Attempts to draw the lower tribe card at the specified index for the specified player. + * The operation succeeds only if the index is valid, the game stage is {@code RESOLVING_ACTIONS}, + * the specified player is the current player, at least one lower card draw is still available, + * and the selected tribe card is not an event card. + * If successful, the card is inserted into the player's collection, removed from the board, + * and the number of remaining lower draws is decremented. + * If both lower and upper draws become zero, the next player setup is triggered. + * + * @param player the player performing the draw. + * @param cardIndex the index of the lower tribe card to draw. + * @return {@code true} if the draw succeeds, {@code false} otherwise. + */ public boolean DrawLowerTribeCardByIndex(Player player, int cardIndex) { if( cardIndex<0 || cardIndex >=board.lowerListTribe.size()) return false; @@ -261,6 +330,18 @@ public class Game implements Serializable { } + /** + * Attempts to draw the upper building card at the specified index for the specified player. + * The operation succeeds only if the index is valid, the game stage is {@code RESOLVING_ACTIONS}, + * the specified player is the current player, at least one upper card draw is still available, + * and the selected building card can be bought by the player. + * If successful, the building card is removed from the board and the number of remaining upper draws is decremented. + * If both upper and lower draws become zero, the next player setup is triggered. + * + * @param player the player performing the draw. + * @param cardIndex the index of the upper building card to draw. + * @return {@code true} if the draw succeeds, {@code false} otherwise. + */ public boolean DrawUpperBuildingCardByIndex(Player player,int cardIndex) { if( cardIndex<0 || cardIndex >=board.upperListBuilding.size()) return false; @@ -287,6 +368,19 @@ public class Game implements Serializable { return true; } + + /** + * Attempts to draw the lower building card at the specified index for the specified player. + * The operation succeeds only if the index is valid, the game stage is {@code RESOLVING_ACTIONS}, + * the specified player is the current player, at least one lower card draw is still available, + * and the selected building card can be bought by the player. + * If successful, the building card is removed from the board and the number of remaining lower draws is decremented. + * If both lower and upper draws become zero, the next player setup is triggered. + * + * @param player the player performing the draw. + * @param cardIndex the index of the lower building card to draw. + * @return {@code true} if the draw succeeds, {@code false} otherwise. + */ public boolean DrawLowerBuildingCardByIndex(Player player,int cardIndex) { if( cardIndex<0 || cardIndex >=board.lowerListBuilding.size()) return false; From 84010ffd66756a0e9d66bf895a4d3d03b53c6eaf Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Sun, 19 Apr 2026 18:23:12 +0200 Subject: [PATCH 25/28] Fix Game and add: Observer --- src/main/java/it/polimi/ingsw/gc14/Model/Game.java | 14 +++++++++++++- .../it/polimi/ingsw/gc14/Network/Observer.java | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/Observer.java 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 72842ff..69fccdc 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -17,7 +17,7 @@ import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; - +import it.polimi.ingsw.gc14.Network.Observer; /** * Represents the main game model. * A Game object stores the players, the current state of the match, @@ -25,6 +25,18 @@ import java.util.stream.Collectors; */ public class Game implements Serializable { + private transient List observers = new ArrayList<>(); // transient! non serializzare + + public void addObserver(Observer observer) { + observers.add(observer); + } + + private void notifyObservers() { + for (Observer o : observers) { + o.update(this); + } + } + /** * The list of players participating in the game. */ diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/Observer.java b/src/main/java/it/polimi/ingsw/gc14/Network/Observer.java new file mode 100644 index 0000000..7677d92 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/Observer.java @@ -0,0 +1,7 @@ +package it.polimi.ingsw.gc14.Network; + +import it.polimi.ingsw.gc14.Model.Game; + +public interface Observer { + public void update(Game model); +} From 2572f8f387aa11fa098e93a462c6c68364532a5a Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Sun, 19 Apr 2026 19:05:44 +0200 Subject: [PATCH 26/28] Add: Added TCP Client Handling Logic in TCPClient.java + Refactoring. --- .../gc14/Network/{TCP => }/EventType.java | 2 +- .../gc14/Network/NetworkEvents/AddPlayer.java | 2 +- .../NetworkEvents/DrawLowerBuildingCard.java | 2 +- .../NetworkEvents/DrawLowerTribeCard.java | 2 +- .../NetworkEvents/DrawUpperBuildingCard.java | 2 +- .../NetworkEvents/DrawUpperTribeCard.java | 2 +- .../PickOptionalBuildingCard.java | 2 +- .../NetworkEvents/PickOptionalTribeCard.java | 2 +- .../Network/NetworkEvents/SlotChoice.java | 2 +- .../gc14/Network/TCP/Client/TCPClient.java | 82 +++++++++++++------ .../ingsw/gc14/Network/TCP/NetworkEvent.java | 10 --- .../Network/TCP/NetworkEvents/AddPlayer.java | 26 ------ .../NetworkEvents/DrawLowerBuildingCard.java | 29 ------- .../TCP/NetworkEvents/DrawLowerTribeCard.java | 29 ------- .../NetworkEvents/DrawUpperBuildingCard.java | 29 ------- .../TCP/NetworkEvents/DrawUpperTribeCard.java | 29 ------- .../PickOptionalBuildingCard.java | 29 ------- .../NetworkEvents/PickOptionalTribeCard.java | 29 ------- .../Network/TCP/NetworkEvents/SlotChoice.java | 29 ------- .../Network/TCP/Server/ClientHandler.java | 6 +- .../gc14/Network/TCP/Server/TCPServer.java | 6 +- .../java/it/polimi/ingsw/gc14/View/IView.java | 2 - 22 files changed, 74 insertions(+), 279 deletions(-) rename src/main/java/it/polimi/ingsw/gc14/Network/{TCP => }/EventType.java (82%) delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvent.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/AddPlayer.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/EventType.java b/src/main/java/it/polimi/ingsw/gc14/Network/EventType.java similarity index 82% rename from src/main/java/it/polimi/ingsw/gc14/Network/TCP/EventType.java rename to src/main/java/it/polimi/ingsw/gc14/Network/EventType.java index 3b63fcb..aae15bf 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/EventType.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/EventType.java @@ -1,4 +1,4 @@ -package it.polimi.ingsw.gc14.Network.TCP; +package it.polimi.ingsw.gc14.Network; public enum EventType { ADD_PLAYER, diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java index 3032dfb..3d3a9c5 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java index 3cfcadd..a4910dd 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java index 0884279..85f4598 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java index d77d759..77f7c60 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java index 92cc9c0..0f6d04d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java index 1b3b89c..dcf3822 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java index 886db9f..46cc783 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java index 035dfdf..700335a 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; +import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.View.IView; 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 b4cc5ec..113cd2e 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 @@ -1,37 +1,69 @@ package it.polimi.ingsw.gc14.Network.TCP.Client; -import it.polimi.ingsw.gc14.View.IView; +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer; import java.io.*; import java.net.*; -public class TCPClient implements Serializable { - public TCPClient(IView view) { - IView iView = view; +public class TCPClient implements Serializable{ + Socket communicationSocket = null; + ObjectInputStream socketReceive; + ObjectOutputStream socketSend; + + GameController controller; + String hostname; + int port; + + public TCPClient(GameController controller, String hostname, int port){ + this.controller = controller; + this.hostname = hostname; + this.port = port; } - public static void main(String[] args) { - String hostName = "127.0.0.1"; - int portNumber = 5200; - Socket communicationSocket = null; - try { - communicationSocket = new Socket(hostName, portNumber); + + public boolean start(String user){ + try{ + communicationSocket = new Socket(hostname, port); + socketSend = new ObjectOutputStream(communicationSocket.getOutputStream()); + socketReceive = new ObjectInputStream(communicationSocket.getInputStream()); + + socketSend.writeObject(new AddPlayer(user)); + if(communicationSocket.getInputStream().read() == -1){ + return false; + } + else{ + Thread listener = new Thread(() -> ReceiveMessage()); + listener.start(); + return true; + } + } + catch(Exception e){ + return false; + } + } + + private void ReceiveMessage(){ + while(true){ + try{ + ((NetworkEvent)(socketReceive.readObject())).apply(controller); + } + catch(IOException e){ + e.printStackTrace(); + } + catch(ClassNotFoundException e){ + throw new RuntimeException(e); + } + return; + } + } + + private void SendEvent(NetworkEvent event){ + try{ + socketSend.writeObject(event); } catch (IOException e) { - System.err.println(e.toString() + " " + hostName); - System.exit(1); - } - PrintWriter out = null; - BufferedReader in = null; - try { - out = new PrintWriter(communicationSocket.getOutputStream(), true); - in = new BufferedReader(new InputStreamReader(communicationSocket.getInputStream())); - } catch (IOException e) { - System.err.println(e.toString() + " " + hostName); - System.exit(1); - } - String userInput = ""; - while (true) { - + e.printStackTrace(); } } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvent.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvent.java deleted file mode 100644 index addbf74..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvent.java +++ /dev/null @@ -1,10 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP; - -import it.polimi.ingsw.gc14.Controller.GameController; - -import java.io.Serializable; - -public abstract class NetworkEvent implements Serializable { - - public abstract boolean apply(GameController gameController); -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/AddPlayer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/AddPlayer.java deleted file mode 100644 index 0067bcd..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/AddPlayer.java +++ /dev/null @@ -1,26 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class AddPlayer extends NetworkEvent implements Serializable { - private String username; - private EventType eventType; - public AddPlayer(String username) { - this.username = username; - this.eventType = EventType.ADD_PLAYER; - } - @Override - public boolean apply(GameController gameController) - { - return gameController.addPlayer(username); - } - public String apply(IView gameController) - { - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java deleted file mode 100644 index 8e7fc80..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerBuildingCard.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public DrawLowerBuildingCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_LOWER_BUILD; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.drawLowerBuildingCard(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java deleted file mode 100644 index 17848d1..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawLowerTribeCard.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class DrawLowerTribeCard extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public DrawLowerTribeCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_LOWER_TRIBE; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.drawLowerTribeCard(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java deleted file mode 100644 index a39bbb1..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperBuildingCard.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public DrawUpperBuildingCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_UPPER_BUILD; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.drawUpperBuildingCard(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java deleted file mode 100644 index 6f261ed..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/DrawUpperTribeCard.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class DrawUpperTribeCard extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public DrawUpperTribeCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_UPPER_TRIBE; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.drawUpperTribeCard(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java deleted file mode 100644 index 1946be3..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalBuildingCard.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public PickOptionalBuildingCard(String username, int pos){ - this.username = username; - this.eventType = EventType.PICK_OPTIONAL_BUILD; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.pickOptionalBuildingCard(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java deleted file mode 100644 index 5620976..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/PickOptionalTribeCard.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class PickOptionalTribeCard extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public PickOptionalTribeCard(String username, int pos){ - this.username = username; - this.eventType = EventType.PICK_OPTIONAL_TRIBE; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.pickOptionalTribeCard(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java deleted file mode 100644 index 2c6caa1..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/NetworkEvents/SlotChoice.java +++ /dev/null @@ -1,29 +0,0 @@ -package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents; - -import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; -import it.polimi.ingsw.gc14.View.IView; - -import java.io.Serializable; - -public class SlotChoice extends NetworkEvent implements Serializable{ - private String username; - private EventType eventType; - private int pos; - - public SlotChoice(String username, int pos){ - this.username = username; - this.eventType = EventType.SLOT_CHOICE; - this.pos = pos; - } - - @Override - public boolean apply(GameController gameController){ - return gameController.slotChoice(username, pos); - } - - public String apply(IView gameController){ - return gameController.toString(); - } -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java index 1a3b94f..97ea3c1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java @@ -1,8 +1,8 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.EventType; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.Network.EventType; import java.io.*; import java.net.*; @@ -40,7 +40,7 @@ public class ClientHandler implements Runnable { } while(true){ try{ - input = (NetworkEvent)(in.readObject()); + input = (NetworkEvent) (in.readObject()); if(input.apply(gameController)){ server.broadcastUpdate(input); } 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 38fafe1..da62d1d 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 @@ -1,7 +1,7 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; +import it.polimi.ingsw.gc14.Network.NetworkEvent; import java.io.*; import java.net.*; @@ -41,9 +41,13 @@ public class TCPServer { try{ clientSocket = serverTCP.accept(); if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers > gameController.getModel().getNPlayers()){ + clientSocket.getOutputStream().write((int)(-1)); clientSocket.close(); System.out.println("Invalid parameters. Connection terminated.\n"); } + else{ + clientSocket.getOutputStream().write((int)(1)); + } // gestione di ADD_PLAYER } catch (IOException e){ diff --git a/src/main/java/it/polimi/ingsw/gc14/View/IView.java b/src/main/java/it/polimi/ingsw/gc14/View/IView.java index 042191d..ac13003 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -1,7 +1,5 @@ package it.polimi.ingsw.gc14.View; -import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent; - public interface IView { // --- Setup --- void showWelcome(); From 6195ac0586dcd534d615e700b31e45d1f50499a1 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Sun, 19 Apr 2026 19:51:00 +0200 Subject: [PATCH 27/28] Fixed: RMI Implementation --- .../gc14/Controller/ClientController.java | 29 +++++++++++ .../RMI/Client/ClientCallbackImpl.java | 34 ++++++++++++ .../gc14/Network/RMI/Client/GameClient.java | 10 ++++ .../Network/RMI/Client/IClientCallback.java | 1 - .../gc14/Network/RMI/Client/RMIClient.java | 52 ++++++++++++++++++- .../gc14/Network/RMI/Server/IGameServer.java | 2 +- .../gc14/Network/RMI/Server/IRMIServer.java | 2 +- .../Network/RMI/Server/RMIGameController.java | 24 ++++++--- .../gc14/Network/RMI/Server/RMIServer.java | 28 ++++++---- .../java/it/polimi/ingsw/gc14/View/IView.java | 17 ++---- 10 files changed, 164 insertions(+), 35 deletions(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java diff --git a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java new file mode 100644 index 0000000..e330a90 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Controller; + +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.Observer; +import it.polimi.ingsw.gc14.View.IView; + +public class ClientController { + + private Game localModel; + public GameController localController; + private final IView view; + + public ClientController(IView view,Game localModel) { + this.view = view; + this.localModel = localModel; + this.localController = new GameController(localModel); + } + + public void setModel(Game model) { + this.localModel = model; + localController.setModel(model); + localModel.addObserver((Observer) view); // registra la view come observer + } + + public void onError(String message) { + view.showError(message); + } + +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java new file mode 100644 index 0000000..d79d357 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java @@ -0,0 +1,34 @@ +package it.polimi.ingsw.gc14.Network.RMI.Client; + +import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.NetworkEvent; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; + +public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCallback { + + private final ClientController clientController; + + public ClientCallbackImpl(ClientController clientController) throws RemoteException { + this.clientController = clientController; + } + + @Override + public void onGameInit(Game model) throws RemoteException { + clientController.setModel(model); // setta il model + } + + @Override + public void onAction(NetworkEvent event) throws RemoteException { + event.apply(clientController.localController); // delega tutto al controller + } + + + @Override + public void onError(String message) throws RemoteException { + clientController.onError(message); + } + +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java new file mode 100644 index 0000000..ffb42ff --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java @@ -0,0 +1,10 @@ +package it.polimi.ingsw.gc14.Network.RMI.Client; + +import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +public interface GameClient { + boolean connect( String username,ClientController clientController ); + void doEvent(NetworkEvent event) throws Exception; +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java index d70ddcc..0d3772f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java @@ -9,5 +9,4 @@ public interface IClientCallback extends Remote { void onGameInit(Game model) throws RemoteException; void onAction(NetworkEvent action) throws RemoteException; void onError(String message) throws RemoteException; - void onGameOver(String winner) throws RemoteException; } \ No newline at end of file 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 e31222e..39ada7c 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 @@ -4,6 +4,54 @@ import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; -public class RMIClient { +import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.Network.RMI.Server.*; +import it.polimi.ingsw.gc14.View.IView; -} +public class RMIClient implements GameClient { + + private final String host; + private final int port; + private IGameServer stub; + + public RMIClient(String host, int port) { + this.host = host; + this.port = port; + } + + @Override + public boolean connect(String username,ClientController clientController) { + // 1. Connettiti al registry + try { + Registry registry = LocateRegistry.getRegistry(host, port); + + // 2. Prendi lo stub del server + this.stub = (IGameServer) registry.lookup("RMIGameServer"); + + // 3. Crea il callback e registralo + ClientCallbackImpl callback = new ClientCallbackImpl(clientController); + + if (!stub.joinGame(username, callback)) + { + stub = null; + return false; + } + else + { + return true; + } + } + catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + @Override + public void doEvent( NetworkEvent event) throws Exception { + stub.doEvent(event); + } + +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java index e966683..17398b1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java @@ -7,7 +7,7 @@ import java.rmi.*; public interface IGameServer extends Remote { - String joinGame(String username, IClientCallback callback) throws RemoteException; + boolean joinGame(String username, IClientCallback callback) throws RemoteException; boolean doEvent(NetworkEvent event) throws RemoteException; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java index 2fddf40..70d7813 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java @@ -3,7 +3,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import java.rmi.RemoteException; public interface IRMIServer { - void connect(VirtualView client) throws RemoteException; + void add(Integer number) throws RemoteException; void reset() throws RemoteException; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java index b32bc2f..2f7cf82 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.DrawUpperBuildingCard; @@ -19,21 +20,23 @@ public class RMIGameController implements IGameServer { } @Override - public String joinGame(String username, IClientCallback callback) throws RemoteException { - controller.addPlayer(username); - clients.put(username, callback); - callback.onGameInit(controller.getModel()); - return username; + public boolean joinGame(String username, IClientCallback callback) { + if(controller.addPlayer(username)) + { + clients.put(username, callback); + return true; + } + return false; } @Override - public void doEvent(NetworkEvent event) throws RemoteException { + public boolean doEvent(NetworkEvent event) throws RemoteException { boolean result = event.apply(controller); if (!result) { - notifyError("","Mossa non valida"); + notifyError(event.getUsername(),"Mossa non valida"); } else { - notifyAll(new DrawUpperBuildingCard(username, pos)); + notifyAll(event); } return result; } @@ -43,6 +46,11 @@ public class RMIGameController implements IGameServer { cb.onAction(action); } } + private void notifyAll(Game model) throws RemoteException { + for (IClientCallback cb : clients.values()) { + cb.onGameInit(model); + } + } private void notifyError(String username, String message) throws RemoteException { IClientCallback cb = clients.get(username); 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 ecc802a..e07a1da 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 @@ -11,22 +11,30 @@ public class RMIServer { private final RMIGameController controller; private Registry registry; - - public RMIServer(RMIGameController controller) { + private int nPort; + public RMIServer(RMIGameController controller, int nPort) { this.controller = controller; + this.nPort = nPort; } - @Override - public void start() throws Exception { - IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); - registry = LocateRegistry.createRegistry(1099); - registry.rebind("GameServer", stub); - System.out.println("RMI Server avviato sulla porta 1099"); + + public boolean start() throws Exception { + try { + IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); + registry = LocateRegistry.createRegistry(nPort); + registry.rebind("RMIGameServer", stub); + System.out.println("RMI Server avviato sulla porta "+nPort); + return true; + } + catch (Exception e) { + e.printStackTrace(); + return false; + } } - @Override + public void stop() throws Exception { - registry.unbind("GameServer"); + registry.unbind("RMIGameServer"); UnicastRemoteObject.unexportObject(controller, true); System.out.println("RMI Server fermato"); } diff --git a/src/main/java/it/polimi/ingsw/gc14/View/IView.java b/src/main/java/it/polimi/ingsw/gc14/View/IView.java index ac13003..80c7616 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -1,19 +1,12 @@ package it.polimi.ingsw.gc14.View; -public interface IView { - // --- Setup --- - void showWelcome(); - String askPlayerName(); +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.NetworkEvent; - // --- Rendering --- - void render(GameState state); +public interface IView { + + void render(Game model); void showMessage(String message); void showError(String message); - void showWinner(String winner); - // --- Input giocatore --- - NetworkEvent askMove(GameState state); - - // --- Lifecycle --- - void close(); } From d70ea5a6cdc870da747924ab0923529c621943eb Mon Sep 17 00:00:00 2001 From: MatteoPellegrino05 Date: Mon, 20 Apr 2026 17:35:38 +0200 Subject: [PATCH 28/28] Add: JavaDoc for Game optional card and round management methods --- .../java/it/polimi/ingsw/gc14/Model/Game.java | 87 +++++++++++++++++-- 1 file changed, 80 insertions(+), 7 deletions(-) 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 69fccdc..7265930 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -425,6 +425,21 @@ public class Game implements Serializable { //endregion //region Optional Card Methods + + + /** + * Attempts to pick the upper optional tribe card at the specified index for the specified player. + * The operation succeeds only if the current game stage is {@code OPTIONAL_CARD_EFFECT}, + * the specified player is the current player, the index is valid, + * and the selected tribe card is not an event card. + * If successful, the card is inserted into the player's collection, + * removed from the board, the player is removed from the optional card queue, + * and the next player setup is triggered. + * + * @param player the player performing the optional tribe card pick. + * @param cardIndex the index of the upper optional tribe card to pick. + * @return {@code true} if the operation succeeds, {@code false} otherwise. + */ public boolean PickOptionalTribeCardByIndex(Player player,int cardIndex) { if(currentState.getGameStage() != GameStages.OPTIONAL_CARD_EFFECT){ return false; @@ -447,6 +462,19 @@ public class Game implements Serializable { nextPlayerSetup(); return true; } + + /** + * Attempts to pick the upper optional building card at the specified index for the specified player. + * The operation succeeds only if the current game stage is {@code OPTIONAL_CARD_EFFECT}, + * the specified player is the current player, the index is valid, + * and the selected building card can be bought by the player. + * If successful, the card is removed from the board, the next player setup is triggered, + * and the player is removed from the optional card queue. + * + * @param player the player performing the optional building card pick. + * @param cardIndex the index of the upper optional building card to pick. + * @return {@code true} if the operation succeeds, {@code false} otherwise. + */ public boolean PickOptionalBuildingCard(Player player, int cardIndex) { if(currentState.getGameStage() != GameStages.OPTIONAL_CARD_EFFECT){ return false; @@ -468,6 +496,17 @@ public class Game implements Serializable { return true; } + + /** + * Skips the optional card choice for the specified player. + * The operation succeeds only if the current game stage is {@code OPTIONAL_CARD_EFFECT} + * and the specified player is the current player. + * If successful, the player is removed from the optional card queue + * and the next player setup is triggered. + * + * @param player the player skipping the optional card choice. + * @return {@code true} if the operation succeeds, {@code false} otherwise. + */ public boolean NoOptionalCard(Player player) { if(currentState.getGameStage() != GameStages.OPTIONAL_CARD_EFFECT){ return false; @@ -483,6 +522,23 @@ public class Game implements Serializable { //endregion + /** + * Prepares the next player and updates the game state according to the current game stage. + * If the current stage is {@code SLOT_CHOICE}, the next player is taken from the order logic card. + * If no player is available, the game stage is updated to {@code RESOLVING_ACTIONS} + * and the first assigned slot is selected. + * If the current stage is {@code RESOLVING_ACTIONS}, the current player is pushed back + * into the order logic card, the current slot is freed, and the next assigned slot is selected. + * If no assigned slots remain, the game stage is updated to {@code OPTIONAL_CARD_EFFECT}, + * the optional card queue is built from players owning building cards with effect id equal to 12, + * and the first player in that queue is selected. + * If no player is available for optional card resolution, the game stage is updated to + * {@code RESOLVING_EVENT}; then, if the round number is less than 10, the next round is prepared, + * otherwise event resolution is performed, the game stage is updated to {@code ENDING}, + * and the game is ended. + * If the current stage is {@code OPTIONAL_CARD_EFFECT}, the next player is taken from the optional card queue. + * If no player is available, the game stage is updated to {@code RESOLVING_EVENT}. + */ private void nextPlayerSetup() { if(GameStages.SLOT_CHOICE==currentState.getGameStage()) { Player tempPlayer = orderLogicCard.pull(); @@ -554,7 +610,11 @@ public class Game implements Serializable { } } - + /** + * Resolves all pending event cards if the current game stage is {@code RESOLVING_EVENT}. + * All pending events are activated on the player list. + * Event cards of type {@code SUSTENANCE} are resolved after all other pending events. + */ private void EventResolution() { if(currentState.getGameStage()!= GameStages.RESOLVING_EVENT) @@ -576,6 +636,14 @@ public class Game implements Serializable { } } + + + /** + * Advances the game to the next round. + * The method first resolves pending events. + * If the current round is 10, the game stage is updated to {@code ENDING} and the game is ended. + * Otherwise, the era is updated if the board changes era, and the round number is incremented. + */ private void nextRound() { EventResolution(); @@ -590,6 +658,10 @@ public class Game implements Serializable { } + /** + * Ends the game by applying all final building effects owned by each player + * and updating the game stage to {@code ENDED}. + */ private void endGame() { playersList.forEach( p -> p.buildingCards.stream().filter(x -> x.getEffectType() == EffectType.FINAL). @@ -598,6 +670,13 @@ public class Game implements Serializable { currentState.GameStageUpdate(GameStages.ENDED); } + /** + * Sets the configured number of players for this game. + * The operation succeeds only if the current configured number of players is 0. + * + * @param nPlayers the new configured number of players. + * @return {@code true} if the number of players is updated, {@code false} otherwise. + */ public boolean setNPlayer(int nPlayers) { if(this.nPlayers!=0) @@ -606,10 +685,4 @@ public class Game implements Serializable { return true; } - - - - - - }