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.tbs;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.util.Iterator;
26
27 import net.sf.magicproject.clickable.ability.AbilityType;
28 import net.sf.magicproject.clickable.ability.Optimization;
29 import net.sf.magicproject.clickable.ability.Priority;
30 import net.sf.magicproject.token.TrueFalseAuto;
31 import net.sf.magicproject.tools.MToolKit;
32 import net.sf.magicproject.xml.XmlEvent;
33 import net.sf.magicproject.xml.XmlParser;
34 import net.sf.magicproject.xml.XmlTbs;
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 */
42 public class Replacementability implements XmlToMDB {
43
44 /***
45 * <ul>
46 * Structure of stream : Data[size]
47 * <li>ability type [1]</li>
48 * <li>name name + '\0' [...]</li>
49 * <li>ability tags = hidden+unique [1]</li>
50 * <li>event [...]</li>
51 * <li>nb of actions for effect part [1]</li>
52 * <li>pay action i [...]</li>
53 * </ul>
54 *
55 * @param node
56 * the XML ability structure
57 * @param out
58 * outputstream where the card structure will be saved
59 * @return the amount of written action in the output.
60 * @see net.sf.magicproject.clickable.ability.TriggeredAbility
61 * @see net.sf.magicproject.clickable.ability.ReplacementAbility
62 * @throws IOException
63 * error while writting.
64 */
65 public final int buildMdb(XmlParser.Node node, OutputStream out)
66 throws IOException {
67
68 AbilityType.REPLACEMENT_ABILITY.write(out);
69
70
71 MToolKit.writeString(out, node.getAttribute("name"));
72
73
74 String layer = node.getAttribute("layer");
75 if (layer == null) {
76 Priority.high.write(out);
77 } else {
78 Priority.values()[Integer.parseInt(layer)].write(out);
79 }
80
81 if (Optimization.action.equals(node.getAttribute("optimize"))) {
82 Optimization.action.write(out);
83 } else {
84 Optimization.first.write(out);
85 }
86 TrueFalseAuto.valueOfXsd(node.getAttribute("play-as-spell")).serialize(out);
87
88
89 Iterator<?> it = node.iterator();
90 XmlTools.defaultOnMeTag = false;
91 while (it.hasNext()) {
92 Object obj = it.next();
93 if (obj instanceof XmlParser.Node) {
94 XmlParser.Node child = (XmlParser.Node) obj;
95 if ("text".equals(child.getTag())) {
96 System.out.println("text element in ability is not yet implement");
97 } else {
98 XmlEvent.getEvent(child.getTag()).buildMdb(child, out);
99 break;
100 }
101 }
102 }
103
104
105 out.write(0);
106
107 XmlTbs.currentInEffect = true;
108 XmlTbs.writeActionList(node.get("effects"), out);
109 return 0;
110 }
111
112 }