mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
Guava migration - Replace select usages of Aggregates methods
This commit is contained in:
@@ -4,6 +4,7 @@ import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import forge.ai.simulation.GameStateEvaluator;
|
||||
import forge.card.mana.ManaCost;
|
||||
@@ -81,12 +82,11 @@ public class ComputerUtilCard {
|
||||
* @return a {@link forge.game.card.Card} object.
|
||||
*/
|
||||
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
|
||||
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
|
||||
*/
|
||||
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
|
||||
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
|
||||
*/
|
||||
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
|
||||
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) {
|
||||
@@ -182,13 +180,13 @@ public class ComputerUtilCard {
|
||||
* @return a {@link forge.game.card.Card} object.
|
||||
*/
|
||||
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) {
|
||||
all = CardLists.filter(all, c -> c.canBeTargetedBy(spell));
|
||||
cardStream = cardStream.filter(c -> c.canBeTargetedBy(spell));
|
||||
}
|
||||
|
||||
// get biggest Enchantment
|
||||
return Aggregates.itemWithMax(all, Card::getCMC);
|
||||
return cardStream.max(Comparator.comparing(Card::getCMC)).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -380,7 +380,10 @@ public class CountersPutAi extends CountersAi {
|
||||
sa.setXManaCostPaid(amount);
|
||||
} else if ("ExiledCreatureFromGraveCMC".equals(logic)) {
|
||||
// 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)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -168,8 +168,10 @@ public class DamageDealAi extends DamageAiBase {
|
||||
}
|
||||
} 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
|
||||
List<Card> wolves = CardLists.getValidCards(ai.getCardsIn(ZoneType.Battlefield), "Creature.Wolf+untapped+YouCtrl+Other", ai, source, sa);
|
||||
dmg = Aggregates.sum(wolves, Card::getNetPower);
|
||||
dmg = ai.getCardsIn(ZoneType.Battlefield).stream()
|
||||
.filter(CardPredicates.restriction("Creature.Wolf+untapped+YouCtrl+Other", ai, source, sa))
|
||||
.mapToInt(Card::getNetPower)
|
||||
.sum();
|
||||
} else if ("Triskelion".equals(logic)) {
|
||||
final int n = source.getCounters(CounterEnumType.P1P1);
|
||||
if (n > 0) {
|
||||
|
||||
@@ -2887,14 +2887,18 @@ public class AbilityUtils {
|
||||
// TODO move below to handlePaid
|
||||
if (sq[0].startsWith("SumPower")) {
|
||||
final String[] restrictions = l[0].split("_");
|
||||
CardCollection filteredCards = CardLists.getValidCards(game.getCardsIn(ZoneType.Battlefield), restrictions[1], player, c, ctb);
|
||||
return doXMath(Aggregates.sum(filteredCards, Card::getNetPower), expr, c, ctb);
|
||||
int sumPower = game.getCardsIn(ZoneType.Battlefield).stream()
|
||||
.filter(CardPredicates.restriction(restrictions[1], player, c, ctb))
|
||||
.mapToInt(Card::getNetPower).sum();
|
||||
return doXMath(sumPower, expr, c, ctb);
|
||||
}
|
||||
if (sq[0].startsWith("DifferentPower_")) {
|
||||
final String restriction = l[0].substring(15);
|
||||
CardCollection list = CardLists.getValidCards(game.getCardsIn(ZoneType.Battlefield), restriction, player, c, ctb);
|
||||
final Iterable<Card> powers = Aggregates.uniqueByLast(list, Card::getNetPower);
|
||||
return doXMath(Iterables.size(powers), expr, c, ctb);
|
||||
final int uniquePowers = (int) game.getCardsIn(ZoneType.Battlefield).stream()
|
||||
.filter(CardPredicates.restriction(restriction, player, c, ctb))
|
||||
.map(Card::getNetPower)
|
||||
.distinct().count();
|
||||
return doXMath(uniquePowers, expr, c, ctb);
|
||||
}
|
||||
if (sq[0].startsWith("DifferentCounterKinds_")) {
|
||||
final Set<CounterType> kinds = Sets.newHashSet();
|
||||
|
||||
@@ -67,6 +67,7 @@ public class PlayerCollection extends FCollection<Player> {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return Aggregates.min(this, func);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user