forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameData.ts
More file actions
339 lines (308 loc) · 8.78 KB
/
Copy pathgameData.ts
File metadata and controls
339 lines (308 loc) · 8.78 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import crypto from "crypto";
import fs from "fs";
import path from "path";
export type GameObjectRecordData = {
name: string;
objType: number;
tipoPocion?: number;
minModificador?: number;
maxModificador?: number;
grhIndex: number;
anim?: number;
agarrable?: number;
valor: number;
minHit?: number;
maxHit?: number;
minDef?: number;
maxDef?: number;
newbie?: number;
proyectil?: number;
noSeCae?: number;
clasesNoPermitidas?: number[];
indexAbierta?: number;
indexCerrada?: number;
llave?: number;
cerrada?: number;
spellIndex?: number;
razaEnana?: number;
abriga?: number;
apu?: number;
porcentaje?: number;
resistenciaMagica?: number;
staffDamageBonus?: number;
magicDamageBonus?: number;
magicPenetration?: number;
minDefMag?: number;
maxDefMag?: number;
[key: string]: unknown;
};
export type GameNpcTradeEntry = { item: number; cant: number };
export type GameNpcRecordData = {
name: string;
npcType: number;
idHead: number;
idBody: number;
movement: number;
desc?: string;
aguaValida?: number;
exp?: number;
gold?: number;
hp?: number;
maxHp?: number;
minHit?: number;
maxHit?: number;
def?: number;
poderAtaque?: number;
poderEvasion?: number;
magicResistance?: number;
magicDef?: number;
defM?: number;
snd1?: number;
snd2?: number;
soundClose?: number;
drop?: GameNpcTradeEntry[];
objs?: GameNpcTradeEntry[];
[key: string]: unknown;
};
export type GameCraftingRecipeRecordData = {
id: number;
profession: "carpentry" | "blacksmith" | "tailoring";
category: string;
sortOrder?: number;
deleted?: boolean;
itemId: number;
skill: number;
materials: Array<{ itemId: number; amount: number }>;
};
export type GameSmeltingRecipeRecordData = {
id: number;
mineralItemId: number;
ingotItemId: number;
requiredSkill: number;
mineralsPerIngot: number;
};
type ObjectsById = Record<string, GameObjectRecordData>;
type NpcsById = Record<string, GameNpcRecordData>;
type CraftingRecipes = GameCraftingRecipeRecordData[];
type SmeltingRecipes = GameSmeltingRecipeRecordData[];
function resolveSeedJsonPath(
fileName: string,
compactFileName: string,
): string {
const jsonDirectory = path.resolve(__dirname, "../jsons");
const compactPath = path.join(jsonDirectory, compactFileName);
if (fs.existsSync(compactPath)) {
return compactPath;
}
return path.join(jsonDirectory, fileName);
}
const OBJECT_DEFAULTS: Record<string, unknown> = {
name: "",
objType: 0,
tipoPocion: 0,
minModificador: 0,
maxModificador: 0,
grhIndex: 0,
anim: 0,
agarrable: 0,
valor: 0,
minHit: 0,
maxHit: 0,
minDef: 0,
maxDef: 0,
newbie: 0,
proyectil: 0,
noSeCae: 0,
clasesNoPermitidas: [],
indexAbierta: 0,
indexCerrada: 0,
llave: 0,
cerrada: 0,
spellIndex: 0,
razaEnana: 0,
abriga: 0,
apu: 0,
porcentaje: 0,
resistenciaMagica: 0,
staffDamageBonus: 0,
magicDamageBonus: 0,
magicPenetration: 0,
minDefMag: 0,
maxDefMag: 0,
};
const NPC_DEFAULTS: Record<string, unknown> = {
name: "",
npcType: 0,
idHead: 0,
idBody: 0,
movement: 0,
aguaValida: 0,
exp: 0,
gold: 0,
hp: 0,
maxHp: 0,
minHit: 0,
maxHit: 0,
def: 0,
poderAtaque: 0,
poderEvasion: 0,
magicResistance: 0,
magicDef: 0,
defM: 0,
snd1: 0,
snd2: 0,
soundClose: 0,
objs: [],
drop: [],
};
function sortValue(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map(sortValue);
}
if (value && typeof value === "object") {
return Object.fromEntries(
Object.entries(value as Record<string, unknown>)
.sort(([left], [right]) => left.localeCompare(right))
.map(([key, nestedValue]) => [key, sortValue(nestedValue)]),
);
}
return value;
}
export function stableStringify(value: unknown): string {
return JSON.stringify(sortValue(value));
}
export function computeChecksum(value: unknown): string {
return crypto
.createHash("sha256")
.update(stableStringify(value))
.digest("hex");
}
export function normalizeObjectData(
data: GameObjectRecordData,
): GameObjectRecordData {
return {
...OBJECT_DEFAULTS,
...data,
clasesNoPermitidas: Array.isArray(data.clasesNoPermitidas)
? data.clasesNoPermitidas
: [],
} as GameObjectRecordData;
}
export function normalizeNpcData(data: GameNpcRecordData): GameNpcRecordData {
const normalized = {
...NPC_DEFAULTS,
...data,
objs: Array.isArray(data.objs) ? data.objs : [],
drop: Array.isArray(data.drop) ? data.drop : [],
} as GameNpcRecordData;
if (typeof data.magicDef === "number" && typeof data.defM !== "number") {
normalized.defM = data.magicDef;
}
if (typeof data.defM === "number" && typeof data.magicDef !== "number") {
normalized.magicDef = data.defM;
}
return normalized;
}
export function loadSeedObjectsJson(): Array<{
id: number;
data: GameObjectRecordData;
}> {
const filePath = resolveSeedJsonPath("objs.json", "objs.compact.json");
return loadObjectsJsonFromFile(filePath);
}
export function loadObjectsJsonFromFile(
filePath: string,
): Array<{ id: number; data: GameObjectRecordData }> {
const raw = JSON.parse(fs.readFileSync(filePath, "utf8")) as ObjectsById;
return Object.entries(raw)
.map(([id, data]) => ({
id: Number(id),
data: normalizeObjectData(data),
}))
.filter((entry) => Number.isInteger(entry.id) && entry.id > 0);
}
export function loadSeedNpcsJson(): Array<{
id: number;
data: GameNpcRecordData;
}> {
const filePath = resolveSeedJsonPath("npcs.json", "npcs.compact.json");
return loadNpcsJsonFromFile(filePath);
}
export function loadNpcsJsonFromFile(
filePath: string,
): Array<{ id: number; data: GameNpcRecordData }> {
const raw = JSON.parse(fs.readFileSync(filePath, "utf8")) as NpcsById;
return Object.entries(raw)
.map(([id, data]) => ({ id: Number(id), data: normalizeNpcData(data) }))
.filter((entry) => Number.isInteger(entry.id) && entry.id > 0);
}
export function normalizeCraftingRecipeData(
data: GameCraftingRecipeRecordData,
): GameCraftingRecipeRecordData {
return {
id: Number(data.id ?? 0),
profession: data.profession,
category: String(data.category ?? ""),
sortOrder: Number(data.sortOrder ?? data.id ?? 0),
deleted: Boolean(data.deleted ?? false),
itemId: Number(data.itemId ?? 0),
skill: Number(data.skill ?? 0),
materials: Array.isArray(data.materials)
? data.materials.map((material) => ({
itemId: Number(material.itemId ?? 0),
amount: Number(material.amount ?? 0),
}))
: [],
};
}
export function normalizeSmeltingRecipeData(
data: GameSmeltingRecipeRecordData,
): GameSmeltingRecipeRecordData {
return {
id: Number(data.id ?? 0),
mineralItemId: Number(data.mineralItemId ?? 0),
ingotItemId: Number(data.ingotItemId ?? 0),
requiredSkill: Number(data.requiredSkill ?? 0),
mineralsPerIngot: Number(data.mineralsPerIngot ?? 0),
};
}
export function loadSeedCraftingRecipesJson(): Array<{
id: number;
data: GameCraftingRecipeRecordData;
}> {
const filePath = path.resolve(__dirname, "../jsons/craftingRecipes.json");
return loadCraftingRecipesJsonFromFile(filePath);
}
export function loadCraftingRecipesJsonFromFile(
filePath: string,
): Array<{ id: number; data: GameCraftingRecipeRecordData }> {
const raw = JSON.parse(
fs.readFileSync(filePath, "utf8"),
) as CraftingRecipes;
return raw
.map((data) => ({
id: Number(data.id),
data: normalizeCraftingRecipeData(data),
}))
.filter((entry) => Number.isInteger(entry.id) && entry.id > 0);
}
export function loadSeedSmeltingRecipesJson(): Array<{
id: number;
data: GameSmeltingRecipeRecordData;
}> {
const filePath = path.resolve(__dirname, "../jsons/smeltingRecipes.json");
return loadSmeltingRecipesJsonFromFile(filePath);
}
export function loadSmeltingRecipesJsonFromFile(
filePath: string,
): Array<{ id: number; data: GameSmeltingRecipeRecordData }> {
const raw = JSON.parse(
fs.readFileSync(filePath, "utf8"),
) as SmeltingRecipes;
return raw
.map((data) => ({
id: Number(data.id),
data: normalizeSmeltingRecipeData(data),
}))
.filter((entry) => Number.isInteger(entry.id) && entry.id > 0);
}