-
Notifications
You must be signed in to change notification settings - Fork 1
/
createMetadata.ts
91 lines (77 loc) · 2.55 KB
/
createMetadata.ts
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
import fs from "fs";
type Attribute = {
trait_type: string;
value: string | number;
};
function capitalizeFirstLetter(string: string) {
const listOfWords = string.split("_");
const capitalized = listOfWords.map((item) => {
return item.charAt(0).toUpperCase() + item.slice(1);
});
return capitalized.join(" ");
}
function format(str: string) {
return str.replaceAll("_", " ").trim();
}
function createAttributesFromObject(obj) {
const attributes: Attribute[] = [];
Object.keys(obj).forEach((key) => {
if (obj[key]) {
const item: Attribute = { trait_type: "", value: "" };
item["trait_type"] = format(key);
// Check if obj[key] is a number
if (typeof obj[key] === "number") {
item["value"] = obj[key];
} else {
item["value"] = format(obj[key]);
}
attributes.push(item);
}
});
return attributes;
}
const charactersJsonFilePath = "./characters.json";
// Create metadata directory if it doesn't exist
if (!fs.existsSync("./metadata")) {
fs.mkdirSync("./metadata");
}
// Read the characters json file
const charactersJson = fs.readFileSync(charactersJsonFilePath, "utf8");
const characters = JSON.parse(charactersJson);
// Iterate over the characters and create a metadata file for each character
Object.keys(characters).forEach((character) => {
const metadata = {};
const data = characters[character];
// Combine the layers and metadata into a single array of objects
// Start with the layers
const layerAttributes = createAttributesFromObject(data.layers);
// Get a subset of the metadata keys that are relevant for the attributes
const subset = [
"arcana",
"comms",
"grind",
"perception",
"strength",
"shadowiness",
"jbx_range",
"range_width",
].reduce((result, key) => {
if (key === "jbx_range") {
result[capitalizeFirstLetter(key)] = data.metadata[key];
} else {
result[capitalizeFirstLetter(key)] = Number(data.metadata[key]);
}
return result;
}, {});
const metadataAttributes = createAttributesFromObject(subset);
// Combine the layers and metadata into a single array of objects
const attributes = layerAttributes.concat(metadataAttributes);
metadata["name"] = format(data.metadata.name);
// TODO: Add the rest of the metadata
metadata["external_link"] = "";
metadata["description"] = data.metadata.history;
metadata["attributes"] = attributes;
// Write the metadata file
const metadataFilePath = `./metadata/${character}.json`;
fs.writeFileSync(metadataFilePath, JSON.stringify(metadata, null, 2));
});