Card: getUnhiddenKeywords uses cache

that is updated by CardStateView#updateKeywords
This commit is contained in:
Hanmac
2018-01-20 08:09:56 +01:00
parent 4a8cecd225
commit e66194fb9d
3 changed files with 43 additions and 4 deletions

View File

@@ -124,6 +124,9 @@ public class Card extends GameEntity implements Comparable<Card> {
private final CardChangedWords changedTextTypes = new CardChangedWords();
/** List of the keywords that have been added by text changes. */
private final List<KeywordInterface> keywordsGrantedByTextChanges = Lists.newArrayList();
private final Map<CardStateName, List<KeywordInterface>> cachedKeywords = Maps.newEnumMap(CardStateName.class);
/** Original values of SVars changed by text changes. */
private Map<String, String> originalSVars = Maps.newHashMap();
@@ -3362,6 +3365,41 @@ public class Card extends GameEntity implements Comparable<Card> {
return getUnhiddenKeywords(currentState);
}
public final Collection<KeywordInterface> getUnhiddenKeywords(CardState state) {
CardStateName name = null;
for (Entry<CardStateName, CardState> entry : states.entrySet()) {
if (entry.getValue().equals(state)) {
name = entry.getKey();
break;
}
}
if (name == null) {
return Lists.newArrayList();
}
if (!cachedKeywords.containsKey(name)) {
updateKeywordsCache(state);
}
return cachedKeywords.get(name);
}
public final void updateKeywordsCache(final CardState state) {
CardStateName name = null;
for (Entry<CardStateName, CardState> entry : states.entrySet()) {
if (entry.getValue().equals(state)) {
name = entry.getKey();
break;
}
}
if (name == null) {
return;
}
if (cachedKeywords.containsKey(name)) {
cachedKeywords.get(name).clear();
} else {
cachedKeywords.put(name, Lists.newArrayList());
}
KeywordCollection keywords = new KeywordCollection();
//final List<KeywordInterface> keywords = Lists.newArrayList();
@@ -3384,7 +3422,7 @@ public class Card extends GameEntity implements Comparable<Card> {
keywords.insertAll(ck.getKeywords());
}
}
return keywords.getValues();
cachedKeywords.get(name).addAll(keywords.getValues());
}
private void visitUnhiddenKeywords(CardState state, Visitor<KeywordInterface> visitor) {
if (changedCardKeywords.isEmpty()) {
@@ -3396,7 +3434,7 @@ public class Card extends GameEntity implements Comparable<Card> {
visitor.visit(kw);
}
} else {
for (KeywordInterface kw : getUnhiddenKeywords()) {
for (KeywordInterface kw : getUnhiddenKeywords(state)) {
visitor.visit(kw);
}
}

View File

@@ -209,6 +209,8 @@ public final class CardUtil {
// needed to ensure that the LKI object has correct CMC info no matter what state the original card was in
// (e.g. Scrap Trawler + transformed Harvest Hand)
newCopy.setLKICMC(in.getCMC());
// used for the purpose of cards that care about the zone the card was known to be in last
newCopy.setLastKnownZone(in.getLastKnownZone());
if (in.isCloned()) {
newCopy.addAlternateState(CardStateName.Cloner, false);
@@ -260,8 +262,6 @@ public final class CardUtil {
newCopy.setMeldedWith(in.getMeldedWith());
newCopy.setLastKnownZone(in.getZone()); // used for the purpose of cards that care about the zone the card was known to be in last
return newCopy;
}

View File

@@ -977,6 +977,7 @@ public class CardView extends GameEntityView {
set(TrackableProperty.AbilityText, c.getAbilityText(state));
}
void updateKeywords(Card c, CardState state) {
c.updateKeywordsCache(state);
set(TrackableProperty.HasDeathtouch, c.hasKeyword("Deathtouch", state));
set(TrackableProperty.HasHaste, c.hasKeyword("Haste", state));
set(TrackableProperty.HasInfect, c.hasKeyword("Infect", state));