1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.expression;
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.deckbuilder.Deck;
28 import net.sf.magicproject.event.context.ContextEventListener;
29 import net.sf.magicproject.test.Test;
30 import net.sf.magicproject.test.TestFactory;
31 import net.sf.magicproject.tools.Log;
32 import net.sf.magicproject.tools.MCardCompare;
33
34 /***
35 * Count cards in a player's deck.
36 *
37 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38 * @since 0.94
39 */
40 public class DeckCounter extends Expression {
41
42 /***
43 * Creates a new instance of DeckCounter <br>
44 * <ul>
45 * Structure of InputStream : Data[size]
46 * <li>test used to fill counter [Test]
47 * </ul>
48 *
49 * @param inputFile
50 * file containing this action
51 * @throws IOException
52 * if error occurred during the reading process from the specified
53 * input stream
54 */
55 public DeckCounter(InputStream inputFile) throws IOException {
56 super();
57 test = TestFactory.readNextTest(inputFile);
58 }
59
60 @Override
61 public int getValue(Ability ability, Targetable tested,
62 ContextEventListener context) {
63
64 final Targetable previousTested = Counter.superTested;
65 Counter.superTested = tested;
66 int res = 0;
67 for (MCardCompare card : Deck.currentDeck.getCards()) {
68 try {
69 if (test.test(ability, card.getCard(Deck.currentDeck.getMdbStream()))) {
70 res+=card.getAmount();
71 }
72 } catch (IOException e) {
73 Log.error(e);
74 }
75 }
76 lastRanTest = test;
77
78
79 Counter.superTested = previousTested;
80 return res;
81 }
82
83 @Override
84 public String toString() {
85 return "OCCURENCE IN DECK";
86 }
87
88 /***
89 * The test to use for counters
90 */
91 private final Test test;
92
93 /***
94 * The last test running for this class.
95 */
96 public static Test lastRanTest;
97
98 }