mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Make it easier to select creature types by sorting the most prevalent creature types in your deck to the top
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
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 {
|
||||
|
||||
@@ -44,6 +52,7 @@ public class ChooseTypeEffect extends SpellAbilityEffect {
|
||||
break;
|
||||
case "Creature":
|
||||
validTypes.addAll(CardType.getAllCreatureTypes());
|
||||
sortCreatureTypes(validTypes, sa);
|
||||
break;
|
||||
case "Basic Land":
|
||||
validTypes.addAll(CardType.getBasicTypes());
|
||||
@@ -74,4 +83,34 @@ public class ChooseTypeEffect extends SpellAbilityEffect {
|
||||
}
|
||||
}
|
||||
|
||||
//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();
|
||||
validTypes.remove(type);
|
||||
validTypes.add(0, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user