(String cardName, CardEdition edition, int artIndex, String collectorNumber, boolean isFoil)
| 738 | */ |
| 739 | |
| 740 | @Override |
| 741 | public PaperCard getCardFromSet(String cardName, CardEdition edition, int artIndex, String collectorNumber, boolean isFoil) { |
| 742 | if (edition == null || cardName == null) // preview cards |
| 743 | return null; // No cards will be returned |
| 744 | |
| 745 | // Allow to pass in cardNames with foil markers, and adapt accordingly |
| 746 | CardRequest cardNameRequest = CardRequest.fromString(cardName); |
| 747 | cardName = cardNameRequest.cardName; |
| 748 | isFoil = isFoil || cardNameRequest.isFoil; |
| 749 | |
| 750 | String code1 = edition.getCode(), code2 = edition.getCode2(); |
| 751 | |
| 752 | Predicate<PaperCard> filter = (c) -> { |
| 753 | String ed = c.getEdition(); |
| 754 | return ed.equalsIgnoreCase(code1) || ed.equalsIgnoreCase(code2); |
| 755 | }; |
| 756 | if (artIndex > 0) |
| 757 | filter = filter.and((c) -> artIndex == c.getArtIndex()); |
| 758 | if (collectorNumber != null && !collectorNumber.isEmpty() && !collectorNumber.equals(IPaperCard.NO_COLLECTOR_NUMBER)) |
| 759 | filter = filter.and((c) -> collectorNumber.equals(c.getCollectorNumber())); |
| 760 | |
| 761 | List<PaperCard> candidates = getAllCards(cardName, filter); |
| 762 | if (candidates.isEmpty()) |
| 763 | return null; |
| 764 | |
| 765 | if (candidates.stream().map(PaperCard::getRules).distinct().count() > 1) |
| 766 | { |
| 767 | //We've run into either an ambiguous alt-face or an Emeritus situation. Can't do anything for the former, |
| 768 | //but we can bias towards the main face if it's the latter. |
| 769 | String finalCardName = cardName; |
| 770 | if (candidates.stream().map(PaperCard::getName).anyMatch(n -> n.equalsIgnoreCase(finalCardName))) { |
| 771 | candidates.removeIf(c -> !c.getName().equalsIgnoreCase(finalCardName)); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | Iterator<PaperCard> candidatesIterator = candidates.iterator(); |
| 776 | PaperCard candidate = candidatesIterator.next(); |
| 777 | // Before returning make sure that actual candidate has Image. |
| 778 | // If not, try to replace current candidate with one having image, |
| 779 | // so to align this implementation with old one. |
| 780 | // If none will have image, the original candidate will be retained! |
| 781 | PaperCard firstCandidate = candidate; |
| 782 | while (!candidate.hasImage() && candidatesIterator.hasNext()) |
| 783 | candidate = candidatesIterator.next(); |
| 784 | candidate = candidate.hasImage() ? candidate : firstCandidate; |
| 785 | return isFoil ? candidate.getFoiled() : candidate; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * ==================================================== |
no test coverage detected