package it.polimi.ingsw.gc14.Controller; import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.View.IView; import java.util.Objects; /** * Controller responsible for coordinating the client-side components, * including the view, the network client and the local mini model. * *
It receives user actions from the view, performs basic client-side * checks and forwards valid requests to the network client. */ public class ClientController { /** Local mini model representing the client-side game state. */ public MiniModel miniModel; /** View of the client. */ public IView view; /** Network client, either TCP or RMI. */ private IClient client; /** Username associated with this client. */ public String myUsername; /** * Constructs a client controller with the specified view. * *
The network client is initially unset and an empty local mini model * is created. * * @param view the client view, either TUI or GUI. */ public ClientController(IView view) { this.view = view; 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 local mini model and updates the view accordingly. * * @param model the mini model to set. */ public void setModel(MiniModel model) { this.miniModel = model; view.setModel(miniModel); } /** * Sets the username associated with this client. * * @param username the username to set. */ public void setMyUsername(String username) { this.myUsername = username; } /** * 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. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the request is forwarded to the network client. * * @param playerUsername the username 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, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.drawUpperTribeCard(playerUsername, pos); } } /** * Requests to draw a tribe card from the lower list. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the request is forwarded to the network client. * * @param playerUsername the username 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, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.drawLowerTribeCard(playerUsername, pos); } } /** * Requests to draw a building card from the upper list. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the request is forwarded to the network client. * * @param playerUsername the username 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, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.drawUpperBuildingCard(playerUsername, pos); } } /** * Requests to draw a building card from the lower list. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the request is forwarded to the network client. * * @param playerUsername the username 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, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.drawLowerBuildingCard(playerUsername, pos); } } /** * Requests to skip the current turn. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the request is forwarded to the network client. * * @param playerUsername the username of the player performing the action. */ public void skipTurn(String playerUsername) { if (!Objects.equals(playerUsername, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.skipTurn(playerUsername); } } /** * Requests the selection of a slot by the specified player. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the request is forwarded to the network client. * * @param playerUsername the username 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, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.slotChoice(playerUsername, pos); } } /** * Handles the selection of a totem by the specified player. * *
If the specified player is not the current player, an error message * is shown. Otherwise, the selected totem is retrieved from the available * totems list and the choice is forwarded to the network client. * * @param playerUsername the username of the player making the choice. * @param pos the index of the selected totem in the available totems list. */ public void totemChoice(String playerUsername, int pos) { if (!Objects.equals(playerUsername, miniModel.currentState.getCurrentPlayer().getUserName())) { view.showError("It's not your turn!"); } else { client.totemChoice(playerUsername, String.valueOf(miniModel.availableTotems.get(pos))); } } }