1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.test;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.sf.magicproject.clickable.ability.Ability;
26 import net.sf.magicproject.clickable.targetable.Targetable;
27 import net.sf.magicproject.clickable.targetable.card.SystemCard;
28 import net.sf.magicproject.deckbuilder.Deck;
29 import net.sf.magicproject.expression.Counter;
30 import net.sf.magicproject.expression.Expression;
31 import net.sf.magicproject.expression.ExpressionFactory;
32 import net.sf.magicproject.token.IdConst;
33 import net.sf.magicproject.tools.Log;
34 import net.sf.magicproject.tools.MCardCompare;
35
36 /***
37 * Count cards in a player's deck until a threshold.
38 *
39 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40 * @since 0.94
41 */
42 public class DeckCounter extends Test {
43
44 /***
45 * The test to use for counters
46 */
47 private final Test test;
48
49 /***
50 * The last test running for this class.
51 */
52 public static DeckCounter lastRanInstance;
53
54 /***
55 * The last card tested with a counter.
56 */
57 public static MCardCompare lastRanCard;
58
59 /***
60 * The threshold of this counter.
61 */
62 private final Expression threshold;
63
64 /***
65 * Creates a new instance of DeckCounter <br>
66 * <ul>
67 * Structure of InputStream : Data[size]
68 * <li>test used to fill counter [Test]
69 * <li>threshold [Expression]
70 * </ul>
71 *
72 * @param inputFile
73 * file containing this action
74 * @throws IOException
75 * if error occurred during the reading process from the specified
76 * input stream
77 */
78 public DeckCounter(InputStream inputFile) throws IOException {
79 super(inputFile);
80 test = TestFactory.readNextTest(inputFile);
81 threshold = ExpressionFactory.readNextExpression(inputFile);
82 }
83
84 @Override
85 public boolean test(Ability ability, Targetable tested) {
86
87 final Targetable previousTested = Counter.superTested;
88 final int threshold = this.threshold.getValue(ability, tested, null);
89 Counter.superTested = tested;
90 lastRanCard = null;
91 int res = 0;
92 for (MCardCompare card : Deck.currentDeck.getCards()) {
93 try {
94 if (test.test(ability, card.getCard(Deck.currentDeck.getMdbStream()))) {
95 res += card.getAmount();
96 if (res > threshold) {
97 if (lastRanCard == null) {
98 lastRanCard = card;
99 lastRanInstance = this;
100 }
101 return false;
102 }
103 } else if (threshold == IdConst.ALL) {
104
105 if (lastRanCard == null) {
106 lastRanCard = card;
107 lastRanInstance = this;
108 }
109 return false;
110 }
111 } catch (IOException e) {
112 Log.error(e);
113 }
114 }
115 lastRanInstance = this;
116
117
118 Counter.superTested = previousTested;
119 return true;
120 }
121
122 /***
123 * Return the threshold of this counter.
124 *
125 * @return the threshold of this counter.
126 */
127 public Expression getThreshold() {
128 return this.threshold;
129 }
130
131 @Override
132 public String toString() {
133 return "OCCURENCE IN DECK <= "
134 + threshold.getPreemptionValue(null, SystemCard.instance, null);
135 }
136
137 }