Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Critters = require('./resources/critters.json');
- const fs = require("fs"); // Filesystem access
- var list = {};
- /* Critter handling functions */
- var months = ["january","february","march","april","may","june","july","august","september","october","november","december"];
- var seasons = ["winter", "spring", "summer", "autumn"];
- var shadows = ["tiny", "small", "medium", "large", "huge", "huge with fin", "thin", "medium with fin"];
- var getShadow = function(size) {
- return upFirst(shadows[size-1]);
- }
- var upFirst = function(str) {
- return str.charAt(0).toUpperCase() + str.slice(1);
- }
- var getSeason = function(month) {
- month--; // makes it so we can input indexing from 1
- month = (month+1)%12;
- month = Math.floor(month/3)
- return seasons[month];
- }
- var formatPrice = function(price) {
- return (price).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " Bells";
- }
- var monthToNum = function(month) {
- return months.indexOf(month.toLowerCase())+1;
- }
- var numToMonth = function(num) {
- return months[(num-1)%12];
- }
- var hemiswap = function(month, hemi) {
- if (hemi == "south") {
- return numToMonth(monthToNum(month)+6);
- } else {
- return month;
- }
- }
- var isRealCritter = function(critter) {
- var type;
- try {
- if (Critters.fishes[critter]) {
- type = "fishes";
- } else if (Critters.bugs[critter]) {
- type = "bugs"
- }
- } catch (error) {
- return false;
- }
- return type;
- }
- var getByMonth = function(month, hemi, type) {
- if (hemi) {
- month = hemiswap(month, hemi);
- }
- var relevant = [];
- if (type != "bugs") {
- for (let fish in Critters.fishes) {
- if (Critters.fishes[fish][month]) {
- relevant.push(fish);
- }
- }
- }
- if (type != "fishes") {
- for (let bug in Critters.bugs) {
- if(Critters.bugs[bug][month]) {
- relevant.push(bug);
- }
- }
- }
- return relevant;
- }
- var nextMonthGone = function(month, hemi, type) {
- if (hemi) {
- month = hemiswap(month, hemi);
- }
- var relevant = [];
- if (type != "bugs") {
- for (let fish in Critters.fishes) {
- if (Critters.fishes[fish][month] && !Critters.fishes[fish][numToMonth(monthToNum(month)+1)]) {
- relevant.push(fish);
- }
- }
- }
- if (type != "fishes") {
- for (let bug in Critters.bugs) {
- if (Critters.bugs[bug][month] && !Critters.bugs[bug][numToMonth(monthToNum(month)+1)]) {
- relevant.push(bug);
- }
- }
- }
- return relevant;
- }
- var thisMonthNew = function(month, hemi, type) {
- if (hemi) {
- month = hemiswap(month, hemi);
- }
- if (monthToNum(month) == 1) {
- var lastMonth = numToMonth(12);
- } else {
- var lastMonth = numToMonth(monthToNum(month)-1);
- }
- var relevant = [];
- if (type != "bugs") {
- for (let fish in Critters.fishes) {
- if (Critters.fishes[fish][month] && !Critters.fishes[fish][lastMonth]) {
- relevant.push(fish);
- }
- }
- }
- if (type != "fishes") {
- for (let bug in Critters.bugs) {
- if (Critters.bugs[bug][month] && !Critters.bugs[bug][lastMonth]) {
- relevant.push(bug);
- }
- }
- }
- return relevant;
- }
- /* END Critter math functions */
- /* Data handling functions */
- var getData = function(type, month) {
- try {
- list[type][month].north.total = getByMonth(month, "north", type);
- list[type][month].north.newest = thisMonthNew(month, "north", type);
- list[type][month].north.leaving = nextMonthGone(month, "north", type);
- list[type][month].south.total = getByMonth(month, "south", type);
- list[type][month].south.newest = thisMonthNew(month, "south", type);
- list[type][month].south.leaving = nextMonthGone(month, "south", type);
- } catch (error) {
- console.error(error);
- console.log("Attempting to write to list."+type+"."+month);
- console.log(JSON.stringify(list));
- console.log(i);
- }
- }
- /* END data handling functions */
- /* Formatting functions */
- const tableHeader = '{| class="article-table" style="width: 100%"\n!Name\n!Image\n!Price\n!Location\n!Shadow size\n!Time\n|-\n'
- const tableFooter = '|}\n\n'
- var createRow = function(name, type) {
- // console.log("Type: "+type+"\nName: "+name);
- critter = Critters[type][name];
- if (type == "fishes") {
- 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";
- } else {
- return "| "+critter.name+"\n| [[File:NH-Icon-"+name+".png|64px]]\n| "+formatPrice(critter.price)+"\n| "+critter["location"]+"\n| "+critter.time+"\n|-\n";
- }
- }
- var createTable = function(input, type) {
- var table = tableHeader;
- for (var i = 0; i < input.length; i++) {
- table = table + createRow(input[i], type);
- }
- table = table + tableFooter;
- return table;
- }
- var createIntro = function(month, type) {
- if (type == "fishes") {
- 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";
- } else {
- 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";
- }
- }
- var header = function(txt) {
- return "=== "+txt+" ===\n\n";
- }
- var createSection = function(month, type, hemi) {
- var section = "== "+upFirst(hemi)+"ern hemisphere ==\n\n"; //create header
- section += header("Available in "+upFirst(month));
- section += createTable(getByMonth(month, hemi, type), type);
- section += header("New in "+upFirst(month));
- section += createTable(thisMonthNew(month, hemi, type), type);
- section += header("Leaving after "+upFirst(month));
- section += createTable(nextMonthGone(month, hemi, type), type);
- return section;
- }
- var createPage = function(month, type) {
- var page = "";
- page = page + createIntro(month, type);
- page = page + createSection(month, type, "north");
- page = page + createSection(month, type, "south");
- page = page + "[[Category:Guides]]";
- return page;
- }
- /* END formatting functions */
- /* Main loop */
- var main = function() {
- list.fishes = {}
- list.bugs = {}
- for (var i = 0; i < months.length; i++) {
- var m = months[i]
- list.fishes[m] = {
- north: {},
- south: {}
- }
- list.bugs[m] = {
- north: {},
- south: {}
- }
- getData("fishes", m);
- getData("bugs", m);
- // April fish list (New Horizons)
- var filename_fish = "./output/"+upFirst(m)+" fish list (New Horizons).txt";
- var filename_bugs = "./output/"+upFirst(m)+" bugs list (New Horizons).txt";
- console.log(filename_fish);
- var page_fish = createPage(m, "fishes");
- var page_bugs = createPage(m, "bugs")
- fs.writeFile(filename_fish, page_fish, (err) => {
- if (err) console.error(err)
- });
- fs.writeFile(filename_bugs, page_bugs, (err) => {
- if (err) console.error(err)
- });
- }
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement