forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportGameData.ts
More file actions
145 lines (122 loc) · 4.8 KB
/
Copy pathimportGameData.ts
File metadata and controls
145 lines (122 loc) · 4.8 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import path from "path";
import pool from "../db";
import {
loadCraftingRecipesJsonFromFile,
loadNpcsJsonFromFile,
loadObjectsJsonFromFile,
loadSeedCraftingRecipesJson,
loadSeedNpcsJson,
loadSeedObjectsJson,
loadSeedSmeltingRecipesJson,
loadSmeltingRecipesJsonFromFile,
} from "../lib/gameData";
import { upsertGameCraftingRecipe } from "../repositories/gameCraftingRecipes";
import { upsertGameNpc } from "../repositories/gameNpcs";
import { upsertGameObject } from "../repositories/gameObjects";
import { upsertGameSmeltingRecipe } from "../repositories/gameSmeltingRecipes";
function getOptionValue(name: string): string | null {
const index = process.argv.findIndex((argument) => argument === `--${name}`);
if (index < 0) {
return null;
}
const nextValue = process.argv[index + 1];
return nextValue?.trim() ? nextValue.trim() : null;
}
function resolveOptionalPath(value: string | null): string | null {
return value ? path.resolve(process.cwd(), value) : null;
}
async function importObjects(filePath?: string | null): Promise<{ total: number; changed: number; unchanged: number }> {
const rows = filePath ? loadObjectsJsonFromFile(filePath) : loadSeedObjectsJson();
let changed = 0;
let unchanged = 0;
for (const row of rows) {
const result = await upsertGameObject(row.id, row.data);
if (result.unchanged) {
unchanged += 1;
} else {
changed += 1;
}
}
return { total: rows.length, changed, unchanged };
}
async function importNpcs(filePath?: string | null): Promise<{ total: number; changed: number; unchanged: number }> {
const rows = filePath ? loadNpcsJsonFromFile(filePath) : loadSeedNpcsJson();
let changed = 0;
let unchanged = 0;
for (const row of rows) {
const result = await upsertGameNpc(row.id, row.data);
if (result.unchanged) {
unchanged += 1;
} else {
changed += 1;
}
}
return { total: rows.length, changed, unchanged };
}
async function importCraftingRecipes(filePath?: string | null): Promise<{ total: number; changed: number; unchanged: number }> {
const rows = filePath ? loadCraftingRecipesJsonFromFile(filePath) : loadSeedCraftingRecipesJson();
let changed = 0;
let unchanged = 0;
for (const row of rows) {
const result = await upsertGameCraftingRecipe(row.id, row.data);
if (result.unchanged) unchanged += 1;
else changed += 1;
}
return { total: rows.length, changed, unchanged };
}
async function importSmeltingRecipes(filePath?: string | null): Promise<{ total: number; changed: number; unchanged: number }> {
const rows = filePath ? loadSmeltingRecipesJsonFromFile(filePath) : loadSeedSmeltingRecipesJson();
let changed = 0;
let unchanged = 0;
for (const row of rows) {
const result = await upsertGameSmeltingRecipe(row.id, row.data);
if (result.unchanged) unchanged += 1;
else changed += 1;
}
return { total: rows.length, changed, unchanged };
}
async function main(): Promise<void> {
const positionalArgs = process.argv.slice(2).filter((argument) => argument !== "--");
const mode = (positionalArgs[0] ?? "all").trim().toLowerCase();
const objectsPath = resolveOptionalPath(getOptionValue("objects-path"));
const npcsPath = resolveOptionalPath(getOptionValue("npcs-path"));
const craftingPath = resolveOptionalPath(getOptionValue("crafting-path"));
const smeltingPath = resolveOptionalPath(getOptionValue("smelting-path"));
if (!["all", "objs", "npcs", "crafting", "smelting"].includes(mode)) {
throw new Error(
"Uso: pnpm import-game-data [all|objs|npcs|crafting|smelting] [--objects-path ruta] [--npcs-path ruta] [--crafting-path ruta] [--smelting-path ruta]",
);
}
if (mode === "all" || mode === "objs") {
const objects = await importObjects(objectsPath);
console.log(
`Objects importados. Total: ${objects.total}. Nuevos/actualizados: ${objects.changed}. Sin cambios: ${objects.unchanged}.`,
);
}
if (mode === "all" || mode === "npcs") {
const npcs = await importNpcs(npcsPath);
console.log(
`NPCs importados. Total: ${npcs.total}. Nuevos/actualizados: ${npcs.changed}. Sin cambios: ${npcs.unchanged}.`,
);
}
if (mode === "all" || mode === "crafting") {
const crafting = await importCraftingRecipes(craftingPath);
console.log(
`Crafting importado. Total: ${crafting.total}. Nuevos/actualizados: ${crafting.changed}. Sin cambios: ${crafting.unchanged}.`,
);
}
if (mode === "all" || mode === "smelting") {
const smelting = await importSmeltingRecipes(smeltingPath);
console.log(
`Fundicion importada. Total: ${smelting.total}. Nuevos/actualizados: ${smelting.changed}. Sin cambios: ${smelting.unchanged}.`,
);
}
}
void main()
.catch((error) => {
console.error("No se pudo importar game data", error);
process.exit(1);
})
.finally(async () => {
await pool.end();
});