This commit is contained in:
1 parent
e85e4d74ea
commit
2e4ed76121
378 files changed
+22404
-20686
No files matched your search
@@ -5,10 +5,10 @@
|
||||
* No third-party APIs (habbofurni.com) needed.
|
||||
*/
|
||||
|
||||
import type { FurniListResult, HabboFurniItem } from '@/types/furni'
|
||||
import { getHabboItFurnidata } from './habbo-furnidata-cache'
|
||||
import type { FurniListResult, HabboFurniItem } from "@/types/furni";
|
||||
import { getHabboItFurnidata } from "./habbo-furnidata-cache";
|
||||
|
||||
export type { FurniListResult, HabboFurniItem }
|
||||
export type { FurniListResult, HabboFurniItem };
|
||||
|
||||
// ── List / Browse endpoint ────────────────────────────────────────
|
||||
|
||||
@@ -18,32 +18,32 @@ export type { FurniListResult, HabboFurniItem }
|
||||
*/
|
||||
export async function listFurniture(
|
||||
options: {
|
||||
search?: string
|
||||
page?: number
|
||||
perPage?: number
|
||||
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>
|
||||
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 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
|
||||
const map = await getHabboItFurnidata();
|
||||
if (map.size === 0) return empty;
|
||||
|
||||
// Convert to array and optionally filter by search
|
||||
let items = Array.from(map.values())
|
||||
let items = Array.from(map.values());
|
||||
|
||||
if (search) {
|
||||
items = items.filter(
|
||||
@@ -51,42 +51,42 @@ export async function listFurniture(
|
||||
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
|
||||
})
|
||||
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)
|
||||
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',
|
||||
description: item.description || "",
|
||||
type: item.itemType === "i" ? "wallitem" : "flooritem",
|
||||
revision: item.revision,
|
||||
category: item.category || item.furniline || '',
|
||||
category: item.category || item.furniline || "",
|
||||
})),
|
||||
meta: { currentPage: page, lastPage, total, perPage },
|
||||
}
|
||||
};
|
||||
} catch (err) {
|
||||
console.warn('[furni] List error:', (err as Error).message)
|
||||
return empty
|
||||
console.warn("[furni] List error:", (err as Error).message);
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,8 +96,8 @@ 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
|
||||
const result = await listFurniture({ search: query, perPage });
|
||||
return result.items;
|
||||
}
|
||||
|
||||
// ── Direct lookup by classname ─────────────────────────────────────
|
||||
@@ -107,33 +107,33 @@ export async function searchFurniture(query: string, perPage = 50): Promise<Habb
|
||||
* Uses the cached habbo.it furnidata.
|
||||
*/
|
||||
export async function getFurnitureByClassname(classname: string): Promise<HabboFurniItem | null> {
|
||||
const map = await getHabboItFurnidata()
|
||||
const map = await getHabboItFurnidata();
|
||||
|
||||
// Try exact match
|
||||
let entry = map.get(classname)
|
||||
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)
|
||||
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`)
|
||||
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',
|
||||
description: entry.description || "",
|
||||
type: entry.itemType === "i" ? "wallitem" : "flooritem",
|
||||
revision: entry.revision,
|
||||
category: entry.category || entry.furniline || '',
|
||||
}
|
||||
category: entry.category || entry.furniline || "",
|
||||
};
|
||||
}
|
||||
|
||||
// ── Batch lookup ───────────────────────────────────────────────────
|
||||
@@ -142,18 +142,16 @@ 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)
|
||||
const item = await getFurnitureByClassname(cn);
|
||||
if (item?.name) {
|
||||
result.set(cn, item)
|
||||
result.set(cn, item);
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user