From 71f491ce650c58e3fc958bdf85d9adfdab652eb1 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 19 Sep 2020 03:59:02 -0700 Subject: [PATCH] modeling saves pet state --- src/server/loaders.js | 30 ++++++++++ src/server/query-tests/Pet.test.js | 8 +-- .../__snapshots__/Pet.test.js.snap | 60 ++++++++++++++++++- src/server/types/Outfit.js | 49 +++++++++++---- 4 files changed, 127 insertions(+), 20 deletions(-) diff --git a/src/server/loaders.js b/src/server/loaders.js index 2837bd0..9e5e944 100644 --- a/src/server/loaders.js +++ b/src/server/loaders.js @@ -577,6 +577,33 @@ const buildCanonicalPetStateForBodyLoader = (db, loaders) => ); }); +const buildPetStateByPetTypeAndAssetsLoader = (db) => + new DataLoader( + async (petTypeIdAndAssetIdsPairs) => { + const qs = petTypeIdAndAssetIdsPairs + .map((_) => "(pet_type_id = ? AND swf_asset_ids = ?)") + .join(" OR "); + const values = petTypeIdAndAssetIdsPairs.map( + ({ petTypeId, swfAssetIds }) => [petTypeId, swfAssetIds] + ); + const [rows, _] = await db.execute( + `SELECT * FROM pet_states WHERE ${qs}`, + values + ); + + const entities = rows.map(normalizeRow); + + return petTypeIdAndAssetIdsPairs.map(({ petTypeId, swfAssetIds }) => + entities.find( + (e) => e.petTypeId === petTypeId && e.swfAssetIds === swfAssetIds + ) + ); + }, + { + cacheKeyFn: ({ petTypeId, swfAssetIds }) => `${petTypeId}-${swfAssetIds}`, + } + ); + const buildUserLoader = (db) => new DataLoader(async (ids) => { const qs = ids.map((_) => "?").join(","); @@ -716,6 +743,9 @@ function buildLoaders(db) { db, loaders ); + loaders.petStateByPetTypeAndAssetsLoader = buildPetStateByPetTypeAndAssetsLoader( + db + ); loaders.speciesLoader = buildSpeciesLoader(db); loaders.speciesTranslationLoader = buildSpeciesTranslationLoader(db); loaders.userLoader = buildUserLoader(db); diff --git a/src/server/query-tests/Pet.test.js b/src/server/query-tests/Pet.test.js index 9233a12..3ca5544 100644 --- a/src/server/query-tests/Pet.test.js +++ b/src/server/query-tests/Pet.test.js @@ -89,13 +89,7 @@ describe("Pet", () => { petAppearance(colorId: "75", speciesId: "54", pose: SAD_MASC) { id pose - layers { - id - swfUrl - } - restrictedZones { - id - } + bodyId } items( diff --git a/src/server/query-tests/__snapshots__/Pet.test.js.snap b/src/server/query-tests/__snapshots__/Pet.test.js.snap index 2e13d2b..3f6a1af 100644 --- a/src/server/query-tests/__snapshots__/Pet.test.js.snap +++ b/src/server/query-tests/__snapshots__/Pet.test.js.snap @@ -481,6 +481,33 @@ Array [ "0000000000000000000000000000000000000000000000000000", ], ], + Array [ + "SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?)", + Array [ + "54", + "75", + ], + ], + Array [ + "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", + Array [ + Array [ + "4795", + "21057,21060,24008,7941,7942,7946", + ], + ], + ], + Array [ + "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", + Array [ + false, + true, + 2, + "4795", + "21057,21060,24008,7941,7942,7946", + false, + ], + ], ] `; @@ -560,7 +587,11 @@ Object { "thumbnailUrl": "http://images.neopets.com/items/clo_altcuplogo_brooch.gif", }, ], - "petAppearance": null, + "petAppearance": Object { + "bodyId": "180", + "id": "28561", + "pose": "SAD_MASC", + }, } `; @@ -939,6 +970,33 @@ Array [ "43397", ], ], + Array [ + "SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?)", + Array [ + "54", + "75", + ], + ], + Array [ + "SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)", + Array [ + Array [ + "4795", + "21057,21060,24008,7941,7942,7946", + ], + ], + ], + Array [ + "INSERT INTO pet_states (female, labeled, mood_id, pet_type_id, swf_asset_ids, unconverted) VALUES (?, ?, ?, ?, ?, ?);", + Array [ + false, + true, + 2, + "4795", + "21057,21060,24008,7941,7942,7946", + false, + ], + ], ] `; diff --git a/src/server/types/Outfit.js b/src/server/types/Outfit.js index 438946c..88ba1f5 100644 --- a/src/server/types/Outfit.js +++ b/src/server/types/Outfit.js @@ -52,6 +52,7 @@ const resolvers = { { db, petTypeBySpeciesAndColorLoader, + petStateByPetTypeAndAssetsLoader, itemLoader, itemTranslationLoader, swfAssetByRemoteIdLoader, @@ -65,6 +66,7 @@ const resolvers = { await saveModelingData(customPetData, petMetaData, { db, petTypeBySpeciesAndColorLoader, + petStateByPetTypeAndAssetsLoader, itemLoader, itemTranslationLoader, swfAssetByRemoteIdLoader, @@ -157,6 +159,7 @@ async function saveModelingData( { db, petTypeBySpeciesAndColorLoader, + petStateByPetTypeAndAssetsLoader, itemLoader, itemTranslationLoader, swfAssetByRemoteIdLoader, @@ -266,20 +269,42 @@ async function saveModelingData( // TODO: If we look up the potentially existing pet state earlier, then I // think we can prime the cache and avoid creating a waterfall of - // queries here, even though it looks waterfall-y! + // queries here in the happy case, even though it'll look waterfall-y! // NOTE: This pet type should have been looked up when syncing pet type, so // this should be cached. - // const petType = await petTypeBySpeciesAndColorLoader.load({ - // colorId: String(customPet.color_id), - // speciesId: String(customPet.species_id), - // }); - // const incomingPetStates = [ - // { - // petTypeId: petType.id, - // swfAssetIds: incomingPetSwfAssets.map(a => a.remoteId).sort().join(","), - // female: - // }, - // ]; + const petType = await petTypeBySpeciesAndColorLoader.load({ + colorId: String(customPet.color_id), + speciesId: String(customPet.species_id), + }); + const incomingPetStates = [ + { + petTypeId: petType.id, + swfAssetIds: incomingPetSwfAssets + .map((a) => a.remoteId) + .sort() + .join(","), + female: petMetaData.gender === 2, // sorry for this column name :/ + moodId: petMetaData.mood, + unconverted: incomingPetSwfAssets.length === 1, + labeled: true, + }, + ]; + + await syncToDb(db, incomingPetStates, { + loader: petStateByPetTypeAndAssetsLoader, + tableName: "pet_states", + buildLoaderKey: (row) => ({ + petTypeId: row.petTypeId, + swfAssetIds: row.swfAssetIds, + }), + buildUpdateCondition: (row) => [ + `pet_type_id = ? AND swf_asset_ids = ?`, + row.petTypeId, + row.swfAssetIds, + ], + includeCreatedAt: false, + includeUpdatedAt: false, + }); } /**