View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 				// Global error
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 }