fix: harden furni import pipeline for production
- importSingleFurni: actually update an existing item instead of failing on a primary key INSERT collision; refuse to reassign a spriteId owned by a different classname - reconcileFurniDataWithItemsBase: also sync each entry's offerid to the DB sprite id, not just the id - Move the full-table interaction_modes_count verification out of the per-item import hot path and run it once per batch/single import - clone-import: apply the auto-detected interaction_type (was hardcoded 'default'), set FurnitureData offerid and catalog_items.offer_id to the new local sprite id (was the source hotel's id / -1) - clone batch route: run the same post-import reconcile/verify/ownership/RCON consolidation as the furni batch route - upload-import: set offer_id to the allocated id in both the direct insert and the generated SQL migration (was -1) - generateCatalogSql: use the classname as catalog_name and the item id as offer_id so rows match the app-managed import convention - stats + missing-nitro endpoints: match catalog membership via item_ids instead of the catalog_name join, so translated display names no longer break counts - deleteImportedItem/rollback: delete catalog rows by the canonical item_ids link only - classifyFurni: wall items always classify as 'walls' before prefix rules (rare_/val_/xmas_) can misfile them
This commit is contained in:
1 parent
2b9a015afb
commit
6021a91ef7
9 files changed
+162
-66
No files matched your search
@@ -10,8 +10,16 @@ import { getSource } from "@/lib/services/clone-sources";
|
|||||||
import {
|
import {
|
||||||
appendFurniEntriesBatch,
|
appendFurniEntriesBatch,
|
||||||
appendFurniEntry,
|
appendFurniEntry,
|
||||||
|
reconcileFurniDataWithItemsBase,
|
||||||
} from "@/lib/services/furni-data";
|
} from "@/lib/services/furni-data";
|
||||||
|
import {
|
||||||
|
ensureFurniOwnership,
|
||||||
|
reconcileImportedOfferIds,
|
||||||
|
syncAssetsToGamedataBundle,
|
||||||
|
verifyAndFixInteractionModesCount,
|
||||||
|
} from "@/lib/services/furni-import";
|
||||||
import { runSseBatch } from "@/lib/services/import/core/sse-batch";
|
import { runSseBatch } from "@/lib/services/import/core/sse-batch";
|
||||||
|
import { rcon } from "@/lib/services/rcon";
|
||||||
|
|
||||||
interface BatchItem {
|
interface BatchItem {
|
||||||
classname: string;
|
classname: string;
|
||||||
@@ -126,6 +134,22 @@ export const POST = withAdmin(
|
|||||||
`FurnitureData batch write failed: ${(err as Error).message}`,
|
`FurnitureData batch write failed: ${(err as Error).message}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Post-import consolidation, mirroring the furni batch route:
|
||||||
|
// keep offer_id / FurnitureData offerid in sync with the new local
|
||||||
|
// sprite ids, verify interaction_modes_count, and refresh the
|
||||||
|
// emulator + asset ownership. Never fails the stream — best effort.
|
||||||
|
try {
|
||||||
|
await reconcileImportedOfferIds();
|
||||||
|
await reconcileFurniDataWithItemsBase();
|
||||||
|
await verifyAndFixInteractionModesCount();
|
||||||
|
await ensureFurniOwnership();
|
||||||
|
await syncAssetsToGamedataBundle();
|
||||||
|
await rcon.updateCatalog();
|
||||||
|
await rcon.updateItems();
|
||||||
|
} catch {
|
||||||
|
/* best effort — post-import consolidation is non-fatal */
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
importSingleFurni,
|
importSingleFurni,
|
||||||
reconcileImportedOfferIds,
|
reconcileImportedOfferIds,
|
||||||
syncAssetsToGamedataBundle,
|
syncAssetsToGamedataBundle,
|
||||||
|
verifyAndFixInteractionModesCount,
|
||||||
} from "@/lib/services/furni-import";
|
} from "@/lib/services/furni-import";
|
||||||
import { rcon } from "@/lib/services/rcon";
|
import { rcon } from "@/lib/services/rcon";
|
||||||
import type { ImportSingleResult } from "@/types/furni";
|
import type { ImportSingleResult } from "@/types/furni";
|
||||||
@@ -223,8 +224,10 @@ export const POST = withAdmin(
|
|||||||
let nitrosSynced: string[] = [];
|
let nitrosSynced: string[] = [];
|
||||||
let iconsSynced: string[] = [];
|
let iconsSynced: string[] = [];
|
||||||
let furniDataFixedIds = 0;
|
let furniDataFixedIds = 0;
|
||||||
|
let furniDataFixedOfferIds = 0;
|
||||||
let furniDataMissing = 0;
|
let furniDataMissing = 0;
|
||||||
let furniDataConflicts = 0;
|
let furniDataConflicts = 0;
|
||||||
|
let interactionFixed = 0;
|
||||||
try {
|
try {
|
||||||
offerIdsFixed = (await reconcileImportedOfferIds()).fixed;
|
offerIdsFixed = (await reconcileImportedOfferIds()).fixed;
|
||||||
ownershipFixed = (await ensureFurniOwnership()).fixed;
|
ownershipFixed = (await ensureFurniOwnership()).fixed;
|
||||||
@@ -233,8 +236,10 @@ export const POST = withAdmin(
|
|||||||
iconsSynced = assetsSynced.copiedIcons;
|
iconsSynced = assetsSynced.copiedIcons;
|
||||||
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
||||||
furniDataFixedIds = furniReconcile.fixedIds;
|
furniDataFixedIds = furniReconcile.fixedIds;
|
||||||
|
furniDataFixedOfferIds = furniReconcile.fixedOfferIds;
|
||||||
furniDataMissing = furniReconcile.missing;
|
furniDataMissing = furniReconcile.missing;
|
||||||
furniDataConflicts = furniReconcile.conflicts;
|
furniDataConflicts = furniReconcile.conflicts;
|
||||||
|
interactionFixed = (await verifyAndFixInteractionModesCount()).fixed;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(
|
console.warn(
|
||||||
"[import-furni] Post-import reconcile/ownership failed:",
|
"[import-furni] Post-import reconcile/ownership failed:",
|
||||||
@@ -252,8 +257,10 @@ export const POST = withAdmin(
|
|||||||
nitrosSynced,
|
nitrosSynced,
|
||||||
iconsSynced,
|
iconsSynced,
|
||||||
furniDataFixedIds,
|
furniDataFixedIds,
|
||||||
|
furniDataFixedOfferIds,
|
||||||
furniDataMissing,
|
furniDataMissing,
|
||||||
furniDataConflicts,
|
furniDataConflicts,
|
||||||
|
interactionFixed,
|
||||||
duration: Date.now() - startTime,
|
duration: Date.now() - startTime,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import {
|
|||||||
reconcileImportedOfferIds,
|
reconcileImportedOfferIds,
|
||||||
resetCatalogPageCache,
|
resetCatalogPageCache,
|
||||||
syncAssetsToGamedataBundle,
|
syncAssetsToGamedataBundle,
|
||||||
|
verifyAndFixInteractionModesCount,
|
||||||
} from "@/lib/services/furni-import";
|
} from "@/lib/services/furni-import";
|
||||||
import { getOfficialHabboFurnidata } from "@/lib/services/habbo-furnidata-cache";
|
import { getOfficialHabboFurnidata } from "@/lib/services/habbo-furnidata-cache";
|
||||||
import { listFurniture } from "@/lib/services/habbofurni";
|
import { listFurniture } from "@/lib/services/habbofurni";
|
||||||
@@ -103,15 +104,14 @@ export const GET = withAdmin(
|
|||||||
// Stats endpoint
|
// Stats endpoint
|
||||||
const action = request.nextUrl.searchParams.get("action");
|
const action = request.nextUrl.searchParams.get("action");
|
||||||
if (action === "stats") {
|
if (action === "stats") {
|
||||||
const [totalResult, catalogResult, nitroMissingResult] =
|
const [totalResult, catalogResult, allIdsResult, referencedResult] =
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
db.execute(sql`SELECT COUNT(*) as cnt FROM items_base`),
|
db.execute(sql`SELECT COUNT(*) as cnt FROM items_base`),
|
||||||
db.execute(
|
db.execute(
|
||||||
sql`SELECT COUNT(DISTINCT item_ids) as cnt FROM catalog_items`,
|
sql`SELECT COUNT(DISTINCT item_ids) as cnt FROM catalog_items`,
|
||||||
),
|
),
|
||||||
db.execute(
|
db.execute(sql`SELECT id FROM items_base`),
|
||||||
sql`SELECT COUNT(*) as cnt FROM items_base WHERE item_name NOT IN (SELECT catalog_name FROM catalog_items)`,
|
db.execute(sql`SELECT item_ids FROM catalog_items`),
|
||||||
),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const totalInDb = Number(
|
const totalInDb = Number(
|
||||||
@@ -120,15 +120,34 @@ export const GET = withAdmin(
|
|||||||
const inCatalog = Number(
|
const inCatalog = Number(
|
||||||
(catalogResult as unknown as [[{ cnt: bigint }], unknown])[0][0].cnt,
|
(catalogResult as unknown as [[{ cnt: bigint }], unknown])[0][0].cnt,
|
||||||
);
|
);
|
||||||
const notInCatalog = Number(
|
|
||||||
(nitroMissingResult as unknown as [[{ cnt: bigint }], unknown])[0][0]
|
// An item is "in the catalog" when its items_base id is referenced by
|
||||||
.cnt,
|
// any catalog_items.item_ids (the canonical link — catalog_name can be
|
||||||
);
|
// a translated display name, so matching on it is unreliable).
|
||||||
|
const allIds = (
|
||||||
|
allIdsResult as unknown as [Array<{ id: number }>, unknown]
|
||||||
|
)[0].map((r) => Number(r.id));
|
||||||
|
const referenced = new Set<number>();
|
||||||
|
for (const row of (
|
||||||
|
referencedResult as unknown as [
|
||||||
|
Array<{ item_ids: string | null }>,
|
||||||
|
unknown,
|
||||||
|
]
|
||||||
|
)[0]) {
|
||||||
|
for (const part of String(row.item_ids ?? "").split(/[;,]/)) {
|
||||||
|
const n = Number(part.trim());
|
||||||
|
if (Number.isFinite(n) && n > 0) referenced.add(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const notInCatalog = allIds.filter((id) => !referenced.has(id)).length;
|
||||||
|
|
||||||
// Count missing .nitro files by checking filesystem
|
// Count missing .nitro files by checking filesystem
|
||||||
const [importedItems] = (await db.execute(sql`
|
const [importedItems] = (await db.execute(sql`
|
||||||
SELECT ib.item_name FROM items_base ib
|
SELECT ib.item_name FROM items_base ib
|
||||||
INNER JOIN catalog_items ci ON ci.catalog_name = ib.item_name
|
WHERE EXISTS (
|
||||||
|
SELECT 1 FROM catalog_items ci
|
||||||
|
WHERE FIND_IN_SET(ib.id, REPLACE(ci.item_ids, ';', ',')) > 0
|
||||||
|
)
|
||||||
LIMIT 10000
|
LIMIT 10000
|
||||||
`)) as unknown as [Array<{ item_name: string }>, unknown];
|
`)) as unknown as [Array<{ item_name: string }>, unknown];
|
||||||
|
|
||||||
@@ -159,7 +178,10 @@ export const GET = withAdmin(
|
|||||||
|
|
||||||
const [importedItems] = (await db.execute(sql`
|
const [importedItems] = (await db.execute(sql`
|
||||||
SELECT ib.item_name, ib.public_name, ib.sprite_id, ib.type FROM items_base ib
|
SELECT ib.item_name, ib.public_name, ib.sprite_id, ib.type FROM items_base ib
|
||||||
INNER JOIN catalog_items ci ON ci.catalog_name = ib.item_name
|
WHERE EXISTS (
|
||||||
|
SELECT 1 FROM catalog_items ci
|
||||||
|
WHERE FIND_IN_SET(ib.id, REPLACE(ci.item_ids, ';', ',')) > 0
|
||||||
|
)
|
||||||
`)) as unknown as [
|
`)) as unknown as [
|
||||||
Array<{
|
Array<{
|
||||||
item_name: string;
|
item_name: string;
|
||||||
@@ -419,6 +441,9 @@ export const POST = withAdmin(
|
|||||||
// Verify the furnidata matches the DB ids
|
// Verify the furnidata matches the DB ids
|
||||||
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
||||||
|
|
||||||
|
// Verify + fix interaction_modes_count once for the whole batch
|
||||||
|
const verification = await verifyAndFixInteractionModesCount();
|
||||||
|
|
||||||
const succeeded = results.filter((r) => r.ok).length;
|
const succeeded = results.filter((r) => r.ok).length;
|
||||||
const failed = results.filter((r) => !r.ok).length;
|
const failed = results.filter((r) => !r.ok).length;
|
||||||
|
|
||||||
@@ -432,8 +457,10 @@ export const POST = withAdmin(
|
|||||||
nitrosSynced: assetsSynced.copiedNitros,
|
nitrosSynced: assetsSynced.copiedNitros,
|
||||||
iconsSynced: assetsSynced.copiedIcons,
|
iconsSynced: assetsSynced.copiedIcons,
|
||||||
furniDataFixedIds: furniReconcile.fixedIds,
|
furniDataFixedIds: furniReconcile.fixedIds,
|
||||||
|
furniDataFixedOfferIds: furniReconcile.fixedOfferIds,
|
||||||
furniDataMissing: furniReconcile.missing,
|
furniDataMissing: furniReconcile.missing,
|
||||||
furniDataConflicts: furniReconcile.conflicts,
|
furniDataConflicts: furniReconcile.conflicts,
|
||||||
|
interactionFixed: verification.fixed,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,6 +522,9 @@ export const POST = withAdmin(
|
|||||||
// Verify the furnidata matches the DB ids
|
// Verify the furnidata matches the DB ids
|
||||||
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
const furniReconcile = await reconcileFurniDataWithItemsBase();
|
||||||
|
|
||||||
|
// Verify + fix interaction_modes_count (single consolidated pass)
|
||||||
|
const verification = await verifyAndFixInteractionModesCount();
|
||||||
|
|
||||||
logAudit({
|
logAudit({
|
||||||
userId: ctx.session.user.id,
|
userId: ctx.session.user.id,
|
||||||
action: "furni_import",
|
action: "furni_import",
|
||||||
@@ -520,8 +550,10 @@ export const POST = withAdmin(
|
|||||||
nitrosSynced: assetsSynced.copiedNitros,
|
nitrosSynced: assetsSynced.copiedNitros,
|
||||||
iconsSynced: assetsSynced.copiedIcons,
|
iconsSynced: assetsSynced.copiedIcons,
|
||||||
furniDataFixedIds: furniReconcile.fixedIds,
|
furniDataFixedIds: furniReconcile.fixedIds,
|
||||||
|
furniDataFixedOfferIds: furniReconcile.fixedOfferIds,
|
||||||
furniDataMissing: furniReconcile.missing,
|
furniDataMissing: furniReconcile.missing,
|
||||||
furniDataConflicts: furniReconcile.conflicts,
|
furniDataConflicts: furniReconcile.conflicts,
|
||||||
|
interactionFixed: verification.fixed,
|
||||||
spriteCount: result.spriteCount,
|
spriteCount: result.spriteCount,
|
||||||
warnings: result.warnings,
|
warnings: result.warnings,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -83,6 +83,9 @@ export const CLASSNAME_RULES: Array<{
|
|||||||
|
|
||||||
/** Determine the catalog category for a furniture item based on its classname and type. */
|
/** Determine the catalog category for a furniture item based on its classname and type. */
|
||||||
export function classifyFurni(classname: string, itemType: string): string {
|
export function classifyFurni(classname: string, itemType: string): string {
|
||||||
|
// Wall items are authoritative by type — a prefix rule (rare_, val_, xmas_,
|
||||||
|
// …) must never pull a wall item out of the "walls" category.
|
||||||
|
if (itemType === "wallitem" || itemType === "i") return "walls";
|
||||||
for (const rule of CLASSNAME_RULES) {
|
for (const rule of CLASSNAME_RULES) {
|
||||||
if (rule.test(classname, itemType)) return rule.cat;
|
if (rule.test(classname, itemType)) return rule.cat;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -382,9 +382,12 @@ export async function generateCatalogSql(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const price = autoPriceFurni(entry.classname);
|
const price = autoPriceFurni(entry.classname);
|
||||||
const displayName = entry.publicName || entry.classname;
|
// catalog_name must match items_base.item_name (the classname), exactly
|
||||||
|
// like the app's own import pipeline, so the studio's in-catalog joins
|
||||||
|
// and the emulator's item lookup stay consistent. offer_id is the item id
|
||||||
|
// (sprite id), not a literal '-1'.
|
||||||
statements.push(
|
statements.push(
|
||||||
`INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata) VALUES (${nextId}, ${pageId}, ${entry.itemId}, '${escSqlLiteral(displayName)}', ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, '-1', '');`,
|
`INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata) VALUES (${nextId}, ${pageId}, ${entry.itemId}, '${escSqlLiteral(entry.classname)}', ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${entry.itemId}, '');`,
|
||||||
);
|
);
|
||||||
nextId++;
|
nextId++;
|
||||||
generated++;
|
generated++;
|
||||||
|
|||||||
@@ -383,6 +383,10 @@ export async function cloneSingleFurni(params: {
|
|||||||
animationStates: nitroAnimationStatesCount(clonedNitroJson),
|
animationStates: nitroAnimationStatesCount(clonedNitroJson),
|
||||||
});
|
});
|
||||||
const interactionModesCount = autoInteraction.interactionModesCount;
|
const interactionModesCount = autoInteraction.interactionModesCount;
|
||||||
|
// The auto-detected interaction type (wired/gate/teleport/…) is real data
|
||||||
|
// (source furnidata + cloned .nitro), never a keyword guess — apply it so
|
||||||
|
// wired and mechanical furniture actually work on the emulator.
|
||||||
|
const interactionType = autoInteraction.interactionType;
|
||||||
|
|
||||||
let newId: number;
|
let newId: number;
|
||||||
try {
|
try {
|
||||||
@@ -393,7 +397,7 @@ export async function cloneSingleFurni(params: {
|
|||||||
allow_stack, allow_sit, allow_lay, allow_walk, interaction_type, interaction_modes_count, customparams)
|
allow_stack, allow_sit, allow_lay, allow_walk, interaction_type, interaction_modes_count, customparams)
|
||||||
VALUES
|
VALUES
|
||||||
(${nextId}, ${nextId}, ${entry.name}, ${classname}, ${itemType}, ${entry.xdim}, ${entry.ydim}, ${stackHeight},
|
(${nextId}, ${nextId}, ${entry.name}, ${classname}, ${itemType}, ${entry.xdim}, ${entry.ydim}, ${stackHeight},
|
||||||
${allowStack}, ${entry.cansiton ? "1" : "0"}, ${entry.canlayon ? "1" : "0"}, ${entry.canstandon ? "1" : "0"}, 'default', ${interactionModesCount}, ${entry.customparams})`);
|
${allowStack}, ${entry.cansiton ? "1" : "0"}, ${entry.canlayon ? "1" : "0"}, ${entry.canstandon ? "1" : "0"}, ${interactionType}, ${interactionModesCount}, ${entry.customparams})`);
|
||||||
return nextId;
|
return nextId;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -412,9 +416,15 @@ export async function cloneSingleFurni(params: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// FurnitureData entry — reuse the source's furnitype object, with our id.
|
// FurnitureData entry — reuse the source's furnitype object, but with our id
|
||||||
|
// AND our offerid. The source's offerid points at the source hotel's sprite
|
||||||
|
// ids, which would silently break the emulator's catalog offer lookup.
|
||||||
onProgress?.("writing_furnidata");
|
onProgress?.("writing_furnidata");
|
||||||
const furniDataEntry = { ...entry, id: newId } as Record<string, unknown>;
|
const furniDataEntry = {
|
||||||
|
...entry,
|
||||||
|
id: newId,
|
||||||
|
offerid: newId,
|
||||||
|
} as Record<string, unknown>;
|
||||||
if (deferFurniData) {
|
if (deferFurniData) {
|
||||||
// Batch mode: the caller collects entries and appends them once at the end.
|
// Batch mode: the caller collects entries and appends them once at the end.
|
||||||
} else {
|
} else {
|
||||||
@@ -436,7 +446,7 @@ export async function cloneSingleFurni(params: {
|
|||||||
await allocateCatalogItemId(async (nextCatalogId) => {
|
await allocateCatalogItemId(async (nextCatalogId) => {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
||||||
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, '-1', '')`);
|
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${newId}, '')`);
|
||||||
return nextCatalogId;
|
return nextCatalogId;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -447,6 +447,8 @@ export async function appendFurniEntriesBatch(
|
|||||||
export interface FurniDataReconcileResult {
|
export interface FurniDataReconcileResult {
|
||||||
/** Entries whose stale spriteId was corrected to the DB id. */
|
/** Entries whose stale spriteId was corrected to the DB id. */
|
||||||
fixedIds: number;
|
fixedIds: number;
|
||||||
|
/** Entries whose stale offerid was corrected to the DB id. */
|
||||||
|
fixedOfferIds: number;
|
||||||
/** items_base rows with no matching furnitype entry (need re-import). */
|
/** items_base rows with no matching furnitype entry (need re-import). */
|
||||||
missing: number;
|
missing: number;
|
||||||
/** spriteIds used by a different classname in the file; left untouched. */
|
/** spriteIds used by a different classname in the file; left untouched. */
|
||||||
@@ -458,6 +460,8 @@ export interface FurniDataReconcileResult {
|
|||||||
* repair drift:
|
* repair drift:
|
||||||
* - an entry matching the classname but carrying a stale spriteId is updated
|
* - an entry matching the classname but carrying a stale spriteId is updated
|
||||||
* to the DB id (items_base is authoritative);
|
* to the DB id (items_base is authoritative);
|
||||||
|
* - the same is done for a stale `offerid`, which must equal the spriteId so
|
||||||
|
* the emulator's catalog offer lookup stays in sync with the local ids;
|
||||||
* - rows missing entirely from the file are counted (they need a re-import
|
* - rows missing entirely from the file are counted (they need a re-import
|
||||||
* or batch-regen, since building an entry requires the SWF/nitro metadata);
|
* or batch-regen, since building an entry requires the SWF/nitro metadata);
|
||||||
* - genuine spriteId conflicts (id used by a different classname) are counted
|
* - genuine spriteId conflicts (id used by a different classname) are counted
|
||||||
@@ -498,21 +502,33 @@ export async function reconcileFurniDataWithItemsBase(): Promise<FurniDataReconc
|
|||||||
}
|
}
|
||||||
|
|
||||||
let fixedIds = 0;
|
let fixedIds = 0;
|
||||||
|
let fixedOfferIds = 0;
|
||||||
let missing = 0;
|
let missing = 0;
|
||||||
let conflicts = 0;
|
let conflicts = 0;
|
||||||
|
|
||||||
|
const entryOfferId = (entry: Record<string, unknown>): number => {
|
||||||
|
const v = Number(entry.offerid);
|
||||||
|
return Number.isFinite(v) ? v : -1;
|
||||||
|
};
|
||||||
|
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const spriteId = row.id;
|
const spriteId = row.id;
|
||||||
const existing = byId.get(spriteId);
|
const existing = byId.get(spriteId);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
if (existing.entry.classname !== row.itemName) {
|
if (existing.entry.classname !== row.itemName) {
|
||||||
conflicts++;
|
conflicts++;
|
||||||
|
} else if (entryOfferId(existing.entry) !== spriteId) {
|
||||||
|
existing.entry.offerid = spriteId;
|
||||||
|
fixedOfferIds++;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const byName = byClass.get(row.itemName);
|
const byName = byClass.get(row.itemName);
|
||||||
if (byName) {
|
if (byName) {
|
||||||
byName.entry.id = spriteId;
|
byName.entry.id = spriteId;
|
||||||
|
if (entryOfferId(byName.entry) !== spriteId) {
|
||||||
|
byName.entry.offerid = spriteId;
|
||||||
|
}
|
||||||
byId.set(spriteId, byName);
|
byId.set(spriteId, byName);
|
||||||
fixedIds++;
|
fixedIds++;
|
||||||
} else {
|
} else {
|
||||||
@@ -520,9 +536,9 @@ export async function reconcileFurniDataWithItemsBase(): Promise<FurniDataReconc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fixedIds > 0) {
|
if (fixedIds > 0 || fixedOfferIds > 0) {
|
||||||
await writeFurniData(furniData as Record<string, unknown>);
|
await writeFurniData(furniData as Record<string, unknown>);
|
||||||
}
|
}
|
||||||
return { fixedIds, missing, conflicts };
|
return { fixedIds, fixedOfferIds, missing, conflicts };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -544,15 +544,22 @@ export async function importSingleFurni(params: {
|
|||||||
.from(ItemsBase)
|
.from(ItemsBase)
|
||||||
.where(eq(ItemsBase.itemName, classname))
|
.where(eq(ItemsBase.itemName, classname))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (existsByName) {
|
if (existsByName && !updateExisting) {
|
||||||
if (!updateExisting) {
|
return {
|
||||||
return {
|
ok: false,
|
||||||
ok: false,
|
warnings,
|
||||||
warnings,
|
error: `Item "${classname}" already exists (id: ${existsByName.id})`,
|
||||||
error: `Item "${classname}" already exists (id: ${existsByName.id})`,
|
};
|
||||||
};
|
}
|
||||||
}
|
if (existsByName && existsByName.id !== originalId) {
|
||||||
// Update existing item - fall through to update logic
|
// The classname already occupies a different spriteId. Reassigning the
|
||||||
|
// id would orphan every reference (catalog item_ids, player items,
|
||||||
|
// furnidata), so this can never be an "update".
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
warnings,
|
||||||
|
error: `Item "${classname}" already exists with id ${existsByName.id} — cannot reassign to spriteId ${originalId}`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
const [existsById] = await db
|
const [existsById] = await db
|
||||||
.select({ itemName: ItemsBase.itemName })
|
.select({ itemName: ItemsBase.itemName })
|
||||||
@@ -560,16 +567,17 @@ export async function importSingleFurni(params: {
|
|||||||
.where(eq(ItemsBase.id, originalId))
|
.where(eq(ItemsBase.id, originalId))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (existsById && (!existsByName || existsById.itemName !== classname)) {
|
if (existsById && (!existsByName || existsById.itemName !== classname)) {
|
||||||
if (!updateExisting) {
|
// The spriteId is owned by a different classname. Even with
|
||||||
return {
|
// updateExisting this is a genuine conflict, not an update.
|
||||||
ok: false,
|
return {
|
||||||
warnings,
|
ok: false,
|
||||||
error: `spriteId ${originalId} already used by "${existsById.itemName}"`,
|
warnings,
|
||||||
};
|
error: `spriteId ${originalId} already used by "${existsById.itemName}"`,
|
||||||
}
|
};
|
||||||
// Update existing item - fall through to update logic
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isUpdate = !!existsByName;
|
||||||
|
|
||||||
// ── Check if spriteId already used in FurnitureData.json ──
|
// ── Check if spriteId already used in FurnitureData.json ──
|
||||||
const takenBy = await findFurniDataIdConflict(originalId, classname);
|
const takenBy = await findFurniDataIdConflict(originalId, classname);
|
||||||
if (takenBy && !updateExisting) {
|
if (takenBy && !updateExisting) {
|
||||||
@@ -604,18 +612,25 @@ export async function importSingleFurni(params: {
|
|||||||
// ── Insert into DB with enriched data ──────────────────────────────
|
// ── Insert into DB with enriched data ──────────────────────────────
|
||||||
onProgress?.("writing_db");
|
onProgress?.("writing_db");
|
||||||
try {
|
try {
|
||||||
// NOTE: live Arcturus `items_base` has no description/revision/rare columns —
|
if (isUpdate) {
|
||||||
// those metadata fields are persisted in FurnitureData.json (see entry below),
|
// NOTE: live Arcturus `items_base` has no description/revision/rare columns —
|
||||||
// not in the DB. Inserting them here throws "Unknown column 'description'".
|
// those metadata fields are persisted in FurnitureData.json (see entry below),
|
||||||
await db.execute(sql`
|
// not in the DB. Only refresh the columns this import manages.
|
||||||
INSERT INTO items_base (id, sprite_id, public_name, item_name, type)
|
await db.execute(sql`
|
||||||
VALUES (${spriteId}, ${spriteId}, ${bestName}, ${classname}, ${itemType})
|
UPDATE items_base SET sprite_id = ${spriteId}, public_name = ${bestName}, item_name = ${classname}, type = ${itemType}
|
||||||
`);
|
WHERE id = ${spriteId}
|
||||||
|
`);
|
||||||
|
} else {
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO items_base (id, sprite_id, public_name, item_name, type)
|
||||||
|
VALUES (${spriteId}, ${spriteId}, ${bestName}, ${classname}, ${itemType})
|
||||||
|
`);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return {
|
||||||
ok: false,
|
ok: false,
|
||||||
warnings,
|
warnings,
|
||||||
error: `DB insert failed: ${(err as Error).message}`,
|
error: `DB ${isUpdate ? "update" : "insert"} failed: ${(err as Error).message}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -995,23 +1010,6 @@ export async function importSingleFurni(params: {
|
|||||||
warnings.push("Catalog entry creation failed");
|
warnings.push("Catalog entry creation failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify and fix interaction_modes_count for all furniture
|
|
||||||
try {
|
|
||||||
const verification = await verifyAndFixInteractionModesCount();
|
|
||||||
if (verification.fixed > 0) {
|
|
||||||
warnings.push(
|
|
||||||
`Fixed interaction_modes_count for ${verification.fixed} of ${verification.checked} items`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
for (const err of verification.errors) {
|
|
||||||
warnings.push(`Interaction modes verification: ${err}`);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
warnings.push(
|
|
||||||
`Interaction modes verification failed: ${(err as Error).message}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ok: true,
|
ok: true,
|
||||||
itemId: spriteId,
|
itemId: spriteId,
|
||||||
@@ -1193,11 +1191,13 @@ export async function deleteImportedItem(classname: string): Promise<{
|
|||||||
starIdx !== -1 ? classname.substring(0, starIdx) : classname;
|
starIdx !== -1 ? classname.substring(0, starIdx) : classname;
|
||||||
const iconFileName = classname.replace(/\*/g, "_");
|
const iconFileName = classname.replace(/\*/g, "_");
|
||||||
|
|
||||||
// 1. Delete catalog_items
|
// 1. Delete catalog_items — match by the canonical item_ids link only.
|
||||||
|
// catalog_name may have been translated to a public name, so adding a
|
||||||
|
// name condition would silently leave an orphaned catalog row behind.
|
||||||
try {
|
try {
|
||||||
const itemIdStr = String(spriteId);
|
const itemIdStr = String(spriteId);
|
||||||
await db.execute(
|
await db.execute(
|
||||||
sql`DELETE FROM catalog_items WHERE item_ids = ${itemIdStr} AND catalog_name = ${classname}`,
|
sql`DELETE FROM catalog_items WHERE item_ids = ${itemIdStr}`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
errors.push(`catalog_items: ${(err as Error).message}`);
|
errors.push(`catalog_items: ${(err as Error).message}`);
|
||||||
@@ -1361,10 +1361,11 @@ export async function rollbackFurniImport(
|
|||||||
|
|
||||||
const errorsForItem: string[] = [];
|
const errorsForItem: string[] = [];
|
||||||
|
|
||||||
// 1. Delete from catalog_items
|
// 1. Delete from catalog_items — match by the canonical item_ids link only
|
||||||
|
// (catalog_name may be a translated display name).
|
||||||
try {
|
try {
|
||||||
await db.execute(
|
await db.execute(
|
||||||
sql`DELETE FROM catalog_items WHERE item_ids = ${String(item.spriteId)} AND catalog_name = ${item.classname}`,
|
sql`DELETE FROM catalog_items WHERE item_ids = ${String(item.spriteId)}`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
errorsForItem.push(`catalog_items: ${(err as Error).message}`);
|
errorsForItem.push(`catalog_items: ${(err as Error).message}`);
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ async function writeSqlMigration(params: {
|
|||||||
`INSERT INTO catalog_items`,
|
`INSERT INTO catalog_items`,
|
||||||
` (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)`,
|
` (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)`,
|
||||||
`SELECT (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), p.id, @next_id, ${escapeSql(params.classname)},`,
|
`SELECT (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), p.id, @next_id, ${escapeSql(params.classname)},`,
|
||||||
` ${params.price.credits}, ${params.price.points}, ${params.price.pointsType}, 1, 1, '-1', ''`,
|
` ${params.price.credits}, ${params.price.points}, ${params.price.pointsType}, 1, 1, @next_id, ''`,
|
||||||
`FROM catalog_pages p`,
|
`FROM catalog_pages p`,
|
||||||
`WHERE p.caption_save = ${escapeSql(captionSave)};`,
|
`WHERE p.caption_save = ${escapeSql(captionSave)};`,
|
||||||
"",
|
"",
|
||||||
@@ -391,7 +391,7 @@ export async function uploadSingleFurni(params: {
|
|||||||
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
|
||||||
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, '-1', '')`);
|
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${newId}, '')`);
|
||||||
return nextCatalogId;
|
return nextCatalogId;
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in new issue
Block a user