From 5ccf1619345735773400b60ad9a43a9cd5a586ed Mon Sep 17 00:00:00 2001 From: Arturo Date: Mon, 1 Oct 2018 23:36:46 +0200 Subject: [PATCH 1/2] Added one dollar limit --- .../setup/cardlists/MakeAllCardsList.java | 96 ++++++++++++------- .../resources/MTGO_Set_Abbreviations_test | 6 ++ 2 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 src/main/resources/MTGO_Set_Abbreviations_test diff --git a/src/main/java/setup/cardlists/MakeAllCardsList.java b/src/main/java/setup/cardlists/MakeAllCardsList.java index da22687..bd4f1cf 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); + Map paperCards = getCardNamesAndPrices(paperWebpage); - 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!"); } @@ -116,11 +143,12 @@ private static Map getCardNamesAndPrices(List html) { 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 + if (str.length() > 17 && str.substring(0, 17).equals("") && + ((str.indexOf("#online\">") > -1) || (str.indexOf("#paper\">") > -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,7 +159,7 @@ 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){ + switch (cardname) { case "Seance": cardname = "Séance"; break; case "Dandan": cardname = "Dandân"; @@ -156,9 +184,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 +195,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 From 2f809e3976967d921e23fca6ad996c349da3bb6b Mon Sep 17 00:00:00 2001 From: Arturo Date: Tue, 2 Oct 2018 21:21:52 +0200 Subject: [PATCH 2/2] Check only for paper or mtgo cards each time --- .../setup/cardlists/MakeAllCardsList.java | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main/java/setup/cardlists/MakeAllCardsList.java b/src/main/java/setup/cardlists/MakeAllCardsList.java index bd4f1cf..53ca93f 100644 --- a/src/main/java/setup/cardlists/MakeAllCardsList.java +++ b/src/main/java/setup/cardlists/MakeAllCardsList.java @@ -109,8 +109,8 @@ private static Set getLegalSnapshot(List setCodes) throws Except List mtgoWebpage = FileConverter.readToList(mtgoUrl); List paperWebpage = FileConverter.readToList(paperUrl); - Map mtgoCards = getCardNamesAndPrices(mtgoWebpage); - Map paperCards = getCardNamesAndPrices(paperWebpage); + Map mtgoCards = getCardNamesAndPrices(mtgoWebpage, "online"); + Map paperCards = getCardNamesAndPrices(paperWebpage, "paper"); mtgoCards.entrySet().stream() // .filter(e -> e.getValue() == 0.01) // @@ -138,17 +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) { - linesSinceMarker++; - if (str.length() > 17 && str.substring(0, 17).equals("") && - ((str.indexOf("#online\">") > -1) || (str.indexOf("#paper\">") > -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); @@ -160,20 +159,27 @@ private static Map getCardNamesAndPrices(List html) { //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; + 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