From da72837d9e4204d0ba8853890aa1c3311103dd8b Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 6 Oct 2020 05:41:09 -0700 Subject: [PATCH] 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 --- src/server/modeling.js | 44 ++++- src/server/query-tests/Pet.test.js | 7 + .../__snapshots__/Pet.test.js.snap | 184 +++++++++++++++++- 3 files changed, 230 insertions(+), 5 deletions(-) diff --git a/src/server/modeling.js b/src/server/modeling.js index 5695ff9f..a92127bc 100644 --- a/src/server/modeling.js +++ b/src/server/modeling.js @@ -212,6 +212,18 @@ async function saveSwfAssetModelingData(customPetData, context) { 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, { loader: swfAssetByRemoteIdLoader, tableName: "swf_assets", @@ -222,6 +234,36 @@ async function saveSwfAssetModelingData(customPetData, context) { row.remoteId, ], 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() ); if (afterInsert) { - await afterInsert(); + await afterInsert(inserts); } } diff --git a/src/server/query-tests/Pet.test.js b/src/server/query-tests/Pet.test.js index f0c1f8cb..401e975d 100644 --- a/src/server/query-tests/Pet.test.js +++ b/src/server/query-tests/Pet.test.js @@ -192,6 +192,13 @@ describe("Pet", () => { rarityIndex isNc createdAt + + appearanceOn(colorId: "75", speciesId: "54") { + layers { + id + swfUrl + } + } } } `, diff --git a/src/server/query-tests/__snapshots__/Pet.test.js.snap b/src/server/query-tests/__snapshots__/Pet.test.js.snap index 1111b37a..bcb3f366 100644 --- a/src/server/query-tests/__snapshots__/Pet.test.js.snap +++ b/src/server/query-tests/__snapshots__/Pet.test.js.snap @@ -488,6 +488,36 @@ Array [ "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 [ "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", Array [ @@ -561,6 +591,14 @@ exports[`Pet models new pet and item data 3`] = ` Object { "items": Array [ 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", "description": "What does this ball actually do?", "id": "37229", @@ -570,6 +608,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/gif_magicball_table.gif", }, 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", "description": "Dont forget to wish upon a star.", "id": "37375", @@ -579,6 +625,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/bg_moonstars.gif", }, 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", "description": "Hide your face and hair so no one can recognise you.", "id": "38911", @@ -588,6 +642,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_hood.gif", }, 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", "description": "This robe is great for being stealthy.", "id": "38912", @@ -597,6 +659,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_robe.gif", }, 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", "description": "Dont leave any trace that you were there with these gloves.", "id": "38913", @@ -606,6 +676,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_gloves.gif", }, 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", "description": "These leaves almost look magical with their gentle glow.", "id": "43014", @@ -615,6 +693,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/toy_stringlight_illleaf.gif", }, 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", "description": "This jewelled staff shines with a magical light.", "id": "43397", @@ -624,6 +710,14 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/mall_staff_jewelled.gif", }, 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", "description": "Even the announcers of the Altador Cup celebrate. This was given out by the Advent Calendar in Y11.", "id": "48313", @@ -712,6 +806,31 @@ Array [ "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 [ "SELECT sa.*, rel.parent_id FROM swf_assets sa INNER JOIN parents_swf_assets rel ON @@ -1060,6 +1179,36 @@ Array [ "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 [ "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", Array [ @@ -1473,11 +1622,30 @@ 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 [ - "0", - "object", - "6829", + "Item", + "38913", + "14855", + "Item", + "38912", + "14856", + "Item", + "38911", + "14857", + "Item", + "43014", + "36414", + "Item", + "43397", + "39646", + "Item", + "37229", + "51959", + "Item", + "48313", + "56478", ], ], Array [ @@ -1487,6 +1655,14 @@ Array [ "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 [ "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", Array [