Files
Progetto-ingegneria-del-sof…/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java
T
rubenpirreram 374247f984 Fixed: various
2026-05-15 16:27:23 +02:00

222 lines
7.1 KiB
Java

package it.polimi.ingsw.gc14.Controller;
import it.polimi.ingsw.gc14.Model.*;
import it.polimi.ingsw.gc14.Model.GamePackage.Board;
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
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.Map;
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 MiniModel miniModel;
/** View of the client */
public IView view;
/** Network client (either TCP or RMI) */
private IClient client;
public String myUsername;
/**
* Constructs the ClientController.
* Initializes all attributes.
* @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 model in the GameController and updates the view.
* @param model the model to set
*/
public void setModel(MiniModel model) {
this.miniModel = model;
view.setModel(miniModel);
}
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.
* 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, 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.
* 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, 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.
* 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, 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.
* 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,miniModel.currentState.getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else {
client.drawLowerBuildingCard(playerUsername,pos);
}
}
/**
* Requests to skip turn .
* This action is available only when the player cannot draw any tribe card.
* @param playerUsername the name 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);
}
}
/**
* 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,miniModel.currentState.getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else {
client.pickOptionalTribeCard(playerUsername,pos);
}
}
/**
* 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, miniModel.currentState.getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else {
client.pickOptionalBuildingCard(playerUsername,pos);
}
}
/**
* 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, miniModel.currentState.getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else {
client.noOptionalCard(playerUsername);
}
}
/**
* 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, miniModel.currentState.getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else {
client.slotChoice(playerUsername,pos);
}
}
//TODO
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)));
}
}
}