Advertisement
wartab

swshparser

Nov 12th, 2019 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let fs = require("fs");
  2.  
  3. let encounters = {};
  4.  
  5. let lines = fs.readFileSync("./overworld.txt", "utf-8")
  6.     .split("\n")
  7.     .filter(l => l.trim());
  8.  
  9.  
  10. let location = null;
  11. let condition = null;
  12. let levelRange = null;
  13.  
  14.  
  15. for (let i = 0; i < lines.length; ++i) {
  16.     let line = lines[i];
  17.  
  18.     if (line.startsWith("\t\t")) {
  19.         line = line.trim();
  20.  
  21.         let matches = line.match(/- (.*)[ ]*\t(\d+)%/);
  22.  
  23.         if (!matches) {
  24.             console.error("Error parsing line " + i);
  25.             console.error(lines[i]);
  26.             break;
  27.         }
  28.  
  29.         let poke = matches[1].trim();
  30.         let odds = parseInt(matches[2], 10);
  31.  
  32.         if (!encounters[poke]) {
  33.             encounters[poke] = [];
  34.         }
  35.  
  36.         encounters[poke].push({
  37.             location: location,
  38.             condition: condition,
  39.             levelRange: levelRange,
  40.             odds: odds,
  41.             overworld: true,
  42.         });
  43.     } else if (line.startsWith("\t")) {
  44.         line = line.trim();
  45.         let matches = line.match(/(.*) \(Lv\. (\d+-\d+)\)/);
  46.         if (!matches) {
  47.             console.error("Error parsing line " + i);
  48.             console.error(lines[i]);
  49.             break;
  50.         }
  51.  
  52.         condition = matches[1];
  53.         levelRange = matches[2];
  54.     } else {
  55.         location = line.trim().substr(0, line.length - 2);
  56.     }
  57. }
  58.  
  59. lines = fs.readFileSync("./non-overworld.txt", "utf-8")
  60.     .split("\n")
  61.     .filter(l => l.trim());
  62.  
  63.  
  64. location = null;
  65. condition = null;
  66. levelRange = null;
  67.  
  68.  
  69. for (let i = 0; i < lines.length; ++i) {
  70.     let line = lines[i];
  71.  
  72.     if (line.startsWith("\t\t")) {
  73.         line = line.trim();
  74.  
  75.         let matches = line.match(/- (.*)[ ]*\t(\d+)%/);
  76.  
  77.         if (!matches) {
  78.             console.error("Error parsing line " + i);
  79.             console.error(lines[i]);
  80.             break;
  81.         }
  82.  
  83.         let poke = matches[1].trim();
  84.         let odds = parseInt(matches[2], 10);
  85.  
  86.         if (!encounters[poke]) {
  87.             encounters[poke] = [];
  88.         }
  89.  
  90.         encounters[poke].push({
  91.             location: location,
  92.             condition: condition,
  93.             levelRange: levelRange,
  94.             odds: odds,
  95.             overworld: false,
  96.         });
  97.     } else if (line.startsWith("\t")) {
  98.         line = line.trim();
  99.         let matches = line.match(/(.*) \(Lv\. (\d+-\d+)\)/);
  100.         if (!matches) {
  101.             console.error("Error parsing line " + i);
  102.             console.error(lines[i]);
  103.             break;
  104.         }
  105.  
  106.         condition = matches[1];
  107.         levelRange = matches[2];
  108.     } else {
  109.         location = line.trim().substr(0, line.length - 2);
  110.     }
  111. }
  112.  
  113. let finalResult = [];
  114.  
  115. for (let poke in encounters) {
  116.     if (encounters.hasOwnProperty(poke)) {
  117.         finalResult.push({
  118.             poke: poke,
  119.             encounters: encounters[poke],
  120.         });
  121.     }
  122. }
  123.  
  124. finalResult.sort((a, b) => a.poke.localeCompare(b.poke));
  125.  
  126. // fs.writeFileSync("sword.json", JSON.stringify(finalResult, undefined, 2));
  127. fs.writeFileSync("shield.json", JSON.stringify(finalResult, undefined, 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement