Coverage Summary for Class: ClientController (it.polimi.ingsw.gc14.Controller)
| Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
| ClientController |
0%
(0/1)
|
0%
(0/16)
|
0%
(0/8)
|
0%
(0/22)
|
package it.polimi.ingsw.gc14.Controller;
import it.polimi.ingsw.gc14.ErrorType;
import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.View.IView;
/**
* Controller responsible for coordinating the client-side components,
* including the view, the network client and the local mini model.
*
* <p>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. */
private MiniModel miniModel;
/** View of the client. */
private final IView view;
/** Network client, either TCP or RMI. */
private IClient client;
/** Username associated with this client. */
private String myUsername;
/**
* Constructs a client controller with the specified view.
*
* <p>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;
}
/**
* Returns the local mini model.
*
* @return the local mini model.
*/
public MiniModel getMiniModel() {
return miniModel;
}
/**
* Returns the client view.
*
* @return the client view.
*/
public IView getView() {
return view;
}
/**
* Returns the username associated with this client.
*
* @return the username.
*/
public String getMyUsername() {
return myUsername;
}
/**
* Sets the network client.
*
* @param client the client to set, either TCP or RMI.
*/
public void setClient(IClient client) {
this.client = client;
}
/**
* Gets the network client.
*
* @return client return the client, either TCP or RMI.
*/
public IClient getClient() {
return this.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;
}
/**
* Requests to draw a tribe card from the upper list.
*
* <p>The request is forwarded to the network client; the server validates it.
*
* @param pos the index of the card to draw.
*/
public void drawUpperTribeCard(int pos) {
client.drawUpperTribeCard(myUsername, pos);
}
/**
* Requests to draw a tribe card from the lower list.
*
* <p>The request is forwarded to the network client; the server validates it.
*
* @param pos the index of the card to draw.
*/
public void drawLowerTribeCard(int pos) {
client.drawLowerTribeCard(myUsername, pos);
}
/**
* Requests to draw a building card from the upper list.
*
* <p>The request is forwarded to the network client; the server validates it.
*
* @param pos the index of the card to draw.
*/
public void drawUpperBuildingCard(int pos) {
client.drawUpperBuildingCard(myUsername, pos);
}
/**
* Requests to draw a building card from the lower list.
*
* <p>The request is forwarded to the network client; the server validates it.
*
* @param pos the index of the card to draw.
*/
public void drawLowerBuildingCard(int pos) {
client.drawLowerBuildingCard(myUsername, pos);
}
/**
* Requests to skip the current turn.
*
* <p>The request is forwarded to the network client; the server validates it.
*/
public void skipTurn() {
client.skipTurn(myUsername);
}
/**
* Requests the selection of a slot by the specified player.
*
* <p>The request is forwarded to the network client; the server validates it.
*
* @param pos the index of the selected slot.
*/
public void slotChoice(int pos) {
client.slotChoice(myUsername, pos);
}
/**
* Handles the selection of a totem by the specified player.
*
* <p>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 pos the index of the selected totem in the available totems list.
*/
public void totemChoice(int pos) {
if (miniModel == null || miniModel.availableTotems == null
|| pos < 0 || pos >= miniModel.availableTotems.size()) {
view.showError(ErrorType.WRONG_ACTION, "ERROR: Invalid Totem Choice");
return;
}
client.totemChoice(myUsername, String.valueOf(miniModel.availableTotems.get(pos)));
}
/**
* Notifies the server of a voluntary disconnection and closes the
* network client connection.
*/
public void disconnect()
{
client.notifyDisconnection();
}
}