From 5246818fca9c1a8e41f60a2d767a0cb7e3acf863 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Thu, 30 Apr 2026 18:34:40 +0200 Subject: [PATCH] Fix: MVC (Client) Add:IClient --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 20 ++-- .../gc14/Controller/ClientController.java | 92 ++++++++++++++++++- .../it/polimi/ingsw/gc14/Network/IClient.java | 8 ++ .../gc14/Network/RMI/Client/RMIClient.java | 11 ++- .../gc14/Network/TCP/Client/TCPClient.java | 10 +- src/main/java/module-info.java | 1 + 6 files changed, 127 insertions(+), 15 deletions(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/IClient.java diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 4993aeb..09a606f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -8,7 +8,7 @@ import it.polimi.ingsw.gc14.View.TUI.TUI; import java.util.Scanner; public class ClientLauncherTUI { - private IView view; + private TUI view; public void main() throws InterruptedException { view=new TUI(null); ClientController controller = new ClientController(view); @@ -34,22 +34,30 @@ public class ClientLauncherTUI { System.out.println("RMI connection refused\n\n"); return; } - + controller.setClient(client); + int h; + while(controller.localController.getModel()==null){ + h = scanner.nextInt(); + if(controller.localController.getModel()!=null) + { + break; + } + } while(true) { - int h = scanner.nextInt(); + h = scanner.nextInt(); } } else if (networkType == 1) { TCPClient client = new TCPClient(controller, "localhost", 8080); - if (client.start(username, proposedNumPlayers)) { + if (client.connect(username, proposedNumPlayers)) { System.out.println("Succesfully connected to TCP server\n\n"); } else { System.out.println("TCP connection refused\n\n"); + return; } - - + controller.setClient(client); while(true) { int h = scanner.nextInt(); } diff --git a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java index 74ab567..3224152 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -1,6 +1,9 @@ package it.polimi.ingsw.gc14.Controller; import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Model.Player; +import it.polimi.ingsw.gc14.Network.IClient; +import it.polimi.ingsw.gc14.Network.NetworkEvents.*; import it.polimi.ingsw.gc14.Network.Observer; import it.polimi.ingsw.gc14.View.IView; @@ -8,12 +11,15 @@ public class ClientController { public GameController localController; public IView view=null; + private IClient client; public ClientController(IView view) { this.view = view; this.localController = new GameController(); } - + public void setClient(IClient client) { + this.client = client; + } public void setModel(Game model) { localController.setModel(model); view.update(localController.getModel()); @@ -23,4 +29,88 @@ public class ClientController { view.showError(message); } + /** + * 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 void drawUpperTribeCard(String playerUsername,int pos) { + client.doEvent(new DrawUpperTribeCard(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 void drawLowerTribeCard(String playerUsername,int pos) { + client.doEvent(new DrawLowerTribeCard(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 void drawUpperBuildingCard(String playerUsername,int pos) { + client.doEvent(new DrawUpperBuildingCard(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 void drawLowerBuildingCard(String playerUsername,int pos) { + client.doEvent(new DrawLowerBuildingCard(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 void pickOptionalTribeCard(String playerUsername,int pos) { + client.doEvent(new PickOptionalTribeCard(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 void pickOptionalBuildingCard(String playerUsername,int pos) { + client.doEvent(new PickOptionalBuildingCard(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 void slotChoice(String playerUsername,int pos) { + client.doEvent(new SlotChoice(playerUsername,pos)); + } + } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java new file mode 100644 index 0000000..86f6435 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/IClient.java @@ -0,0 +1,8 @@ +package it.polimi.ingsw.gc14.Network; + +import java.rmi.RemoteException; + +public interface IClient { + public boolean connect(String username,int preferredInt); + public void doEvent(NetworkEvent event) ; +} 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 3bb68b4..03e0251 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,7 @@ import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; @@ -11,7 +12,7 @@ import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; /** * Client RMI. Uses the methods exposed by the server RMI. */ -public class RMIClient { +public class RMIClient implements IClient { /** The host address of the RMI server */ private final String host; @@ -67,8 +68,12 @@ public class RMIClient { * @param event the event to send * @throws RemoteException if any RMI error occurs */ - public void doEvent(NetworkEvent event) throws RemoteException { - stub.doEvent(event); + public void doEvent(NetworkEvent event) { + try { + stub.doEvent(event); + }catch (Exception e) { + + } } } \ No newline at end of file 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 42b2d6c..3408e99 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,8 +1,8 @@ package it.polimi.ingsw.gc14.Network.TCP.Client; import it.polimi.ingsw.gc14.Controller.ClientController; -import it.polimi.ingsw.gc14.Controller.GameController; import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer; @@ -12,7 +12,7 @@ import java.net.*; /** * Client TCP. Sends and receives messages with the TCP server. */ -public class TCPClient { +public class TCPClient implements IClient { /** Socket TCP */ Socket communicationSocket; @@ -54,7 +54,7 @@ public class TCPClient { * @param proposedNPlayers The desired number of players for the game * @return true if the connection is successful, false otherwise. */ - public boolean start(String user, int proposedNPlayers) { + public boolean connect(String user, int proposedNPlayers) { try { communicationSocket = new Socket(hostname, port); @@ -62,7 +62,7 @@ public class TCPClient { socketReceive = new ObjectInputStream(communicationSocket.getInputStream()); - sendEvent(new AddPlayer(user, proposedNPlayers)); + doEvent(new AddPlayer(user, proposedNPlayers)); if (communicationSocket.getInputStream().read() == -1) { System.out.println("Could not connect to server"); return false; @@ -112,7 +112,7 @@ public class TCPClient { * Sends a {@link NetworkEvent} to the server. * @param event The NetworkEvent to send. */ - private void sendEvent(NetworkEvent event) { + public void doEvent(NetworkEvent event) { try { socketSend.writeObject(event); } catch (IOException e) { diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index e178647..5e72256 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -5,6 +5,7 @@ module it.polimi.ingsw.gc14 { requires java.rmi; requires java.smartcardio; requires com.google.gson; + requires it.polimi.ingsw.gc14; opens it.polimi.ingsw.gc14 to javafx.fxml, com.google.gson; opens it.polimi.ingsw.gc14.Model to com.google.gson;