Coverage Summary for Class: MoveMotherNature (it.polimi.ingsw.Controller.Actions)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| MoveMotherNature | 100% (1/1) | 100% (3/3) | 64,3% (9/14) | 93,8% (15/16) |
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.AssistantCard; 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 * This {@link PlayerAction} allows the caller to move the mother nature entity forward by a specified amount. This action 18 * is linked to the Action Phase. 19 */ 20 public class MoveMotherNature extends PlayerAction { 21 @Serial 22 private static final long serialVersionUID = 204L; // convention: 2 for controller, (01 -> 99) for objects 23 24 private final int distanceToMove; 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 distanceToMove the amount of distance Mother Nature is going to be moved 31 */ 32 public MoveMotherNature(int playerBoardId, int distanceToMove) { 33 super(playerBoardId, true); 34 this.distanceToMove = distanceToMove; 35 } 36 37 /** 38 * {@inheritDoc} 39 * <ul> 40 * <li>This action can be called only after having used all possible {@link MoveStudent} actions</li> 41 * <li>The previous {@link PlayerAction}s must be either {@link MoveStudent} or {@link PlayCharacterCard}</li> 42 * <li>The distance declared to move must be within acceptable ranges</li> 43 * </ul> 44 * 45 * @param history the controller stores a {@link List} of previous {@link PlayerAction}s related to the player taking 46 * the current turn (at every new turn, the history is cleared). 47 * Some actions may use this {@link List} to check for duplicates. 48 * @param ctx a reference to {@link Model}. Some actions may use this reference to check for consistency between what 49 * the actions declares and what the Model offers. 50 * @return An empty {@link OptionalValue} in case of a successful validation. Otherwise the returned {@link OptionalValue} 51 * contains the related {@link InputValidationException} 52 */ 53 @Override 54 protected OptionalValue<InputValidationException> customValidation(List<PlayerAction> history, Model ctx) { 55 PlayerBoard currentPlayer = ctx.getMutableTurnOrder().getMutableCurrentPlayer(); 56 int maxCount = ctx.getMutablePlayerBoards().size() == 3 ? 4 : 3; 57 OptionalValue<AssistantCard> optionalAssistantCard = ctx.getMutableTurnOrder().getMutableSelectedCard(currentPlayer); 58 if (!(countSimilarClassOccurrences(MoveStudent.class, history) == maxCount)) { 59 return OptionalValue.of(new GenericInputValidationException("History", "MotherNature can't be moved before having placed all " + maxCount + " pawns")); 60 } 61 if (!(history.get(history.size() - 1).getClass() == MoveStudent.class || (history.get(history.size() - 1).getClass() == PlayCharacterCard.class))) { 62 return OptionalValue.of(new GenericInputValidationException("History", "This action can only be executed after a MoveStudent action or PlayCharacterCard action")); 63 } 64 int maxMovement = optionalAssistantCard.get().getMaxMovement(); 65 if (!(distanceToMove >= 1 && 66 distanceToMove <= (ctx.getMutableEffects().isMotherNatureMovementIncreased() ? 67 maxMovement + 2 : maxMovement) 68 )) { 69 return OptionalValue.of(new InvalidElementException("DistanceToMove")); 70 } 71 return OptionalValue.empty(); 72 } 73 74 @Override 75 public void unsafeExecute(Model ctx) { 76 ctx.moveAndActMotherNature(distanceToMove); 77 } 78 79 }