modeling saves item-to-asset relationships

this is the last one to get parity with current modeling, I think?? I'm gonna add one more feature though: removing no-longer-used assets from the item
This commit is contained in:
Emi Matchu 2020-10-06 05:41:09 -07:00
parent fefb798e87
commit da72837d9e
3 changed files with 230 additions and 5 deletions

View file

@ -212,6 +212,18 @@ async function saveSwfAssetModelingData(customPetData, context) {
const incomingSwfAssets = [...incomingItemSwfAssets, ...incomingPetSwfAssets]; const incomingSwfAssets = [...incomingItemSwfAssets, ...incomingPetSwfAssets];
// Build a map from asset ID to item ID. We'll use this later to build the
// new parents_swf_assets rows.
const assetIdToItemIdMap = new Map();
const objectInfos = Object.values(customPetData.object_info_registry);
for (const objectInfo of objectInfos) {
const itemId = String(objectInfo.obj_info_id);
const assetIds = Object.values(objectInfo.assets_by_zone).map(String);
for (const assetId of assetIds) {
assetIdToItemIdMap.set(assetId, itemId);
}
}
syncToDb(db, incomingSwfAssets, { syncToDb(db, incomingSwfAssets, {
loader: swfAssetByRemoteIdLoader, loader: swfAssetByRemoteIdLoader,
tableName: "swf_assets", tableName: "swf_assets",
@ -222,6 +234,36 @@ async function saveSwfAssetModelingData(customPetData, context) {
row.remoteId, row.remoteId,
], ],
includeUpdatedAt: false, includeUpdatedAt: false,
afterInsert: async (inserts) => {
// After inserting the assets, insert corresponding rows in
// parents_swf_assets for item assets, to mark the asset as belonging to
// the item. (We do this separately for pet states, so that we can get
// the pet state ID first.)
const itemAssetInserts = inserts.filter((i) => i.type === "object");
const qs = itemAssetInserts
.map(
(_) =>
// A bit cheesy: we use a subquery here to insert _our_ ID for the
// asset, despite only having remote_id available here. This saves
// us from another round-trip to SELECT the inserted IDs.
`(?, ?, ` +
`(SELECT id FROM swf_assets WHERE type = "object" AND remote_id = ?))`
)
.join(", ");
const values = itemAssetInserts
.map(({ remoteId: swfAssetId }) => [
"Item",
assetIdToItemIdMap.get(swfAssetId),
swfAssetId,
])
.flat();
await db.execute(
`INSERT INTO parents_swf_assets (parent_type, parent_id, swf_asset_id)
VALUES ${qs}`,
values
);
},
}); });
} }
@ -340,7 +382,7 @@ async function syncToDb(
rowValues.flat() rowValues.flat()
); );
if (afterInsert) { if (afterInsert) {
await afterInsert(); await afterInsert(inserts);
} }
} }

View file

@ -192,6 +192,13 @@ describe("Pet", () => {
rarityIndex rarityIndex
isNc isNc
createdAt createdAt
appearanceOn(colorId: "75", speciesId: "54") {
layers {
id
swfUrl
}
}
} }
} }
`, `,

View file

@ -488,6 +488,36 @@ Array [
"75", "75",
], ],
], ],
Array [
"INSERT INTO parents_swf_assets (parent_type, parent_id, swf_asset_id)
VALUES (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?))",
Array [
"Item",
"37375",
"6829",
"Item",
"38913",
"14855",
"Item",
"38912",
"14856",
"Item",
"38911",
"14857",
"Item",
"43014",
"36414",
"Item",
"43397",
"39646",
"Item",
"37229",
"51959",
"Item",
"48313",
"56478",
],
],
Array [ Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [ Array [
@ -561,6 +591,14 @@ exports[`Pet models new pet and item data 3`] = `
Object { Object {
"items": Array [ "items": Array [
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "7",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/051/51959_4439727c48.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "What does this ball actually do?", "description": "What does this ball actually do?",
"id": "37229", "id": "37229",
@ -570,6 +608,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/gif_magicball_table.gif", "thumbnailUrl": "http://images.neopets.com/items/gif_magicball_table.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "1",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "Dont forget to wish upon a star.", "description": "Dont forget to wish upon a star.",
"id": "37375", "id": "37375",
@ -579,6 +625,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/bg_moonstars.gif", "thumbnailUrl": "http://images.neopets.com/items/bg_moonstars.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "4",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/014/14857_d43380ef66.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "Hide your face and hair so no one can recognise you.", "description": "Hide your face and hair so no one can recognise you.",
"id": "38911", "id": "38911",
@ -588,6 +642,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_hood.gif", "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_hood.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "3",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/014/14856_46c1b32797.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "This robe is great for being stealthy.", "description": "This robe is great for being stealthy.",
"id": "38912", "id": "38912",
@ -597,6 +659,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_robe.gif", "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_robe.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "2",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/014/14855_215f367070.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "Dont leave any trace that you were there with these gloves.", "description": "Dont leave any trace that you were there with these gloves.",
"id": "38913", "id": "38913",
@ -606,6 +676,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_gloves.gif", "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_gloves.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "5",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/036/36414_1e2aaab4ad.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "These leaves almost look magical with their gentle glow.", "description": "These leaves almost look magical with their gentle glow.",
"id": "43014", "id": "43014",
@ -615,6 +693,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/toy_stringlight_illleaf.gif", "thumbnailUrl": "http://images.neopets.com/items/toy_stringlight_illleaf.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "6",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/039/39646_e129e22ada.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "This jewelled staff shines with a magical light.", "description": "This jewelled staff shines with a magical light.",
"id": "43397", "id": "43397",
@ -624,6 +710,14 @@ Object {
"thumbnailUrl": "http://images.neopets.com/items/mall_staff_jewelled.gif", "thumbnailUrl": "http://images.neopets.com/items/mall_staff_jewelled.gif",
}, },
Object { Object {
"appearanceOn": Object {
"layers": Array [
Object {
"id": "8",
"swfUrl": "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf",
},
],
},
"createdAt": "2020-01-01T00:00:00.000Z", "createdAt": "2020-01-01T00:00:00.000Z",
"description": "Even the announcers of the Altador Cup celebrate. This was given out by the Advent Calendar in Y11.", "description": "Even the announcers of the Altador Cup celebrate. This was given out by the Advent Calendar in Y11.",
"id": "48313", "id": "48313",
@ -712,6 +806,31 @@ Array [
"1", "1",
], ],
], ],
Array [
"SELECT sa.*, rel.parent_id FROM swf_assets sa
INNER JOIN parents_swf_assets rel ON
rel.parent_type = \\"Item\\" AND
rel.swf_asset_id = sa.id
WHERE (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0)) OR (rel.parent_id = ? AND (sa.body_id = ? OR sa.body_id = 0))",
Array [
"37229",
"180",
"37375",
"180",
"38911",
"180",
"38912",
"180",
"38913",
"180",
"43014",
"180",
"43397",
"180",
"48313",
"180",
],
],
Array [ Array [
"SELECT sa.*, rel.parent_id FROM swf_assets sa "SELECT sa.*, rel.parent_id FROM swf_assets sa
INNER JOIN parents_swf_assets rel ON INNER JOIN parents_swf_assets rel ON
@ -1060,6 +1179,36 @@ Array [
"43397", "43397",
], ],
], ],
Array [
"INSERT INTO parents_swf_assets (parent_type, parent_id, swf_asset_id)
VALUES (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?))",
Array [
"Item",
"37375",
"6829",
"Item",
"38913",
"14855",
"Item",
"38912",
"14856",
"Item",
"38911",
"14857",
"Item",
"43014",
"36414",
"Item",
"43397",
"39646",
"Item",
"37229",
"51959",
"Item",
"48313",
"56478",
],
],
Array [ Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)",
Array [ Array [
@ -1473,11 +1622,30 @@ Array [
], ],
], ],
Array [ Array [
"UPDATE swf_assets SET body_id = ? WHERE type = ? AND remote_id = ? LIMIT 1;", "INSERT INTO parents_swf_assets (parent_type, parent_id, swf_asset_id)
VALUES (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?)), (?, ?, (SELECT id FROM swf_assets WHERE type = \\"object\\" AND remote_id = ?))",
Array [ Array [
"0", "Item",
"object", "38913",
"6829", "14855",
"Item",
"38912",
"14856",
"Item",
"38911",
"14857",
"Item",
"43014",
"36414",
"Item",
"43397",
"39646",
"Item",
"37229",
"51959",
"Item",
"48313",
"56478",
], ],
], ],
Array [ Array [
@ -1487,6 +1655,14 @@ Array [
"7941,7942,7946,21057,21060,24008", "7941,7942,7946,21057,21060,24008",
], ],
], ],
Array [
"UPDATE swf_assets SET body_id = ? WHERE type = ? AND remote_id = ? LIMIT 1;",
Array [
"0",
"object",
"6829",
],
],
Array [ Array [
"INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);",
Array [ Array [