From 61caba4b4748d9b2c4e9eacab83bce570289502e Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Thu, 30 Apr 2026 15:32:26 +0200 Subject: [PATCH 1/9] Fix: OrderPlayer is now serializable --- src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java | 4 +++- .../java/it/polimi/ingsw/gc14/Model/Orders/OrderPlayer.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 8da0d9e..cb0e1ae 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -40,10 +40,12 @@ public class ClientLauncherTUI { } Thread.sleep(500); } - System.out.println("Model set\n\n"); + System.out.print("\033[H\033[2J"); + System.out.flush(); + System.out.println(controller.localModel); } else if (networkType == 1) { diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Orders/OrderPlayer.java b/src/main/java/it/polimi/ingsw/gc14/Model/Orders/OrderPlayer.java index 360bebc..0332e70 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Orders/OrderPlayer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Orders/OrderPlayer.java @@ -2,12 +2,14 @@ package it.polimi.ingsw.gc14.Model.Orders; import it.polimi.ingsw.gc14.Model.Player; +import java.io.Serializable; + /** * 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 class OrderPlayer{ +public class OrderPlayer implements Serializable { public Player player; public boolean played; public OrderPlayer(Player player,boolean played){ From fe5e08e5cdccb3addcf47ecf8e2b49e2f62fcf23 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Thu, 30 Apr 2026 16:08:13 +0200 Subject: [PATCH 2/9] Fix: client TCP complete connection and receive game model --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 29 +++++++++++++---- .../gc14/Network/RMI/Client/RMIClient.java | 12 ++++--- .../gc14/Network/TCP/Client/TCPClient.java | 9 ++++-- .../Network/TCP/Server/ClientHandler.java | 31 +++++++++---------- .../gc14/Network/TCP/Server/TCPServer.java | 7 +++-- 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index cb0e1ae..a7e08fa 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14; import it.polimi.ingsw.gc14.Controller.ClientController; import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient; +import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient; import it.polimi.ingsw.gc14.View.IView; import java.util.Scanner; @@ -13,21 +14,18 @@ public class ClientLauncherTUI { System.out.println("Selezionare nome utente: "); String username = scanner.next(); - System.out.println(username); System.out.println("Selezionare numero di giocatori desiderato: "); int proposedNumPlayers = scanner.nextInt(); - System.out.println(proposedNumPlayers); System.out.println("Selezionare RMI[0] o TCP[1]: "); int networkType = scanner.nextInt(); - System.out.println(networkType); scanner.close(); if (networkType == 0) { - RMIClient client = new RMIClient("localhost", 1099); - if (client.connect(username, proposedNumPlayers, controller)) { + RMIClient client = new RMIClient(controller, "localhost", 1099); + if (client.connect(username, proposedNumPlayers)) { System.out.println("Succesfully connected to RMI server\n\n"); } else { System.out.println("RMI connection refused\n\n"); @@ -49,7 +47,26 @@ public class ClientLauncherTUI { } else if (networkType == 1) { - return; + TCPClient client = new TCPClient(controller, "localhost", 8080); + if (client.start(username, proposedNumPlayers)) { + System.out.println("Succesfully connected to TCP server\n\n"); + } else { + System.out.println("TCP connection refused\n\n"); + } + + while(true) { + System.out.flush(); + if (controller.localModel!=null) { + break; + } + Thread.sleep(500); + } + System.out.println("Model set\n\n"); + + + System.out.print("\033[H\033[2J"); + System.out.flush(); + System.out.println(controller.localModel); } } } \ 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 19bd574..3bb68b4 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 @@ -22,13 +22,18 @@ public class RMIClient { /** The remote stub used to call methods on the server */ private IGameServer stub; + /** Client game's controller */ + ClientController controller; + /** * Class constructor. + * @param controller the client controller used to create the callback * @param host the host address of the RMI server * @param port the port of the RMI server */ - public RMIClient(String host, int port) { + public RMIClient(ClientController controller, String host, int port) { + this.controller=controller; this.host = host; this.port = port; } @@ -40,14 +45,13 @@ public class RMIClient { * Then, creates a {@link ClientCallbackImpl} and calls {@link RMIServer#joinGame(String, int, IClientCallback)}. * @param username the player's username * @param preferredInt the desired number of players - * @param clientController the client controller used to create the callback * @return true if the player successfully joined the game, false otherwise */ - public boolean connect(String username,int preferredInt, ClientController clientController) { + public boolean connect(String username,int preferredInt) { try { Registry registry = LocateRegistry.getRegistry(host, port); this.stub = (IGameServer) registry.lookup("RMIGameServer"); - ClientCallbackImpl callback = new ClientCallbackImpl(clientController); + ClientCallbackImpl callback = new ClientCallbackImpl(controller); return stub.joinGame(username, preferredInt, callback); } 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 c69734e..3806ce5 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,5 +1,6 @@ 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.NetworkEvent; @@ -23,7 +24,7 @@ public class TCPClient { ObjectOutputStream socketSend; /** Client game's controller */ - GameController controller; + ClientController controller; /** IP address of the server to connect to */ String hostname; @@ -38,7 +39,7 @@ public class TCPClient { * @param hostname The IP address of the server * @param port The TCP port of the server */ - public TCPClient(GameController controller, String hostname, int port) { + public TCPClient(ClientController controller, String hostname, int port) { this.controller = controller; this.hostname = hostname; this.port = port; @@ -55,10 +56,12 @@ public class TCPClient { */ public boolean start(String user, int proposedNPlayers) { try { + communicationSocket = new Socket(hostname, port); socketSend = new ObjectOutputStream(communicationSocket.getOutputStream()); socketReceive = new ObjectInputStream(communicationSocket.getInputStream()); + sendEvent(new AddPlayer(user, proposedNPlayers)); if (communicationSocket.getInputStream().read() == -1) { System.out.println("Could not connect to server"); @@ -89,7 +92,7 @@ public class TCPClient { if (event.getIsError()) { System.out.println(event); } else { - event.apply(controller); + event.apply(controller.localController); //clientController.view.update(); TODO } } else if (read instanceof Game model) { 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 07864f0..3da705e 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 @@ -41,8 +41,10 @@ public class ClientHandler implements Runnable { * @param clientHandlers The shared list of all active client handlers * @param actionQueue The queue containing incoming events */ - public ClientHandler(Socket clientSocket, List clientHandlers, BlockingQueue actionQueue) { + public ClientHandler(Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List clientHandlers, BlockingQueue actionQueue) { this.clientSocket = clientSocket; + this.in = in; + this.out = out; this.clientHandlers = clientHandlers; this.actionQueue = actionQueue; } @@ -76,15 +78,13 @@ public class ClientHandler implements Runnable { * Sends a {@link NetworkEvent} to the client. * @param event The network event to send to the client. */ - public void notifyEvent(NetworkEvent event) { - synchronized (out) { - try { - out = new ObjectOutputStream(clientSocket.getOutputStream()); - out.writeObject(event); - } catch (IOException e) { - e.printStackTrace(); - } + public synchronized void notifyEvent(NetworkEvent event) { + try { + out.writeObject(event); + } catch (IOException e) { + e.printStackTrace(); } + } @@ -92,14 +92,11 @@ public class ClientHandler implements Runnable { * Sends the current game model to this client. * @param game The current state of the game to send to the client. */ - public void notifyModel(Game game) { - synchronized (out) { - try { - out = new ObjectOutputStream(clientSocket.getOutputStream()); - out.writeObject(game); - } catch (IOException e) { - e.printStackTrace(); - } + public synchronized void notifyModel(Game game) { + try { + out.writeObject(game); + } catch (IOException e) { + e.printStackTrace(); } } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java index 16c4cfb..55fd6a6 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 @@ -88,8 +88,9 @@ public class TCPServer { try{ clientSocket = socketTCP.accept(); - ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream()); - NetworkEvent event = (NetworkEvent) clientSocketObj.readObject(); + ObjectOutputStream clientSend = new ObjectOutputStream(clientSocket.getOutputStream()); + ObjectInputStream clientReceive = new ObjectInputStream(clientSocket.getInputStream()); + NetworkEvent event = (NetworkEvent) clientReceive.readObject(); if(!(event.getEventType() == EventType.ADD_PLAYER)){ clientSocket.getOutputStream().write((int)(-1)); @@ -114,7 +115,7 @@ public class TCPServer { clientSocket.getOutputStream().write((int) (1)); System.out.println("Accepted player: " + eventAddPlayer.getUsername()); - ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, actionQueue); + ClientHandler clientHandler = new ClientHandler(clientSocket, clientSend, clientReceive, clientHandlers, actionQueue); clientHandlers.add(clientHandler); ConnectedPlayers++; From 406b9c1b3e2f9d19ab7aad517f901305d8503c82 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Thu, 30 Apr 2026 17:39:01 +0200 Subject: [PATCH 3/9] Removed parameter when creating client controller --- .../java/it/polimi/ingsw/gc14/Controller/ClientController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1da7933..463237d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -17,7 +17,7 @@ public class ClientController { } public ClientController() { - this.localController = new GameController(localModel); + this.localController = new GameController(); } public void setModel(Game model) { From 7e3623d3aee1e30f39291b762243d29c8898edc2 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Thu, 30 Apr 2026 18:01:35 +0200 Subject: [PATCH 4/9] Bind: TUI->Launcher --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 14 +++++++---- .../gc14/Controller/ClientController.java | 13 ++-------- .../RMI/Client/ClientCallbackImpl.java | 3 ++- .../java/it/polimi/ingsw/gc14/View/IView.java | 4 ++-- .../it/polimi/ingsw/gc14/View/TUI/TUI.java | 24 +++++++++++++++---- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index a7e08fa..2280b72 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -3,12 +3,15 @@ import it.polimi.ingsw.gc14.Controller.ClientController; import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient; import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient; import it.polimi.ingsw.gc14.View.IView; +import it.polimi.ingsw.gc14.View.TUI.TUI; import java.util.Scanner; public class ClientLauncherTUI { + private IView view; public void main() throws InterruptedException { - ClientController controller = new ClientController(); + view=new TUI(null); + ClientController controller = new ClientController(view); Scanner scanner = new Scanner(System.in); @@ -29,11 +32,12 @@ public class ClientLauncherTUI { System.out.println("Succesfully connected to RMI server\n\n"); } else { System.out.println("RMI connection refused\n\n"); + return; } while(true) { System.out.flush(); - if (controller.localModel!=null) { + if (controller.localController.getModel()!=null) { break; } Thread.sleep(500); @@ -43,7 +47,7 @@ public class ClientLauncherTUI { System.out.print("\033[H\033[2J"); System.out.flush(); - System.out.println(controller.localModel); + System.out.println(controller.localController.getModel()); } else if (networkType == 1) { @@ -56,7 +60,7 @@ public class ClientLauncherTUI { while(true) { System.out.flush(); - if (controller.localModel!=null) { + if (controller.localController.getModel()!=null) { break; } Thread.sleep(500); @@ -66,7 +70,7 @@ public class ClientLauncherTUI { System.out.print("\033[H\033[2J"); System.out.flush(); - System.out.println(controller.localModel); + System.out.println(controller.localController.getModel()); } } } \ No newline at end of file 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 463237d..74ab567 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -6,28 +6,19 @@ import it.polimi.ingsw.gc14.View.IView; public class ClientController { - public Game localModel; public GameController localController; public IView view=null; - public ClientController(IView view,Game localModel) { + public ClientController(IView view) { this.view = view; - this.localModel = localModel; - this.localController = new GameController(localModel); - } - - public ClientController() { this.localController = new GameController(); } public void setModel(Game model) { - this.localModel = model; localController.setModel(model); - // localModel.addObserver((Observer) view); // registra la view come observer + view.update(localController.getModel()); } - - 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 index 5b16b76..beb9c91 100644 --- 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 @@ -39,6 +39,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa @Override public void onGameInit(Game model) throws RemoteException { clientController.setModel(model); + clientController.view.render(); } @@ -55,7 +56,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa System.out.println(event.toString()); } else { event.apply(clientController.localController); - //clientController.view.update(); TODO + clientController.view.render(); } } } 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 9ffaa16..2169583 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -4,8 +4,8 @@ import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; public interface IView { - - public void render(Game model); + public void update(Game game); + public void render(); public void showMessage(String message); public void showError(String message); diff --git a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java index 911ef66..caaa040 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java @@ -1,18 +1,31 @@ package it.polimi.ingsw.gc14.View.TUI; import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.Observer; import it.polimi.ingsw.gc14.View.IView; -public class TUI implements IView { +public class TUI implements IView, Observer { // ── dati di stato ─────────────────────────────────────────── private BorderStyle style = BorderStyle.UNICODE; private Game model; + private String username; public TUI(Game model) { this.model = model; + this.username=""; } + public void setUsername(String username) { + this.username = username; + } + + @Override + public void update(Game model) { + this.model = model; + } + // ── punto di ingresso ─────────────────────────────────────── - public void render(Game model) + public void render() { + this.model=model; try{ String os = System.getProperty("os.name").toLowerCase(); ProcessBuilder pb; @@ -28,7 +41,7 @@ public class TUI implements IView { System.out.println(model.toString()); } - public void renderBoard(Game model) + public void renderBoard() { try{ String os = System.getProperty("os.name").toLowerCase(); @@ -44,7 +57,7 @@ public class TUI implements IView { } System.out.println(model.BoardStamp()); } - public void renderPlayer(Game model) + public void renderPlayer() { try{ String os = System.getProperty("os.name").toLowerCase(); @@ -60,7 +73,7 @@ public class TUI implements IView { } System.out.println(model.BoardStamp()); } - public void renderMyHand(Game model,String username) + public void renderMyHand() { try{ String os = System.getProperty("os.name").toLowerCase(); @@ -76,6 +89,7 @@ public class TUI implements IView { } System.out.println(model.getPlayerByUsername(username)); } + public void showMessage(String message) { From d66af3a112b6c0d53642a3d26a76711e2cd3dea6 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Thu, 30 Apr 2026 18:10:11 +0200 Subject: [PATCH 5/9] Both TCP and RMI works and receive correctly the model. After receiving the model, it is printed in stdout --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 28 ++++--------------- .../gc14/Network/TCP/Client/TCPClient.java | 3 +- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 2280b72..4993aeb 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -24,7 +24,7 @@ public class ClientLauncherTUI { System.out.println("Selezionare RMI[0] o TCP[1]: "); int networkType = scanner.nextInt(); - scanner.close(); + if (networkType == 0) { RMIClient client = new RMIClient(controller, "localhost", 1099); @@ -36,19 +36,10 @@ public class ClientLauncherTUI { } while(true) { - System.out.flush(); - if (controller.localController.getModel()!=null) { - break; - } - Thread.sleep(500); + int h = scanner.nextInt(); } - System.out.println("Model set\n\n"); - System.out.print("\033[H\033[2J"); - System.out.flush(); - System.out.println(controller.localController.getModel()); - } else if (networkType == 1) { TCPClient client = new TCPClient(controller, "localhost", 8080); @@ -58,19 +49,12 @@ public class ClientLauncherTUI { System.out.println("TCP connection refused\n\n"); } + while(true) { - System.out.flush(); - if (controller.localController.getModel()!=null) { - break; - } - Thread.sleep(500); + int h = scanner.nextInt(); } - System.out.println("Model set\n\n"); - - - System.out.print("\033[H\033[2J"); - System.out.flush(); - System.out.println(controller.localController.getModel()); } + + scanner.close(); } } \ 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 3806ce5..42b2d6c 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 @@ -93,10 +93,11 @@ public class TCPClient { System.out.println(event); } else { event.apply(controller.localController); - //clientController.view.update(); TODO + controller.view.render(); } } else if (read instanceof Game model) { controller.setModel(model); + controller.view.render(); } } catch (IOException e) { e.printStackTrace(); From 5246818fca9c1a8e41f60a2d767a0cb7e3acf863 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Thu, 30 Apr 2026 18:34:40 +0200 Subject: [PATCH 6/9] 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; From 671c523599298b5253fa45d714797799c9e911d2 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Thu, 30 Apr 2026 19:20:56 +0200 Subject: [PATCH 7/9] Client Launcher TUI input handler added --- .../polimi/ingsw/gc14/ClientLauncherTUI.java | 50 ++++++++++++------- .../it/polimi/ingsw/gc14/View/TUI/TUI.java | 1 - 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 09a606f..9facc69 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -1,32 +1,30 @@ package it.polimi.ingsw.gc14; import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient; import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient; -import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.TUI.TUI; import java.util.Scanner; public class ClientLauncherTUI { - private TUI view; public void main() throws InterruptedException { - view=new TUI(null); + TUI view=new TUI(null); ClientController controller = new ClientController(view); Scanner scanner = new Scanner(System.in); - System.out.println("Selezionare nome utente: "); String username = scanner.next(); - System.out.println("Selezionare numero di giocatori desiderato: "); int proposedNumPlayers = scanner.nextInt(); - System.out.println("Selezionare RMI[0] o TCP[1]: "); int networkType = scanner.nextInt(); + // RMI if (networkType == 0) { + // Connect RMIClient client = new RMIClient(controller, "localhost", 1099); if (client.connect(username, proposedNumPlayers)) { System.out.println("Succesfully connected to RMI server\n\n"); @@ -35,21 +33,19 @@ public class ClientLauncherTUI { return; } controller.setClient(client); - int h; - while(controller.localController.getModel()==null){ - h = scanner.nextInt(); - if(controller.localController.getModel()!=null) - { - break; - } - } + + // Play + //while(controller.localController.getModel()==null){ + // scanner.nextInt(); + //} while(true) { - h = scanner.nextInt(); + getInput(scanner, controller, username); } - + // TCP } else if (networkType == 1) { + // Connect TCPClient client = new TCPClient(controller, "localhost", 8080); if (client.connect(username, proposedNumPlayers)) { System.out.println("Succesfully connected to TCP server\n\n"); @@ -58,11 +54,31 @@ public class ClientLauncherTUI { return; } controller.setClient(client); + + // Play while(true) { - int h = scanner.nextInt(); + getInput(scanner, controller, username); } } scanner.close(); } + + + + private void getInput(Scanner scanner, ClientController controller, String username) { + int action = scanner.nextInt(); + int pos = scanner.nextInt(); + + switch(action) { + case 1 -> controller.drawLowerBuildingCard(username, pos); + case 2 -> controller.drawLowerTribeCard(username, pos); + case 3 -> controller.drawUpperBuildingCard(username, pos); + case 4 -> controller.drawUpperTribeCard(username, pos); + case 5 -> controller.pickOptionalBuildingCard(username, pos); + case 6 -> controller.slotChoice(username, pos); + } + + return; + } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java index caaa040..a71487a 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java @@ -25,7 +25,6 @@ public class TUI implements IView, Observer { // ── punto di ingresso ─────────────────────────────────────── public void render() { - this.model=model; try{ String os = System.getProperty("os.name").toLowerCase(); ProcessBuilder pb; From 34952deb39ae80874daa49946e5737b8ae47a8de Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Thu, 30 Apr 2026 19:52:12 +0200 Subject: [PATCH 8/9] Fix: TCPClient --- .../it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 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 3da705e..539416e 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 @@ -57,8 +57,7 @@ public class ClientHandler implements Runnable { @Override public void run() { try { - in = new ObjectInputStream(clientSocket.getInputStream()); - out = new ObjectOutputStream(clientSocket.getOutputStream()); + while (true) { NetworkEvent event = (NetworkEvent) in.readObject(); if (!actionQueue.add(event)) { From 3e863a8a1e7c0732ae38369d3e18068222de4c9b Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Thu, 30 Apr 2026 19:53:12 +0200 Subject: [PATCH 9/9] Fix: module info --- src/main/java/module-info.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 5e72256..e178647 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -5,7 +5,6 @@ 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;