Coverage Summary for Class: ChooseCloudTile (it.polimi.ingsw.Controller.Actions)

Class Class, % Method, % Branch, % Line, %
ChooseCloudTile 100% (1/1) 100% (3/3) 72,2% (13/18) 87,5% (21/24)


1 package it.polimi.ingsw.Controller.Actions; 2  3 import it.polimi.ingsw.Exceptions.Input.GenericInputValidationException; 4 import it.polimi.ingsw.Exceptions.Input.InputValidationException; 5 import it.polimi.ingsw.Exceptions.Input.InvalidElementException; 6 import it.polimi.ingsw.Misc.OptionalValue; 7 import it.polimi.ingsw.Model.Cloud; 8 import it.polimi.ingsw.Model.Model; 9 import it.polimi.ingsw.Model.PlayerBoard; 10  11 import java.io.Serial; 12 import java.util.List; 13  14 import static it.polimi.ingsw.Misc.Utils.countSimilarClassOccurrences; 15  16 /** 17  * {@link PlayerAction} allowing a user to choose one of many cloud tiles present on the gameboard. This action is 18  * linked to the Action Phase 19  */ 20 public class ChooseCloudTile extends PlayerAction { 21  22  @Serial 23  private static final long serialVersionUID = 201L; // convention: 2 for controller, (01 -> 99) for objects 24  private final int selectedTile; 25  26  /** 27  * Create a new instance of this class with the following inputs: 28  * 29  * @param playerBoardId the ID of the current {@link PlayerBoard} 30  * @param selectedTile the ID of the {@link Cloud} the player has chosen 31  */ 32  public ChooseCloudTile(int playerBoardId, int selectedTile) { 33  super(playerBoardId, true); 34  this.selectedTile = selectedTile; 35  } 36  37  /** 38  * {@inheritDoc} 39  * <ul> 40  * <li>This action can be called only after having called one and only one {@link MoveMotherNature} action</li> 41  * <li>The previous {@link PlayerAction}s must be either {@link MoveMotherNature} or {@link PlayCharacterCard}</li> 42  * <li>The distance declared to move must be within acceptable ranges</li> 43  * <li>The Player who calls the action must have enough space in its "entrance" field to allow for all pawns on the 44  * tile to be transferred</li> 45  * <li>The selected cloud tile must not be empty (unless no other non-empty cloud tiles are present)</li> 46  * </ul> 47  * 48  * @param history the controller stores a {@link List} of previous {@link PlayerAction}s related to the player taking 49  * the current turn (at every new turn, the history is cleared). 50  * Some actions may use this {@link List} to check for duplicates. 51  * @param ctx a reference to {@link Model}. Some actions may use this reference to check for consistency between what 52  * the actions declares and what the Model offers. 53  * @return An empty {@link OptionalValue} in case of a successful validation. Otherwise the returned {@link OptionalValue} 54  * contains the related {@link InputValidationException} 55  */ 56  @Override 57  protected OptionalValue<InputValidationException> customValidation(List<PlayerAction> history, Model ctx) { 58  if (countSimilarClassOccurrences(MoveMotherNature.class, history) != 1) { 59  return OptionalValue.of(new GenericInputValidationException("History", "MoveMotherNature action has not been executed")); 60  } 61  if (!(history.get(history.size() - 1).getClass() == MoveMotherNature.class || (history.get(history.size() - 1).getClass() == PlayCharacterCard.class))) { 62  return OptionalValue.of(new GenericInputValidationException("History", "This action can only be executed after a MoveMotherNature action or PlayCharacterCard action")); 63  } 64  if (!(this.selectedTile >= 0 && selectedTile <= ctx.getClouds().size() - 1)) { 65  return OptionalValue.of(new InvalidElementException("Cloud")); 66  } 67  PlayerBoard caller = ctx.getMutableTurnOrder().getMutableCurrentPlayer(); 68  Cloud selectedCloud = ctx.getClouds().get(selectedTile); 69  if (!(caller.getEntranceSpaceLeft() >= selectedCloud.getContents().size())) { 70  return OptionalValue.of(new GenericInputValidationException("Entrance", 71  "can't contain " + selectedCloud.getContents().size() 72  + " elements without overflowing.")); 73  } 74  if (ctx.getClouds().stream().anyMatch(cloud -> cloud.getContents().size() != 0)) { 75  if (selectedCloud.getContents().size() == 0) { 76  return OptionalValue.of(new GenericInputValidationException("Cloud", 77  "has already been emptied")); 78  } 79  } 80  return OptionalValue.empty(); 81  } 82  83  @Override 84  public void unsafeExecute(Model ctx) { 85  Cloud selectedCloud = ctx.getClouds().get(selectedTile); //get cloud 86  try { 87  ctx.getMutableTurnOrder() 88  .getMutableCurrentPlayer() 89  .addStudentsToEntrance(selectedCloud.extractContents());//fill playerboard's entrance 90  } catch (Exception e) { 91  e.printStackTrace(); 92  } 93  } 94 }