diff --git a/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/CollectionRequestHandler.java b/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/CollectionRequestHandler.java index bcd2436e1..a928cd248 100644 --- a/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/CollectionRequestHandler.java +++ b/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/CollectionRequestHandler.java @@ -68,18 +68,9 @@ public class CollectionRequestHandler extends LotroServerRequestHandler implemen private void importCollection(HttpRequest request, ResponseWriter responseWriter) throws Exception { QueryStringDecoder queryDecoder = new QueryStringDecoder(request.getUri()); - String participantId = getQueryParameterSafely(queryDecoder, "participantId"); String rawDecklist = getQueryParameterSafely(queryDecoder, "decklist"); - Player resourceOwner = getResourceOwnerSafely(request, participantId); - - CardCollection collection = constructCollection(resourceOwner, "default"); - - if (collection == null) - throw new HttpProcessingException(404); - - Iterable items = collection.getAll(); - List importResult = _importCards.process(rawDecklist, items, _library); + List importResult = _importCards.process(rawDecklist, _library); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/ImportCards.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/ImportCards.java index dc8432132..633f54187 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/ImportCards.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/ImportCards.java @@ -6,144 +6,105 @@ import com.gempukku.lotro.logic.GameUtils; import java.util.*; public class ImportCards { - private SitesBlock _siteBlock; - //For a deck to be legal in a Pre-shadows format, it must contain one of these sites - private List _fellowshipSiteCheck = new ArrayList(Arrays.asList("Council Courtyard", + private Set fellowshipSiteCheck = new HashSet<>(Arrays.asList("Council Courtyard", "Ford of Bruinen", "Frodo's Bedroom", "Rivendell Terrace", "Rivendell Valley", "Rivendell Waterfall", "House of Elrond")); - private List _towersSiteCheck = new ArrayList(Arrays.asList("Derndingle", "Eastfold", + private Set towersSiteCheck = new HashSet<>(Arrays.asList("Derndingle", "Eastfold", "Fangorn Forest", "Plains of Rohan Camp", "Rohirrim Village", "Uruk Camp", "Wold of Rohan")); - private List _kingSiteCheck = new ArrayList(Arrays.asList("King's Tent", "Rohirrim Camp", + private Set kingSiteCheck = new HashSet<>(Arrays.asList("King's Tent", "Rohirrim Camp", "West Road")); - - public List process(String rawDecklist, Iterable items, LotroCardBlueprintLibrary cardLibrary) { - List decklist = getDecklist(rawDecklist); - _siteBlock = null; - List result = new ArrayList(); - Set pendingSites = new HashSet(); - Map cardBlueprintMap = new HashMap<>(); - for (String line : decklist) { - for (T item : items) { - String blueprintId = item.getBlueprintId(); - if (isPack(blueprintId)) - continue; - try { - cardBlueprintMap.put(blueprintId, cardLibrary.getLotroCardBlueprint(blueprintId)); - int outcome = importCriteria(cardLibrary, cardBlueprintMap, blueprintId, line); - if (outcome == 1) { - result.add(item); + public List process(String rawDecklist, LotroCardBlueprintLibrary cardLibrary) { + List decklist = getDecklist(rawDecklist); + SitesBlock sitesBlock = determineBlock(decklist); + + List result = new ArrayList<>(); + for (CardCount cardCount : decklist) { + for (Map.Entry cardBlueprint : cardLibrary.getBaseCards().entrySet()) { + String id = cardBlueprint.getKey(); + if (isFromOfficialSet(id)) { + LotroCardBlueprint blueprint = cardBlueprint.getValue(); + if (isNotSiteOrSiteFromBlock(blueprint, sitesBlock)) { + if (exactNameMatch(blueprint, cardCount.getName())) { + result.add(CardCollection.Item.createItem(id, cardCount.getCount())); + break; + } } - if (outcome == 0) - pendingSites.add(item); - } catch (CardNotFoundException e) { - // Ignore the card } } } - if (_siteBlock == null) - _siteBlock = SitesBlock.SHADOWS; - for (T siteItem : pendingSites) { - String siteId = siteItem.getBlueprintId(); - try { - LotroCardBlueprint siteBlueprint = cardLibrary.getLotroCardBlueprint(siteId); - if (siteBlockFilter(siteBlueprint)) - result.add(siteItem); - } catch (CardNotFoundException e) { - // Ignore the card - } - } - return result; } - private int importCriteria(LotroCardBlueprintLibrary library, Map cardBlueprint, String blueprintId, String title) { - final LotroCardBlueprint blueprint = cardBlueprint.get(blueprintId); - if (exactMatch(blueprint,title)) { - if (setFilter(blueprintId)) { - if (blueprint.getCardType() == CardType.SITE) { - if (_siteBlock == null) - findSiteBlock(blueprint); - //add all sites to the set, as some formats would add duplicates otherwise - return 0; - } - return 1; - } - } - return -1; + private boolean isNotSiteOrSiteFromBlock(LotroCardBlueprint blueprint, SitesBlock sitesBlock) { + return blueprint.getCardType() != CardType.SITE || blueprint.getSiteBlock() == sitesBlock; } - private boolean exactMatch(LotroCardBlueprint blueprint, String title) { - if (blueprint == null || !replaceSpecialCharacters(GameUtils.getFullName(blueprint).toLowerCase()).equals(title)) - return false; - return true; - } - - private boolean setFilter(String blueprintId) { - try { - int setNo = Integer.parseInt(blueprintId.split("_")[0]); - if (setNo < 20) - return true; - } catch (Exception e) { - //Not a card + private SitesBlock determineBlock(List decklist) { + for (CardCount card : decklist) { + String name = card.getName(); + if (fellowshipSiteCheck.contains(name)) + return SitesBlock.FELLOWSHIP; + if (towersSiteCheck.contains(name)) + return SitesBlock.TWO_TOWERS; + if (kingSiteCheck.contains(name)) + return SitesBlock.KING; } - return false; + + return SitesBlock.SHADOWS; } - private boolean siteBlockFilter(LotroCardBlueprint blueprint) { - if (blueprint.getSiteBlock() == _siteBlock) - return true; - return false; + private boolean exactNameMatch(LotroCardBlueprint blueprint, String title) { + return blueprint != null + && SortAndFilterCards.replaceSpecialCharacters(GameUtils.getFullName(blueprint).toLowerCase()).equals(title); } - - private void findSiteBlock(LotroCardBlueprint cardBlueprint) { - String site = cardBlueprint.getTitle(); - if (_fellowshipSiteCheck.contains(site)) - _siteBlock = SitesBlock.FELLOWSHIP; - else if (_towersSiteCheck.contains(site)) - _siteBlock = SitesBlock.TWO_TOWERS; - else if (_kingSiteCheck.contains(site)) - _siteBlock = SitesBlock.KING; + + private boolean isFromOfficialSet(String id) { + return Integer.parseInt(id.split("_")[0]) < 20; } - - private List getDecklist(String rawDecklist) { - List result = new ArrayList(); + + private List getDecklist(String rawDecklist) { + List result = new ArrayList<>(); for (String line : rawDecklist.split("~")) { if (line.length() == 0) continue; - int quantity = 1; + line = line.toLowerCase(); - String cardLine = ""; + int quantity; + + String cardLine; if (Character.isDigit(line.charAt(0))) { quantity = Character.getNumericValue(line.charAt(0)); cardLine = line.substring(line.indexOf(" ")); - } - else if (Character.isDigit(line.charAt(line.length()-1))) { - quantity = Character.getNumericValue(line.charAt(line.length()-1)); - cardLine = line.substring(0,line.indexOf(" ",line.length()-3)); - } - else { + } else if (Character.isDigit(line.charAt(line.length() - 1))) { + quantity = Character.getNumericValue(line.charAt(line.length() - 1)); + cardLine = line.substring(0, line.indexOf(" ", line.length() - 3)); + } else { quantity = 1; cardLine = line; } - for (int i = 0; i < quantity; i++) - result.add(replaceSpecialCharacters(cardLine).trim()); + result.add(new CardCount(SortAndFilterCards.replaceSpecialCharacters(cardLine).trim(), quantity)); } return result; } - private String replaceSpecialCharacters(String text) { - return text - .replace('é', 'e') - .replace('ú', 'u') - .replace('ë', 'e') - .replace('û', 'u') - .replace('ó', 'o'); - } + private static class CardCount { + private String name; + private int count; - private static boolean isPack(String blueprintId) { - return !blueprintId.contains("_"); + public CardCount(String name, int count) { + this.name = name; + this.count = count; + } + + public String getName() { + return name; + } + + public int getCount() { + return count; + } } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SortAndFilterCards.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SortAndFilterCards.java index ce100c672..380517bbc 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SortAndFilterCards.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SortAndFilterCards.java @@ -274,7 +274,7 @@ public class SortAndFilterCards { return true; } - private String replaceSpecialCharacters(String text) { + public static String replaceSpecialCharacters(String text) { return text .replace('é', 'e') .replace('ú', 'u')