This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
+107
-100
@@ -17,77 +17,77 @@ export type { FurniListResult, HabboFurniItem };
|
||||
* Data comes from habbo.it/gamedata/furnidata_json/1 (cached 30 min).
|
||||
*/
|
||||
export async function listFurniture(
|
||||
options: {
|
||||
search?: string;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
/**
|
||||
* Optional set of already-imported classnames. When provided, non-imported
|
||||
* items are sorted to the top so the user finds them on page 1 instead of
|
||||
* paging through 15+ pages of already-imported newest items.
|
||||
*/
|
||||
importedSet?: Set<string>;
|
||||
} = {},
|
||||
options: {
|
||||
search?: string;
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
/**
|
||||
* Optional set of already-imported classnames. When provided, non-imported
|
||||
* items are sorted to the top so the user finds them on page 1 instead of
|
||||
* paging through 15+ pages of already-imported newest items.
|
||||
*/
|
||||
importedSet?: Set<string>;
|
||||
} = {},
|
||||
): Promise<FurniListResult> {
|
||||
const empty: FurniListResult = {
|
||||
items: [],
|
||||
meta: { currentPage: 1, lastPage: 1, total: 0, perPage: 50 },
|
||||
};
|
||||
const perPage = Math.min(options.perPage || 50, 100);
|
||||
const page = options.page || 1;
|
||||
const search = options.search?.toLowerCase().trim() || "";
|
||||
const importedSet = options.importedSet;
|
||||
const empty: FurniListResult = {
|
||||
items: [],
|
||||
meta: { currentPage: 1, lastPage: 1, total: 0, perPage: 50 },
|
||||
};
|
||||
const perPage = Math.min(options.perPage || 50, 100);
|
||||
const page = options.page || 1;
|
||||
const search = options.search?.toLowerCase().trim() || "";
|
||||
const importedSet = options.importedSet;
|
||||
|
||||
try {
|
||||
const map = await getHabboItFurnidata();
|
||||
if (map.size === 0) return empty;
|
||||
try {
|
||||
const map = await getHabboItFurnidata();
|
||||
if (map.size === 0) return empty;
|
||||
|
||||
// Convert to array and optionally filter by search
|
||||
let items = Array.from(map.values());
|
||||
// Convert to array and optionally filter by search
|
||||
let items = Array.from(map.values());
|
||||
|
||||
if (search) {
|
||||
items = items.filter(
|
||||
(item) =>
|
||||
item.classname.toLowerCase().includes(search) ||
|
||||
item.name.toLowerCase().includes(search) ||
|
||||
item.description.toLowerCase().includes(search),
|
||||
);
|
||||
}
|
||||
if (search) {
|
||||
items = items.filter(
|
||||
(item) =>
|
||||
item.classname.toLowerCase().includes(search) ||
|
||||
item.name.toLowerCase().includes(search) ||
|
||||
item.description.toLowerCase().includes(search),
|
||||
);
|
||||
}
|
||||
|
||||
if (importedSet) {
|
||||
// Non-imported first (id desc), then imported (id desc).
|
||||
items.sort((a, b) => {
|
||||
const aImp = importedSet.has(a.classname) ? 1 : 0;
|
||||
const bImp = importedSet.has(b.classname) ? 1 : 0;
|
||||
if (aImp !== bImp) return aImp - bImp;
|
||||
return b.id - a.id;
|
||||
});
|
||||
} else {
|
||||
// Sort by id descending (newest first)
|
||||
items.sort((a, b) => b.id - a.id);
|
||||
}
|
||||
if (importedSet) {
|
||||
// Non-imported first (id desc), then imported (id desc).
|
||||
items.sort((a, b) => {
|
||||
const aImp = importedSet.has(a.classname) ? 1 : 0;
|
||||
const bImp = importedSet.has(b.classname) ? 1 : 0;
|
||||
if (aImp !== bImp) return aImp - bImp;
|
||||
return b.id - a.id;
|
||||
});
|
||||
} else {
|
||||
// Sort by id descending (newest first)
|
||||
items.sort((a, b) => b.id - a.id);
|
||||
}
|
||||
|
||||
const total = items.length;
|
||||
const lastPage = Math.max(Math.ceil(total / perPage), 1);
|
||||
const start = (page - 1) * perPage;
|
||||
const pageItems = items.slice(start, start + perPage);
|
||||
const total = items.length;
|
||||
const lastPage = Math.max(Math.ceil(total / perPage), 1);
|
||||
const start = (page - 1) * perPage;
|
||||
const pageItems = items.slice(start, start + perPage);
|
||||
|
||||
return {
|
||||
items: pageItems.map((item) => ({
|
||||
id: item.id,
|
||||
classname: item.classname,
|
||||
name: item.name || item.classname,
|
||||
description: item.description || "",
|
||||
type: item.itemType === "i" ? "wallitem" : "flooritem",
|
||||
revision: item.revision,
|
||||
category: item.category || item.furniline || "",
|
||||
})),
|
||||
meta: { currentPage: page, lastPage, total, perPage },
|
||||
};
|
||||
} catch (err) {
|
||||
console.warn("[furni] List error:", (err as Error).message);
|
||||
return empty;
|
||||
}
|
||||
return {
|
||||
items: pageItems.map((item) => ({
|
||||
id: item.id,
|
||||
classname: item.classname,
|
||||
name: item.name || item.classname,
|
||||
description: item.description || "",
|
||||
type: item.itemType === "i" ? "wallitem" : "flooritem",
|
||||
revision: item.revision,
|
||||
category: item.category || item.furniline || "",
|
||||
})),
|
||||
meta: { currentPage: page, lastPage, total, perPage },
|
||||
};
|
||||
} catch (err) {
|
||||
console.warn("[furni] List error:", (err as Error).message);
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Search endpoint ────────────────────────────────────────────────
|
||||
@@ -95,9 +95,12 @@ export async function listFurniture(
|
||||
/**
|
||||
* Search furniture by query string.
|
||||
*/
|
||||
export async function searchFurniture(query: string, perPage = 50): Promise<HabboFurniItem[]> {
|
||||
const result = await listFurniture({ search: query, perPage });
|
||||
return result.items;
|
||||
export async function searchFurniture(
|
||||
query: string,
|
||||
perPage = 50,
|
||||
): Promise<HabboFurniItem[]> {
|
||||
const result = await listFurniture({ search: query, perPage });
|
||||
return result.items;
|
||||
}
|
||||
|
||||
// ── Direct lookup by classname ─────────────────────────────────────
|
||||
@@ -106,34 +109,36 @@ export async function searchFurniture(query: string, perPage = 50): Promise<Habb
|
||||
* Get a single furniture item by classname.
|
||||
* Uses the cached habbo.it furnidata.
|
||||
*/
|
||||
export async function getFurnitureByClassname(classname: string): Promise<HabboFurniItem | null> {
|
||||
const map = await getHabboItFurnidata();
|
||||
export async function getFurnitureByClassname(
|
||||
classname: string,
|
||||
): Promise<HabboFurniItem | null> {
|
||||
const map = await getHabboItFurnidata();
|
||||
|
||||
// Try exact match
|
||||
let entry = map.get(classname);
|
||||
// Try exact match
|
||||
let entry = map.get(classname);
|
||||
|
||||
// Try without color variant
|
||||
if (!entry && classname.includes("*")) {
|
||||
const base = classname.split("*")[0];
|
||||
entry = map.get(`${base}*0`) || map.get(base);
|
||||
}
|
||||
// Try without color variant
|
||||
if (!entry && classname.includes("*")) {
|
||||
const base = classname.split("*")[0];
|
||||
entry = map.get(`${base}*0`) || map.get(base);
|
||||
}
|
||||
|
||||
// Try with *0
|
||||
if (!entry && !classname.includes("*")) {
|
||||
entry = map.get(`${classname}*0`);
|
||||
}
|
||||
// Try with *0
|
||||
if (!entry && !classname.includes("*")) {
|
||||
entry = map.get(`${classname}*0`);
|
||||
}
|
||||
|
||||
if (!entry) return null;
|
||||
if (!entry) return null;
|
||||
|
||||
return {
|
||||
id: entry.id,
|
||||
classname: entry.classname,
|
||||
name: entry.name || entry.classname,
|
||||
description: entry.description || "",
|
||||
type: entry.itemType === "i" ? "wallitem" : "flooritem",
|
||||
revision: entry.revision,
|
||||
category: entry.category || entry.furniline || "",
|
||||
};
|
||||
return {
|
||||
id: entry.id,
|
||||
classname: entry.classname,
|
||||
name: entry.name || entry.classname,
|
||||
description: entry.description || "",
|
||||
type: entry.itemType === "i" ? "wallitem" : "flooritem",
|
||||
revision: entry.revision,
|
||||
category: entry.category || entry.furniline || "",
|
||||
};
|
||||
}
|
||||
|
||||
// ── Batch lookup ───────────────────────────────────────────────────
|
||||
@@ -142,16 +147,18 @@ export async function getFurnitureByClassname(classname: string): Promise<HabboF
|
||||
* Batch lookup: get data for a list of classnames.
|
||||
* Much faster than the old habbofurni.com API — single cache lookup, no network.
|
||||
*/
|
||||
export async function batchLookupByClassnames(classnames: string[]): Promise<Map<string, HabboFurniItem>> {
|
||||
const result = new Map<string, HabboFurniItem>();
|
||||
if (classnames.length === 0) return result;
|
||||
export async function batchLookupByClassnames(
|
||||
classnames: string[],
|
||||
): Promise<Map<string, HabboFurniItem>> {
|
||||
const result = new Map<string, HabboFurniItem>();
|
||||
if (classnames.length === 0) return result;
|
||||
|
||||
for (const cn of classnames) {
|
||||
const item = await getFurnitureByClassname(cn);
|
||||
if (item?.name) {
|
||||
result.set(cn, item);
|
||||
}
|
||||
}
|
||||
for (const cn of classnames) {
|
||||
const item = await getFurnitureByClassname(cn);
|
||||
if (item?.name) {
|
||||
result.set(cn, item);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user