Coverage Summary for Class: PlayAssistantCard (it.polimi.ingsw.Controller.Actions)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
PlayAssistantCard |
100%
(1/1)
|
100%
(3/3)
|
75%
(9/12)
|
94,4%
(17/18)
|
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.Exceptions.Operation.OperationException;
7 import it.polimi.ingsw.Misc.OptionalValue;
8 import it.polimi.ingsw.Model.AssistantCard;
9 import it.polimi.ingsw.Model.Enums.GamePhase;
10 import it.polimi.ingsw.Model.Model;
11 import it.polimi.ingsw.Model.PlayerBoard;
12 import it.polimi.ingsw.Model.TurnOrder;
13
14 import java.io.Serial;
15 import java.util.List;
16
17
18 /**
19 * This {@link PlayerAction} allows the caller to select the desired assistant card to be played this round. This action
20 * is linked to the Setup (Preparation) Phase.
21 */
22 public class PlayAssistantCard extends PlayerAction {
23 @Serial
24 private static final long serialVersionUID = 206L; // convention: 2 for controller, (01 -> 99) for objects
25
26 private final int selectedAssistant;
27
28 /**
29 * Create a new instance of this class with the following inputs:
30 *
31 * @param playerBoardId the ID of the current {@link PlayerBoard}
32 * @param selectedAssistant the priority of the {@link AssistantCard} to be selected
33 */
34 public PlayAssistantCard(int playerBoardId, int selectedAssistant) {
35 super(playerBoardId, true);
36 this.selectedAssistant = selectedAssistant - 1;
37 }
38
39 /**
40 * {@inheritDoc}
41 * <ul>
42 * <li>The {@link GamePhase} must be {@link GamePhase#SETUP}</li>
43 * <li>The selected assistant card must be within bounds (always greater or equal to 0, always lesser or equal to the size of
44 * the player's assistants deck</li>
45 * <li>The player must play a card that has not been chosen by other players before (unless there are no other cards left to choose from)</li>
46 * <li>The selected {@link AssistantCard} can only be used once by the player</li>
47 * </ul>
48 *
49 * @param history the controller stores a {@link List} of previous {@link PlayerAction}s related to the player taking
50 * the current turn (at every new turn, the history is cleared).
51 * Some actions may use this {@link List} to check for duplicates.
52 * @param ctx a reference to {@link Model}. Some actions may use this reference to check for consistency between what
53 * the actions declares and what the Model offers.
54 * @return An empty {@link OptionalValue} in case of a successful validation. Otherwise the returned {@link OptionalValue}
55 * contains the related {@link InputValidationException}
56 */
57 @Override
58 protected OptionalValue<InputValidationException> customValidation(List<PlayerAction> history, Model ctx) {
59 PlayerBoard currentPlayer = ctx.getMutableTurnOrder().getMutableCurrentPlayer();
60 TurnOrder turnOrder = ctx.getMutableTurnOrder();
61 if (ctx.getMutableTurnOrder().getGamePhase() != GamePhase.SETUP) {
62 return OptionalValue.of(new GenericInputValidationException("Assitant Card", "may only be used during the setup phase"));
63 }
64 if (!(this.selectedAssistant >= 0 && this.selectedAssistant <= currentPlayer.getMutableAssistantCards().size() - 1)) {
65 return OptionalValue.of(new InvalidElementException("Assitant Card"));
66 }
67 AssistantCard selectedCard = currentPlayer.getMutableAssistantCards().get(selectedAssistant);
68 if (selectedCard.getUsed()) {
69 return OptionalValue.of(new GenericInputValidationException("Assitant Card", "can only be used once"));
70 }
71 if (ctx.getMutableTurnOrder().isAlreadyInSelection(selectedCard) && turnOrder.canPlayUniqueCard(currentPlayer)) {
72 return OptionalValue.of(new GenericInputValidationException("Assitant Card", "has already been selected by another player"));
73 }
74 return OptionalValue.empty();
75 }
76
77 public void unsafeExecute(Model ctx) throws OperationException {
78 PlayerBoard pb = ctx.getMutableTurnOrder().getMutableCurrentPlayer();
79 AssistantCard sa = pb.getMutableAssistantCards().get(selectedAssistant);
80 ctx.getMutableTurnOrder().setSelectedCard(pb, sa);
81 ctx.getMutableTurnOrder().stepToNextPlayer();
82 }
83 }