Coverage Summary for Class: Card11 (it.polimi.ingsw.Model)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
Card11 |
100%
(1/1)
|
100%
(5/5)
|
93,8%
(15/16)
|
85,2%
(23/27)
|
1 package it.polimi.ingsw.Model;
2
3 import it.polimi.ingsw.Exceptions.Container.EmptyContainerException;
4 import it.polimi.ingsw.Exceptions.Input.GenericInputValidationException;
5 import it.polimi.ingsw.Exceptions.Input.InputValidationException;
6 import it.polimi.ingsw.Exceptions.Input.InvalidElementException;
7 import it.polimi.ingsw.Exceptions.Operation.FailedOperationException;
8 import it.polimi.ingsw.Logger;
9 import it.polimi.ingsw.Misc.OptionalValue;
10 import it.polimi.ingsw.Model.Enums.PawnColour;
11 import it.polimi.ingsw.Model.Enums.StateType;
12
13 import java.io.Serial;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16
17 /**
18 * In Setup, draw 4 Students and place them on this card <br>
19 * EFFECT: Take 1 Student from this card and place it in
20 * your Dining Room. Then, draw a new Student from the
21 * Bag and place it on this card.
22 */
23 public class Card11 extends StatefulEffect {
24 @Serial
25 private static final long serialVersionUID = 113L; // convention: 1 for model, (01 -> 99) for objects
26
27 private final PawnColour[] students = new PawnColour[4];
28
29 public Card11(Model ctx) {
30 super(11, 2, StateType.PAWNCOLOUR, ctx);
31 for (int i = 0; i < 4; i++) {
32 try {
33 this.students[i] = ctx.getMutableStudentBag().extract();
34 } catch (EmptyContainerException e) {
35 // should never happen
36 Logger.severe("student bag was found empty while adding a student to Card11. Critical, unrecoverable, error");
37 throw new RuntimeException(e);
38 }
39 }
40 }
41
42 public ArrayList<Object> getState() {
43 return new ArrayList<>(Arrays.asList(students));
44 }
45
46 public StateType getStateType() {
47 return stateType;
48 }
49
50 /**
51 * Refer to: {@link CharacterCard#overridableCheckInput(CharacterCardInput)} for further information
52 *
53 * @param input CharacterCardInput should contain:
54 * <ul>
55 * <li>A valid pawnColour from card's state </li>
56 * </ul>
57 */
58 @Override
59 public OptionalValue<InputValidationException> overridableCheckInput(CharacterCardInput input) {
60 if (input.getTargetPawn().isEmpty()) {
61 return OptionalValue.of(new InvalidElementException("Target Pawn Colour"));
62 }
63 // find if the target pawn colour is present in the card's stored pawn
64 if (Arrays.stream(this.students).noneMatch(cell -> cell == input.getTargetPawn().get())) {
65 return OptionalValue.of(new InvalidElementException("Target Pawn Colour"));
66 }
67
68 PlayerBoard playerBoard = input.getCaller();
69 // validate size of dining room
70 if (playerBoard.isDiningRoomFull(input.getTargetPawn().get())) {
71 return OptionalValue.of(new GenericInputValidationException("Dining Room",
72 "can't contain " + input.getTargetPawn().get()
73 + "without overflowing."));
74 }
75 if (context.getMutableStudentBag().getSize() == 0) {
76 return OptionalValue.of(new GenericInputValidationException("Student Bag", "is empty"));
77 }
78 //all tests passed
79 return OptionalValue.empty();
80 }
81
82 /**
83 * Refer to: {@link CharacterCard#unsafeApplyEffect(CharacterCardInput)} for further information
84 */
85 @Override
86 protected void unsafeApplyEffect(CharacterCardInput input) throws Exception {
87 PawnColour movedPawn = input.getTargetPawn().get();
88 //add target pawn to caller's dining room
89 this.context.addStudentToDiningRoom(movedPawn, input.getCaller());
90 // find first occurrence of same target pawn in card state and swap it with a new pawn
91 for (int i = 0; i < 4; i++) {
92 if (this.students[i] == movedPawn) {
93 this.students[i] = context.getMutableStudentBag().extract();
94 return;
95 }
96 }
97 throw new FailedOperationException("Card011.unsafeApplyEffect", "Target pawn was not contained in card's state");
98 }
99
100 //test-purpose only
101 }