forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiki-data.ts
More file actions
141 lines (127 loc) · 4.58 KB
/
Copy pathwiki-data.ts
File metadata and controls
141 lines (127 loc) · 4.58 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
import { unstable_cache } from "next/cache";
import { getApiBaseUrlCandidates } from "@/lib/api-base-url";
import type { PublicWikiResponse } from "@/lib/wiki";
export const WIKI_REVALIDATE_SECONDS = 60 * 60 * 24 * 30;
function inferEquipmentCategory(
objType: number,
name?: string,
): {
category: "weapon" | "armor" | "shield" | "helmet" | "magic_weapon";
categoryLabel: string;
} | null {
const normalizedName = String(name ?? "")
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase();
if (
objType === 26 ||
normalizedName.includes("baston") ||
normalizedName.includes("laud") ||
normalizedName.includes("flauta")
) {
return { category: "magic_weapon", categoryLabel: "Armas Mágicas" };
}
switch (objType) {
case 2:
return { category: "weapon", categoryLabel: "Armas" };
case 3:
return { category: "armor", categoryLabel: "Armaduras" };
case 16:
return { category: "shield", categoryLabel: "Escudos" };
case 17:
return { category: "helmet", categoryLabel: "Cascos" };
default:
return null;
}
}
function normalizeWikiResponse(payload: unknown): PublicWikiResponse {
const raw = (payload ?? {}) as Record<string, unknown>;
const rawEquipment = Array.isArray(raw.equipment) ? raw.equipment : [];
const rawObjects = Array.isArray(raw.objects) ? raw.objects : [];
const normalizedEquipment =
rawEquipment.length > 0
? rawEquipment
: rawObjects
.map((entry) => {
const item = entry as Record<string, unknown>;
const inferred = inferEquipmentCategory(
Number(item.objType ?? 0),
String(item.name ?? ""),
);
if (!inferred) {
return null;
}
return {
...item,
grhIndex: Number(item.grhIndex ?? 0),
category: inferred.category,
categoryLabel: inferred.categoryLabel,
};
})
.filter(Boolean);
const rawStats = (raw.stats ?? {}) as Record<string, unknown>;
return {
generatedAt: String(raw.generatedAt ?? new Date().toISOString()),
stats: {
npcCount: Number(rawStats.npcCount ?? 0),
combatNpcCount: Number(rawStats.combatNpcCount ?? 0),
equipmentCount: Number(
rawStats.equipmentCount ??
rawStats.objectCount ??
normalizedEquipment.length,
),
spellCount: Number(rawStats.spellCount ?? 0),
trainingMapCount: Number(rawStats.trainingMapCount ?? 0),
},
npcs: Array.isArray(raw.npcs)
? (raw.npcs as PublicWikiResponse["npcs"])
: [],
equipment: normalizedEquipment as PublicWikiResponse["equipment"],
spells: Array.isArray(raw.spells)
? (raw.spells as PublicWikiResponse["spells"])
: [],
trainingMaps: Array.isArray(raw.trainingMaps)
? (raw.trainingMaps as PublicWikiResponse["trainingMaps"])
: [],
};
}
function createEmptyWikiResponse(): PublicWikiResponse {
return {
generatedAt: new Date().toISOString(),
stats: {
npcCount: 0,
combatNpcCount: 0,
equipmentCount: 0,
spellCount: 0,
trainingMapCount: 0,
},
npcs: [],
equipment: [],
spells: [],
trainingMaps: [],
};
}
export const getWikiData = unstable_cache(
async (): Promise<PublicWikiResponse> => {
for (const apiBaseUrl of getApiBaseUrlCandidates()) {
try {
const response = await fetch(`${apiBaseUrl}/wiki`, {
cache: "no-store",
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return normalizeWikiResponse(await response.json());
} catch (error) {
console.error(
`No se pudo cargar la wiki desde ${apiBaseUrl}:`,
error,
);
}
}
console.error("No se pudo cargar la wiki desde ningun origen.");
return createEmptyWikiResponse();
},
["public-wiki"],
{ revalidate: WIKI_REVALIDATE_SECONDS },
);