forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-headswithfile.mjs
More file actions
95 lines (79 loc) · 2.41 KB
/
Copy pathgenerate-headswithfile.mjs
File metadata and controls
95 lines (79 loc) · 2.41 KB
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
import { mkdir, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
const FRONT_DIRECTION = "2";
const OUTPUT_PATH = path.resolve(
process.cwd(),
"public/init/headswithfile.json",
);
function pickHeadGraphicId(headData) {
return (
headData?.[FRONT_DIRECTION] ??
headData?.["1"] ??
Object.values(headData ?? {})[0] ??
null
);
}
function resolveGraphicFrame(graphicsDB, graphicId) {
if (!graphicId) {
return null;
}
const graphic = graphicsDB[String(graphicId)];
if (!graphic) {
return null;
}
if (graphic.numFile) {
return graphic;
}
const frameId =
graphic.frames?.[FRONT_DIRECTION] ??
graphic.frames?.["1"] ??
Object.values(graphic.frames ?? {})[0] ??
null;
if (!frameId) {
return null;
}
return graphicsDB[String(frameId)] ?? null;
}
async function fetchJson(pathname) {
const filePath = path.join(process.cwd(), "public", pathname);
return JSON.parse(await readFile(filePath, "utf8"));
}
async function main() {
const [headsDB, graphicsDB] = await Promise.all([
fetchJson("/init/heads.json"),
fetchJson("/init/graficos.json"),
]);
const output = Object.fromEntries(
Object.entries(headsDB)
.sort(([left], [right]) => Number(left) - Number(right))
.flatMap(([headId, headData]) => {
const graphic = resolveGraphicFrame(
graphicsDB,
pickHeadGraphicId(headData),
);
if (!graphic) {
return [];
}
return [
[
headId,
[
String(graphic.numFile),
graphic.sX,
graphic.sY,
graphic.width,
graphic.height,
],
],
];
}),
);
await mkdir(path.dirname(OUTPUT_PATH), { recursive: true });
await writeFile(OUTPUT_PATH, JSON.stringify(output), "utf8");
console.log(`Generado ${OUTPUT_PATH}`);
console.log(`Heads procesadas: ${Object.keys(output).length}`);
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});