Force catalog_items.offer_id to always equal its own row id
Every write path now sets offer_id = catalog_items.id instead of the sprite id or furnidata offerid: single import insert/update, clone import, upload import (direct DB + generated SQL migration), catalog repair inserts, reconcileImportedOfferIds and rebuildCatalogOfferIds. rebuildCatalogOfferIds is reduced to one bulk UPDATE that repairs any drift across all rows after every import; 74.838 legacy rows were repaired on the live database with this change.
This commit is contained in:
1 parent
adb56eb80b
commit
5311ee1fb8
10 files changed
+45
-103
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { existsSync, promises as fs } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { CatalogItems, db, ItemsBase } from "@/lib/db";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { db, ItemsBase } from "@/lib/db";
|
||||
import {
|
||||
getGamedataRoot,
|
||||
getNitroFilesRoot,
|
||||
@@ -601,91 +601,27 @@ export interface CatalogOfferRebuildResult {
|
||||
checked: number;
|
||||
/** Rows whose offer_id was corrected. */
|
||||
fixed: number;
|
||||
/** Fixed rows with no furnidata entry (offer_id fell back to the sprite id). */
|
||||
fallback: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild EVERY catalog_items.offer_id from the local FurnitureData.json.
|
||||
* Force EVERY catalog_items.offer_id to equal its own row id.
|
||||
*
|
||||
* The Nitro client resolves offers through the furnidata `offerid`, and the
|
||||
* emulator maps that offer id back to a catalog item (CatalogManager
|
||||
* offerDefs). The chain only works when catalog_items.offer_id equals the
|
||||
* furnidata offerid — which this CMS reconciles to the items_base sprite id.
|
||||
*
|
||||
* For every catalog row: resolve item_ids → items_base classname → furnidata
|
||||
* offerid and write it into offer_id. Rows without a furnidata entry fall
|
||||
* back to the sprite id itself (same value in practice). Run after every
|
||||
* import so drift from manual edits or legacy rows is repaired automatically.
|
||||
* The emulator maps offer ids back to catalog rows, so offer_id must always
|
||||
* mirror the primary key. Run after every import so drift from manual edits,
|
||||
* legacy rows or older imports is repaired automatically.
|
||||
*/
|
||||
export async function rebuildCatalogOfferIds(): Promise<CatalogOfferRebuildResult> {
|
||||
return withFurniDataLock(async () => {
|
||||
const furniData = (await readFurniData()) as Record<
|
||||
string,
|
||||
{ furnitype?: Array<Record<string, unknown>> }
|
||||
>;
|
||||
const offerById = new Map<number, number>();
|
||||
const offerByClass = new Map<string, number>();
|
||||
for (const section of ["roomitemtypes", "wallitemtypes"] as const) {
|
||||
for (const e of furniData[section]?.furnitype ?? []) {
|
||||
const cn = typeof e?.classname === "string" ? e.classname : "";
|
||||
const entryId = Number(e?.id);
|
||||
const offer = Number(e?.offerid);
|
||||
if (
|
||||
cn &&
|
||||
Number.isFinite(offer) &&
|
||||
offer > 0 &&
|
||||
!offerByClass.has(cn)
|
||||
) {
|
||||
offerByClass.set(cn, offer);
|
||||
}
|
||||
if (
|
||||
Number.isFinite(entryId) &&
|
||||
entryId > 0 &&
|
||||
!offerById.has(entryId)
|
||||
) {
|
||||
offerById.set(
|
||||
entryId,
|
||||
Number.isFinite(offer) && offer > 0 ? offer : entryId,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
const [countRows] = (await db.execute(
|
||||
sql`SELECT COUNT(*) AS total FROM catalog_items`,
|
||||
)) as unknown as [Array<{ total: number }>, unknown];
|
||||
const checked = Number(countRows?.[0]?.total ?? 0);
|
||||
|
||||
const itemRows = await db
|
||||
.select({ id: ItemsBase.id, itemName: ItemsBase.itemName })
|
||||
.from(ItemsBase);
|
||||
const nameById = new Map<number, string>();
|
||||
for (const r of itemRows) nameById.set(r.id, r.itemName);
|
||||
const [result] = (await db.execute(sql`
|
||||
UPDATE catalog_items SET offer_id = id WHERE offer_id <> id
|
||||
`)) as unknown as [Record<string, unknown>, unknown];
|
||||
|
||||
const catalogRows = await db
|
||||
.select({
|
||||
id: CatalogItems.id,
|
||||
itemIds: CatalogItems.itemIds,
|
||||
offerId: CatalogItems.offerId,
|
||||
})
|
||||
.from(CatalogItems);
|
||||
|
||||
let fixed = 0;
|
||||
let fallback = 0;
|
||||
for (const row of catalogRows) {
|
||||
const itemId = Number(String(row.itemIds ?? "").split(/[;,]/)[0]);
|
||||
if (!Number.isFinite(itemId) || itemId <= 0) continue;
|
||||
const spriteId = itemId;
|
||||
const classname = nameById.get(itemId);
|
||||
const furnidataOffer =
|
||||
offerById.get(itemId) ??
|
||||
(classname ? offerByClass.get(classname) : undefined);
|
||||
const target = furnidataOffer ?? spriteId;
|
||||
if (row.offerId === spriteId) continue;
|
||||
await db
|
||||
.update(CatalogItems)
|
||||
.set({ offerId: target })
|
||||
.where(eq(CatalogItems.id, row.id));
|
||||
fixed++;
|
||||
if (furnidataOffer === undefined) fallback++;
|
||||
}
|
||||
return { checked: catalogRows.length, fixed, fallback };
|
||||
return { checked, fixed: Number(result.affectedRows ?? 0) };
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user