Coverage Summary for Class: GameController (it.polimi.ingsw.gc14.Controller)

Class Class, % Method, % Branch, % Line, %
GameController 100% (1/1) 93.3% (14/15) 95.5% (21/22) 95.9% (47/49)


 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 synchronized Game getModel() {
         return model;
     }
 
     /**
      * Updates the game model managed by this controller.
      *
      * @param model the new game model managed by this controller.
      */
     public synchronized 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) {
         if(username.trim().length()>10)
             return false;
         Player player = model.getPlayerByUsername(username);
         if(player!=null)
             return false;
         player = new Player(username);
         return model.addPlayer(player);
     }
 
     /**
      * 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(player, 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(player, 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(player, 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(player, 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(player);
     }
 
 
 
     /**
      * 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(player, 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));
     }
 
     /**
      * 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();
     }
 }