Skip to content

Commit 25968b4

Browse files
committed
Reworked token filtering logic
1 parent 4acadb4 commit 25968b4

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

forge-core/src/main/java/forge/token/TokenDb.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import com.google.common.collect.Maps;
66
import com.google.common.collect.Multimap;
77

8+
import forge.StaticData;
89
import forge.card.CardDb;
910
import forge.card.CardEdition;
1011
import forge.card.CardRules;
1112
import forge.item.IPaperCard;
1213
import forge.item.PaperToken;
1314
import forge.util.Aggregates;
15+
import org.apache.commons.lang3.function.Predicates;
1416

1517
import java.util.*;
1618
import java.util.function.Predicate;
@@ -103,32 +105,35 @@ protected PaperToken fallbackToken(String name, CardEdition realEdition) {
103105

104106
// Find latest token before release of original card, or earliest after that
105107
private PaperToken smartFallbackToken(String name, CardEdition realEdition) {
106-
PaperToken coreExpansionReprintToken = smartFallbackToken(name, realEdition, true);
107-
return coreExpansionReprintToken != null ? coreExpansionReprintToken : smartFallbackToken(name, realEdition, false);
108+
109+
final EnumSet<CardEdition.Type> specialEditions = EnumSet.of(CardEdition.Type.FUNNY, CardEdition.Type.ONLINE, CardEdition.Type.OTHER);
110+
specialEditions.remove(realEdition.getType());
111+
CardDb.CardArtPreference artPreference = StaticData.instance().getCardArtPreference();
112+
113+
PaperToken paperToken = smartFallbackToken(name, realEdition, Predicate.<CardEdition>not(
114+
edition -> specialEditions.contains(edition.getType())).and(artPreference::accept));
115+
return paperToken != null ? paperToken : smartFallbackToken(name, realEdition, Predicates.falsePredicate());
108116
}
109117

110-
private PaperToken smartFallbackToken(String name, CardEdition realEdition, boolean onlyCoreExpansionOrReprint) {
111-
String latestFound = null;
112-
boolean pastRealEdition = false;
113-
final EnumSet<CardEdition.Type> coreExpansionOrReprint = EnumSet.of(CardEdition.Type.CORE, CardEdition.Type.EXPANSION);
114-
coreExpansionOrReprint.addAll(CardEdition.Type.REPRINT_SET_TYPES);
118+
private PaperToken smartFallbackToken(String name, CardEdition realEdition, Predicate<CardEdition> eligible) {
119+
String lastMatchedKey = null;
120+
boolean reachedRealEdition = false;
115121
for (CardEdition edition : this.editions.getOrderedEditions(false)) {
116122
if (edition.equals(realEdition)) {
117-
pastRealEdition = true;
123+
reachedRealEdition = true;
118124
}
119-
if (onlyCoreExpansionOrReprint && !coreExpansionOrReprint.contains(edition.getType())) {
120-
// Core, Expansion and Reprint sets are more likely to have available token images
125+
if (!eligible.test(edition)) {
121126
continue;
122127
}
123128
String fullName = String.format("%s_%s", name, edition.getCode().toLowerCase());
124129
if (loadTokenFromSet(edition, name)) {
125-
latestFound = fullName;
130+
lastMatchedKey = fullName;
126131
}
127-
if (pastRealEdition && latestFound != null) {
132+
if (reachedRealEdition && lastMatchedKey != null) {
128133
break;
129134
}
130135
}
131-
return latestFound != null ? Aggregates.random(allTokenByName.get(latestFound)) : null;
136+
return lastMatchedKey != null ? Aggregates.random(allTokenByName.get(lastMatchedKey)) : null;
132137
}
133138

134139
@Override

0 commit comments

Comments
 (0)