1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.event.phase;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.List;
26 import java.util.Map;
27
28 import net.sf.magicproject.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.ability.Priority;
30 import net.sf.magicproject.clickable.ability.ReplacementAbility;
31 import net.sf.magicproject.clickable.targetable.card.MCard;
32 import net.sf.magicproject.event.Event;
33 import net.sf.magicproject.event.MEventListener;
34 import net.sf.magicproject.event.context.ContextEventListener;
35 import net.sf.magicproject.event.context.MContextCardCardIntInt;
36 import net.sf.magicproject.expression.Expression;
37 import net.sf.magicproject.stack.EventManager;
38 import net.sf.magicproject.stack.StackManager;
39 import net.sf.magicproject.test.Or;
40 import net.sf.magicproject.test.Test;
41 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
42
43 /***
44 * End of phase events. Raised just before to go to the next phase <br>
45 * If idPhase value is NO_CARE, this event would be acivated at each end of
46 * phase <br>
47 *
48 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
49 * @since 0.54
50 */
51 public class EndOfPhase extends PhaseEvent {
52
53 /***
54 * Create an instance of MEventEndOfPhase by reading a file Offset's file must
55 * pointing on the first byte of this event <br>
56 * <ul>
57 * Structure of InputStream : Data[size]
58 * <li>idZone [1]</li>
59 * <li>test [...]</li>
60 * <li>idPhase(phase identifiant) [2]</li>
61 * </ul>
62 *
63 * @param inputFile
64 * is the file containing this event
65 * @param card
66 * is the card owning this event
67 * @throws IOException
68 * if error occurred during the reading process from the specified
69 * input stream
70 */
71 public EndOfPhase(InputStream inputFile, MCard card) throws IOException {
72 super(inputFile, card);
73 }
74
75 /***
76 * Creates a new instance of MEventEndOfPhase specifying all attributes of
77 * this class. All parameters are copied, not cloned. So this new object
78 * shares the card and the specified codes
79 *
80 * @param idZone
81 * the place constraint to activate this event
82 * @param test
83 * the test of this event
84 * @param card
85 * is the card owning this card
86 * @param idPhase
87 * the looked for phase identifiant
88 * @param phaseFilter
89 * the filter.
90 */
91 public EndOfPhase(int idZone, Test test, MCard card, Expression idPhase,
92 PhaseFilter phaseFilter) {
93 super(idZone, test, card, idPhase, phaseFilter);
94 }
95
96 @Override
97 public MEventListener clone(MCard card) {
98 return new EndOfPhase(idZone, test, card, idPhase, phaseFilter);
99 }
100
101 /***
102 * Dispatch this event to all active event listeners able to understand this
103 * event. The listening events able to understand this event are <code>this
104 * </code>
105 * and other multiple event listeners. For each event listeners having
106 * responded they have been activated, the corresponding ability is added to
107 * the triggered buffer zone of player owning this ability. <br>
108 */
109 public static void dispatchEvent() {
110 dispatchEvent(EVENT);
111 }
112
113 /***
114 * Dispatch this event to replacement abilites only. If one or several
115 * abilities have been activated this way, this function will return false.
116 * The return value must be checked. In case of <code>false</code> value,
117 * the caller should not call any stack resolution since activated abilities
118 * are being played. Unusually, when several replacement abilities are found
119 * for this event, ONLY the first replacement found replacement ability is
120 * picked and played.
121 *
122 * @param currentIdPhase
123 * the current phase type identifiant (not index)
124 * @return true if the event has not been replaced.
125 */
126 public static boolean tryAction(int currentIdPhase) {
127 final Map<Priority, List<ReplacementAbility>> map = getReplacementAbilities(EVENT);
128 for (Priority priority : Priority.values()) {
129 for (ReplacementAbility ability : map.get(priority)) {
130 if (((EndOfPhase) ability.eventComing()).isMatching(ability)) {
131 if (StackManager.newSpell(ability
132 .getTriggeredClone(new MContextCardCardIntInt(StackManager
133 .currentPlayer(), currentIdPhase)))) {
134 StackManager.resolveStack();
135 }
136 return false;
137 }
138 }
139 }
140 return true;
141 }
142
143 @Override
144 public final Event getIdEvent() {
145 return EVENT;
146 }
147
148 /***
149 * The event type.
150 */
151 public static final Event EVENT = Event.EOP;
152
153 @Override
154 public String toHtmlString(Ability ability, ContextEventListener context) {
155 return LanguageManagerMDB.getString("event-eop", LanguageManagerMDB
156 .getString(EventManager.turnStructure[idPhase.getValue(ability, null,
157 context)].phaseName));
158 }
159
160 @Override
161 public MEventListener appendOr(MEventListener other) {
162 if (((PhaseEvent) other).idPhase == idPhase) {
163 return new EndOfPhase(idZone, Or.append(this.test, other.test), card,
164 idPhase, phaseFilter);
165 }
166 return null;
167 }
168 }