use of mage.cards.repository.CardInfo in project mage by magefree.
the class CardTestAPIImpl method addCard.
/**
* Add any amount of cards to specified zone of specified player.
*
* @param gameZone {@link mage.constants.Zone} to add cards to.
* @param player {@link Player} to add cards for. Use either playerA or playerB.
* @param cardName Card name in string format.
* @param count Amount of cards to be added.
* @param tapped In case gameZone is Battlefield, determines whether permanent should be tapped.
* In case gameZone is other than Battlefield, {@link IllegalArgumentException} is thrown
*/
@Override
public void addCard(Zone gameZone, TestPlayer player, String cardName, int count, boolean tapped) {
if (gameZone == Zone.BATTLEFIELD) {
for (int i = 0; i < count; i++) {
CardInfo cardInfo = CardRepository.instance.findCard(cardName);
Card card = cardInfo != null ? cardInfo.getCard() : null;
if (card == null) {
throw new IllegalArgumentException("[TEST] Couldn't find a card: " + cardName);
}
PermanentCard p = new PermanentCard(card, null, currentGame);
p.setTapped(tapped);
if (player.equals(playerA)) {
battlefieldCardsA.add(p);
} else if (player.equals(playerB)) {
battlefieldCardsB.add(p);
}
}
} else {
if (tapped) {
throw new IllegalArgumentException("Parameter tapped=true can be used only for Zone.BATTLEFIELD.");
}
List<Card> cards = getCardList(gameZone, player);
for (int i = 0; i < count; i++) {
CardInfo cardInfo = CardRepository.instance.findCard(cardName);
Card card = cardInfo != null ? cardInfo.getCard() : null;
cards.add(card);
}
}
}
use of mage.cards.repository.CardInfo in project mage by magefree.
the class AbilityPickerTest method getAbilitiesFromCard.
private Abilities<Ability> getAbilitiesFromCard(String cardName) {
CardInfo info = CardRepository.instance.findCard(cardName);
PermanentImpl permanent = new PermanentCard(info.getCard(), playerA.getId(), currentGame);
return permanent.getAbilities(currentGame);
}
use of mage.cards.repository.CardInfo in project mage by magefree.
the class DckDeckImporter method readLine.
@Override
protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) {
if (line.isEmpty() || line.startsWith("#")) {
return;
}
// AUTO-FIX apply (if card number was fixed before then it can be replaced in layout or other lines too)
for (Map.Entry<String, String> fix : this.possibleFixes.entrySet()) {
if (line.contains(fix.getKey())) {
line = line.replace(fix.getKey(), fix.getValue());
}
}
fixedInfo.setFixedLine(line);
Matcher m = pattern.matcher(line);
if (m.matches()) {
boolean sideboard = false;
if ("SB:".equals(m.group(1))) {
sideboard = true;
}
int count = Integer.parseInt(m.group(2));
String setCode = m.group(3);
String cardNum = m.group(4);
String cardName = m.group(5);
cardNum = cardNum == null ? "" : cardNum.trim();
setCode = setCode == null ? "" : setCode.trim();
cardName = cardName == null ? "" : cardName.trim();
// text for auto-fix (don't work on extra spaces between numbers and name -- it's ok)
String originalNumbers = "";
String originalNumbersWithName = "";
if (!setCode.isEmpty() && !cardNum.isEmpty()) {
// [ISD:144] card_name
originalNumbers = "[" + setCode + ":" + cardNum + "]";
originalNumbersWithName = originalNumbers + " " + cardName;
}
// search priority: set/number -> name
// with bulletproof on card number or name changes
DeckCardInfo deckCardInfo = null;
// search by set/number
CardInfo foundedCard = CardRepository.instance.findCard(setCode, cardNum, true);
boolean wasOutdated = false;
if ((foundedCard != null) && !foundedCard.getName().equals(cardName)) {
sbMessage.append("Line ").append(lineCount).append(": ").append("found outdated card number or name, will try to replace: ").append(line).append('\n');
wasOutdated = true;
foundedCard = null;
}
// search by name
if (foundedCard == null) {
if (!wasOutdated) {
sbMessage.append("Line ").append(lineCount).append(": ").append("can't find card by number, will try to replace: ").append(line).append('\n');
}
if (!cardName.equals("")) {
foundedCard = CardRepository.instance.findPreferredCoreExpansionCard(cardName, false, setCode);
}
if (foundedCard != null) {
if (foundedCard.isNightCard()) {
sbMessage.append("Line ").append(lineCount).append(": ").append("ERROR, you can't use night card in deck [").append(foundedCard.getName()).append("]").append('\n');
} else {
sbMessage.append("Line ").append(lineCount).append(": ").append("replaced to [").append(foundedCard.getSetCode()).append(":").append(foundedCard.getCardNumberAsInt()).append("] ").append(foundedCard.getName()).append('\n');
// AUTO-FIX POSSIBLE (apply and save it for another lines like layout)
// [ISD:144] card
String fixNumbers = "[" + foundedCard.getSetCode() + ":" + foundedCard.getCardNumber() + "]";
String fixNumbersWithName = fixNumbers + " " + foundedCard.getName();
// name fix must goes first
this.possibleFixes.put(originalNumbersWithName, fixNumbersWithName);
this.possibleFixes.put(originalNumbers, fixNumbers);
String fixedLine = fixedInfo.getOriginalLine().replace(originalNumbersWithName, fixNumbersWithName).replace(originalNumbers, fixNumbers);
fixedInfo.setFixedLine(fixedLine);
}
} else {
sbMessage.append("Line ").append(lineCount).append(": ").append("ERROR, can't find card [").append(cardName).append("]").append('\n');
}
}
if (foundedCard != null) {
deckCardInfo = new DeckCardInfo(foundedCard.getName(), foundedCard.getCardNumber(), foundedCard.getSetCode());
}
if (deckCardInfo != null) {
for (int i = 0; i < count; i++) {
if (!sideboard) {
deckList.getCards().add(deckCardInfo);
} else {
deckList.getSideboard().add(deckCardInfo);
}
}
}
} else if (line.startsWith("NAME:")) {
deckList.setName(line.substring(5));
} else if (line.startsWith("AUTHOR:")) {
deckList.setAuthor(line.substring(7));
} else if (line.startsWith("LAYOUT")) {
Matcher m2 = layoutPattern.matcher(line);
if (m2.find()) {
String target = m2.group(1);
int rows = Integer.parseInt(m2.group(2));
int cols = Integer.parseInt(m2.group(3));
String settings = m2.group(4);
String stackData = m2.group(5);
Matcher stackMatcher = layoutStackPattern.matcher(stackData);
//
List<List<List<DeckCardInfo>>> grid = new ArrayList<>();
int totalCardCount = 0;
for (int row = 0; row < rows; ++row) {
List<List<DeckCardInfo>> rowData = new ArrayList<>();
grid.add(rowData);
for (int col = 0; col < cols; ++col) {
List<DeckCardInfo> stack = new ArrayList<>();
rowData.add(stack);
if (stackMatcher.find()) {
String thisStackData = stackMatcher.group(1);
Matcher stackEntries = layoutStackEntryPattern.matcher(thisStackData);
while (stackEntries.find()) {
++totalCardCount;
stack.add(new DeckCardInfo("", stackEntries.group(2), stackEntries.group(1)));
}
} else {
sbMessage.append("Missing stack\n.");
}
}
}
//
DeckCardLayout layout = new DeckCardLayout(grid, settings);
int expectedCount = 0;
switch(target) {
case "MAIN":
deckList.setCardLayout(layout);
expectedCount = deckList.getCards().size();
break;
case "SIDEBOARD":
deckList.setSideboardLayout(layout);
expectedCount = deckList.getSideboard().size();
break;
default:
sbMessage.append("Bad target `").append(target).append("` for layout.\n");
break;
}
//
if (totalCardCount != expectedCount) {
sbMessage.append("Layout mismatch: Expected ").append(expectedCount).append(" cards, but got ").append(totalCardCount).append(" in layout `").append(target).append("`\n.");
}
} else {
sbMessage.append("Malformed layout line");
}
}
}
use of mage.cards.repository.CardInfo in project mage by magefree.
the class MWSDeckImporter method readLine.
@Override
protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) {
if (line.isEmpty() || line.startsWith("//")) {
return;
}
boolean sideboard = false;
if (line.startsWith("SB:")) {
line = line.substring(3).trim();
sideboard = true;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String setCode = "";
if (line.indexOf('[') != -1) {
int setStart = line.indexOf('[') + 1;
int setEnd = line.indexOf(']');
setCode = line.substring(setStart, setEnd).trim();
delim = setEnd;
}
String lineName = line.substring(delim + 1).trim();
try {
int num = Integer.parseInt(lineNum);
CardInfo cardInfo = null;
if (!setCode.isEmpty()) {
CardCriteria criteria = new CardCriteria();
criteria.name(lineName);
criteria.setCodes(setCode);
List<CardInfo> cards = getCardLookup().lookupCardInfo(criteria);
if (!cards.isEmpty()) {
cardInfo = cards.get(RandomUtil.nextInt(cards.size()));
}
}
if (cardInfo == null) {
cardInfo = getCardLookup().lookupCardInfo(lineName).orElse(null);
}
if (cardInfo == null) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append('\n');
} else {
for (int i = 0; i < num; i++) {
if (!sideboard) {
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
} else {
deckList.getSideboard().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append('\n');
}
}
use of mage.cards.repository.CardInfo in project mage by magefree.
the class MtgaImporter method readLine.
@Override
protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) {
if (line.trim().equals("")) {
sideboard = true;
return;
}
Matcher m = MTGA_PATTERN.matcher(CardNameUtil.normalizeCardName(line));
if (m.matches()) {
int count = Integer.parseInt(m.group(1));
String name = m.group(2);
String set = SET_REMAPPING.getOrDefault(m.group(3), m.group(3));
String cardNumber = m.group(4);
final List<DeckCardInfo> zone = sideboard ? deckList.getSideboard() : deckList.getCards();
Optional<CardInfo> found = lookup.lookupCardInfo(name, set, cardNumber);
if (!found.isPresent()) {
sbMessage.append("Cound not find card for '").append(line).append("'\n");
} else {
found.ifPresent(card -> zone.addAll(Collections.nCopies(count, new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode()))));
}
} else {
sbMessage.append("Error reading '").append(line).append("'\n");
}
}
Aggregations