222 lines
8.0 KiB
Java
222 lines
8.0 KiB
Java
package it.polimi.ingsw.gc14.Controller;
|
|
|
|
import it.polimi.ingsw.gc14.Model.Game;
|
|
import it.polimi.ingsw.gc14.Model.Player;
|
|
import it.polimi.ingsw.gc14.Model.Totems;
|
|
|
|
/**
|
|
* Controller responsible for managing interactions with the {@link Game} model.
|
|
*
|
|
* <p>It provides methods to update the game state by delegating player-related
|
|
* actions and game actions to the underlying model.
|
|
*/
|
|
public class GameController {
|
|
|
|
/**
|
|
* The game model managed by this controller.
|
|
*/
|
|
private Game model;
|
|
|
|
/**
|
|
* Creates a GameController associated with the specified game model.
|
|
*
|
|
* @param model the game model managed by this controller.
|
|
*/
|
|
public GameController(Game model) {
|
|
this.model = model;
|
|
}
|
|
|
|
/**
|
|
* Creates a GameController without an associated game model.
|
|
*/
|
|
public GameController() {
|
|
}
|
|
|
|
/**
|
|
* Marks the player associated with the specified username as disconnected.
|
|
*
|
|
* @param username the username of the player who disconnected.
|
|
* @return {@code true} if the disconnection is handled successfully,
|
|
* {@code false} if no player with the specified username exists
|
|
* or if the operation fails.
|
|
*/
|
|
public synchronized boolean disconnectedPlayer(String username)
|
|
{
|
|
Player player= model.getPlayerByUsername(username);
|
|
if(player==null)
|
|
return false;
|
|
return model.disconnectedPlayer(player);
|
|
}
|
|
|
|
/**
|
|
* Marks the player associated with the specified username as reconnected.
|
|
*
|
|
* @param username the username of the player who reconnected.
|
|
* @return {@code true} if the reconnection is handled successfully,
|
|
* {@code false} if no player with the specified username exists
|
|
* or if the operation fails.
|
|
*/
|
|
public synchronized boolean reconnectPlayer(String username)
|
|
{
|
|
Player player= model.getPlayerByUsername(username);
|
|
if(player==null)
|
|
return false;
|
|
return model.reconnectPlayer(player);
|
|
}
|
|
|
|
/**
|
|
* Returns the game model managed by this controller.
|
|
*
|
|
* @return the game model managed by this controller.
|
|
*/
|
|
public Game getModel() {
|
|
return model;
|
|
}
|
|
|
|
/**
|
|
* Updates the game model managed by this controller.
|
|
*
|
|
* @param model the new game model managed by this controller.
|
|
*/
|
|
public void setModel(Game model) {
|
|
this.model = model;
|
|
}
|
|
|
|
/**
|
|
* Attempts to add a new player with the specified username to the game model.
|
|
*
|
|
* @param username the username of the player to add.
|
|
* @return {@code true} if the player is successfully added,
|
|
* {@code false} otherwise.
|
|
*/
|
|
public synchronized boolean addPlayer(String username) {
|
|
return model.addPlayer(new Player(username));
|
|
}
|
|
|
|
/**
|
|
* 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 synchronized boolean drawUpperTribeCard(String playerUsername,int pos) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.DrawUpperTribeCardByIndex(model.getPlayerByUsername(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 synchronized boolean drawLowerTribeCard(String playerUsername,int pos) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.DrawLowerTribeCardByIndex(model.getPlayerByUsername(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 synchronized boolean drawUpperBuildingCard(String playerUsername,int pos) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.DrawUpperBuildingCardByIndex(model.getPlayerByUsername(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 synchronized boolean drawLowerBuildingCard(String playerUsername,int pos) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.DrawLowerBuildingCardByIndex(model.getPlayerByUsername(playerUsername), pos);
|
|
}
|
|
|
|
|
|
/**
|
|
* Skips the card drawing action for the specified player.
|
|
*
|
|
* @param playerUsername the username of the player performing the action.
|
|
* @return {@code true} if the skip action is valid and successfully performed,
|
|
* {@code false} if the player does not exist or if the action is not valid.
|
|
*/
|
|
public synchronized boolean skipTurn(String playerUsername) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.skipTurn(model.getPlayerByUsername(playerUsername));
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 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 synchronized boolean slotChoice(String playerUsername,int pos) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.SlotChoiceByIndex(model.getPlayerByUsername(playerUsername),pos);
|
|
}
|
|
|
|
/**
|
|
* Applies the totem choice made by the player associated with the specified username.
|
|
*
|
|
* @param playerUsername the username of the player making the choice.
|
|
* @param totem the name of the selected totem.
|
|
* @return {@code true} if the choice is handled successfully,
|
|
* {@code false} if no player with the specified username exists
|
|
* or if the choice operation fails.
|
|
*/
|
|
public synchronized boolean totemChoice(String playerUsername,String totem) {
|
|
Player player= model.getPlayerByUsername(playerUsername);
|
|
if(player==null)
|
|
return false;
|
|
return model.totemChoice(player, Totems.valueOf(totem));
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use {@link #endGameForFeit()} instead.
|
|
*/
|
|
@Deprecated
|
|
public synchronized void EndGameForFeit() { endGameForFeit(); }
|
|
|
|
/**
|
|
* Ends the game due to forfeit: all remaining players are absent,
|
|
* so the game is terminated and final scores are computed.
|
|
*/
|
|
public synchronized void endGameForFeit() {
|
|
model.endGameForFeit();
|
|
}
|
|
} |