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) =>
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);

View file

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

View file

@ -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,
],
],
]
`;

View file

@ -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,
});
}
/**