Consolidating memory resolving
This commit is contained in:
@@ -5,6 +5,7 @@ import com.gempukku.lotro.cards.build.field.effect.EffectAppender;
|
|||||||
import com.gempukku.lotro.cards.build.field.effect.appender.DelayedAppender;
|
import com.gempukku.lotro.cards.build.field.effect.appender.DelayedAppender;
|
||||||
import com.gempukku.lotro.common.Filterable;
|
import com.gempukku.lotro.common.Filterable;
|
||||||
import com.gempukku.lotro.common.Zone;
|
import com.gempukku.lotro.common.Zone;
|
||||||
|
import com.gempukku.lotro.filters.Filter;
|
||||||
import com.gempukku.lotro.filters.Filters;
|
import com.gempukku.lotro.filters.Filters;
|
||||||
import com.gempukku.lotro.game.PhysicalCard;
|
import com.gempukku.lotro.game.PhysicalCard;
|
||||||
import com.gempukku.lotro.game.state.LotroGame;
|
import com.gempukku.lotro.game.state.LotroGame;
|
||||||
@@ -26,6 +27,7 @@ import java.util.Collections;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class CardResolver {
|
public class CardResolver {
|
||||||
public static EffectAppender resolveStackedCards(String type, ValueSource countSource, FilterableSource stackedOn,
|
public static EffectAppender resolveStackedCards(String type, ValueSource countSource, FilterableSource stackedOn,
|
||||||
@@ -41,25 +43,14 @@ public class CardResolver {
|
|||||||
public static EffectAppender resolveStackedCards(String type, FilterableSource choiceFilter, FilterableSource playabilityFilter, ValueSource countSource, FilterableSource stackedOn,
|
public static EffectAppender resolveStackedCards(String type, FilterableSource choiceFilter, FilterableSource playabilityFilter, ValueSource countSource, FilterableSource stackedOn,
|
||||||
String memory, String choicePlayer, String choiceText, CardGenerationEnvironment environment) throws InvalidCardDefinitionException {
|
String memory, String choicePlayer, String choiceText, CardGenerationEnvironment environment) throws InvalidCardDefinitionException {
|
||||||
if (type.startsWith("memory(") && type.endsWith(")")) {
|
if (type.startsWith("memory(") && type.endsWith(")")) {
|
||||||
String sourceMemory = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
Function<ActionContext, Iterable<? extends PhysicalCard>> cardSource = new Function<ActionContext, Iterable<? extends PhysicalCard>>() {
|
||||||
if (sourceMemory.contains("(") || sourceMemory.contains(")"))
|
|
||||||
throw new InvalidCardDefinitionException("Memory name cannot contain parenthesis");
|
|
||||||
return new DelayedAppender() {
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPlayableInFull(ActionContext actionContext) {
|
public Iterable<? extends PhysicalCard> apply(ActionContext actionContext) {
|
||||||
return true;
|
final Filterable stackedOnFilter = stackedOn.getFilterable(actionContext);
|
||||||
}
|
return Filters.filterActive(actionContext.getGame(), stackedOnFilter);
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Effect createEffect(boolean cost, CostToEffectAction action, ActionContext actionContext) {
|
|
||||||
return new UnrespondableEffect() {
|
|
||||||
@Override
|
|
||||||
protected void doPlayEffect(LotroGame game) {
|
|
||||||
actionContext.setCardMemory(memory, actionContext.getCardsFromMemory(sourceMemory));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
return resolveMemoryCards(type, choiceFilter, playabilityFilter, countSource, memory, cardSource);
|
||||||
} else if (type.startsWith("choose(") && type.endsWith(")")) {
|
} else if (type.startsWith("choose(") && type.endsWith(")")) {
|
||||||
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
||||||
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
||||||
@@ -156,36 +147,15 @@ public class CardResolver {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else if (type.startsWith("memory(") && type.endsWith(")")) {
|
} else if (type.startsWith("memory(") && type.endsWith(")")) {
|
||||||
String sourceMemory = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
|
||||||
if (sourceMemory.contains("(") || sourceMemory.contains(")"))
|
|
||||||
throw new InvalidCardDefinitionException("Memory name cannot contain parenthesis");
|
|
||||||
|
|
||||||
final PlayerSource handSource = PlayerResolver.resolvePlayer(handPlayer, environment);
|
final PlayerSource handSource = PlayerResolver.resolvePlayer(handPlayer, environment);
|
||||||
|
Function<ActionContext, Iterable<? extends PhysicalCard>> cardSource = new Function<ActionContext, Iterable<? extends PhysicalCard>>() {
|
||||||
return new DelayedAppender() {
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPlayableInFull(ActionContext actionContext) {
|
public Iterable<? extends PhysicalCard> apply(ActionContext actionContext) {
|
||||||
if (additionalFilter != null) {
|
String handPlayer = handSource.getPlayer(actionContext);
|
||||||
int min = countSource.getMinimum(null);
|
return actionContext.getGame().getGameState().getHand(handPlayer);
|
||||||
String handId = handSource.getPlayer(actionContext);
|
|
||||||
final Collection<? extends PhysicalCard> cardsFromMemory = actionContext.getCardsFromMemory(sourceMemory);
|
|
||||||
Filterable filter = additionalFilter.getFilterable(actionContext);
|
|
||||||
final LotroGame game = actionContext.getGame();
|
|
||||||
return Filters.filter(game.getGameState().getHand(handId), game, filter, Filters.in(cardsFromMemory)).size() >= min;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Effect createEffect(boolean cost, CostToEffectAction action, ActionContext actionContext) {
|
|
||||||
return new UnrespondableEffect() {
|
|
||||||
@Override
|
|
||||||
protected void doPlayEffect(LotroGame game) {
|
|
||||||
actionContext.setCardMemory(memory, actionContext.getCardsFromMemory(sourceMemory));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
return resolveMemoryCards(type, additionalFilter, additionalFilter, countSource, memory, cardSource);
|
||||||
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
||||||
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
||||||
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
||||||
@@ -296,35 +266,13 @@ public class CardResolver {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else if (type.startsWith("memory(") && type.endsWith(")")) {
|
} else if (type.startsWith("memory(") && type.endsWith(")")) {
|
||||||
String sourceMemory = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
|
||||||
if (sourceMemory.contains("(") || sourceMemory.contains(")"))
|
|
||||||
throw new InvalidCardDefinitionException("Memory name cannot contain parenthesis");
|
|
||||||
|
|
||||||
final PlayerSource playerSource = PlayerResolver.resolvePlayer(choicePlayer, environment);
|
final PlayerSource playerSource = PlayerResolver.resolvePlayer(choicePlayer, environment);
|
||||||
|
|
||||||
return new DelayedAppender() {
|
Function<ActionContext, Iterable<? extends PhysicalCard>> cardSource = actionContext -> {
|
||||||
@Override
|
String choicePlayerId = playerSource.getPlayer(actionContext);
|
||||||
public boolean isPlayableInFull(ActionContext actionContext) {
|
return actionContext.getGame().getGameState().getDiscard(choicePlayerId);
|
||||||
int min = countSource.getMinimum(null);
|
|
||||||
String choicePlayerId = playerSource.getPlayer(actionContext);
|
|
||||||
final Collection<? extends PhysicalCard> cardsFromMemory = actionContext.getCardsFromMemory(sourceMemory);
|
|
||||||
Filterable additionalFilterable = Filters.any;
|
|
||||||
if (playabilityFilter != null)
|
|
||||||
additionalFilterable = playabilityFilter.getFilterable(actionContext);
|
|
||||||
final LotroGame game = actionContext.getGame();
|
|
||||||
return Filters.filter(game.getGameState().getDiscard(choicePlayerId), game, Filters.in(cardsFromMemory), additionalFilterable).size() >= min;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Effect createEffect(boolean cost, CostToEffectAction action, ActionContext actionContext) {
|
|
||||||
return new UnrespondableEffect() {
|
|
||||||
@Override
|
|
||||||
protected void doPlayEffect(LotroGame game) {
|
|
||||||
actionContext.setCardMemory(memory, actionContext.getCardsFromMemory(sourceMemory));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
return resolveMemoryCards(type, choiceFilter, playabilityFilter, countSource, memory, cardSource);
|
||||||
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
||||||
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
||||||
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
||||||
@@ -390,25 +338,12 @@ public class CardResolver {
|
|||||||
|
|
||||||
public static EffectAppender resolveCardsInDeck(String type, FilterableSource choiceFilter, ValueSource countSource, String memory, String choicePlayer, String choiceText, CardGenerationEnvironment environment) throws InvalidCardDefinitionException {
|
public static EffectAppender resolveCardsInDeck(String type, FilterableSource choiceFilter, ValueSource countSource, String memory, String choicePlayer, String choiceText, CardGenerationEnvironment environment) throws InvalidCardDefinitionException {
|
||||||
if (type.startsWith("memory(") && type.endsWith(")")) {
|
if (type.startsWith("memory(") && type.endsWith(")")) {
|
||||||
String sourceMemory = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
final PlayerSource playerSource = PlayerResolver.resolvePlayer(choicePlayer, environment);
|
||||||
if (sourceMemory.contains("(") || sourceMemory.contains(")"))
|
Function<ActionContext, Iterable<? extends PhysicalCard>> cardSource = actionContext -> {
|
||||||
throw new InvalidCardDefinitionException("Memory name cannot contain parenthesis");
|
String playerId = playerSource.getPlayer(actionContext);
|
||||||
return new DelayedAppender() {
|
return actionContext.getGame().getGameState().getDeck(playerId);
|
||||||
@Override
|
|
||||||
public boolean isPlayableInFull(ActionContext actionContext) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Effect createEffect(boolean cost, CostToEffectAction action, ActionContext actionContext) {
|
|
||||||
return new UnrespondableEffect() {
|
|
||||||
@Override
|
|
||||||
protected void doPlayEffect(LotroGame game) {
|
|
||||||
actionContext.setCardMemory(memory, actionContext.getCardsFromMemory(sourceMemory));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
return resolveMemoryCards(type, choiceFilter, choiceFilter, countSource, memory, cardSource);
|
||||||
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
||||||
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
||||||
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
||||||
@@ -521,33 +456,9 @@ public class CardResolver {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else if (type.startsWith("memory(") && type.endsWith(")")) {
|
} else if (type.startsWith("memory(") && type.endsWith(")")) {
|
||||||
String sourceMemory = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
Function<ActionContext, Iterable<? extends PhysicalCard>> cardSource = actionContext ->
|
||||||
if (sourceMemory.contains("(") || sourceMemory.contains(")"))
|
Filters.filterActive(actionContext.getGame(), Filters.any);
|
||||||
throw new InvalidCardDefinitionException("Memory name cannot contain parenthesis");
|
return resolveMemoryCards(type, additionalFilter, playabilityFilter, countSource, memory, cardSource);
|
||||||
return new DelayedAppender() {
|
|
||||||
@Override
|
|
||||||
public boolean isPlayableInFull(ActionContext actionContext) {
|
|
||||||
if (playabilityFilter != null) {
|
|
||||||
final Collection<? extends PhysicalCard> cardsFromMemory = actionContext.getCardsFromMemory(sourceMemory);
|
|
||||||
for (PhysicalCard physicalCard : cardsFromMemory) {
|
|
||||||
if (!PlayConditions.isActive(actionContext.getGame(), physicalCard, playabilityFilter.getFilterable(actionContext)))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Effect createEffect(boolean cost, CostToEffectAction action, ActionContext actionContext) {
|
|
||||||
return new UnrespondableEffect() {
|
|
||||||
@Override
|
|
||||||
protected void doPlayEffect(LotroGame game) {
|
|
||||||
actionContext.setCardMemory(memory, actionContext.getCardsFromMemory(sourceMemory));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
} else if (type.startsWith("all(") && type.endsWith(")")) {
|
||||||
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
final String filter = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
||||||
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
final FilterableSource filterableSource = environment.getFilterFactory().generateFilter(filter, environment);
|
||||||
@@ -603,4 +514,39 @@ public class CardResolver {
|
|||||||
}
|
}
|
||||||
throw new InvalidCardDefinitionException("Unable to resolve card resolver of type: " + type);
|
throw new InvalidCardDefinitionException("Unable to resolve card resolver of type: " + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static DelayedAppender resolveMemoryCards(String type, FilterableSource choiceFilter, FilterableSource playabilityFilter,
|
||||||
|
ValueSource countSource, String memory,
|
||||||
|
Function<ActionContext, Iterable<? extends PhysicalCard>> cardSource) throws InvalidCardDefinitionException {
|
||||||
|
String sourceMemory = type.substring(type.indexOf("(") + 1, type.lastIndexOf(")"));
|
||||||
|
if (sourceMemory.contains("(") || sourceMemory.contains(")"))
|
||||||
|
throw new InvalidCardDefinitionException("Memory name cannot contain parenthesis");
|
||||||
|
|
||||||
|
return new DelayedAppender() {
|
||||||
|
@Override
|
||||||
|
public boolean isPlayableInFull(ActionContext actionContext) {
|
||||||
|
int min = countSource.getMinimum(null);
|
||||||
|
return filterCards(actionContext, playabilityFilter).size() >= min;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Effect createEffect(boolean cost, CostToEffectAction action, ActionContext actionContext) {
|
||||||
|
Collection<PhysicalCard> result = filterCards(actionContext, choiceFilter);
|
||||||
|
return new UnrespondableEffect() {
|
||||||
|
@Override
|
||||||
|
protected void doPlayEffect(LotroGame game) {
|
||||||
|
actionContext.setCardMemory(memory, result);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private Collection<PhysicalCard> filterCards(ActionContext actionContext, FilterableSource filter) {
|
||||||
|
Collection<? extends PhysicalCard> cardsFromMemory = actionContext.getCardsFromMemory(sourceMemory);
|
||||||
|
Filterable additionalFilterable = Filters.any;
|
||||||
|
if (filter != null)
|
||||||
|
additionalFilterable = filter.getFilterable(actionContext);
|
||||||
|
return Filters.filter(cardSource.apply(actionContext), actionContext.getGame(), Filters.in(cardsFromMemory), additionalFilterable);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user