forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiki.ts
More file actions
840 lines (756 loc) · 25.6 KB
/
Copy pathwiki.ts
File metadata and controls
840 lines (756 loc) · 25.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
import { existsSync } from "fs";
import fs from "fs/promises";
import path from "path";
import { getCurrentGameNpcVersion, listGameNpcChangesSince } from "./gameNpcs";
import {
getCurrentGameObjectVersion,
listGameObjectChangesSince,
} from "./gameObjects";
import {
getCurrentGameCraftingRecipeVersion,
listGameCraftingRecipeChangesSince,
} from "./gameCraftingRecipes";
import {
getCurrentGameSmeltingRecipeVersion,
listGameSmeltingRecipeChangesSince,
} from "./gameSmeltingRecipes";
import {
loadAllMapNpcPlacements,
type MapNpcPlacement,
} from "../lib/mapNpcStorage";
type WikiMapMetadata = {
id: number;
name: string;
terreno: string;
zona: string;
restringir: string;
minLevel: number;
maxLevel: number;
pk: boolean;
};
type WikiMapReference = {
mapId: number;
mapName: string;
spawns: number;
};
type WikiItemReference = {
itemId: number;
itemName: string;
quantity: number;
};
type WikiNpcReference = {
npcId: number;
npcName: string;
quantity: number;
};
type WikiSpellSourceReference = {
itemId: number;
itemName: string;
soldBy: WikiNpcReference[];
droppedBy: WikiNpcReference[];
};
type WikiNpcEntry = {
id: number;
name: string;
npcType: number;
npcTypeLabel: string;
description: string;
maxHp: number;
expReward: number;
goldReward: number;
minHit: number;
maxHit: number;
defense: number;
expPerHp: number | null;
isCombatNpc: boolean;
maps: WikiMapReference[];
sells: WikiItemReference[];
drops: WikiItemReference[];
totalSpawns: number;
searchIndex: string;
};
type WikiEquipmentCategory =
| "weapon"
| "armor"
| "shield"
| "helmet"
| "magic_weapon";
type WikiEquipmentEntry = {
id: number;
name: string;
grhIndex: number;
objType: number;
objTypeLabel: string;
category: WikiEquipmentCategory;
categoryLabel: string;
value: number;
minHit: number;
maxHit: number;
minDef: number;
maxDef: number;
tier: number;
newbie: boolean;
blockedClasses: number[];
soldBy: WikiNpcReference[];
droppedBy: WikiNpcReference[];
searchIndex: string;
};
type WikiSpellEntry = {
id: number;
name: string;
description: string;
type: number;
typeLabel: string;
minSkill: number;
manaRequired: number;
target: number;
words: string;
minPower: number;
maxPower: number;
sources: WikiSpellSourceReference[];
searchIndex: string;
};
type WikiTrainingMapEntry = WikiMapMetadata & {
totalNpcSpawns: number;
totalCombatSpawns: number;
uniqueCombatNpcs: number;
topNpcs: Array<{
npcId: number;
npcName: string;
spawns: number;
expReward: number;
maxHp: number;
}>;
searchIndex: string;
};
export type PublicWikiResponse = {
generatedAt: string;
stats: {
npcCount: number;
combatNpcCount: number;
equipmentCount: number;
spellCount: number;
trainingMapCount: number;
};
npcs: WikiNpcEntry[];
equipment: WikiEquipmentEntry[];
spells: WikiSpellEntry[];
trainingMaps: WikiTrainingMapEntry[];
};
const CURRENT_EXP_MULTIPLIER = 5;
const CURRENT_GOLD_MULTIPLIER = 3;
function resolveApiDataPath(): string {
const candidates = [
path.resolve(__dirname, ".."),
path.resolve(__dirname, "..", "..", "src"),
];
for (const candidate of candidates) {
if (
existsSync(path.join(candidate, "jsons", "spells.json")) &&
existsSync(path.join(candidate, "mapas_source"))
) {
return candidate;
}
}
return candidates[0];
}
const API_DATA_PATH = resolveApiDataPath();
const MAPS_SOURCE_PATH = path.join(API_DATA_PATH, "mapas_source");
const SPELLS_PATH = path.join(API_DATA_PATH, "jsons", "spells.json");
const NPC_TYPE_LABELS: Record<number, string> = {
0: "Comun",
1: "Sacerdote",
2: "Guardia",
3: "Entrenador",
4: "Banquero",
5: "Noble",
6: "Dragon",
7: "Timbero",
8: "Guardia del Caos",
9: "Sacerdote Newbie",
10: "Comerciante",
};
const OBJECT_TYPE_LABELS: Record<number, string> = {
1: "Comida",
2: "Armas",
3: "Armaduras",
4: "Arboles",
5: "Dinero",
6: "Puerta",
7: "Contenedor",
8: "Carteles",
9: "Llaves",
10: "Foros",
11: "Pociones",
12: "Libros",
13: "Bebidas",
14: "Lenia",
15: "Fogata",
16: "Escudos",
17: "Cascos",
18: "Anillos",
19: "Teleport",
20: "Muebles",
21: "Joyas",
22: "Yacimientos",
23: "Metales",
24: "Pergaminos",
25: "Aura",
26: "Instrumentos",
27: "Yunque",
28: "Fraguas",
29: "Gemas",
30: "Flores",
31: "Barcos",
32: "Flechas",
33: "Botellas vacias",
34: "Botellas llenas",
35: "Manchas",
};
function getNpcTypeLabel(npcType: number): string {
return NPC_TYPE_LABELS[npcType] ?? `Tipo ${npcType}`;
}
function getObjectTypeLabel(objType: number): string {
return OBJECT_TYPE_LABELS[objType] ?? `Tipo ${objType}`;
}
function getEquipmentCategory(objType: number): {
key: WikiEquipmentCategory;
label: string;
} | null {
switch (objType) {
case 2:
return { key: "weapon", label: "Armas" };
case 3:
return { key: "armor", label: "Armaduras" };
case 16:
return { key: "shield", label: "Escudos" };
case 17:
return { key: "helmet", label: "Cascos" };
default:
return null;
}
}
function isMagicWeapon(
data: Record<string, unknown>,
objType: number,
): boolean {
const normalizedName = normalizeSearchText(String(data.name ?? ""));
return (
objType === 26 ||
normalizedName.includes("baston") ||
normalizedName.includes("laud") ||
normalizedName.includes("flauta")
);
}
function getSpellTypeLabel(type: number): string {
switch (type) {
case 1:
return "Activo";
case 2:
return "Utilidad";
default:
return `Tipo ${type}`;
}
}
function toNumber(value: unknown): number {
return typeof value === "number" && Number.isFinite(value) ? value : 0;
}
function normalizeSearchText(value: string): string {
return value
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase();
}
function isCombatNpc(data: Record<string, unknown>): boolean {
const maxHp = toNumber(data.maxHp) || toNumber(data.hp);
return (
maxHp > 0 &&
(toNumber(data.exp) > 0 ||
toNumber(data.gold) > 0 ||
toNumber(data.minHit) > 0 ||
toNumber(data.maxHit) > 0 ||
toNumber(data.poderAtaque) > 0)
);
}
function isLocalMapName(name: string): boolean {
return normalizeSearchText(name).includes("local");
}
function isTrainingMapCandidate(map: WikiMapMetadata): boolean {
const normalizedName = normalizeSearchText(map.name);
return (
map.zona !== "CIUDAD" &&
map.restringir !== "GM" &&
!normalizedName.includes("local") &&
!normalizedName.includes("gms")
);
}
async function readMapNpcSpawns(): Promise<MapNpcPlacement[]> {
return loadAllMapNpcPlacements(MAPS_SOURCE_PATH);
}
async function readMapMetadata(): Promise<Map<number, WikiMapMetadata>> {
const metadata = new Map<number, WikiMapMetadata>();
if (!existsSync(MAPS_SOURCE_PATH)) {
return metadata;
}
const entries = await fs.readdir(MAPS_SOURCE_PATH, { withFileTypes: true });
await Promise.all(
entries
.filter(
(entry) => entry.isDirectory() && /^mapa_\d+$/.test(entry.name),
)
.map(async (entry) => {
const match = entry.name.match(/^mapa_(\d+)$/);
if (!match) {
return;
}
const mapId = Number(match[1]);
const metaFilePath = path.join(
MAPS_SOURCE_PATH,
entry.name,
"meta.json",
);
if (!existsSync(metaFilePath)) {
return;
}
const rawFile = await fs.readFile(metaFilePath, "utf8");
const parsed = JSON.parse(rawFile) as Record<string, unknown>;
metadata.set(mapId, {
id: mapId,
name: String(parsed.name ?? `Mapa ${mapId}`),
terreno: String(parsed.terreno ?? "-"),
zona: String(parsed.zona ?? "-"),
restringir: String(parsed.restringir ?? "No"),
minLevel: Number(parsed.minLevel ?? 0),
maxLevel: Number(parsed.maxLevel ?? 0),
pk: Number(parsed.pk ?? 0) === 1,
});
}),
);
return metadata;
}
async function readSpells(): Promise<
Array<{
id: number;
data: Record<string, unknown>;
}>
> {
const rawFile = await fs.readFile(SPELLS_PATH, "utf8");
const parsed = JSON.parse(rawFile) as Record<
string,
Record<string, unknown>
>;
return Object.entries(parsed)
.map(([id, data]) => ({ id: Number(id), data }))
.filter((entry) => Number.isInteger(entry.id) && entry.id > 0);
}
function buildSearchIndex(parts: Array<string | number>): string {
return normalizeSearchText(parts.join(" "));
}
type WikiCacheEntry = {
signature: string;
payload: PublicWikiResponse;
};
let wikiCache: WikiCacheEntry | null = null;
async function getWikiVersionSignature(): Promise<string> {
const [npcVersion, objectVersion, craftingVersion, smeltingVersion] =
await Promise.all([
getCurrentGameNpcVersion(),
getCurrentGameObjectVersion(),
getCurrentGameCraftingRecipeVersion(),
getCurrentGameSmeltingRecipeVersion(),
]);
return [npcVersion, objectVersion, craftingVersion, smeltingVersion].join(
":",
);
}
export async function getPublicWiki(): Promise<PublicWikiResponse> {
const signature = await getWikiVersionSignature();
if (wikiCache?.signature === signature) {
return wikiCache.payload;
}
const [
npcChanges,
objectChanges,
craftingChanges,
smeltingChanges,
mapMetadataById,
spellsData,
] = await Promise.all([
listGameNpcChangesSince(0),
listGameObjectChangesSince(0),
listGameCraftingRecipeChangesSince(0),
listGameSmeltingRecipeChangesSince(0),
readMapMetadata(),
readSpells(),
]);
const objectsById = new Map(
objectChanges.changes.map((entry) => [entry.id, entry.data]),
);
const spawns = await readMapNpcSpawns();
const npcMapCounts = new Map<number, Map<number, number>>();
const mapNpcCounts = new Map<number, Map<number, number>>();
const npcIdsInMaps = new Set<number>();
for (const spawn of spawns) {
const npcId = Number(spawn.npcIndex ?? 0);
const mapId = Number(spawn.mapNum ?? 0);
if (!npcId || !mapId) {
continue;
}
const mapName = mapMetadataById.get(mapId)?.name;
if (mapName && isLocalMapName(mapName)) {
continue;
}
npcIdsInMaps.add(npcId);
const npcMaps = npcMapCounts.get(npcId) ?? new Map<number, number>();
npcMaps.set(mapId, (npcMaps.get(mapId) ?? 0) + 1);
npcMapCounts.set(npcId, npcMaps);
const mapNpcs = mapNpcCounts.get(mapId) ?? new Map<number, number>();
mapNpcs.set(npcId, (mapNpcs.get(npcId) ?? 0) + 1);
mapNpcCounts.set(mapId, mapNpcs);
}
const soldByObject = new Map<number, WikiNpcReference[]>();
const droppedByObject = new Map<number, WikiNpcReference[]>();
const craftResultObjectIds = new Set<number>();
const craftMaterialObjectIds = new Set<number>();
const smeltingOutputObjectIds = new Set<number>();
const smeltingMaterialObjectIds = new Set<number>();
for (const recipe of craftingChanges.changes) {
craftResultObjectIds.add(Number(recipe.data.itemId ?? 0));
for (const material of recipe.data.materials ?? []) {
craftMaterialObjectIds.add(Number(material.itemId ?? 0));
}
}
for (const recipe of smeltingChanges.changes) {
smeltingMaterialObjectIds.add(Number(recipe.data.mineralItemId ?? 0));
smeltingOutputObjectIds.add(Number(recipe.data.ingotItemId ?? 0));
}
const npcs: WikiNpcEntry[] = npcChanges.changes
.filter((entry) => npcIdsInMaps.has(entry.id))
.map((entry) => {
const data = entry.data as Record<string, unknown>;
const maxHp = toNumber(data.maxHp) || toNumber(data.hp);
const expReward = Math.floor(
toNumber(data.exp) * CURRENT_EXP_MULTIPLIER,
);
const goldReward = Math.floor(
toNumber(data.gold) * CURRENT_GOLD_MULTIPLIER,
);
const npcMaps =
npcMapCounts.get(entry.id) ?? new Map<number, number>();
const maps = Array.from(npcMaps.entries())
.map(([mapId, spawnsInMap]) => ({
mapId,
mapName:
mapMetadataById.get(mapId)?.name ?? `Mapa ${mapId}`,
spawns: spawnsInMap,
}))
.sort(
(left, right) =>
right.spawns - left.spawns || left.mapId - right.mapId,
);
const sells = Array.isArray(data.objs)
? data.objs
.map((item) => {
const trade = item as Record<string, unknown>;
const itemId = toNumber(trade.item);
return {
itemId,
itemName: String(
objectsById.get(itemId)?.name ??
`Objeto ${itemId}`,
),
quantity: toNumber(trade.cant),
};
})
.filter((item) => item.itemId > 0)
: [];
const drops = Array.isArray(data.drop)
? data.drop
.map((item) => {
const trade = item as Record<string, unknown>;
const itemId = toNumber(trade.item);
return {
itemId,
itemName: String(
objectsById.get(itemId)?.name ??
`Objeto ${itemId}`,
),
quantity: toNumber(trade.cant),
};
})
.filter((item) => item.itemId > 0)
: [];
for (const item of sells) {
const references = soldByObject.get(item.itemId) ?? [];
references.push({
npcId: entry.id,
npcName: String(data.name ?? `NPC ${entry.id}`),
quantity: item.quantity,
});
soldByObject.set(item.itemId, references);
}
for (const item of drops) {
const references = droppedByObject.get(item.itemId) ?? [];
references.push({
npcId: entry.id,
npcName: String(data.name ?? `NPC ${entry.id}`),
quantity: item.quantity,
});
droppedByObject.set(item.itemId, references);
}
const combatNpc = isCombatNpc(data);
const totalSpawns = maps.reduce(
(total, mapEntry) => total + mapEntry.spawns,
0,
);
const npcType = toNumber(data.npcType);
return {
id: entry.id,
name: String(data.name ?? `NPC ${entry.id}`),
npcType,
npcTypeLabel: getNpcTypeLabel(npcType),
description: String(data.desc ?? ""),
maxHp,
expReward,
goldReward,
minHit: toNumber(data.minHit),
maxHit: toNumber(data.maxHit),
defense: toNumber(data.def),
expPerHp:
maxHp > 0 && expReward > 0
? Number((expReward / maxHp).toFixed(4))
: null,
isCombatNpc: combatNpc,
maps,
sells,
drops,
totalSpawns,
searchIndex: buildSearchIndex([
entry.id,
String(data.name ?? ""),
getNpcTypeLabel(npcType),
maps.map((mapEntry) => mapEntry.mapName).join(" "),
sells.map((item) => item.itemName).join(" "),
drops.map((item) => item.itemName).join(" "),
]),
};
})
.sort((left, right) => left.id - right.id);
const equipment: WikiEquipmentEntry[] = objectChanges.changes
.map((entry) => {
const data = entry.data as Record<string, unknown>;
const objType = toNumber(data.objType);
const defaultCategory = getEquipmentCategory(objType);
const category = isMagicWeapon(data, objType)
? { key: "magic_weapon" as const, label: "Armas Mágicas" }
: defaultCategory;
const soldBy = (soldByObject.get(entry.id) ?? []).sort(
(left, right) =>
right.quantity - left.quantity || left.npcId - right.npcId,
);
const droppedBy = (droppedByObject.get(entry.id) ?? []).sort(
(left, right) =>
right.quantity - left.quantity || left.npcId - right.npcId,
);
if (!category) {
return null;
}
return {
id: entry.id,
name: String(data.name ?? `Objeto ${entry.id}`),
grhIndex: toNumber(data.grhIndex),
objType,
objTypeLabel: getObjectTypeLabel(objType),
category: category.key,
categoryLabel: category.label,
value: toNumber(data.valor),
minHit: toNumber(data.minHit),
maxHit: toNumber(data.maxHit),
minDef: toNumber(data.minDef),
maxDef: toNumber(data.maxDef),
tier: toNumber(data.tier),
newbie: toNumber(data.newbie) === 1,
blockedClasses: Array.isArray(data.clasesNoPermitidas)
? data.clasesNoPermitidas.filter(
(value): value is number => typeof value === "number",
)
: [],
soldBy,
droppedBy,
searchIndex: buildSearchIndex([
entry.id,
String(data.name ?? ""),
category.label,
getObjectTypeLabel(objType),
soldBy.map((npc) => npc.npcName).join(" "),
droppedBy.map((npc) => npc.npcName).join(" "),
]),
};
})
.filter((entry): entry is WikiEquipmentEntry => Boolean(entry))
.filter(
(entry) => entry.soldBy.length > 0 || entry.droppedBy.length > 0,
)
.sort((left, right) => left.id - right.id);
const spellSourcesBySpellId = new Map<number, WikiSpellSourceReference[]>();
for (const entry of objectChanges.changes) {
const data = entry.data as Record<string, unknown>;
const spellId = toNumber(data.spellIndex);
if (spellId <= 0) {
continue;
}
const soldBy = (soldByObject.get(entry.id) ?? []).sort(
(left, right) =>
right.quantity - left.quantity || left.npcId - right.npcId,
);
const droppedBy = (droppedByObject.get(entry.id) ?? []).sort(
(left, right) =>
right.quantity - left.quantity || left.npcId - right.npcId,
);
if (soldBy.length === 0 && droppedBy.length === 0) {
continue;
}
const currentSources = spellSourcesBySpellId.get(spellId) ?? [];
currentSources.push({
itemId: entry.id,
itemName: String(data.name ?? `Objeto ${entry.id}`),
soldBy,
droppedBy,
});
spellSourcesBySpellId.set(spellId, currentSources);
}
const spells: WikiSpellEntry[] = spellsData
.map((entry) => {
const data = entry.data;
const sources = (spellSourcesBySpellId.get(entry.id) ?? []).sort(
(left, right) => left.itemId - right.itemId,
);
if (sources.length === 0) {
return null;
}
const type = toNumber(data.type);
return {
id: entry.id,
name: String(data.name ?? `Hechizo ${entry.id}`),
description: String(data.desc ?? ""),
type,
typeLabel: getSpellTypeLabel(type),
minSkill: toNumber(data.minSkill),
manaRequired: toNumber(data.manaRequired),
target: toNumber(data.target),
words: String(data.palabrasMagicas ?? ""),
minPower: toNumber(data.minHp),
maxPower: toNumber(data.maxHp),
sources,
searchIndex: buildSearchIndex([
entry.id,
String(data.name ?? ""),
String(data.desc ?? ""),
getSpellTypeLabel(type),
sources.map((source) => source.itemName).join(" "),
sources
.flatMap((source) => [
source.soldBy.map((npc) => npc.npcName).join(" "),
source.droppedBy
.map((npc) => npc.npcName)
.join(" "),
])
.join(" "),
]),
};
})
.filter((entry): entry is WikiSpellEntry => Boolean(entry))
.sort((left, right) => left.id - right.id);
const npcById = new Map(npcs.map((npc) => [npc.id, npc]));
const trainingMaps: WikiTrainingMapEntry[] = Array.from(
mapNpcCounts.entries(),
)
.map(([mapId, npcCounts]) => {
const metadata = mapMetadataById.get(mapId) ?? {
id: mapId,
name: `Mapa ${mapId}`,
terreno: "-",
zona: "-",
restringir: "No",
minLevel: 0,
maxLevel: 0,
pk: false,
};
const topNpcs = Array.from(npcCounts.entries())
.map(([npcId, spawnsInMap]) => {
const npc = npcById.get(npcId);
return npc
? {
npcId,
npcName: npc.name,
spawns: spawnsInMap,
expReward: npc.expReward,
maxHp: npc.maxHp,
isCombatNpc: npc.isCombatNpc,
}
: null;
})
.filter((entry): entry is NonNullable<typeof entry> =>
Boolean(entry),
)
.sort(
(left, right) =>
right.spawns - left.spawns ||
right.expReward - left.expReward,
);
const combatNpcs = topNpcs.filter((npc) => npc.isCombatNpc);
return {
...metadata,
totalNpcSpawns: topNpcs.reduce(
(total, npc) => total + npc.spawns,
0,
),
totalCombatSpawns: combatNpcs.reduce(
(total, npc) => total + npc.spawns,
0,
),
uniqueCombatNpcs: combatNpcs.length,
topNpcs: combatNpcs
.slice(0, 8)
.map(({ isCombatNpc: _isCombatNpc, ...npc }) => npc),
searchIndex: buildSearchIndex([
metadata.id,
metadata.name,
metadata.terreno,
metadata.zona,
combatNpcs.map((npc) => npc.npcName).join(" "),
]),
};
})
.filter(
(map) =>
isTrainingMapCandidate(map) &&
map.uniqueCombatNpcs > 0 &&
map.totalCombatSpawns > 0,
)
.sort(
(left, right) =>
right.uniqueCombatNpcs - left.uniqueCombatNpcs ||
right.totalCombatSpawns - left.totalCombatSpawns ||
left.id - right.id,
);
const payload = {
generatedAt: new Date().toISOString(),
stats: {
npcCount: npcs.length,
combatNpcCount: npcs.filter((npc) => npc.isCombatNpc).length,
equipmentCount: equipment.length,
spellCount: spells.length,
trainingMapCount: trainingMaps.length,
},
npcs,
equipment,
spells,
trainingMaps,
};
wikiCache = {
signature,
payload,
};
return payload;
}