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.clickable.ability.Ability;
26 import net.sf.magicproject.event.context.ContextEventListener;
27 import net.sf.magicproject.expression.Expression;
28 import net.sf.magicproject.expression.ExpressionFactory;
29 import net.sf.magicproject.tools.MToolKit;
30 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
31
32 /***
33 * It's the mana source action, modify directly the mana pool like : Land, other
34 * mana sources. Only one color is added.
35 *
36 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37 * @since 0.86
38 */
39 class GiveManaBasic extends GiveMana {
40
41 /***
42 * Create an instance of GiveManaBasic by reading a file Offset's file must
43 * pointing on the first byte of this action <br>
44 * <ul>
45 * Structure of stream : Data[size]
46 * <li>restriction usage test [...]</li>
47 * <li>idPlayer receiving this mana [2]</li>
48 * <li>mana color to give : expression [...]</li>
49 * <li>mana amount to give : expression [...]</li>
50 * </ul>
51 *
52 * @param inputFile
53 * file containing this action
54 * @throws IOException
55 * if error occurred during the reading process from the specified
56 * input stream
57 */
58 GiveManaBasic(InputStream inputFile) throws IOException {
59 super(inputFile);
60 this.manaColor = ExpressionFactory.readNextExpression(inputFile);
61 this.valueExpr = ExpressionFactory.readNextExpression(inputFile);
62 }
63
64 @Override
65 public boolean play(ContextEventListener context, Ability ability) {
66 giveMana(ability, context, manaColor.getValue(ability, null, context),
67 valueExpr.getValue(ability, null, context));
68 return false;
69 }
70
71 @Override
72 public String toHtmlString(Ability ability, ContextEventListener context) {
73 String res = null;
74 final int realCode = valueExpr.getValue(ability, null, context);
75 final int color = manaColor.getValue(ability, null, context);
76 if (color <= 0) {
77 if (realCode > 0) {
78 res = LanguageManagerMDB.getString("add-mana")
79 + MToolKit.getHtmlMana(0, realCode);
80 }
81 } else if (realCode > 0) {
82 res = LanguageManagerMDB.getString("add-mana");
83 if (realCode > PayMana.thresholdColored) {
84 res += MToolKit.getHtmlMana(color, 1) + "x" + realCode;
85 } else {
86 res += MToolKit.getHtmlMana(color, realCode);
87 }
88 }
89 if (res == null) {
90 return LanguageManagerMDB.getString("add-mana")
91 + MToolKit.getHtmlMana(0, 0);
92 }
93 return res;
94 }
95
96 @Override
97 public String toString(Ability ability) {
98 String res = null;
99 final int realCode = valueExpr.getValue(ability, null, null);
100 final int color = manaColor.getValue(ability, null, null);
101 if (color == 0) {
102 if (realCode > 0) {
103 res = "Add " + realCode;
104 }
105 } else if (color < 0) {
106 res = "Add ?? x" + realCode + ",";
107 } else if (realCode > 0) {
108 res = "Add color" + color + "x" + realCode + ",";
109 }
110 if (res == null) {
111 return "Add 0";
112 }
113 return res;
114 }
115
116 @Override
117 public final Actiontype getIdAction() {
118 return Actiontype.GIVE_MANA_BASIC;
119 }
120
121 /***
122 * The mana's color
123 */
124 private Expression manaColor;
125
126 /***
127 * represent the amount of mana to add to the mana pool.
128 */
129 private Expression valueExpr = null;
130
131 }