1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.magicproject.action;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import net.sf.magicproject.action.context.ActionContextWrapper;
29 import net.sf.magicproject.action.context.Int;
30 import net.sf.magicproject.action.handler.InitAction;
31 import net.sf.magicproject.action.handler.RollBackAction;
32 import net.sf.magicproject.action.handler.StandardAction;
33 import net.sf.magicproject.clickable.ability.Ability;
34 import net.sf.magicproject.event.context.ContextEventListener;
35 import net.sf.magicproject.expression.Expression;
36 import net.sf.magicproject.expression.ExpressionFactory;
37 import net.sf.magicproject.stack.StackManager;
38 import net.sf.magicproject.test.Test;
39 import net.sf.magicproject.test.TestFactory;
40
41 /***
42 * Depending on result of test, jump to the next action with hop equal to 1
43 * (normal case) for a false result, and jump to a sepecified hop for the true
44 * result. Like the if...then...else instruction. <br>
45 *
46 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
47 * @since 0.54
48 * @see net.sf.magicproject.action.Hop
49 */
50 class IfThenHop extends UserAction implements StandardAction, InitAction,
51 RollBackAction {
52
53 /***
54 * Create an instance of IfThenHop by reading a file Offset's file must
55 * pointing on the first byte of this action <br>
56 * <ul>
57 * Structure of InputStream : Data[size]
58 * <li>test [...]</li>
59 * <li>"hop" for 'else' case. Is 1 for 'then' case [2]</li>
60 * </ul>
61 *
62 * @param inputFile
63 * file containing this action
64 * @throws IOException
65 * if error occurred during the reading process from the specified
66 * input stream
67 */
68 IfThenHop(InputStream inputFile) throws IOException {
69 super(inputFile);
70 condition = TestFactory.readNextTest(inputFile);
71 valueExpr = ExpressionFactory.readNextExpression(inputFile);
72 }
73
74 @Override
75 public final Actiontype getIdAction() {
76 return Actiontype.IF_THEN_ELSE;
77 }
78
79 public boolean init(ActionContextWrapper actionContext,
80 ContextEventListener context, Ability ability) {
81 actionContext.actionContext = new Int(condition.test(ability, ability
82 .getCard()) ? 1 : 0);
83 return replay(actionContext, context, ability);
84 }
85
86 public boolean replay(ActionContextWrapper actionContext,
87 ContextEventListener context, Ability ability) {
88 if (((Int) actionContext.actionContext).getInt() == 0) {
89 StackManager.actionManager.setHop(valueExpr.getValue(ability, null,
90 context));
91 }
92 return true;
93 }
94
95 public void rollback(ActionContextWrapper actionContext,
96 ContextEventListener context, Ability ability) {
97
98 }
99
100 public boolean play(ContextEventListener context, Ability ability) {
101 if (!condition.test(ability, ability.getCard())) {
102 StackManager.actionManager.setHop(valueExpr.getValue(ability, null,
103 context));
104 }
105 return true;
106 }
107
108 @Override
109 public String toString(Ability ability) {
110 return "If ... Then ... Else ...";
111 }
112
113 /***
114 * The condition checked to determine the 'hop' value
115 */
116 private Test condition;
117
118 /***
119 * The number of action to skip in case of false condition. In case of true
120 * condition, 'hop' is equal to 1. The complex expression to use for the right
121 * value. Is null if the IdToken number is not a complex expression.
122 */
123 private Expression valueExpr = null;
124
125 }