package it.polimi.ingsw.gc14.Controller; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.Network.NetworkEvents.*; import it.polimi.ingsw.gc14.View.IView; import java.rmi.RemoteException; import java.util.Objects; /** * Controller class that holds all the components of the client, such as view, network client and Game Controller. * It provides methods to set the client components and to execute requested actions. */ public class ClientController { /** Game Controller of the client */ public GameController localController; /** View of the client */ public IView view; /** Network client (either TCP or RMI) */ private IClient client; /** * Constructs the ClientController. * Initializes all attributes. * @param view the client view (either TUI or GUI) */ public ClientController(IView view) { this.view = view; this.localController = new GameController(); this.client = null; } /** * Sets the network client. * @param client the client to set (either TCP or RMI) */ public void setClient(IClient client) { this.client = client; } /** * Sets the model in the GameController and updates the view. * @param model the model to set */ public void setModel(Game model) { localController.setModel(model); view.setModel(localController.getModel()); } /** * Displays an error message in the view. * @param message the error message to display */ public void onError(String message) { view.showError(message); } /** * Requests to draw a tribe card from the upper list. * Creates a NetworkEvent and sends it through the network client. * @param playerUsername the name of the player performing the action * @param pos the index of the card to draw */ public void drawUpperTribeCard(String playerUsername, int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { try { client.drawUpperTribeCard(playerUsername, pos); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Requests to draw a tribe card from the lower list. * Creates a NetworkEvent and sends it through the network client. * @param playerUsername the name of the player performing the action * @param pos the index of the card to draw */ public void drawLowerTribeCard(String playerUsername,int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else try { client.drawLowerTribeCard(playerUsername, pos); } catch (RemoteException e) { throw new RuntimeException(e); } } /** * Requests to draw a building card from the upper list. * Creates a NetworkEvent and sends it through the network client. * @param playerUsername the name of the player performing the action * @param pos the index of the card to draw */ public void drawUpperBuildingCard(String playerUsername,int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.drawUpperBuildingCard(playerUsername,pos); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Requests to draw a building card from the lower list. * Creates a NetworkEvent and sends it through the network client. * @param playerUsername the name of the player performing the action * @param pos the index of the card to draw */ public void drawLowerBuildingCard(String playerUsername,int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.drawLowerBuildingCard(playerUsername,pos); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Requests to skip drawing from the upper list. * This action is available only when the upper list is empty or the player cannot draw any card. * @param playerUsername the name of the player performing the action */ public void skipUpper(String playerUsername) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.skipUpper(playerUsername); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Requests to skip drawing from the lower list. * This action is available only when the lower list is empty or the player cannot draw any card. * @param playerUsername the name of the player performing the action */ public void skipLower(String playerUsername) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.skipLower(playerUsername); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Used to draw a tribe card from the upper list. * Available only if the player owns the building 12. * @param playerUsername the name of the player performing the action * @param pos the index of the card to draw */ public void pickOptionalTribeCard(String playerUsername,int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.pickOptionalTribeCard(playerUsername,pos); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Used to draw a building card from the upper list. * Available only if the player owns the building 12. * @param playerUsername the name of the player performing the action * @param pos the index of the card to draw */ public void pickOptionalBuildingCard(String playerUsername,int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.pickOptionalBuildingCard(playerUsername,pos); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Used to skip the action of drawing a card from the upper list. * Available only if the player owns the building 12. * @param playerUsername the name of the player performing the action */ public void noOptionalCard(String playerUsername) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.noOptionalCard(playerUsername); } catch (RemoteException e) { throw new RuntimeException(e); } } } /** * Used to perform the slot choice action for the specified player at the specified position. * @param playerUsername the name of the player performing the action * @param pos the index of the selected slot */ public void slotChoice(String playerUsername,int pos) { if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) view.showError("It's not your turn!"); else { try { client.slotChoice(playerUsername,pos); } catch (RemoteException e) { throw new RuntimeException(e); } } } }