1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.action;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.sf.magicproject.action.handler.StandardAction;
26 import net.sf.magicproject.clickable.ability.Ability;
27 import net.sf.magicproject.clickable.targetable.card.MCard;
28 import net.sf.magicproject.event.context.ContextEventListener;
29 import net.sf.magicproject.modifier.ModifierFactory;
30 import net.sf.magicproject.modifier.ModifierModel;
31 import net.sf.magicproject.modifier.RegisterIndirectionModel;
32 import net.sf.magicproject.modifier.RegisterModifierModel;
33 import net.sf.magicproject.modifier.StaticModifierModel;
34 import net.sf.magicproject.stack.StackManager;
35 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
36
37 /***
38 * Add a modifier on the components of target-list.
39 *
40 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41 */
42 class AddModifier extends UserAction implements StandardAction {
43
44 /***
45 * Creates a new instance of AddModifier <br>
46 * <ul>
47 * Structure of stream : Data[size]
48 * <li>idAction [1]</li>
49 * <li>nb modifier [1]</li>
50 * <li>modifier i [...]</li>
51 * </ul>
52 *
53 * @param inputFile
54 * stream containing this action.
55 * @throws IOException
56 * If some other I/O error occurs
57 */
58 AddModifier(InputStream inputFile) throws IOException {
59 super(inputFile);
60 int count = inputFile.read();
61 modifiers = new ModifierModel[count];
62 for (int i = count; i-- > 0;) {
63 modifiers[i] = ModifierFactory.readModifier(inputFile);
64 }
65 }
66
67 @Override
68 public final Actiontype getIdAction() {
69 return Actiontype.ADD_MODIFIER;
70 }
71
72 public boolean play(ContextEventListener context, Ability ability) {
73 for (ModifierModel modifier : modifiers) {
74 if (modifier instanceof StaticModifierModel) {
75
76 modifier.addModifierFromModel(ability, null);
77 } else {
78 for (int j = StackManager.getInstance().getTargetedList().size(); j-- > 0;) {
79 if (checkTimeStamp(context, (MCard) StackManager.getInstance()
80 .getTargetedList().get(j))) {
81 modifier.addModifierFromModel(ability, (MCard) StackManager
82 .getInstance().getTargetedList().get(j));
83 }
84 }
85 }
86 }
87 return true;
88 }
89
90 @Override
91 public String toHtmlString(Ability ability, ContextEventListener context) {
92 if (actionName != null) {
93 if (actionName.charAt(0) == '%') {
94 return "";
95 }
96 if (actionName.charAt(0) == '@') {
97 final String picture = ActionFactory.PICTURES.get(actionName);
98 if (picture != null) {
99 return toHtmlString(ability, picture);
100 }
101 } else {
102 return LanguageManagerMDB.getString(actionName);
103 }
104 }
105
106 StringBuilder result = null;
107 for (int i = 0; i < modifiers.length; i++) {
108 if (modifiers[i] instanceof RegisterModifierModel) {
109 final String tmpResult = ((RegisterModifierModel) modifiers[i])
110 .toHtmlString(ability, context, i == modifiers.length - 1
111 || !(modifiers[i + 1] instanceof RegisterModifierModel) ? null
112 : (RegisterModifierModel) modifiers[++i]);
113 if (result == null) {
114 result = new StringBuilder(tmpResult);
115 } else {
116 result.append(", ");
117 result.append(tmpResult);
118 }
119 continue;
120 } else if (modifiers[i] instanceof RegisterIndirectionModel) {
121 final String tmpResult = ((RegisterIndirectionModel) modifiers[i])
122 .toHtmlString(
123 ability,
124 context,
125 i == modifiers.length - 1
126 || !(modifiers[i + 1] instanceof RegisterIndirectionModel) ? null
127 : (RegisterIndirectionModel) modifiers[++i]);
128 if (result == null) {
129 result = new StringBuilder(tmpResult);
130 } else {
131 result.append(", ");
132 result.append(tmpResult);
133 }
134 continue;
135 }
136 if (result == null) {
137 result = new StringBuilder(modifiers[i].toHtmlString(ability, context));
138 } else {
139 result.append(", ");
140 result.append(modifiers[i].toHtmlString(ability, context));
141 }
142 }
143 if (result == null) {
144 return null;
145 }
146 return result.toString();
147 }
148
149 @Override
150 public String toString(Ability ability) {
151 return "add modifiers";
152 }
153
154 /***
155 * The modifiers model to add to the target list
156 */
157 private ModifierModel[] modifiers;
158
159 }