diff --git a/scripts/setup-mysql-dev-constants.sql b/scripts/setup-mysql-dev-constants.sql index edf4a25..b1911cb 100644 --- a/scripts/setup-mysql-dev-constants.sql +++ b/scripts/setup-mysql-dev-constants.sql @@ -133,4 +133,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2020-09-19 3:12:59 +-- Dump completed on 2020-09-19 3:34:44 diff --git a/scripts/setup-mysql-dev-schema.sql b/scripts/setup-mysql-dev-schema.sql index ee50b72..315d516 100644 --- a/scripts/setup-mysql-dev-schema.sql +++ b/scripts/setup-mysql-dev-schema.sql @@ -88,6 +88,28 @@ CREATE TABLE `pet_types` ( ) ENGINE=InnoDB AUTO_INCREMENT=4795 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `pet_states` +-- + +DROP TABLE IF EXISTS `pet_states`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `pet_states` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `pet_type_id` mediumint(9) NOT NULL, + `swf_asset_ids` text COLLATE utf8_unicode_ci NOT NULL, + `female` tinyint(1) DEFAULT NULL, + `mood_id` int(11) DEFAULT NULL, + `unconverted` tinyint(1) DEFAULT NULL, + `labeled` tinyint(1) NOT NULL DEFAULT '0', + `glitched` tinyint(1) NOT NULL DEFAULT '0', + `artist_neopets_username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `pet_states_pet_type_id` (`pet_type_id`) +) ENGINE=InnoDB AUTO_INCREMENT=28561 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `swf_assets` -- @@ -126,4 +148,4 @@ CREATE TABLE `swf_assets` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2020-09-19 3:12:53 +-- Dump completed on 2020-09-19 3:34:36 diff --git a/src/server/query-tests/__snapshots__/Pet.test.js.snap b/src/server/query-tests/__snapshots__/Pet.test.js.snap index 71143a8..2e13d2b 100644 --- a/src/server/query-tests/__snapshots__/Pet.test.js.snap +++ b/src/server/query-tests/__snapshots__/Pet.test.js.snap @@ -191,7 +191,7 @@ Array [ ], ], Array [ - "SELECT * FROM swf_assets WHERE (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?)", + "SELECT * FROM swf_assets WHERE (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?)", Array [ "object", "6829", @@ -209,6 +209,18 @@ Array [ "51959", "object", "56478", + "biology", + "7942", + "biology", + "7941", + "biology", + "24008", + "biology", + "21060", + "biology", + "21057", + "biology", + "7946", ], ], Array [ @@ -367,7 +379,7 @@ Array [ ], ], Array [ - "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", + "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", Array [ 180, 2020-01-01T00:00:00.000Z, @@ -425,6 +437,48 @@ Array [ "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf", 27, "", + 0, + 2020-01-01T00:00:00.000Z, + "7942", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf", + 5, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "7941", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf", + 15, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "24008", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf", + 30, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "21060", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf", + 33, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "21057", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf", + 34, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "7946", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf", + 37, + "0000000000000000000000000000000000000000000000000000", ], ], ] @@ -545,6 +599,15 @@ Array [ "48313", ], ], + Array [ + "SELECT * FROM pet_states + WHERE pet_type_id IN (?) + ORDER BY (mood_id IS NULL) ASC, mood_id ASC, female DESC, + unconverted DESC, glitched ASC, id DESC", + Array [ + "4795", + ], + ], ] `; @@ -584,7 +647,7 @@ Array [ ], ], Array [ - "SELECT * FROM swf_assets WHERE (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?)", + "SELECT * FROM swf_assets WHERE (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?)", Array [ "object", "6829", @@ -602,6 +665,18 @@ Array [ "51959", "object", "56478", + "biology", + "7942", + "biology", + "7941", + "biology", + "24008", + "biology", + "21060", + "biology", + "21057", + "biology", + "7946", ], ], Array [ @@ -743,7 +818,7 @@ Array [ ], ], Array [ - "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", + "INSERT INTO swf_assets (body_id, created_at, remote_id, type, url, zone_id, zones_restrict) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?);", Array [ 180, 2020-01-01T00:00:00.000Z, @@ -801,6 +876,48 @@ Array [ "http://images.neopets.com/cp/items/swf/000/000/056/56478_eabc28e7c7.swf", 27, "", + 0, + 2020-01-01T00:00:00.000Z, + "7942", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/007/7942_2eab06fd7b.swf", + 5, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "7941", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/007/7941_2c4cc4b846.swf", + 15, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "24008", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/024/24008_a05fe9876a.swf", + 30, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "21060", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/021/21060_d77ba93b7b.swf", + 33, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "21057", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/021/21057_4550efbb2f.swf", + 34, + "0000000000000000000000000000000000000000000000000000", + 0, + 2020-01-01T00:00:00.000Z, + "7946", + "biology", + "http://images.neopets.com/cp/bio/swf/000/000/007/7946_0348dad587.swf", + 37, + "0000000000000000000000000000000000000000000000000000", ], ], Array [ diff --git a/src/server/types/Outfit.js b/src/server/types/Outfit.js index d533598..438946c 100644 --- a/src/server/types/Outfit.js +++ b/src/server/types/Outfit.js @@ -163,9 +163,8 @@ async function saveModelingData( } ) { const customPet = customPetData.custom_pet; - const objectInfos = Object.values(customPetData.object_info_registry); - const objectAssets = Object.values(customPetData.object_asset_registry); + const objectInfos = Object.values(customPetData.object_info_registry); const incomingItems = objectInfos.map((objectInfo) => ({ id: String(objectInfo.obj_info_id), zonesRestrict: objectInfo.zones_restrict, @@ -176,7 +175,6 @@ async function saveModelingData( price: objectInfo.price, weightLbs: objectInfo.weight_lbs, })); - const incomingItemTranslations = objectInfos.map((objectInfo) => ({ itemId: String(objectInfo.obj_info_id), locale: "en", @@ -185,6 +183,7 @@ async function saveModelingData( rarity: objectInfo.rarity, })); + const objectAssets = Object.values(customPetData.object_asset_registry); const incomingItemSwfAssets = objectAssets.map((objectAsset) => ({ type: "object", remoteId: String(objectAsset.asset_id), @@ -197,6 +196,18 @@ async function saveModelingData( bodyId: customPet.body_id, })); + const biologyAssets = Object.values(customPet.biology_by_zone); + const incomingPetSwfAssets = biologyAssets.map((biologyAsset) => ({ + type: "biology", + remoteId: String(biologyAsset.part_id), + url: biologyAsset.asset_url, + zoneId: biologyAsset.zone_id, + zonesRestrict: biologyAsset.zones_restrict, + bodyId: 0, + })); + + const incomingSwfAssets = [...incomingItemSwfAssets, ...incomingPetSwfAssets]; + const incomingPetTypes = [ { colorId: String(customPet.color_id), @@ -240,7 +251,7 @@ async function saveModelingData( row.itemId, ], }), - syncToDb(db, incomingItemSwfAssets, { + syncToDb(db, incomingSwfAssets, { loader: swfAssetByRemoteIdLoader, tableName: "swf_assets", buildLoaderKey: (row) => ({ type: row.type, remoteId: row.remoteId }), @@ -252,6 +263,23 @@ async function saveModelingData( includeUpdatedAt: false, }), ]); + + // 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! + // 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: + // }, + // ]; } /** @@ -298,6 +326,11 @@ async function syncToDb( insert.updatedAt = new Date(); } inserts.push(insert); + + // Remove this from the loader cache, so that loading again will fetch + // the inserted row. + loader.clear(buildLoaderKey(incomingRow)); + continue; } @@ -315,6 +348,10 @@ async function syncToDb( update.updatedAt = new Date(); } updates.push({ incomingRow, update }); + + // Remove this from the loader cache, so that loading again will fetch + // the updated row. + loader.clear(buildLoaderKey(incomingRow)); } }