View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// we count cards, we save the upper counter test.
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  		// restore the previous upper counter test.
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  }