Advertisement
dragonfree97

Untitled

Apr 1st, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Critters = require('./resources/critters.json');
  2. const fs = require("fs"); // Filesystem access
  3.  
  4. var list = {};
  5.  
  6. /* Critter handling functions */
  7.  
  8. var months = ["january","february","march","april","may","june","july","august","september","october","november","december"];
  9. var seasons = ["winter", "spring", "summer", "autumn"];
  10. var shadows = ["tiny", "small", "medium", "large", "huge", "huge with fin", "thin", "medium with fin"];
  11.  
  12. var getShadow = function(size) {
  13.     return upFirst(shadows[size-1]);
  14. }
  15.  
  16. var upFirst = function(str) {
  17.     return str.charAt(0).toUpperCase() + str.slice(1);
  18. }
  19.  
  20. var getSeason = function(month) {
  21.     month--; // makes it so we can input indexing from 1
  22.     month = (month+1)%12;
  23.     month = Math.floor(month/3)
  24.     return seasons[month];
  25. }
  26.  
  27. var formatPrice = function(price) {
  28.     return (price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " Bells";
  29. }
  30.  
  31. var monthToNum = function(month) {
  32.     return months.indexOf(month.toLowerCase())+1;
  33. }
  34. var numToMonth = function(num) {
  35.     return months[(num-1)%12];
  36. }
  37. var hemiswap = function(month, hemi) {
  38.     if (hemi == "south") {
  39.         return numToMonth(monthToNum(month)+6);
  40.     } else {
  41.         return month;
  42.     }
  43. }
  44. var isRealCritter = function(critter) {
  45.     var type;
  46.     try {
  47.         if (Critters.fishes[critter]) {
  48.             type = "fishes";
  49.         } else if (Critters.bugs[critter]) {
  50.             type = "bugs"
  51.         }
  52.     } catch (error) {
  53.         return false;
  54.     }
  55.     return type;
  56. }
  57. var getByMonth = function(month, hemi, type) {
  58.     if (hemi) {
  59.         month = hemiswap(month, hemi);
  60.     }
  61.     var relevant = [];
  62.     if (type != "bugs") {
  63.         for (let fish in Critters.fishes) {
  64.             if (Critters.fishes[fish][month]) {
  65.                 relevant.push(fish);
  66.             }
  67.         }
  68.     }
  69.     if (type != "fishes") {
  70.         for (let bug in Critters.bugs) {
  71.             if(Critters.bugs[bug][month]) {
  72.                 relevant.push(bug);
  73.             }
  74.         }
  75.     }
  76.     return relevant;
  77. }
  78. var nextMonthGone = function(month, hemi, type) {
  79.     if (hemi) {
  80.         month = hemiswap(month, hemi);
  81.     }
  82.     var relevant = [];
  83.     if (type != "bugs") {
  84.         for (let fish in Critters.fishes) {
  85.             if (Critters.fishes[fish][month] && !Critters.fishes[fish][numToMonth(monthToNum(month)+1)]) {
  86.                 relevant.push(fish);
  87.             }
  88.         }
  89.     }
  90.     if (type != "fishes") {
  91.         for (let bug in Critters.bugs) {
  92.             if (Critters.bugs[bug][month] && !Critters.bugs[bug][numToMonth(monthToNum(month)+1)]) {
  93.                 relevant.push(bug);
  94.             }
  95.         }
  96.     }
  97.     return relevant;
  98. }
  99. var thisMonthNew = function(month, hemi, type) {
  100.     if (hemi) {
  101.         month = hemiswap(month, hemi);
  102.     }
  103.     if (monthToNum(month) == 1) {
  104.         var lastMonth = numToMonth(12);
  105.     } else {
  106.         var lastMonth = numToMonth(monthToNum(month)-1);
  107.     }
  108.     var relevant = [];
  109.     if (type != "bugs") {
  110.         for (let fish in Critters.fishes) {
  111.             if (Critters.fishes[fish][month] && !Critters.fishes[fish][lastMonth]) {
  112.                 relevant.push(fish);
  113.             }
  114.         }
  115.     }
  116.     if (type != "fishes") {
  117.         for (let bug in Critters.bugs) {
  118.             if (Critters.bugs[bug][month] && !Critters.bugs[bug][lastMonth]) {
  119.                 relevant.push(bug);
  120.             }
  121.         }
  122.     }
  123.     return relevant;
  124. }
  125.  
  126. /* END Critter math functions */
  127. /* Data handling functions */
  128.  
  129. var getData = function(type, month) {
  130.     try {
  131.         list[type][month].north.total = getByMonth(month, "north", type);
  132.         list[type][month].north.newest = thisMonthNew(month, "north", type);
  133.         list[type][month].north.leaving = nextMonthGone(month, "north", type);
  134.         list[type][month].south.total = getByMonth(month, "south", type);
  135.         list[type][month].south.newest = thisMonthNew(month, "south", type);
  136.         list[type][month].south.leaving = nextMonthGone(month, "south", type);
  137.     } catch (error) {
  138.         console.error(error);
  139.         console.log("Attempting to write to list."+type+"."+month);
  140.         console.log(JSON.stringify(list));
  141.         console.log(i);
  142.     }
  143. }
  144.  
  145. /* END data handling functions */
  146. /* Formatting functions */
  147.  
  148. const tableHeader = '{| class="article-table" style="width: 100%"\n!Name\n!Image\n!Price\n!Location\n!Shadow size\n!Time\n|-\n'
  149. const tableFooter = '|}\n\n'
  150.  
  151. var createRow = function(name, type) {
  152.     // console.log("Type: "+type+"\nName: "+name);
  153.     critter = Critters[type][name];
  154.     if (type == "fishes") {
  155.         return "| "+critter.name+"\n| [[File:NH-Icon-"+name+".png|64px]]\n| "+formatPrice(critter.price)+"\n| "+critter["location"]+"\n| "+getShadow(critter.shadow)+"\n| "+critter.time+"\n|-\n";
  156.     } else {
  157.         return "| "+critter.name+"\n| [[File:NH-Icon-"+name+".png|64px]]\n| "+formatPrice(critter.price)+"\n| "+critter["location"]+"\n| "+critter.time+"\n|-\n";
  158.     }
  159. }
  160.  
  161. var createTable = function(input, type) {
  162.     var table = tableHeader;
  163.     for (var i = 0; i < input.length; i++) {
  164.         table = table + createRow(input[i], type);
  165.     }
  166.     table = table + tableFooter;
  167.     return table;
  168. }
  169.  
  170. var createIntro = function(month, type) {
  171.     if (type == "fishes") {
  172.         return "{{NH}} has a total fish count of 80. In the northern hemisphere, "+getByMonth(month, "north", "fishes").length+" can be caught in "+upFirst(month)+", of which "+thisMonthNew(month, "north", "fishes").length+" are new in "+upFirst(month)+", and "+nextMonthGone(month, "north", "fishes").length+" will leave after "+upFirst(month)+". In the southern hemisphere, "+getByMonth(month, "south", "fishes").length+" can be caught in "+upFirst(month)+", of which "+thisMonthNew(month, "south", "fishes").length+" are new in "+upFirst(month)+", and "+nextMonthGone(month, "south", "fishes").length+" will leave after "+upFirst(month)+".\n\n{{Critter Other Months}}\n\n";
  173.     } else {
  174.         return "{{NH}} has a total bug count of 80. In the northern hemisphere, "+getByMonth(month, "north", "bugs").length+" can be caught in "+upFirst(month)+", of which "+thisMonthNew(month, "north", "bugs").length+" are new in "+upFirst(month)+", and "+nextMonthGone(month, "north", "bugs").length+" will leave after "+upFirst(month)+". In the southern hemisphere, "+getByMonth(month, "south", "bugs").length+" can be caught in "+upFirst(month)+", of which "+thisMonthNew(month, "south", "bugs").length+" are new in "+upFirst(month)+", and "+nextMonthGone(month, "south", "bugs").length+" will leave after "+upFirst(month)+".\n\n{{Critter Other Months}}\n\n";
  175.     }
  176. }
  177.  
  178. var header = function(txt) {
  179.     return "=== "+txt+" ===\n\n";
  180. }
  181.  
  182. var createSection = function(month, type, hemi) {
  183.     var section = "== "+upFirst(hemi)+"ern hemisphere ==\n\n"; //create header
  184.    
  185.     section += header("Available in "+upFirst(month));
  186.     section += createTable(getByMonth(month, hemi, type), type);
  187.    
  188.     section += header("New in "+upFirst(month));
  189.     section += createTable(thisMonthNew(month, hemi, type), type);
  190.    
  191.     section += header("Leaving after "+upFirst(month));
  192.     section += createTable(nextMonthGone(month, hemi, type), type);
  193.    
  194.     return section;
  195. }
  196.  
  197. var createPage = function(month, type) {
  198.     var page = "";
  199.     page = page + createIntro(month, type);
  200.     page = page + createSection(month, type, "north");
  201.     page = page + createSection(month, type, "south");
  202.     page = page + "[[Category:Guides]]";
  203.     return page;
  204. }
  205.  
  206. /* END formatting functions */
  207. /* Main loop */
  208.  
  209. var main = function() {
  210.     list.fishes = {}
  211.     list.bugs = {}
  212.     for (var i = 0; i < months.length; i++) {
  213.         var m = months[i]
  214.         list.fishes[m] = {
  215.             north: {},
  216.             south: {}
  217.         }
  218.         list.bugs[m] = {
  219.             north: {},
  220.             south: {}
  221.         }
  222.         getData("fishes", m);
  223.         getData("bugs", m);
  224.        
  225.         // April fish list (New Horizons)
  226.         var filename_fish = "./output/"+upFirst(m)+" fish list (New Horizons).txt";
  227.         var filename_bugs = "./output/"+upFirst(m)+" bugs list (New Horizons).txt";
  228.        
  229.         console.log(filename_fish);
  230.         var page_fish = createPage(m, "fishes");
  231.         var page_bugs = createPage(m, "bugs")
  232.        
  233.         fs.writeFile(filename_fish, page_fish, (err) => {
  234.             if (err) console.error(err)
  235.         });
  236.         fs.writeFile(filename_bugs, page_bugs, (err) => {
  237.             if (err) console.error(err)
  238.         });
  239.     }
  240. }
  241.  
  242. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement