1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.xml.action;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26 import net.sf.magicproject.action.Actiontype;
27 import net.sf.magicproject.operation.IdOperations;
28 import net.sf.magicproject.test.TestOn;
29 import net.sf.magicproject.token.IdCommonToken;
30 import net.sf.magicproject.token.IdTokens;
31 import net.sf.magicproject.tools.MToolKit;
32 import net.sf.magicproject.xml.XmlAction;
33 import net.sf.magicproject.xml.XmlParser;
34 import net.sf.magicproject.xml.XmlTest;
35 import net.sf.magicproject.xml.XmlToMDB;
36 import net.sf.magicproject.xml.XmlTools;
37
38 /***
39 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40 * @since 0.82
41 * @since 0.86 unique color give and restriction usage are supported
42 */
43 public class Givemana implements XmlToMDB {
44
45 /***
46 * <ul>
47 * Structure of stream : Data[size]
48 * <li>idAction GIVE_MANA_BASIC/GIVE_MANA_MULTI[1]</li>
49 * <li>restriction usage test [...]</li>
50 * <li>player receiving this mana [TestOn]</li>
51 * <li>For GIVE_MANA_BASIC option : mana color to give : idNumber [2]</li>
52 * <li>For GIVE_MANA_BASIC option : mana amount to give : expression [...]</li>
53 * <li>For GIVE_MANA_MULTI option : COLORLESS : expression [...] or
54 * (IdTokens#REGISTERS)[2]</li>
55 * <li>For GIVE_MANA_MULTI option : BLACK : expression[...]</li>
56 * <li>For GIVE_MANA_MULTI option : BLUE : expression[...]</li>
57 * <li>For GIVE_MANA_MULTI option : GREEN : expression[...]</li>
58 * <li>For GIVE_MANA_MULTI option : RED : expression[...]</li>
59 * <li>For GIVE_MANA_MULTI option : WHITE : expression[...]</li>
60 * </ul>
61 *
62 * @param node
63 * the XML action structure
64 * @param out
65 * outputstream where the card structure will be saved.
66 * @return the amount of written action in the output.
67 * @throws IOException
68 */
69 public int buildMdb(XmlParser.Node node, OutputStream out) throws IOException {
70
71 final String uniqueColor = node.getAttribute("color");
72 if (uniqueColor != null || node.get("color") != null)
73 XmlAction.buildMdb(Actiontype.GIVE_MANA_BASIC, node, out);
74 else
75 XmlAction.buildMdb(Actiontype.GIVE_MANA_MULTI, node, out);
76
77
78 XmlTest.getTest("test").buildMdb(node.get("restriction"), out);
79
80
81 if (node.getAttribute("to") == null) {
82 TestOn.CONTROLLER.serialize(out);
83 } else {
84 XmlTools.writeTestOn(out, node.getAttribute("to"));
85 }
86
87 if (uniqueColor != null || node.get("color") != null) {
88
89 XmlTools.writeAttrOptions(node, "color", out);
90 XmlTools.writeAttrOptions(node, "value", out);
91 } else {
92 final String braAttr = node.getAttribute("value");
93 if (braAttr != null) {
94 MToolKit.writeInt16(out, IdTokens.MANA_POOL);
95 XmlTools.writeTestOn(out, node.getAttribute("card"));
96 } else {
97 MToolKit.writeInt16(out, 0);
98 for (int i = 0; i < IdCommonToken.COLOR_NAMES.length; i++) {
99 if (node.getAttribute(IdCommonToken.COLOR_NAMES[i]) == null
100 && node.get(IdCommonToken.COLOR_NAMES[i]) == null) {
101
102 out.write(IdOperations.INT_VALUE);
103 MToolKit.writeInt16(out, 0);
104 } else {
105 XmlTools.writeAttrOptions(node, IdCommonToken.COLOR_NAMES[i], out);
106 }
107 }
108 }
109 }
110 return 1;
111 }
112
113 }