1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.event;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import net.sf.magicproject.clickable.ability.Ability;
27 import net.sf.magicproject.clickable.targetable.card.AbstractCard;
28 import net.sf.magicproject.clickable.targetable.card.MCard;
29 import net.sf.magicproject.clickable.targetable.card.TriggeredCard;
30 import net.sf.magicproject.clickable.targetable.player.Player;
31 import net.sf.magicproject.event.context.ContextEventListener;
32 import net.sf.magicproject.network.ConnectionManager;
33 import net.sf.magicproject.network.IdMessages;
34 import net.sf.magicproject.network.MSocketListener;
35 import net.sf.magicproject.stack.StackManager;
36 import net.sf.magicproject.test.Test;
37 import net.sf.magicproject.tools.IntegerWrapper;
38 import net.sf.magicproject.tools.Log;
39 import net.sf.magicproject.ui.MagicUIComponents;
40 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
41 import net.sf.magicproject.ui.wizard.Replacement;
42
43 /***
44 * MEventTriggered.java Created on 2 févr. 2004
45 *
46 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
47 * @since 0.54
48 */
49 public abstract class TriggeredEvent extends MEventListener {
50
51 /***
52 * Create an instance of MEventTriggered by reading a file Offset's file must
53 * pointing on the first byte of this event <br>
54 * <ul>
55 * Structure of InputStream : Data[size]
56 * <li>idZone [1]</li>
57 * <li>test [...]</li>
58 * </ul>
59 *
60 * @param inputFile
61 * is the file containing this event
62 * @param card
63 * is the card owning this event
64 * @throws IOException
65 * if error occured during the reading process from the specified
66 * input stream
67 */
68 public TriggeredEvent(InputStream inputFile, MCard card) throws IOException {
69 super(inputFile, card);
70 }
71
72 /***
73 * Creates a new instance of MEventTriggered specifying all attributes of this
74 * class. All parameters are copied, not cloned. So this new object shares the
75 * card and the specified codes
76 *
77 * @param idZone
78 * the place constraint to activate this event
79 * @param test
80 * the additional test of this event
81 * @param card
82 * is the card owning this card
83 */
84 protected TriggeredEvent(int idZone, Test test, MCard card) {
85 super(idZone, test, card);
86 }
87
88 @Override
89 public abstract MEventListener clone(MCard card);
90
91 @Override
92 public abstract Event getIdEvent();
93
94 @Override
95 public void registerToManager(Ability ability) {
96 if (TRIGGRED_ABILITIES.get(getIdEvent()) == null) {
97 TRIGGRED_ABILITIES.put(getIdEvent(), new ArrayList<Ability>(20));
98
99 TRIGGRED_ABILITIES.get(getIdEvent()).add(ability);
100 } else {
101 ability.optimizeRegisterToManager();
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117 @Override
118 public void removeFromManager(Ability ability) {
119 if (TRIGGRED_ABILITIES.get(getIdEvent()) != null) {
120
121 TRIGGRED_ABILITIES.get(getIdEvent()).remove(ability);
122 }
123 }
124
125 @Override
126 public final boolean isActivated() {
127 return false;
128 }
129
130 @Override
131 public final boolean isTriggered() {
132 return true;
133 }
134
135 @Override
136 public String toHtmlString(Ability ability, ContextEventListener context) {
137 return LanguageManagerMDB.getString("event-"
138 + getClass().getSimpleName().toLowerCase());
139 }
140
141 /***
142 * Return true if the current event has not been replaced. If only one
143 * replcement ability has been found, it xould be used. If severel replacement
144 * abilities have been found a form containing choice would be displaced.
145 *
146 * @param source
147 * the event source.
148 * @param result
149 * the found replacement abilities.
150 * @param eventName
151 * the current event name.
152 * @return true if the current event has not been replaced. False otherwise.
153 */
154 protected static final boolean manageReplacement(MCard source,
155 List<AbstractCard> result, String eventName) {
156 if (result != null) {
157 if (result.size() > 1) {
158 final Player controller = source.getController();
159 replacement = new IntegerWrapper(-1);
160 controller.setHandedPlayer();
161 if (controller.isYou()) {
162 Log.debug("You choose a replacement ability to use");
163 new Replacement(eventName, result).setVisible(true);
164 ConnectionManager.sendToOpponent(IdMessages.REPLACEMENT_ANSWER,
165 Replacement.replacement / 256, Replacement.replacement % 256);
166 } else {
167 MagicUIComponents.logListing.append(1,
168 "choosing a replacement ability to use");
169 StackManager.noReplayToken.release();
170 Log.debug("Opponent chooses a replacement ability to use");
171
172 Replacement.replacement = MSocketListener.getInstance()
173 .readReplacementAnswer();
174 Log.debug("Replacement : virtual mouseClick");
175 MagicUIComponents.logListing.append(1, "Choosen replacement : "
176 + result.get(Replacement.replacement).toString());
177 }
178 } else {
179 Replacement.replacement = 0;
180 }
181
182 if (StackManager.newSpell((TriggeredCard) result
183 .get(Replacement.replacement))) {
184 StackManager.resolveStack();
185 }
186 return false;
187 }
188 return true;
189 }
190
191 /***
192 * The selected replacement ability to use
193 */
194 public static IntegerWrapper replacement;
195 }