1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.firemox.deckbuilder;
20
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26
27 import net.sf.firemox.expression.ExpressionFactory;
28 import net.sf.firemox.test.And;
29 import net.sf.firemox.test.Test;
30 import net.sf.firemox.test.TestFactory;
31 import net.sf.firemox.test.True;
32 import net.sf.firemox.tools.MToolKit;
33
34 /***
35 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36 * @since 0.94
37 */
38 public class DeckConstraints {
39
40 /***
41 * The null deck constraint key name.
42 */
43 public static final String DECK_CONSTRAINT_NAME_NONE = "none";
44
45 /***
46 * The defined deck constraints.
47 */
48 private static final SortedSet<DeckConstraint> CONSTRAINTS = new TreeSet<DeckConstraint>();
49
50 private static int minProperty;
51
52 private static int maxProperty;
53
54 /***
55 * MASTER property for the game. Example: MTG -> Changeling
56 */
57 public static int MASTER;
58
59 /***
60 * Singleton constructor.
61 */
62 private DeckConstraints() {
63
64 }
65
66 /***
67 * Return the constraint associated to the given name.
68 *
69 * @param deckConstraint
70 * the constraint name.
71 * @return the constraint associated to the given name.
72 */
73 public static DeckConstraint getDeckConstraint(String deckConstraint) {
74 for (DeckConstraint constraint : CONSTRAINTS) {
75 if (constraint.getName().equalsIgnoreCase(deckConstraint))
76 return constraint;
77 }
78 return null;
79 }
80
81 /***
82 * Return the available constraints.
83 *
84 * @return the available constraints.
85 */
86 public static Collection<DeckConstraint> getDeckConstraints() {
87 return CONSTRAINTS;
88 }
89
90 /***
91 * Initialize the deck constraints.
92 *
93 * @param dbStream
94 * @throws IOException
95 * If some other I/O error occurs
96 */
97 public static void init(FileInputStream dbStream) throws IOException {
98
99 CONSTRAINTS.clear();
100 CONSTRAINTS.add(new DeckConstraint(DECK_CONSTRAINT_NAME_NONE, True
101 .getInstance()));
102
103
104 minProperty = ExpressionFactory.readNextExpression(dbStream).getValue(null,
105 null, null);
106 maxProperty = ExpressionFactory.readNextExpression(dbStream).getValue(null,
107 null, null);
108 MASTER = ExpressionFactory.readNextExpression(dbStream).getValue(null,
109 null, null);
110 final int count = dbStream.read();
111 for (int i = count; i-- > 0;) {
112
113 String deckName = MToolKit.readString(dbStream);
114
115
116 String extend = MToolKit.readString(dbStream);
117
118
119 Test constraint = TestFactory.readNextTest(dbStream);
120
121
122 if (extend.length() > 0) {
123 DeckConstraint extendConstraint = getDeckConstraint(extend);
124 if (extendConstraint == null) {
125 throw new RuntimeException(
126 "'"
127 + deckName
128 + "' is supposed extending '"
129 + extend
130 + "' but has not been found. Note that declaration order is important.");
131 }
132 constraint = And.append(getDeckConstraint(extend).getConstraint(),
133 constraint);
134 }
135 CONSTRAINTS.add(new DeckConstraint(deckName, constraint));
136 }
137 }
138
139 /***
140 * Return
141 *
142 * @return Returns the minProperty.
143 */
144 public static int getMinProperty() {
145 return minProperty;
146 }
147
148 /***
149 * Return
150 *
151 * @return Returns the maxProperty.
152 */
153 public static int getMaxProperty() {
154 return maxProperty;
155 }
156 }