modeling saves pet state

This commit is contained in:
Emi Matchu 2020-09-19 03:59:02 -07:00
parent 5332c9e265
commit 71f491ce65
4 changed files with 127 additions and 20 deletions

View file

@ -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) => const buildUserLoader = (db) =>
new DataLoader(async (ids) => { new DataLoader(async (ids) => {
const qs = ids.map((_) => "?").join(","); const qs = ids.map((_) => "?").join(",");
@ -716,6 +743,9 @@ function buildLoaders(db) {
db, db,
loaders loaders
); );
loaders.petStateByPetTypeAndAssetsLoader = buildPetStateByPetTypeAndAssetsLoader(
db
);
loaders.speciesLoader = buildSpeciesLoader(db); loaders.speciesLoader = buildSpeciesLoader(db);
loaders.speciesTranslationLoader = buildSpeciesTranslationLoader(db); loaders.speciesTranslationLoader = buildSpeciesTranslationLoader(db);
loaders.userLoader = buildUserLoader(db); loaders.userLoader = buildUserLoader(db);

View file

@ -89,13 +89,7 @@ describe("Pet", () => {
petAppearance(colorId: "75", speciesId: "54", pose: SAD_MASC) { petAppearance(colorId: "75", speciesId: "54", pose: SAD_MASC) {
id id
pose pose
layers { bodyId
id
swfUrl
}
restrictedZones {
id
}
} }
items( items(

View file

@ -481,6 +481,33 @@ Array [
"0000000000000000000000000000000000000000000000000000", "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", "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", "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,
],
],
] ]
`; `;

View file

@ -52,6 +52,7 @@ const resolvers = {
{ {
db, db,
petTypeBySpeciesAndColorLoader, petTypeBySpeciesAndColorLoader,
petStateByPetTypeAndAssetsLoader,
itemLoader, itemLoader,
itemTranslationLoader, itemTranslationLoader,
swfAssetByRemoteIdLoader, swfAssetByRemoteIdLoader,
@ -65,6 +66,7 @@ const resolvers = {
await saveModelingData(customPetData, petMetaData, { await saveModelingData(customPetData, petMetaData, {
db, db,
petTypeBySpeciesAndColorLoader, petTypeBySpeciesAndColorLoader,
petStateByPetTypeAndAssetsLoader,
itemLoader, itemLoader,
itemTranslationLoader, itemTranslationLoader,
swfAssetByRemoteIdLoader, swfAssetByRemoteIdLoader,
@ -157,6 +159,7 @@ async function saveModelingData(
{ {
db, db,
petTypeBySpeciesAndColorLoader, petTypeBySpeciesAndColorLoader,
petStateByPetTypeAndAssetsLoader,
itemLoader, itemLoader,
itemTranslationLoader, itemTranslationLoader,
swfAssetByRemoteIdLoader, swfAssetByRemoteIdLoader,
@ -266,20 +269,42 @@ async function saveModelingData(
// TODO: If we look up the potentially existing pet state earlier, then I // 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 // 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 // NOTE: This pet type should have been looked up when syncing pet type, so
// this should be cached. // this should be cached.
// const petType = await petTypeBySpeciesAndColorLoader.load({ const petType = await petTypeBySpeciesAndColorLoader.load({
// colorId: String(customPet.color_id), colorId: String(customPet.color_id),
// speciesId: String(customPet.species_id), speciesId: String(customPet.species_id),
// }); });
// const incomingPetStates = [ const incomingPetStates = [
// { {
// petTypeId: petType.id, petTypeId: petType.id,
// swfAssetIds: incomingPetSwfAssets.map(a => a.remoteId).sort().join(","), swfAssetIds: incomingPetSwfAssets
// female: .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,
});
} }
/** /**