Support Import, Open, New deck, Save As

in all deck editors with finite catalog
This commit is contained in:
NikolayHD
2018-08-29 01:13:43 +03:00
parent db7ab85f26
commit 66e52cbd6a
18 changed files with 300 additions and 220 deletions

View File

@@ -24,6 +24,7 @@ import forge.StaticData;
import forge.card.CardDb;
import forge.item.IPaperCard;
import forge.item.PaperCard;
import forge.util.ItemPool;
import java.util.*;
import java.util.Map.Entry;
@@ -291,17 +292,13 @@ public class Deck extends DeckBase implements Iterable<Entry<DeckSection, CardPo
return true;
}
@Override
public void importDeck(Deck deck) {
deck.loadDeferredSections();
for (DeckSection section: deck.parts.keySet()) {
this.putSection(section, deck.get(section));
}
}
@Override
public String getImageKey(boolean altState) {
return null;
}
}
@Override
public Deck getHumanDeck() {
return this;
}
}

View File

@@ -6,22 +6,20 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.deck;
import forge.item.InventoryItem;
import java.io.Serializable;
public abstract class DeckBase implements Serializable, Comparable<DeckBase>, InventoryItem {
private static final long serialVersionUID = -7538150536939660052L;
// gameType is from Constant.GameType, like GameType.Regular
@@ -59,7 +57,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
/*
* (non-Javadoc)
*
*
* @see java.lang.Object#hashCode()
*/
@Override
@@ -74,6 +72,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
public String getDirectory() {
return directory;
}
public void setDirectory(String directory0) {
directory = directory0;
}
@@ -101,7 +100,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* <p>
* getComment.
* </p>
*
*
* @return a {@link java.lang.String} object.
*/
public String getComment() {
@@ -149,5 +148,5 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
public abstract boolean isEmpty();
public abstract void importDeck(Deck deck);
public abstract Deck getHumanDeck();
}

View File

@@ -18,9 +18,6 @@
package forge.deck;
import com.google.common.base.Function;
import forge.StaticData;
import forge.item.PaperCard;
import java.util.*;
/**
@@ -47,7 +44,8 @@ public class DeckGroup extends DeckBase {
*
* @return the human deck
*/
public final Deck getHumanDeck() {
@Override
public Deck getHumanDeck() {
return humanDeck;
}
@@ -160,100 +158,4 @@ public class DeckGroup extends DeckBase {
public String getImageKey(boolean altState) {
return null;
}
@Override
public void importDeck(Deck deck) {
CardPool draftedCards = this.getHumanDeck().getAllCardsInASinglePool(false);
this.getHumanDeck().putSection(DeckSection.Main, new CardPool());
this.getHumanDeck().putSection(DeckSection.Sideboard, new CardPool());
HashMap<String, Integer> countByName = getCountByName(deck);
addFromDraftedCardPool(countByName, draftedCards);
addBasicLands(deck, countByName, draftedCards);
}
private HashMap<String, Integer> getCountByName(Deck deck) {
HashMap<String, Integer> result = new HashMap<String, Integer>();
for (Map.Entry<PaperCard, Integer> entry: deck.getMain()) {
PaperCard importedCard = entry.getKey();
Integer previousCount = result.getOrDefault(importedCard.getName(), 0);
int countToAdd = entry.getValue();
result.put(importedCard.getName(), countToAdd + previousCount);
}
return result;
}
private void addFromDraftedCardPool(HashMap<String, Integer> countByName, CardPool availableCards) {
for (Map.Entry<PaperCard, Integer> entry: availableCards) {
PaperCard availableCard = entry.getKey();
Integer availableCount = entry.getValue();
int countToAdd = countByName.getOrDefault(availableCard.getName(), 0);
if (availableCard.getRules().getType().isBasicLand()) {
// basic lands are added regardless from drafted cards
continue;
}
int countMain = Math.min(availableCount, countToAdd);
if (countMain > 0) {
this.getHumanDeck().getMain().add(availableCard, countMain);
countByName.put(availableCard.getName(), countToAdd - countMain);
}
int countSideboard = availableCount - countMain;
if (countSideboard > 0) {
CardPool sideboard = this.getHumanDeck().getOrCreate(DeckSection.Sideboard);
sideboard.add(availableCard, countSideboard);
}
}
}
private void addBasicLands(Deck deck, HashMap<String, Integer> countByName, CardPool availableCards) {
HashMap<String, PaperCard> basicLandsByName = getBasicLandsByName(deck, countByName);
Date dateWithAllCards = StaticData.instance().getEditions().getEarliestDateWithAllCards(availableCards);
for (String cardName: countByName.keySet()) {
PaperCard card = basicLandsByName.getOrDefault(cardName, null);
if (card == null) {
continue;
}
int countToAdd = countByName.get(cardName);
card = StaticData.instance().getCardByEditionDate(card, dateWithAllCards);
this.getHumanDeck().getMain().add(card.getName(), card.getEdition(), countToAdd);
}
}
private HashMap<String, PaperCard> getBasicLandsByName(Deck deck, HashMap<String, Integer> countByName) {
HashMap<String, PaperCard> result = new HashMap<String, PaperCard>();
for (Map.Entry<PaperCard, Integer> entry: deck.getMain()) {
PaperCard card = entry.getKey();
if (!card.getRules().getType().isBasicLand()) {
continue;
}
if (result.containsKey(card.getName())) {
continue;
}
result.put(card.getName(), card);
}
return result;
}
}

View File

@@ -21,9 +21,9 @@ public enum DeckSection {
return null;
}
final String valToCompate = value.trim();
final String valToCompare = value.trim();
for (final DeckSection v : DeckSection.values()) {
if (v.name().compareToIgnoreCase(valToCompate) == 0) {
if (v.name().compareToIgnoreCase(valToCompare) == 0) {
return v;
}
}