Updated and Aligned implementation for DeckController whenever a pick for limited catalog is required.

Most of this commit is for code formatting and cleaning. Major changes include the use fo getAlternativeCardPrint from Static Data (using the same policy that was used before) and the use of the `getTheLatestOfAllEditionsForCardsIn` method :D
This commit is contained in:
leriomaggio
2021-07-28 16:07:38 +01:00
parent 3e63a0c54d
commit cf006d1c16

View File

@@ -19,6 +19,7 @@ package forge.screens.deckeditor.controllers;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import forge.StaticData; import forge.StaticData;
import forge.card.CardEdition;
import forge.deck.*; import forge.deck.*;
import forge.item.PaperCard; import forge.item.PaperCard;
import forge.screens.deckeditor.menus.DeckFileMenu; import forge.screens.deckeditor.menus.DeckFileMenu;
@@ -110,11 +111,13 @@ public class DeckController<T extends DeckBase> {
} }
private Deck pickFromCatalog(Deck deck, CardPool catalog) { private Deck pickFromCatalog(Deck deck, CardPool catalog) {
Date dateWithAllCards = StaticData.instance().getEditions().getEarliestDateWithAllCards(catalog); // Getting Latest among the earliest editions in catalog!
CardEdition referenceEdition = StaticData.instance().getEditions().getTheLatestOfAllTheOriginalEditionsOfCardsIn(catalog);
Date referenceReleaseDate = referenceEdition.getDate();
Deck result = new Deck(); Deck result = new Deck();
for (DeckSection section: EnumSet.allOf(DeckSection.class)) { for (DeckSection section: EnumSet.allOf(DeckSection.class)) {
if (view.isSectionImportable(section)) { if (view.isSectionImportable(section)) {
CardPool cards = pickSectionFromCatalog(catalog, deck.getOrCreate(section), dateWithAllCards); CardPool cards = pickSectionFromCatalog(catalog, deck.getOrCreate(section), referenceReleaseDate);
result.putSection(section, cards); result.putSection(section, cards);
} }
} }
@@ -122,43 +125,35 @@ public class DeckController<T extends DeckBase> {
return result; return result;
} }
private CardPool pickSectionFromCatalog(CardPool catalog, CardPool sourceSection, Date dateWithAllCards) { private CardPool pickSectionFromCatalog(CardPool catalog, CardPool sourceSection, Date referenceReleaseDate) {
HashMap<String, Integer> countByName = groupByName(sourceSection); Map<String, Integer> countByName = groupByName(sourceSection);
HashMap<String, PaperCard> basicLandsByName = getBasicLandsByName(sourceSection); Map<String, PaperCard> basicLandsByName = getBasicLandsByName(sourceSection);
CardPool targetSection = new CardPool(); CardPool targetSection = new CardPool();
pickFromCatalog(countByName, catalog, targetSection); pickFromCatalog(countByName, catalog, targetSection);
importBasicLands(countByName, basicLandsByName, dateWithAllCards, targetSection); importBasicLands(countByName, basicLandsByName, referenceReleaseDate, targetSection);
return targetSection; return targetSection;
} }
private HashMap<String, Integer> groupByName(CardPool section) { private Map<String, Integer> groupByName(CardPool section) {
HashMap<String, Integer> result = new HashMap<>(); Map<String, Integer> result = new HashMap<>();
for (Map.Entry<PaperCard, Integer> entry : section) { for (Map.Entry<PaperCard, Integer> entry : section) {
PaperCard importedCard = entry.getKey(); PaperCard importedCard = entry.getKey();
Integer previousCount = result.getOrDefault(importedCard.getName(), 0); Integer previousCount = result.getOrDefault(importedCard.getName(), 0);
int countToAdd = entry.getValue(); int countToAdd = entry.getValue();
result.put(importedCard.getName(), countToAdd + previousCount); result.put(importedCard.getName(), countToAdd + previousCount);
} }
return result; return result;
} }
private void pickFromCatalog(HashMap<String, Integer> countByName, CardPool catalog, CardPool targetSection) { private void pickFromCatalog(Map<String, Integer> countByName, CardPool catalog, CardPool targetSection) {
CardPool catalogClone = new CardPool(catalog); // clone to iterate modified collection CardPool catalogClone = new CardPool(catalog); // clone to iterate modified collection
for (Map.Entry<PaperCard, Integer> entry : catalogClone) { for (Map.Entry<PaperCard, Integer> entry : catalogClone) {
PaperCard availableCard = entry.getKey(); PaperCard availableCard = entry.getKey();
if (availableCard.getRules().getType().isBasicLand()) { if (availableCard.getRules().getType().isBasicLand()) // basic lands are added regardless of catalog cards
// basic lands are added regardless of catalog cards
continue; continue;
}
Integer availableCount = entry.getValue(); Integer availableCount = entry.getValue();
int toAddByName = countByName.getOrDefault(availableCard.getName(), 0); int toAddByName = countByName.getOrDefault(availableCard.getName(), 0);
int toAdd = Math.min(availableCount, toAddByName); int toAdd = Math.min(availableCount, toAddByName);
@@ -171,18 +166,16 @@ public class DeckController<T extends DeckBase> {
} }
} }
private void importBasicLands(HashMap<String, Integer> countByName, HashMap<String, PaperCard> basicLandsByName, Date dateWithAllCards, CardPool targetSection) { private void importBasicLands(Map<String, Integer> countByName, Map<String, PaperCard> basicLandsByName,
Date referenceReleaseDate, CardPool targetSection) {
for (String cardName : countByName.keySet()) { for (String cardName : countByName.keySet()) {
PaperCard card = basicLandsByName.getOrDefault(cardName, null); PaperCard card = basicLandsByName.getOrDefault(cardName, null);
if (card == null)
if (card == null) {
continue; continue;
}
int countToAdd = countByName.get(cardName); int countToAdd = countByName.get(cardName);
card = StaticData.instance().getAlternativeCardPrint(card, referenceReleaseDate,
card = StaticData.instance().getAlternativeCardPrint(card, dateWithAllCards); true, true);
if (card != null)
targetSection.add(card.getName(), card.getEdition(), countToAdd); targetSection.add(card.getName(), card.getEdition(), countToAdd);
} }
} }