-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilddb.js
109 lines (86 loc) · 3.14 KB
/
builddb.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* This does basic validation of data along with flattening the data into
* single objects.
*/
const fs = require("fs");
const path = require(`path`);
const setBonus = require("./src/data/set_bonus.json");
const possibleEquipmentParts = require("./src/data/equipment_part.json");
//generate skills json db
const skills = {};
const skillFiles = fs.readdirSync(path.resolve(`src/data/skill`));
skillFiles.forEach(filename => {
const file = fs.readFileSync(path.resolve(`src/data/skill/${filename}`));
const skill = JSON.parse(file);
skills[skill.name] = skill;
});
fs.writeFileSync(`src/data/skill.json`, JSON.stringify(skills, null, 2));
const output = [];
//generate equipment json db
const equipmentFiles = fs.readdirSync(path.resolve(`src/data/equipment`));
//go through each registered piece of equipment
equipmentFiles.forEach(filename => {
const file = fs.readFileSync(path.resolve(`src/data/equipment/${filename}`));
const equipment = JSON.parse(file);
equipment.pieces.forEach(piece => {
//check the piece's skills to see if they're all valid skill names
piece.skills.forEach(skill => {
if (!skills[skill.name]) {
throw Error(`${skill.name} is an invalid skill name in ${filename}`);
}
});
//check to see that equipment parts are valid
const validPart = possibleEquipmentParts.includes(piece.part);
if (!validPart) {
throw Error(`${piece.part} is an invalid part in ${filename}`);
}
//TODO do extra validation if needed
//flatten the data structure. End result is an array of individual pieces
//that we can easily filter through
const flattened = {
...piece,
rank: equipment.rank,
bonuses: equipment.bonuses,
set: equipment.set
};
output.push(flattened);
});
});
//we write charms to the equipment object now
//now proceed to write charms file
const charmFiles = fs.readdirSync(path.resolve(`src/data/charm`));
charmFiles.forEach(filename => {
const file = fs.readFileSync(path.resolve(`src/data/charm/${filename}`));
const charm = JSON.parse(file);
charm.part = "charm";
charm.slots = [];
charm.set = "";
output.push(charm);
});
//write the flattened output to disk
fs.writeFileSync(`src/data/equipment.json`, JSON.stringify(output, null, 2));
//now we get decorations
const decorations = [];
const decorationFiles = fs.readdirSync(path.resolve(`src/data/decoration`));
decorationFiles.forEach(filename => {
const file = fs.readFileSync(path.resolve(`src/data/decoration/${filename}`));
const decoration = JSON.parse(file);
decorations.push(decoration);
});
fs.writeFileSync(
`src/data/decoration.json`,
JSON.stringify(decorations, null, 2)
);
//generate weapon db. Split into weapon types.
const weapons = {};
const weaponFiles = fs.readdirSync(path.resolve(`src/data/weapon`));
weaponFiles.forEach(filename => {
const file = fs.readFileSync(path.resolve(`src/data/weapon/${filename}`));
const weapon = JSON.parse(file);
//initialize weapon type
if (!weapons[weapon.type]) {
weapons[weapon.type] = [];
}
weapons[weapon.type].push(weapon);
});
fs.writeFileSync(`src/data/weapon.json`, JSON.stringify(weapons, null, 2));