(refactor) break out assignment into its own package

This commit is contained in:
theelk801 2024-06-18 13:44:11 -04:00
parent 675639013a
commit bac4e7b507
26 changed files with 30 additions and 34 deletions

View file

@ -0,0 +1,98 @@
package mage.abilities.assignment;
import mage.cards.Card;
import mage.cards.Cards;
import mage.game.Game;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
/**
* Used for when you need to know how attributes are assigned among cards, such as for party count
* Can be adapted for various card attributes
*
* @author TheElk801
*/
public abstract class RoleAssignment<T> implements Serializable {
protected final List<T> attributes = new ArrayList<>();
protected RoleAssignment(T... attributes) {
for (T attribute : attributes) {
this.attributes.add(attribute);
}
}
protected abstract Set<T> makeSet(Card card, Game game);
private boolean attemptRearrange(T attribute, UUID uuid, Set<T> attributes, Map<T, UUID> attributeUUIDMap, Map<UUID, Set<T>> attributeSetMap) {
UUID uuid1 = attributeUUIDMap.get(attribute);
if (uuid1 == null) {
return false;
}
Set<T> attributes1 = attributeSetMap.get(uuid1);
for (T attribute1 : attributes1) {
if (attribute == attribute1) {
continue;
}
if (!attributeUUIDMap.containsKey(attribute1)) {
attributeUUIDMap.put(attribute, uuid);
attributeUUIDMap.put(attribute1, uuid1);
return true;
}
}
for (T attribute1 : attributes1) {
if (attribute == attribute1) {
continue;
}
if (attemptRearrange(attribute1, uuid1, attributes, attributeUUIDMap, attributeSetMap)) {
attributeUUIDMap.put(attribute, uuid);
attributeUUIDMap.put(attribute1, uuid1);
return true;
}
}
return false;
}
public int getRoleCount(Cards cards, Game game) {
return getRoleCount(cards.getCards(game), game);
}
public int getRoleCount(Set<? extends Card> cards, Game game) {
Map<UUID, Set<T>> attributeMap = new HashMap<>();
cards.forEach(card -> attributeMap.put(card.getId(), this.makeSet(card, game)));
if (attributeMap.size() < 2) {
return attributeMap.size();
}
Set<T> availableTypes = attributeMap
.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());
if (attributeMap.size() == 2) {
return Math.min(2, availableTypes.size());
}
Map<T, UUID> attributeUUIDMap = new HashMap<>();
for (Map.Entry<UUID, Set<T>> entry : attributeMap.entrySet()) {
for (T attribute : entry.getValue()) {
if (!attributeUUIDMap.containsKey(attribute)) {
attributeUUIDMap.put(attribute, entry.getKey());
break;
}
}
if (attributeUUIDMap.size() >= availableTypes.size()) {
return attributeUUIDMap.size();
} else if (attributeUUIDMap.containsValue(entry.getKey())) {
continue;
} else {
for (T attribute : entry.getValue()) {
if (attemptRearrange(attribute, entry.getKey(), entry.getValue(), attributeUUIDMap, attributeMap)) {
break;
}
}
}
}
return attributeUUIDMap.keySet().size();
}
}

View file

@ -0,0 +1,24 @@
package mage.abilities.assignment.common;
import mage.abilities.assignment.RoleAssignment;
import mage.cards.Card;
import mage.constants.CardType;
import mage.game.Game;
import java.util.Set;
import java.util.stream.Collectors;
public class CardTypeAssignment extends RoleAssignment<CardType> {
public CardTypeAssignment(CardType... subTypes) {
super(subTypes);
}
@Override
protected Set<CardType> makeSet(Card card, Game game) {
return attributes
.stream()
.filter(subType -> card.getCardType(game).contains(subType))
.collect(Collectors.toSet());
}
}

View file

@ -0,0 +1,23 @@
package mage.abilities.assignment.common;
import mage.abilities.assignment.RoleAssignment;
import mage.cards.Card;
import mage.game.Game;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class ColorAssignment extends RoleAssignment<String> {
public ColorAssignment(String... colors) {
super(colors);
}
@Override
protected Set<String> makeSet(Card card, Game game) {
return Arrays.stream(card.getColor(game).toString().split(""))
.filter(attributes::contains)
.collect(Collectors.toSet());
}
}

View file

@ -0,0 +1,24 @@
package mage.abilities.assignment.common;
import mage.abilities.assignment.RoleAssignment;
import mage.cards.Card;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import java.util.Set;
import java.util.stream.Collectors;
public class PredicateCardAssignment extends RoleAssignment<Predicate<? super Card>> {
public PredicateCardAssignment(Predicate<? super Card>... predicates) {
super(predicates);
}
@Override
protected Set<Predicate<? super Card>> makeSet(Card card, Game game) {
return attributes
.stream()
.filter(predicate -> predicate.apply(card, game))
.collect(Collectors.toSet());
}
}

View file

@ -0,0 +1,24 @@
package mage.abilities.assignment.common;
import mage.abilities.assignment.RoleAssignment;
import mage.cards.Card;
import mage.constants.SubType;
import mage.game.Game;
import java.util.Set;
import java.util.stream.Collectors;
public class SubTypeAssignment extends RoleAssignment<SubType> {
public SubTypeAssignment(SubType... subTypes) {
super(subTypes);
}
@Override
protected Set<SubType> makeSet(Card card, Game game) {
return attributes
.stream()
.filter(subType -> card.hasSubtype(subType, game))
.collect(Collectors.toSet());
}
}