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