View Javadoc

1   /*
2    * Created on Nov 2, 2004 
3    * Original filename was PhaseIs.java
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.test;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.List;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.clickable.targetable.card.MCard;
31  import net.sf.magicproject.event.MEventListener;
32  import net.sf.magicproject.event.phase.BeginningPhase;
33  import net.sf.magicproject.event.phase.EndOfPhase;
34  import net.sf.magicproject.event.phase.PhaseFilter;
35  import net.sf.magicproject.expression.Expression;
36  import net.sf.magicproject.expression.ExpressionFactory;
37  import net.sf.magicproject.token.IdZones;
38  
39  /***
40   * Test the current phase
41   * 
42   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
43   * @since 0.72
44   */
45  class PhaseIs extends Test {
46  
47  	/***
48  	 * Create an instance of PhaseIs by reading a file. Offset's file must
49  	 * pointing on the first byte of this test <br>
50  	 * <ul>
51  	 * Structure of InputStream : Data[size]
52  	 * <li>idTest [1]</li>
53  	 * <li>tested on (idTestedOn) [1]</li>
54  	 * <li>phase filter [1]</li>
55  	 * <li>phase index/id : Expression [...]</li>
56  	 * </ul>
57  	 * 
58  	 * @param inputFile
59  	 *          is the file containing this event
60  	 * @throws IOException
61  	 *           if error occurred during the reading process from the specified
62  	 *           input stream
63  	 */
64  	PhaseIs(InputStream inputFile) throws IOException {
65  		super(inputFile);
66  		phaseFilter = PhaseFilter.valueOf(inputFile);
67  		idPhase = ExpressionFactory.readNextExpression(inputFile);
68  	}
69  
70  	@Override
71  	public boolean test(Ability ability, Targetable tested) {
72  		return phaseFilter.getPhaseFilter() == idPhase.getValue(ability, tested,
73  				null);
74  	}
75  
76  	@Override
77  	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
78  			Test globalTest) {
79  		res.add(new EndOfPhase(IdZones.PLAY, globalTest, source, idPhase,
80  				phaseFilter));
81  		res.add(new BeginningPhase(IdZones.PLAY, globalTest, source, idPhase,
82  				phaseFilter));
83  	}
84  
85  	/***
86  	 * Is the phase to match during each test
87  	 */
88  	private Expression idPhase;
89  
90  	/***
91  	 * The phase filter to use.
92  	 */
93  	protected PhaseFilter phaseFilter;
94  
95  }