Solution for the deck editor / sideboarding / construct - mockcards problem.

This commit is contained in:
LevelX2 2013-07-06 00:44:13 +02:00
parent 0eaf9dd268
commit 0f32fc72de
14 changed files with 157 additions and 62 deletions

View file

@ -171,7 +171,7 @@ public class TableController {
if (seat == null) {
throw new GameException("No available seats.");
}
Deck deck = Deck.load(deckList);
Deck deck = Deck.load(deckList, false, false);
if (!Main.isTestMode() && !table.getValidator().validate(deck)) {
throw new InvalidDeckException(name + " has an invalid deck for this format", table.getValidator().getInvalid());
}
@ -211,7 +211,7 @@ public class TableController {
if (table.getState() != TableState.SIDEBOARDING && table.getState() != TableState.CONSTRUCTING) {
return false;
}
Deck deck = Deck.load(deckList);
Deck deck = Deck.load(deckList, false, false);
if (!Main.isTestMode() && !table.getValidator().validate(deck)) {
throw new InvalidDeckException("Invalid deck for this format", table.getValidator().getInvalid());
}
@ -224,7 +224,7 @@ public class TableController {
if (table.getState() != TableState.SIDEBOARDING && table.getState() != TableState.CONSTRUCTING) {
return;
}
Deck deck = Deck.load(deckList);
Deck deck = Deck.load(deckList, false, false);
updateDeck(userId, playerId, deck);
}
@ -454,7 +454,9 @@ public class TableController {
}
public void endGame() {
// get player that chooses who goes first
UUID choosingPlayerId = match.getChooser();
match.endGame();
table.endGame();
// Saving of games caused memory leaks - so save is deactivated

View file

@ -333,7 +333,7 @@ public class GameController implements GameCallback {
public void cheat(UUID userId, UUID playerId, DeckCardLists deckList) {
Deck deck;
try {
deck = Deck.load(deckList);
deck = Deck.load(deckList, false, false);
game.loadCards(deck.getCards(), playerId);
for (Card card: deck.getCards()) {
card.putOntoBattlefield(game, Zone.OUTSIDE, null, playerId);

View file

@ -24,6 +24,7 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import mage.cards.decks.DeckCardInfo;
/**
* Intended to test Mage server under different load patterns.
@ -252,7 +253,7 @@ public class LoadTest {
for (Card card : deck.getCards()) {
CardInfo cardInfo = CardRepository.instance.findCard(card.getExpansionSetCode(), card.getCardNumber());
if (cardInfo != null) {
deckList.getCards().add(cardInfo.getClassName());
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
return deckList;

View file

@ -91,7 +91,7 @@ public abstract class CardTestMultiPlayerBase extends CardTestPlayerAPIImpl {
playerA = createNewPlayer("ComputerA");
playerA.setTestMode(true);
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
if (deck.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size());
@ -101,7 +101,7 @@ public abstract class CardTestMultiPlayerBase extends CardTestPlayerAPIImpl {
playerB = createNewPlayer("ComputerB");
playerB.setTestMode(true);
Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
if (deck2.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size());
}

View file

@ -59,7 +59,7 @@ public abstract class CardTestPlayerBase extends CardTestPlayerAPIImpl {
playerA = createNewPlayer("PlayerA");
playerA.setTestMode(true);
logger.debug("Loading deck...");
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
logger.debug("Done!");
if (deck.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size());
@ -69,7 +69,7 @@ public abstract class CardTestPlayerBase extends CardTestPlayerAPIImpl {
playerB = createNewPlayer("PlayerB");
playerB.setTestMode(true);
Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
if (deck2.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size());
}
@ -114,7 +114,7 @@ public abstract class CardTestPlayerBase extends CardTestPlayerAPIImpl {
playerA = createNewPlayer("ComputerA");
playerA.setTestMode(true);
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
if (deck.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size());
@ -124,7 +124,7 @@ public abstract class CardTestPlayerBase extends CardTestPlayerAPIImpl {
playerB = createNewPlayer("ComputerB");
playerB.setTestMode(true);
Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck2 = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
if (deck2.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck2.getCards().size());
}

View file

@ -84,7 +84,7 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
player = createNewPlayer(name);
player.setTestMode(true);
logger.debug("Loading deck...");
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"));
Deck deck = Deck.load(DeckImporterUtil.importDeck("RB Aggro.dck"), false, false);
logger.debug("Done!");
if (deck.getCards().size() < 40) {
throw new IllegalArgumentException("Couldn't load deck, deck size=" + deck.getCards().size());

View file

@ -31,6 +31,7 @@ package mage.cards;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
import mage.cards.decks.DeckCardInfo;
import mage.constants.CardType;
import mage.constants.ColoredManaSymbol;
import mage.cards.decks.DeckCardLists;
@ -129,8 +130,8 @@ public class Sets extends HashMap<String, ExpansionSet> {
public static void saveDeck(String file, DeckCardLists deck) throws FileNotFoundException {
PrintWriter out = new PrintWriter(file);
Map<String, Integer> deckCards = new HashMap<String, Integer>();
Map<String, Integer> sideboard = new HashMap<String, Integer>();
Map<String, DeckCardInfo> deckCards = new HashMap<String, DeckCardInfo>();
Map<String, DeckCardInfo> sideboard = new HashMap<String, DeckCardInfo>();
try {
if (deck.getName() != null && deck.getName().length() > 0) {
out.println("NAME:" + deck.getName());
@ -138,33 +139,29 @@ public class Sets extends HashMap<String, ExpansionSet> {
if (deck.getAuthor() != null && deck.getAuthor().length() > 0) {
out.println("AUTHOR:" + deck.getAuthor());
}
for (String cardClass: deck.getCards()) {
if (deckCards.containsKey(cardClass)) {
deckCards.put(cardClass, deckCards.get(cardClass) + 1);
for (DeckCardInfo deckCardInfo: deck.getCards()) {
if (deckCards.containsKey(deckCardInfo.getCardKey())) {
deckCards.put(deckCardInfo.getCardKey(), deckCards.get(deckCardInfo.getCardKey()).increaseQuantity());
}
else {
deckCards.put(cardClass, 1);
deckCards.put(deckCardInfo.getCardKey(), deckCardInfo);
}
}
for (String cardClass: deck.getSideboard()) {
if (sideboard.containsKey(cardClass)) {
sideboard.put(cardClass, sideboard.get(cardClass) + 1);
for (DeckCardInfo deckCardInfo: deck.getSideboard()) {
if (sideboard.containsKey(deckCardInfo.getCardKey())) {
sideboard.put(deckCardInfo.getCardKey(), sideboard.get(deckCardInfo.getCardKey()).increaseQuantity());
}
else {
sideboard.put(cardClass, 1);
sideboard.put(deckCardInfo.getCardKey(), deckCardInfo);
}
}
for (Map.Entry<String, Integer> entry: deckCards.entrySet()) {
Card card = CardImpl.createCard(entry.getKey());
if (card != null) {
out.printf("%d [%s:%d] %s%n", entry.getValue(), card.getExpansionSetCode(), card.getCardNumber(), card.getName());
}
for (Map.Entry<String, DeckCardInfo> entry: deckCards.entrySet()) {
out.printf("%d [%s:%d] %s%n", entry.getValue().getQuantity(), entry.getValue().getSetCode(), entry.getValue().getCardNum(), entry.getValue().getCardName());
}
for (Map.Entry<String, Integer> entry: sideboard.entrySet()) {
Card card = CardImpl.createCard(entry.getKey());
if (card != null) {
out.printf("SB: %d [%s:%d] %s%n", entry.getValue(), card.getExpansionSetCode(), card.getCardNumber(), card.getName());
}
for (Map.Entry<String, DeckCardInfo> entry: sideboard.entrySet()) {
out.printf("SB: %d [%s:%d] %s%n", entry.getValue().getQuantity(), entry.getValue().getSetCode(), entry.getValue().getCardNum(), entry.getValue().getCardName());
}
}
finally {

View file

@ -32,6 +32,7 @@ import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;
import mage.cards.*;
import mage.cards.repository.CardRepository;
import mage.game.GameException;
public class Deck implements Serializable {
@ -45,27 +46,37 @@ public class Deck implements Serializable {
}
public static Deck load(DeckCardLists deckCardLists, boolean ignoreErrors) throws GameException {
return Deck.load(deckCardLists, ignoreErrors, true);
}
public static Deck load(DeckCardLists deckCardLists, boolean ignoreErrors, boolean mockCards) throws GameException {
Deck deck = new Deck();
deck.setName(deckCardLists.getName());
for (String cardName: deckCardLists.getCards()) {
Card card = CardImpl.createCard(cardName);
for (DeckCardInfo deckCardInfo: deckCardLists.getCards()) {
Card card;
if (mockCards) {
card = CardRepository.instance.findCard(deckCardInfo.getSetCode(), deckCardInfo.getCardNum()).getMockCard();
} else {
card = CardImpl.createCard(CardRepository.instance.findCard(deckCardInfo.getSetCode(), deckCardInfo.getCardNum()).getClassName());
}
if (card != null) {
deck.cards.add(CardImpl.createCard(cardName));
deck.cards.add(card);
}
else {
if (!ignoreErrors) {
throw new GameException("Error loading card - " + cardName + " for deck - " + deck.getName());
throw new GameException("Error loading card - " + deckCardInfo.getCardName() + " for deck - " + deck.getName());
}
}
}
for (String cardName: deckCardLists.getSideboard()) {
Card card = CardImpl.createCard(cardName);
for (DeckCardInfo deckCardInfo: deckCardLists.getSideboard()) {
Card card = CardRepository.instance.findCard(deckCardInfo.getSetCode(), deckCardInfo.getCardNum()).getMockCard();
// Card card = CardImpl.createCard(cardName);
if (card != null) {
deck.sideboard.add(CardImpl.createCard(cardName));
deck.sideboard.add(card);
}
else {
if (!ignoreErrors) {
throw new GameException("Error loading card - " + cardName + " for deck - " + deck.getName());
throw new GameException("Error loading card - " + deckCardInfo.getCardName() + " for deck - " + deck.getName());
}
}
}
@ -78,10 +89,11 @@ public class Deck implements Serializable {
deckCardLists.setName(name);
for (Card card: cards) {
deckCardLists.getCards().add(card.getClass().getCanonicalName());
deckCardLists.getCards().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getExpansionSetCode()));
}
for (Card card: sideboard) {
deckCardLists.getSideboard().add(card.getClass().getCanonicalName());
deckCardLists.getSideboard().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getExpansionSetCode()));
}
return deckCardLists;

View file

@ -0,0 +1,80 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.cards.decks;
import java.io.Serializable;
/**
*
* @author LevelX2
*/
public class DeckCardInfo implements Serializable {
private String cardName;
private String setCode;
private int cardNum;
private int quantity;
public DeckCardInfo(String cardName, int cardNum, String setCode) {
this(cardName, cardNum, setCode, 1);
}
public DeckCardInfo(String cardName, int cardNum, String setCode, int quantity) {
this.cardName = cardName;
this.cardNum = cardNum;
this.setCode = setCode;
this.quantity = quantity;
}
public String getCardName() {
return cardName;
}
public String getSetCode() {
return setCode;
}
public int getCardNum() {
return cardNum;
}
public int getQuantity() {
return quantity;
}
public DeckCardInfo increaseQuantity() {
quantity++;
return this;
}
public String getCardKey() {
return new StringBuilder(setCode).append(cardNum).toString();
}
}

View file

@ -40,34 +40,34 @@ public class DeckCardLists implements Serializable {
private String name;
private String author;
private List<String> cards = new ArrayList<String>();
private List<String> sideboard = new ArrayList<String>();
private List<DeckCardInfo> cards = new ArrayList<DeckCardInfo>();
private List<DeckCardInfo> sideboard = new ArrayList<DeckCardInfo>();
/**
* @return the cards
*/
public List<String> getCards() {
public List<DeckCardInfo> getCards() {
return cards;
}
/**
* @param cards the cards to set
*/
public void setCards(List<String> cards) {
public void setCards(List<DeckCardInfo> cards) {
this.cards = cards;
}
/**
* @return the sideboard
*/
public List<String> getSideboard() {
public List<DeckCardInfo> getSideboard() {
return sideboard;
}
/**
* @param sideboard the sideboard to set
*/
public void setSideboard(List<String> sideboard) {
public void setSideboard(List<DeckCardInfo> sideboard) {
this.sideboard = sideboard;
}

View file

@ -29,6 +29,7 @@ package mage.cards.decks.importer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
@ -58,17 +59,17 @@ public class DckDeckImporter extends DeckImporter {
String setCode = m.group(3);
int cardNum = Integer.parseInt(m.group(4));
String className = null;
DeckCardInfo deckCardInfo = null;
CardInfo cardInfo = CardRepository.instance.findCard(setCode, cardNum);
if (cardInfo != null) {
className = cardInfo.getClassName();
deckCardInfo = new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode());
}
if (className != null) {
if (deckCardInfo != null) {
for (int i = 0; i < count; i++) {
if (!sideboard) {
deckList.getCards().add(className);
deckList.getCards().add(deckCardInfo);
} else {
deckList.getSideboard().add(className);
deckList.getSideboard().add(deckCardInfo);
}
}
} else {

View file

@ -30,6 +30,7 @@ package mage.cards.decks.importer;
import java.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
@ -63,11 +64,11 @@ public class DecDeckImporter extends DeckImporter {
} else {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
CardInfo cardInfo = cards.get(random.nextInt(cards.size()));
if (!sideboard) {
deckList.getCards().add(className);
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(),cardInfo.getCardNumber(), cardInfo.getSetCode()));
} else {
deckList.getSideboard().add(className);
deckList.getSideboard().add(new DeckCardInfo(cardInfo.getName(),cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
}

View file

@ -29,6 +29,7 @@ package mage.cards.decks.importer;
import java.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
@ -78,11 +79,11 @@ public class MWSDeckImporter extends DeckImporter {
} else {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
CardInfo cardInfo = cards.get(random.nextInt(cards.size()));
if (!sideboard) {
deckList.getCards().add(className);
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(),cardInfo.getCardNumber(), cardInfo.getSetCode()));
} else {
deckList.getSideboard().add(className);
deckList.getSideboard().add(new DeckCardInfo(cardInfo.getName(),cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
}

View file

@ -33,6 +33,7 @@ import java.util.GregorianCalendar;
import java.util.List;
import mage.cards.ExpansionSet;
import mage.cards.Sets;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
@ -81,11 +82,10 @@ public class TxtDeckImporter extends DeckImporter {
lastReleasedCard = cards.get(0);
}
for (int i = 0; i < num; i++) {
String className = lastReleasedCard.getClassName();
if (!sideboard) {
deckList.getCards().add(className);
deckList.getCards().add(new DeckCardInfo(lastReleasedCard.getName(),lastReleasedCard.getCardNumber(), lastReleasedCard.getSetCode()));
} else {
deckList.getSideboard().add(className);
deckList.getSideboard().add(new DeckCardInfo(lastReleasedCard.getName(),lastReleasedCard.getCardNumber(), lastReleasedCard.getSetCode()));
}
}
}