Fix: MVC (Client) Add:IClient

This commit is contained in:
rubenpirreram
2026-04-30 18:34:40 +02:00
parent d66af3a112
commit 5246818fca
6 changed files with 127 additions and 15 deletions
@@ -8,7 +8,7 @@ import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.util.Scanner; import java.util.Scanner;
public class ClientLauncherTUI { public class ClientLauncherTUI {
private IView view; private TUI view;
public void main() throws InterruptedException { public void main() throws InterruptedException {
view=new TUI(null); view=new TUI(null);
ClientController controller = new ClientController(view); ClientController controller = new ClientController(view);
@@ -34,22 +34,30 @@ public class ClientLauncherTUI {
System.out.println("RMI connection refused\n\n"); System.out.println("RMI connection refused\n\n");
return; return;
} }
controller.setClient(client);
int h;
while(controller.localController.getModel()==null){
h = scanner.nextInt();
if(controller.localController.getModel()!=null)
{
break;
}
}
while(true) { while(true) {
int h = scanner.nextInt(); h = scanner.nextInt();
} }
} else if (networkType == 1) { } else if (networkType == 1) {
TCPClient client = new TCPClient(controller, "localhost", 8080); 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"); System.out.println("Succesfully connected to TCP server\n\n");
} else { } else {
System.out.println("TCP connection refused\n\n"); System.out.println("TCP connection refused\n\n");
return;
} }
controller.setClient(client);
while(true) { while(true) {
int h = scanner.nextInt(); int h = scanner.nextInt();
} }
@@ -1,6 +1,9 @@
package it.polimi.ingsw.gc14.Controller; package it.polimi.ingsw.gc14.Controller;
import it.polimi.ingsw.gc14.Model.Game; 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.Network.Observer;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
@@ -8,12 +11,15 @@ public class ClientController {
public GameController localController; public GameController localController;
public IView view=null; public IView view=null;
private IClient client;
public ClientController(IView view) { public ClientController(IView view) {
this.view = view; this.view = view;
this.localController = new GameController(); this.localController = new GameController();
} }
public void setClient(IClient client) {
this.client = client;
}
public void setModel(Game model) { public void setModel(Game model) {
localController.setModel(model); localController.setModel(model);
view.update(localController.getModel()); view.update(localController.getModel());
@@ -23,4 +29,88 @@ public class ClientController {
view.showError(message); 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));
}
} }
@@ -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) ;
}
@@ -4,6 +4,7 @@ import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import it.polimi.ingsw.gc14.Controller.ClientController; 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.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; 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. * 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 */ /** The host address of the RMI server */
private final String host; private final String host;
@@ -67,8 +68,12 @@ public class RMIClient {
* @param event the event to send * @param event the event to send
* @throws RemoteException if any RMI error occurs * @throws RemoteException if any RMI error occurs
*/ */
public void doEvent(NetworkEvent event) throws RemoteException { public void doEvent(NetworkEvent event) {
stub.doEvent(event); try {
stub.doEvent(event);
}catch (Exception e) {
}
} }
} }
@@ -1,8 +1,8 @@
package it.polimi.ingsw.gc14.Network.TCP.Client; package it.polimi.ingsw.gc14.Network.TCP.Client;
import it.polimi.ingsw.gc14.Controller.ClientController; 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.Model.Game;
import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer; 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. * Client TCP. Sends and receives messages with the TCP server.
*/ */
public class TCPClient { public class TCPClient implements IClient {
/** Socket TCP */ /** Socket TCP */
Socket communicationSocket; Socket communicationSocket;
@@ -54,7 +54,7 @@ public class TCPClient {
* @param proposedNPlayers The desired number of players for the game * @param proposedNPlayers The desired number of players for the game
* @return true if the connection is successful, false otherwise. * @return true if the connection is successful, false otherwise.
*/ */
public boolean start(String user, int proposedNPlayers) { public boolean connect(String user, int proposedNPlayers) {
try { try {
communicationSocket = new Socket(hostname, port); communicationSocket = new Socket(hostname, port);
@@ -62,7 +62,7 @@ public class TCPClient {
socketReceive = new ObjectInputStream(communicationSocket.getInputStream()); socketReceive = new ObjectInputStream(communicationSocket.getInputStream());
sendEvent(new AddPlayer(user, proposedNPlayers)); doEvent(new AddPlayer(user, proposedNPlayers));
if (communicationSocket.getInputStream().read() == -1) { if (communicationSocket.getInputStream().read() == -1) {
System.out.println("Could not connect to server"); System.out.println("Could not connect to server");
return false; return false;
@@ -112,7 +112,7 @@ public class TCPClient {
* Sends a {@link NetworkEvent} to the server. * Sends a {@link NetworkEvent} to the server.
* @param event The NetworkEvent to send. * @param event The NetworkEvent to send.
*/ */
private void sendEvent(NetworkEvent event) { public void doEvent(NetworkEvent event) {
try { try {
socketSend.writeObject(event); socketSend.writeObject(event);
} catch (IOException e) { } catch (IOException e) {
+1
View File
@@ -5,6 +5,7 @@ module it.polimi.ingsw.gc14 {
requires java.rmi; requires java.rmi;
requires java.smartcardio; requires java.smartcardio;
requires com.google.gson; 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 to javafx.fxml, com.google.gson;
opens it.polimi.ingsw.gc14.Model to com.google.gson; opens it.polimi.ingsw.gc14.Model to com.google.gson;