Coverage Summary for Class: CharacterCard (it.polimi.ingsw.Model)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| CharacterCard | 100% (1/1) | 85,7% (6/7) | 83,3% (5/6) | 82,4% (14/17) |
1 package it.polimi.ingsw.Model; 2 3 import it.polimi.ingsw.Exceptions.Input.InputValidationException; 4 import it.polimi.ingsw.Exceptions.Input.InvalidElementException; 5 import it.polimi.ingsw.Misc.OptionalValue; 6 7 import java.io.Serial; 8 import java.io.Serializable; 9 10 public abstract class CharacterCard implements Serializable { 11 @Serial 12 private static final long serialVersionUID = 115L; // convention: 1 for model, (01 -> 99) for objects 13 14 protected final int id; 15 protected final int cost; 16 protected final Model context; 17 protected int timeUsed; 18 19 public CharacterCard(int id, int cost, Model context) { 20 this.id = id; 21 this.cost = cost; 22 this.timeUsed = 0; 23 this.context = context; 24 } 25 26 public final int getId() { 27 return this.id; 28 } 29 30 public final int getCost() { 31 return this.timeUsed > 0 ? this.cost + 1 : this.cost; 32 } 33 34 public final int getTimeUsed() { 35 return this.timeUsed; 36 } 37 38 /** 39 * This function checks whether the correct input has been provided. It should always be called BEFORE calling an 40 * unsafeApplyEffect. Keep in mind this function does not alterate the gamestate. 41 * 42 * @param input user's input object 43 * @return a non empty {@link OptionalValue} containing a validation error. Or an empty one when the input is correct 44 */ 45 public final OptionalValue<InputValidationException> checkInput(CharacterCardInput input) { 46 if (input.getCaller() == null || !input.getCaller().getNickname().equals(this.context.getMutableTurnOrder().getMutableCurrentPlayer().getNickname())) { 47 48 return OptionalValue.of(new InvalidElementException("Card Caller")); 49 } 50 return overridableCheckInput(input); 51 } 52 53 /** 54 * This function checks whether the correct input has been provided. It is part of the checkInput function. 55 * Keep in mind this function does not alterate the gamestate. 56 * NOTE: checkInput(input) by default checks whether the correct player has called the card, then relays all other 57 * checks to this function. So don't check the correct user in this function as it is pointless. 58 * 59 * @param input user's input object 60 * @return a non empty {@link OptionalValue} containing a validation error. Or an empty one when the input is correct 61 */ 62 protected abstract OptionalValue<InputValidationException> overridableCheckInput(CharacterCardInput input); 63 64 65 /** 66 * This method must be called after checking user's input 67 * Play 68 * 69 * @param input a {@link CharacterCardInput} object, the same used during validation 70 */ 71 public final void unsafeUseCard(CharacterCardInput input) { 72 try { // we should never get an exception now, if we do we crash 73 unsafeApplyEffect(input); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 addUse(); 78 } 79 80 /** 81 * This method must be called after {@link #checkInput(CharacterCardInput)} 82 * Execute CharacterCard's effect (NOTE: keep in mind this funcion DOES ALTERATE the gamestate) 83 * 84 * @param input verified user's input 85 * @throws Exception not related to user's input and not recoverable 86 */ 87 protected abstract void unsafeApplyEffect(CharacterCardInput input) throws Exception; 88 89 private void addUse() { 90 this.timeUsed++; 91 } 92 }