about summary refs log tree commit diff stats
path: root/scan-csv.js
blob: eea0e1e7c233f31512707ec15c3052e39852a255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const args = require("minimist")(process.argv.slice(2));
const fs = require("fs");
const path = require("path");
const csv = require("fast-csv");

const weathers = {
  pagos: [0, 4, 7, 3, 5, 8],
  pyros: [0, 4, 7, 3, 5, 9],
  hydatos: [0, 1, 7, 5, 6],
};

const file = args._[1];
const zone = args._[0];
parse(file, zone);

async function parse(fpath, zone) {
  const res = [];
  fs.createReadStream(path.resolve(__dirname, fpath))
    .pipe(csv.parse())
    .on("data", (row) => {
      res.push({
        level: Number(row[0]),
        name: getName(row[1]),
        type: getType(row[1]),
        elem: getElem(row[1]),
        conditions: getConditions(row.slice(2, 8), zone),
      });
    })
    .on("end", () => {
      fs.writeFileSync(
        path.resolve(__dirname, `src/${zone}.bestiary.json`),
        JSON.stringify(res),
        { encoding: "utf8" }
      );
      console.log("Done!");
    });
}

function getName(nameStr) {
  return /^([\w\s]+)/i.exec(nameStr)[1];
}

function getType(nameStr) {
  if (/\(M\)/i.test(nameStr)) return 1;
  if (/\(A\)/i.test(nameStr)) return 2;
  return 0;
}

function getElem(nameStr) {
  let res = /\/(\w)/i.exec(nameStr);
  if (res) {
    switch (res[1]) {
      case "W":
        return "Wind";
      case "E":
        return "Thunder";
      case "H":
        return "Water";
      case "F":
        return "Fire";
      case "I":
        return "Ice";
      case "G":
        return "Earth";
    }
  }
  return null;
}

function getConditions(arr, zone) {
  const res = {
    day: [],
    night: [],
  };

  arr.forEach((el, i) => {
    if (el) {
      switch (el) {
        case "D":
          res.day.push(weathers[zone][i]);
          break;
        case "N":
          res.night.push(weathers[zone][i]);
          break;
        case "A":
          res.day.push(weathers[zone][i]);
          res.night.push(weathers[zone][i]);
          break;
      }
    }
  });

  return res;
}