Guava migration - Replace select usages of Aggregates methods

This commit is contained in:
Jetz
2024-09-22 15:56:48 -04:00
parent e739dad1ba
commit cbcfa8f884
5 changed files with 34 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream;
import forge.ai.simulation.GameStateEvaluator; import forge.ai.simulation.GameStateEvaluator;
import forge.card.mana.ManaCost; import forge.card.mana.ManaCost;
@@ -81,12 +82,11 @@ public class ComputerUtilCard {
* @return a {@link forge.game.card.Card} object. * @return a {@link forge.game.card.Card} object.
*/ */
public static Card getBestArtifactAI(final List<Card> list) { public static Card getBestArtifactAI(final List<Card> list) {
List<Card> all = CardLists.filter(list, CardPredicates.ARTIFACTS);
if (all.size() == 0) {
return null;
}
// get biggest Artifact // get biggest Artifact
return Aggregates.itemWithMax(all, Card::getCMC); return list.stream()
.filter(CardPredicates.ARTIFACTS)
.max(Comparator.comparing(Card::getCMC))
.orElse(null);
} }
/** /**
@@ -96,12 +96,11 @@ public class ComputerUtilCard {
* @return best Planeswalker * @return best Planeswalker
*/ */
public static Card getBestPlaneswalkerAI(final List<Card> list) { public static Card getBestPlaneswalkerAI(final List<Card> list) {
List<Card> all = CardLists.filter(list, CardPredicates.PLANESWALKERS);
if (all.isEmpty()) {
return null;
}
// no AI logic, just return most expensive // no AI logic, just return most expensive
return Aggregates.itemWithMax(all, Card::getCMC); return list.stream()
.filter(CardPredicates.PLANESWALKERS)
.max(Comparator.comparing(Card::getCMC))
.orElse(null);
} }
/** /**
@@ -111,12 +110,11 @@ public class ComputerUtilCard {
* @return best Planeswalker * @return best Planeswalker
*/ */
public static Card getWorstPlaneswalkerAI(final List<Card> list) { public static Card getWorstPlaneswalkerAI(final List<Card> list) {
List<Card> all = CardLists.filter(list, CardPredicates.PLANESWALKERS);
if (all.isEmpty()) {
return null;
}
// no AI logic, just return least expensive // no AI logic, just return least expensive
return Aggregates.itemWithMin(all, Card::getCMC); return list.stream()
.filter(CardPredicates.PLANESWALKERS)
.min(Comparator.comparing(Card::getCMC))
.orElse(null);
} }
public static Card getBestPlaneswalkerToDamage(final List<Card> pws) { public static Card getBestPlaneswalkerToDamage(final List<Card> pws) {
@@ -182,13 +180,13 @@ public class ComputerUtilCard {
* @return a {@link forge.game.card.Card} object. * @return a {@link forge.game.card.Card} object.
*/ */
public static Card getBestEnchantmentAI(final List<Card> list, final SpellAbility spell, final boolean targeted) { public static Card getBestEnchantmentAI(final List<Card> list, final SpellAbility spell, final boolean targeted) {
List<Card> all = CardLists.filter(list, CardPredicates.ENCHANTMENTS); Stream<Card> cardStream = list.stream().filter(CardPredicates.ENCHANTMENTS);
if (targeted) { if (targeted) {
all = CardLists.filter(all, c -> c.canBeTargetedBy(spell)); cardStream = cardStream.filter(c -> c.canBeTargetedBy(spell));
} }
// get biggest Enchantment // get biggest Enchantment
return Aggregates.itemWithMax(all, Card::getCMC); return cardStream.max(Comparator.comparing(Card::getCMC)).orElse(null);
} }
/** /**

View File

@@ -380,7 +380,10 @@ public class CountersPutAi extends CountersAi {
sa.setXManaCostPaid(amount); sa.setXManaCostPaid(amount);
} else if ("ExiledCreatureFromGraveCMC".equals(logic)) { } else if ("ExiledCreatureFromGraveCMC".equals(logic)) {
// e.g. Necropolis // e.g. Necropolis
amount = Aggregates.max(CardLists.filter(ai.getCardsIn(ZoneType.Graveyard), CardPredicates.CREATURES), Card::getCMC); amount = ai.getCardsIn(ZoneType.Graveyard).stream()
.filter(CardPredicates.CREATURES)
.mapToInt(Card::getCMC)
.max().orElse(0);
if (amount > 0 && ai.getGame().getPhaseHandler().is(PhaseType.END_OF_TURN)) { if (amount > 0 && ai.getGame().getPhaseHandler().is(PhaseType.END_OF_TURN)) {
return true; return true;
} }

View File

@@ -168,8 +168,10 @@ public class DamageDealAi extends DamageAiBase {
} }
} else if ("WildHunt".equals(logic)) { } else if ("WildHunt".equals(logic)) {
// This dummy ability will just deal 0 damage, but holds the logic for the AI for Master of Wild Hunt // This dummy ability will just deal 0 damage, but holds the logic for the AI for Master of Wild Hunt
List<Card> wolves = CardLists.getValidCards(ai.getCardsIn(ZoneType.Battlefield), "Creature.Wolf+untapped+YouCtrl+Other", ai, source, sa); dmg = ai.getCardsIn(ZoneType.Battlefield).stream()
dmg = Aggregates.sum(wolves, Card::getNetPower); .filter(CardPredicates.restriction("Creature.Wolf+untapped+YouCtrl+Other", ai, source, sa))
.mapToInt(Card::getNetPower)
.sum();
} else if ("Triskelion".equals(logic)) { } else if ("Triskelion".equals(logic)) {
final int n = source.getCounters(CounterEnumType.P1P1); final int n = source.getCounters(CounterEnumType.P1P1);
if (n > 0) { if (n > 0) {

View File

@@ -2887,14 +2887,18 @@ public class AbilityUtils {
// TODO move below to handlePaid // TODO move below to handlePaid
if (sq[0].startsWith("SumPower")) { if (sq[0].startsWith("SumPower")) {
final String[] restrictions = l[0].split("_"); final String[] restrictions = l[0].split("_");
CardCollection filteredCards = CardLists.getValidCards(game.getCardsIn(ZoneType.Battlefield), restrictions[1], player, c, ctb); int sumPower = game.getCardsIn(ZoneType.Battlefield).stream()
return doXMath(Aggregates.sum(filteredCards, Card::getNetPower), expr, c, ctb); .filter(CardPredicates.restriction(restrictions[1], player, c, ctb))
.mapToInt(Card::getNetPower).sum();
return doXMath(sumPower, expr, c, ctb);
} }
if (sq[0].startsWith("DifferentPower_")) { if (sq[0].startsWith("DifferentPower_")) {
final String restriction = l[0].substring(15); final String restriction = l[0].substring(15);
CardCollection list = CardLists.getValidCards(game.getCardsIn(ZoneType.Battlefield), restriction, player, c, ctb); final int uniquePowers = (int) game.getCardsIn(ZoneType.Battlefield).stream()
final Iterable<Card> powers = Aggregates.uniqueByLast(list, Card::getNetPower); .filter(CardPredicates.restriction(restriction, player, c, ctb))
return doXMath(Iterables.size(powers), expr, c, ctb); .map(Card::getNetPower)
.distinct().count();
return doXMath(uniquePowers, expr, c, ctb);
} }
if (sq[0].startsWith("DifferentCounterKinds_")) { if (sq[0].startsWith("DifferentCounterKinds_")) {
final Set<CounterType> kinds = Sets.newHashSet(); final Set<CounterType> kinds = Sets.newHashSet();

View File

@@ -67,6 +67,7 @@ public class PlayerCollection extends FCollection<Player> {
} }
// value functions with Function // value functions with Function
//TODO: Could probably move these up to FCollectionView, apply them, and trim off a bunch of "Aggregates" clauses.
public Integer min(Function<Player, Integer> func) { public Integer min(Function<Player, Integer> func) {
return Aggregates.min(this, func); return Aggregates.min(this, func);
} }