1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.magicproject.event.phase;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import net.sf.magicproject.clickable.ability.Ability;
28 import net.sf.magicproject.clickable.targetable.card.MCard;
29 import net.sf.magicproject.event.Event;
30 import net.sf.magicproject.event.MEventListener;
31 import net.sf.magicproject.event.TriggeredEvent;
32 import net.sf.magicproject.event.context.MContextCardCardIntInt;
33 import net.sf.magicproject.expression.Expression;
34 import net.sf.magicproject.expression.ExpressionFactory;
35 import net.sf.magicproject.stack.StackManager;
36 import net.sf.magicproject.test.Test;
37 import net.sf.magicproject.token.IdConst;
38
39 /***
40 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41 * @since 0.54
42 */
43 abstract class PhaseEvent extends TriggeredEvent {
44
45 /***
46 * Create an instance of MEventPhase by reading a file Offset's file must
47 * pointing on the first byte of this event <br>
48 * <ul>
49 * Structure of InputStream : Data[size]
50 * <li>idZone [1]</li>
51 * <li>test [...]</li>
52 * <li>idPhase(phase identifiant) : Expression [2]</li>
53 * </ul>
54 *
55 * @param inputFile
56 * is the file containing this event
57 * @param card
58 * is the card owning this event
59 * @throws IOException
60 * if error occurred during the reading process from the specified
61 * input stream
62 */
63 protected PhaseEvent(InputStream inputFile, MCard card) throws IOException {
64 super(inputFile, card);
65 phaseFilter = PhaseFilter.valueOf(inputFile);
66 idPhase = ExpressionFactory.readNextExpression(inputFile);
67 }
68
69 /***
70 * Creates a new instance of MEventPhase specifying all attributes of this
71 * class. All parameters are copied, not cloned. So this new object shares the
72 * card and the specified codes
73 *
74 * @param idZone
75 * the place constraint to activate this event
76 * @param test
77 * the test of this event
78 * @param card
79 * is the card owning this card
80 * @param idPhase
81 * represents the phase we are looking for
82 * @param phaseFilter
83 * the filter.
84 */
85 protected PhaseEvent(int idZone, Test test, MCard card, Expression idPhase,
86 PhaseFilter phaseFilter) {
87 super(idZone, test, card);
88 this.idPhase = idPhase;
89 this.phaseFilter = phaseFilter;
90 }
91
92 /***
93 * Tell if the current event matches with this event. If there is an
94 * additional code to check, it'would be checked if the main event matches
95 * with the main event
96 *
97 * @param ability
98 * is the ability owning this test. The card component of this
99 * ability should correspond to the card owning this test too.
100 * @return true if the current event match with this event
101 */
102 protected boolean isMatching(Ability ability) {
103 final int value = idPhase.getValue(ability, null, null);
104 return (value == IdConst.NO_CARE || phaseFilter.getPhaseFilter() == value)
105 && test(ability, StackManager.currentPlayer());
106 }
107
108 /***
109 * Dispatch this event to all active event listeners able to understand this
110 * event. The listening events able to understand this event are <code>this
111 * </code>
112 * and other multiple event listeners. For each event listeners having
113 * responded they have been activated, the corresponding ability is added to
114 * the triggered buffer zone of player owning this ability. <br>
115 * Note that the specified currentIdPhase corresponds to the phase identifier
116 * and not to te phase index. So if a same phase is present two times in the
117 * turn structure, and we are looking for it's end, this event would be
118 * activated twice.
119 *
120 * @param event
121 * the phase idEvent
122 */
123 protected static final void dispatchEvent(Event event) {
124 for (Ability ability : TRIGGRED_ABILITIES.get(event)) {
125 if (((PhaseEvent) ability.eventComing()).isMatching(ability)
126 && ability.isMatching()) {
127 /***
128 * this ability matches with the current event, so we add it to the
129 * triggered buffer zone
130 */
131 ability.triggerIt(new MContextCardCardIntInt(StackManager
132 .currentPlayer(), ((PhaseEvent) ability.eventComing()).phaseFilter
133 .getPhaseFilter()));
134 }
135 }
136 }
137
138 /***
139 * Return the idEvent of this event
140 *
141 * @return the idEvent of this event
142 */
143 @Override
144 public abstract Event getIdEvent();
145
146 /***
147 * Return a copy of this with the specified owner
148 *
149 * @param card
150 * is the card of the ability of this event
151 * @return copy of this event
152 */
153 @Override
154 public abstract MEventListener clone(MCard card);
155
156 /***
157 * Represents the phase we are looking for
158 */
159 protected Expression idPhase;
160
161 /***
162 * The phase filter to use.
163 */
164 protected PhaseFilter phaseFilter;
165
166 }