diff --git a/src/main/java/setup/cardlists/MakeAllCardsList.java b/src/main/java/setup/cardlists/MakeAllCardsList.java index da22687..53ca93f 100644 --- a/src/main/java/setup/cardlists/MakeAllCardsList.java +++ b/src/main/java/setup/cardlists/MakeAllCardsList.java @@ -15,24 +15,34 @@ public class MakeAllCardsList { //This code is meant to be run using some kind of scheduler. + private static boolean test = false; - public static void main(String[] args) throws Exception{ + public static void main(String[] args) throws Exception { - final int TIMES_CHECKED = 168; //Hours in a week - //final int TIMES_CHECKED = 1; //For testing + final int TIMES_CHECKED = (test ? 1 : 168); // 168 hours per week //Create a list of set abbreviations from the provided file, and check how many times the program has been run so far. - List setAbbr; + List setAbbr; - if (!new File("MTGO_Set_Abbreviations").exists()) - { - setAbbr = FileConverter.readToList(MakeAllCardsList.class.getClassLoader().getResource("MTGO_Set_Abbreviations")); - } - else { - setAbbr = FileConverter.readToList("MTGO_Set_Abbreviations"); + if (test) { + if (!new File("MTGO_Set_Abbreviations_test").exists()) + { + setAbbr = FileConverter.readToList(MakeAllCardsList.class.getClassLoader().getResource("MTGO_Set_Abbreviations_test")); + } + else { + setAbbr = FileConverter.readToList("MTGO_Set_Abbreviations_test"); + } + } else { + if (!new File("MTGO_Set_Abbreviations").exists()) + { + setAbbr = FileConverter.readToList(MakeAllCardsList.class.getClassLoader().getResource("MTGO_Set_Abbreviations")); + } + else { + setAbbr = FileConverter.readToList("MTGO_Set_Abbreviations"); + } } - if (!new File("Count.txt").exists()) - { + + if (!new File("Count.txt").exists()) { FileConverter.writeFile(0, "Count.txt"); } int count = FileConverter.readToInt("Count.txt"); @@ -40,12 +50,11 @@ public static void main(String[] args) throws Exception{ //Increase the count of how many times this program has been run, and update the text file to reflect that. count++; - if (count < TIMES_CHECKED){ + if (count <= TIMES_CHECKED) { //In each valid run, get a snapshot of all legal cards and put it in a file FileConverter.writeFile(getLegalSnapshot(setAbbr), "Run_"+String.format("%03d", count)+".txt"); - } else if (count == TIMES_CHECKED) { //On the last run only, after all lists have been created: - - FileConverter.writeFile(getLegalSnapshot(setAbbr), "Run_" + String.format("%03d", count) + ".txt"); + } + if (count == TIMES_CHECKED) { //On the last run only, after all lists have been created: //Makes a map of "card names -> # of times it was at 0.01 tix" Map timesLegalMap = new HashMap<>(); @@ -62,19 +71,27 @@ public static void main(String[] args) throws Exception{ legalCards.add(card); } + //Add to an array all cards that were equal or below $1 50% or more of the time + List dollarLegalCards = new ArrayList<>(); + for (String card : timesLegalMap.keySet()) { + if (timesLegalMap.get(card) >= TIMES_CHECKED / 2) + dollarLegalCards.add(card); + } + //Print the arrays out as usable, readable files + legalCards.retainAll(dollarLegalCards); FileConverter.writeFile(legalCards, "legal_cards.txt"); System.out.println("File written!"); System.out.println("Starting Analysis..."); ChangeAnalyzer.main(new String[0]); - + } FileConverter.writeFile(count, "Count.txt"); } //Updates the map with the snapshot passed to it. - private static void updateMap(List snapshot, Map map){ - for (String card : snapshot){ + private static void updateMap(List snapshot, Map map) { + for (String card : snapshot) { map.merge(card, 1, (val, one) -> val + one); } } @@ -84,19 +101,29 @@ private static Set getLegalSnapshot(List setCodes) throws Except Set legalCards = new HashSet<>(); //Cycle through the "set pages" for every set available on MTGO - for (String str : setCodes){ - URL thisUrl = new URL("https://www.mtggoldfish.com/index/"+str+"#online"); + for (String str : setCodes) { + URL mtgoUrl = new URL("https://www.mtggoldfish.com/index/" + str + "#online"); + URL paperUrl = new URL("https://www.mtggoldfish.com/index/" + str + "#paper"); //Reads the webpage into an array of strings. - List webpage = FileConverter.readToList(thisUrl); + List mtgoWebpage = FileConverter.readToList(mtgoUrl); + List paperWebpage = FileConverter.readToList(paperUrl); - Map cards = getCardNamesAndPrices(webpage); + Map mtgoCards = getCardNamesAndPrices(mtgoWebpage, "online"); + Map paperCards = getCardNamesAndPrices(paperWebpage, "paper"); - cards.entrySet().stream() // + mtgoCards.entrySet().stream() // .filter(e -> e.getValue() == 0.01) // .map(Map.Entry::getKey) // - .forEach(legalCards::add); - + .forEach(legalCards::add); + + Set paperLegalCards = new HashSet<>(); + paperCards.entrySet().stream() // + .filter(e -> e.getValue() <= 1) // + .map(Map.Entry::getKey) // + .forEach(paperLegalCards::add); + legalCards.retainAll(paperLegalCards); + System.out.println("Legal cards from "+str+" found!"); } @@ -111,16 +138,16 @@ private static Set getLegalSnapshot(List setCodes) throws Except return legalCards; } - private static Map getCardNamesAndPrices(List html) { + private static Map getCardNamesAndPrices(List html, String markerStr) { Map cards = new HashMap<>(); int linesSinceMarker = -1; String lastCardFound = null; - for (String str : html){ - + for (String str : html) { linesSinceMarker++; - if (str.length() > 17 && str.substring(0, 17).equals("") && str.indexOf("#online\">") > -1){ //Looks for a marker line + String marker = "#" + markerStr + "\">"; + if (str.length() > 17 && str.substring(0, 17).equals("") && str.indexOf(marker) > -1) { //Looks for a marker line //Get the locations of the second ">" and the third "<", the card's name is located between them int secondClose = str.indexOf('>',str.indexOf('>')+1); @@ -131,21 +158,28 @@ private static Map getCardNamesAndPrices(List html) { String cardname = formatEnglishName(thisCard).replace("'", "'").replace("Lim-Dul","Lim-Dûl").replace("Jotun","Jötun"); //MTGGoldfish doesn't include the accent, but it should, so I add it. - switch (cardname){ - case "Seance": cardname = "Séance"; - break; - case "Dandan": cardname = "Dandân"; - break; - case "Khabal Ghoul": cardname = "Khabál Ghoul"; - break; - case "Junun Efreet": cardname = "Junún Efreet"; - break; - case "Ghazban Ogre": cardname = "Ghazbán Ogre"; - break; - case "Ifh-Biff Efreet": cardname = "Ifh-Bíff Efreet"; - break; - case "Ring of Ma'ruf": cardname = "Ring of Ma'rûf"; - break; + switch (cardname) { + case "Seance": + cardname = "Séance"; + break; + case "Dandan": + cardname = "Dandân"; + break; + case "Khabal Ghoul": + cardname = "Khabál Ghoul"; + break; + case "Junun Efreet": + cardname = "Junún Efreet"; + break; + case "Ghazban Ogre": + cardname = "Ghazbán Ogre"; + break; + case "Ifh-Biff Efreet": + cardname = "Ifh-Bíff Efreet"; + break; + case "Ring of Ma'ruf": + cardname = "Ring of Ma'rûf"; + break; } // Retain knowledge of the last card we found @@ -156,9 +190,9 @@ private static Map getCardNamesAndPrices(List html) { } // If we've found a card, then the third line after the marker has the card price - if (lastCardFound != null && linesSinceMarker == 4){ + if (lastCardFound != null && linesSinceMarker == 4) { cards.put(lastCardFound, Double.parseDouble(str)); - + lastCardFound = null; linesSinceMarker = -1; } @@ -167,13 +201,13 @@ private static Map getCardNamesAndPrices(List html) { return cards; } - private static String formatEnglishName(String name){ + private static String formatEnglishName(String name) { String formatted = ""; - for (int c = 0; c < name.length(); c++){ + for (int c = 0; c < name.length(); c++) { char letter = name.charAt(c); //Some cards with multiple promo printings have (second promo) or something in parens, this gets rid of that so we don't get duplicates - if(letter == '('){ + if (letter == '(') { formatted = formatted.trim(); break; } diff --git a/src/main/resources/MTGO_Set_Abbreviations_test b/src/main/resources/MTGO_Set_Abbreviations_test new file mode 100644 index 0000000..8af3bb5 --- /dev/null +++ b/src/main/resources/MTGO_Set_Abbreviations_test @@ -0,0 +1,6 @@ +RIX +XLN +HOU +AKH +AER +KLD