1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.deckbuilder;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import net.sf.magicproject.clickable.targetable.card.SystemCard;
25 import net.sf.magicproject.test.DeckCounter;
26 import net.sf.magicproject.test.Test;
27 import net.sf.magicproject.token.IdConst;
28 import net.sf.magicproject.ui.i18n.LanguageManager;
29 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
30
31 /***
32 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
33 * @since 0.94
34 */
35 public class DeckConstraint implements Comparable<DeckConstraint> {
36
37 private final static String DECK_CONSTRAINT_KEY_NAME = "deck.constraint.";
38
39 private final Test constraint;
40
41 private final String name;
42
43 /***
44 * Create a new instance of this class.
45 *
46 * @param constraint
47 * the constraint validation test.
48 * @param name
49 * the constraint key name.
50 */
51 public DeckConstraint(String name, Test constraint) {
52 this.constraint = constraint;
53 this.name = name;
54 }
55
56 /***
57 * Return the translated constraint's name.
58 *
59 * @return the translated constraint's name.
60 */
61 public String getConstraintLocalName() {
62 return LanguageManagerMDB.getString(DECK_CONSTRAINT_KEY_NAME + name);
63 }
64
65 @Override
66 public String toString() {
67 return getConstraintLocalName();
68 }
69
70 /***
71 * Return the constraint name.
72 *
73 * @return the constraint name.
74 * @see #getConstraintLocalName()
75 */
76 public String getName() {
77 return name;
78 }
79
80 /***
81 * Return the constraint.
82 *
83 * @return the constraint.
84 */
85 public Test getConstraint() {
86 return constraint;
87 }
88
89 /***
90 * Validate the given deck.
91 *
92 * @param deck
93 * the given deck.
94 * @return a list of errors.
95 */
96 public List<String> validate(Deck deck) {
97 Deck.currentDeck = deck;
98 DeckCounter.lastRanCard = null;
99 final List<String> result = new ArrayList<String>();
100 final boolean testResult = constraint.test(null, null);
101 if (!testResult) {
102 if (DeckCounter.lastRanCard == null) {
103
104 result.add(LanguageManager
105 .getString("wiz_network.deck.constraint.error.global"));
106 } else if (DeckCounter.lastRanInstance.getThreshold().getValue(null,
107 SystemCard.instance, null) == IdConst.ALL) {
108 result.add(LanguageManager.getString(
109 "wiz_network.deck.constraint.error.forbidden",
110 DeckCounter.lastRanCard.getName()));
111 } else {
112 result.add(LanguageManager.getString(
113 "wiz_network.deck.constraint.error.arround",
114 DeckCounter.lastRanCard.getName(), String
115 .valueOf(DeckCounter.lastRanInstance.getThreshold().getValue(
116 null, SystemCard.instance, null))));
117 }
118 }
119 return result;
120 }
121
122 public int compareTo(DeckConstraint o) {
123 if (DeckConstraints.DECK_CONSTRAINT_NAME_NONE.equals(getName())) {
124 return Integer.MIN_VALUE;
125 }
126 if (DeckConstraints.DECK_CONSTRAINT_NAME_NONE.equals(o.getName())) {
127 return Integer.MAX_VALUE;
128 }
129 return getConstraintLocalName().compareTo(o.getConstraintLocalName());
130 }
131 }