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.action;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import net.sf.magicproject.action.handler.StandardAction;
28 import net.sf.magicproject.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.targetable.Targetable;
30 import net.sf.magicproject.deckbuilder.MdbLoader;
31 import net.sf.magicproject.event.context.ContextEventListener;
32 import net.sf.magicproject.expression.Expression;
33 import net.sf.magicproject.expression.ExpressionFactory;
34 import net.sf.magicproject.operation.Operation;
35 import net.sf.magicproject.operation.OperationFactory;
36 import net.sf.magicproject.stack.StackManager;
37 import net.sf.magicproject.test.Test;
38 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
39
40 /***
41 * This action is used to modifiy a public token or a private one of the card.
42 * This action can use the target list when is played : the address must be
43 * IdTokens#TARGET. So the target list must set before this action would be
44 * played.
45 *
46 * @since 0.53 support STACK registers <br>
47 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
48 * @since 0.54
49 * @since 0.72 support counter ability
50 */
51 public abstract class ModifyRegister extends UserAction implements
52 StandardAction {
53
54 /***
55 * Create an instance of ModifyRegister by reading a file Offset's file must
56 * pointing on the first byte of this action <br>
57 * <ul>
58 * Structure of InputStream : Data[size]
59 * <li>idAction [1]</li>
60 * <li>register [Register]</li>
61 * <li>index [Expression]</li>
62 * <li>operation [Operation]</li>
63 * <li>value [Expression]</li>
64 * </ul>
65 *
66 * @param inputFile
67 * file containing this action
68 * @throws IOException
69 * if error occured during the reading process from the specified
70 * input stream
71 */
72 protected ModifyRegister(InputStream inputFile) throws IOException {
73 super(inputFile);
74 index = ExpressionFactory.readNextExpression(inputFile);
75 op = OperationFactory.readNextOperation(inputFile);
76 valueExpr = ExpressionFactory.readNextExpression(inputFile);
77 }
78
79 /***
80 * Create an instance of ModifyRegister with the specified card owning the new
81 * created action
82 *
83 * @param actionName
84 * the action name.
85 * @param index
86 * the modified register index.
87 * @param op
88 * The operation applied to the token id and the value
89 * @param valueExpr
90 * the used right expression.
91 */
92 protected ModifyRegister(String actionName, Expression index, Operation op,
93 Expression valueExpr) {
94 super(actionName);
95 this.index = index;
96 this.op = op;
97 this.valueExpr = valueExpr;
98 }
99
100 @Override
101 public abstract Actiontype getIdAction();
102
103 public abstract boolean play(ContextEventListener context, Ability ability);
104
105 /***
106 * Checks all cards corresponding to this constraints
107 *
108 * @param test
109 * applied to count valid cards
110 * @param ability
111 * is the ability owning this test. The card component of this
112 * ability should correspond to the card owning this test too.
113 * @param restrictionZone
114 * the restriction zone. If is <code>-1</code> the scan would be
115 * processed on all zones.
116 * @return the amount of found cards.
117 */
118 public static int countAllCardsOf(Test test, Ability ability,
119 int restrictionZone) {
120 if (restrictionZone != -1) {
121 return StackManager.PLAYERS[0].zoneManager.getContainer(restrictionZone)
122 .countAllCardsOf(test, ability)
123 + StackManager.PLAYERS[1].zoneManager.getContainer(restrictionZone)
124 .countAllCardsOf(test, ability);
125 }
126 return StackManager.PLAYERS[0].zoneManager.countAllCardsOf(test, ability)
127 + StackManager.PLAYERS[1].zoneManager.countAllCardsOf(test, ability);
128 }
129
130 @Override
131 public abstract String toString(Ability ability);
132
133 @Override
134 public final String toHtmlString(Ability ability, ContextEventListener context) {
135 if (actionName != null && actionName.charAt(0) != '%'
136 && actionName.charAt(0) != '@' && actionName.indexOf("%n") != -1) {
137 int value = 0;
138 try {
139 value = valueExpr.getValue(ability, null, context);
140 } catch (Exception e) {
141 value = -1;
142 }
143 if (value == 1) {
144 return LanguageManagerMDB.getString(actionName.replaceAll("%n", "1"));
145 }
146 if (value == -1) {
147 return LanguageManagerMDB.getString(actionName).replaceAll("%n",
148 MdbLoader.unknownSmlManaHtml);
149 }
150 if (value > 0) {
151 return LanguageManagerMDB.getString(actionName).replaceAll("%n",
152 "" + value);
153 }
154 }
155
156 return super.toHtmlString(ability, context);
157 }
158
159 /***
160 * Return the final value of this test is this value was a counter.
161 *
162 * @param ability
163 * is the ability owning this test. The card component of this
164 * ability should correspond to the card owning this test too.
165 * @param tested
166 * the tested component
167 * @param context
168 * is the context attached to this test.
169 * @return he final value of this test is this value was a counter.
170 */
171 protected int getValue(Ability ability, Targetable tested,
172 ContextEventListener context) {
173 return valueExpr.getValue(ability, tested, context);
174 }
175
176 /***
177 * The index to modify.
178 */
179 protected Expression index = null;
180
181 /***
182 * The complex expression to use for the right value. Is null if the IdToken
183 * number is not a complex expression.
184 */
185 protected Expression valueExpr = null;
186
187 /***
188 * The operation applied to the token id and the value
189 */
190 protected Operation op;
191
192 }