Move creature type sorting to gui controller code

This commit is contained in:
drdev
2015-08-04 01:28:49 +00:00
parent 0a40ff50ee
commit 6140367122
2 changed files with 36 additions and 42 deletions

View File

@@ -1,24 +1,15 @@
package forge.game.ability.effects;
import forge.card.CardType;
import forge.deck.CardPool;
import forge.game.ability.SpellAbilityEffect;
import forge.game.card.Card;
import forge.game.player.Player;
import forge.game.spellability.SpellAbility;
import forge.game.spellability.TargetRestrictions;
import forge.item.PaperCard;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class ChooseTypeEffect extends SpellAbilityEffect {
@@ -52,7 +43,6 @@ public class ChooseTypeEffect extends SpellAbilityEffect {
break;
case "Creature":
validTypes.addAll(CardType.getAllCreatureTypes());
sortCreatureTypes(validTypes, sa);
break;
case "Basic Land":
validTypes.addAll(CardType.getBasicTypes());
@@ -82,36 +72,4 @@ public class ChooseTypeEffect extends SpellAbilityEffect {
throw new InvalidParameterException(sa.getHostCard() + "'s ability resulted in no types to choose from");
}
}
//sort creature types such that the types most prevalent
//in the activating player's deck are sorted to the top
private void sortCreatureTypes(List<String> validTypes, SpellAbility sa) {
//build map of creature types in player's main deck against the occurrences of each
CardPool pool = sa.getActivatingPlayer().getRegisteredPlayer().getDeck().getMain();
HashMap<String, Integer> typesInDeck = new HashMap<String, Integer>();
for (Entry<PaperCard, Integer> entry : pool) {
Set<String> cardCreatureTypes = entry.getKey().getRules().getType().getCreatureTypes();
for (String type : cardCreatureTypes) {
Integer count = typesInDeck.get(type);
if (count == null) { count = 0; }
typesInDeck.put(type, count + entry.getValue());
}
}
//create sorted list from map from least to most frequent
List<Entry<String, Integer>> sortedList = new LinkedList<Entry<String, Integer>>(typesInDeck.entrySet());
Collections.sort(sortedList, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
//loop through sorted list and move each type to the front of the validTypes collection
for (Entry<String, Integer> entry : sortedList) {
String type = entry.getKey();
if (validTypes.remove(type)) { //ensure an invalid type isn't introduced
validTypes.add(0, type);
}
}
}
}

View File

@@ -9,8 +9,10 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -795,12 +797,46 @@ public class PlayerControllerHuman
if (invalidTypes != null && !invalidTypes.isEmpty()) {
Iterables.removeAll(types, invalidTypes);
}
if (kindOfType.equals("Creature")) {
sortCreatureTypes(types);
}
if (isOptional) {
return getGui().oneOrNone("Choose a " + kindOfType.toLowerCase() + " type", types);
}
return getGui().one("Choose a " + kindOfType.toLowerCase() + " type", types);
}
//sort creature types such that those most prevalent in player's deck are sorted to the top
private void sortCreatureTypes(List<String> types) {
//build map of creature types in player's main deck against the occurrences of each
CardPool pool = player.getRegisteredPlayer().getDeck().getMain();
HashMap<String, Integer> typesInDeck = new HashMap<String, Integer>();
for (Entry<PaperCard, Integer> entry : pool) {
Set<String> cardCreatureTypes = entry.getKey().getRules().getType().getCreatureTypes();
for (String type : cardCreatureTypes) {
Integer count = typesInDeck.get(type);
if (count == null) { count = 0; }
typesInDeck.put(type, count + entry.getValue());
}
}
//create sorted list from map from least to most frequent
List<Entry<String, Integer>> sortedList = new LinkedList<Entry<String, Integer>>(typesInDeck.entrySet());
Collections.sort(sortedList, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
//loop through sorted list and move each type to the front of the validTypes collection
for (Entry<String, Integer> entry : sortedList) {
String type = entry.getKey();
if (types.remove(type)) { //ensure an invalid type isn't introduced
types.add(0, type);
}
}
}
@Override
public Object vote(final SpellAbility sa, final String prompt, final List<Object> options, final ListMultimap<Object, Player> votes) {
return getGui().one(prompt, options);