fix bug resolving item fields under pet outfit

This commit is contained in:
Emi Matchu 2020-07-02 14:33:47 -07:00
parent 92d68ea5d6
commit 4c78abee96
6 changed files with 94 additions and 21 deletions

View file

@ -161,31 +161,46 @@ const typeDefs = gql`
const resolvers = { const resolvers = {
Item: { Item: {
name: async (item, _, { itemTranslationLoader }) => { name: async ({ id, name }, _, { itemTranslationLoader }) => {
// Search queries pre-fill this! if (name) return name;
if (item.name) return item.name; const translation = await itemTranslationLoader.load(id);
const translation = await itemTranslationLoader.load(item.id);
return translation.name; return translation.name;
}, },
description: async (item, _, { itemTranslationLoader }) => { description: async ({ id, description }, _, { itemTranslationLoader }) => {
const translation = await itemTranslationLoader.load(item.id); if (description) return description;
const translation = await itemTranslationLoader.load(id);
return translation.description; return translation.description;
}, },
isNc: (item) => item.rarityIndex === 500 || item.rarityIndex === 0, thumbnailUrl: async ({ id, thumbnailUrl }, _, { itemLoader }) => {
if (thumbnailUrl) return thumbnailUrl;
const item = await itemLoader.load(id);
return item.thumbnailUrl;
},
rarityIndex: async ({ id, rarityIndex }, _, { itemLoader }) => {
if (rarityIndex) return rarityIndex;
const item = await itemLoader.load(id);
return item.rarityIndex;
},
isNc: async ({ id, rarityIndex }, _, { itemLoader }) => {
if (rarityIndex) return rarityIndex === 500 || rarityIndex === 0;
const item = await itemLoader.load({ id });
return item.rarityIndex === 500 || item.rarityIndex === 0;
},
appearanceOn: async ( appearanceOn: async (
item, { id },
{ speciesId, colorId }, { speciesId, colorId },
{ petTypeBySpeciesAndColorLoader, itemSwfAssetLoader } { petTypeBySpeciesAndColorLoader, itemSwfAssetLoader, itemLoader }
) => { ) => {
const itemPromise = itemLoader.load(id);
const petType = await petTypeBySpeciesAndColorLoader.load({ const petType = await petTypeBySpeciesAndColorLoader.load({
speciesId: speciesId, speciesId: speciesId,
colorId: colorId, colorId: colorId,
}); });
const allSwfAssets = await itemSwfAssetLoader.load({ const allSwfAssets = await itemSwfAssetLoader.load({
itemId: item.id, itemId: id,
bodyId: petType.bodyId, bodyId: petType.bodyId,
}); });
if (allSwfAssets.length === 0) { if (allSwfAssets.length === 0) {
// If there's no assets at all, treat it as non-fitting: no appearance. // If there's no assets at all, treat it as non-fitting: no appearance.
// (If there are assets but they're non-SWF, we'll treat this as // (If there are assets but they're non-SWF, we'll treat this as
@ -196,6 +211,7 @@ const resolvers = {
const swfAssets = allSwfAssets.filter((sa) => sa.url.endsWith(".swf")); const swfAssets = allSwfAssets.filter((sa) => sa.url.endsWith(".swf"));
const restrictedZones = []; const restrictedZones = [];
const item = await itemPromise;
for (const [i, bit] of Array.from(item.zonesRestrict).entries()) { for (const [i, bit] of Array.from(item.zonesRestrict).entries()) {
if (bit === "1") { if (bit === "1") {
const zone = { id: i + 1 }; const zone = { id: i + 1 };
@ -426,6 +442,10 @@ const resolvers = {
pose: getPoseFromPetData(petMetaData, customPetData), pose: getPoseFromPetData(petMetaData, customPetData),
items: Object.values(customPetData.object_info_registry).map((o) => ({ items: Object.values(customPetData.object_info_registry).map((o) => ({
id: o.obj_info_id, id: o.obj_info_id,
name: o.name,
description: o.description,
thumbnailUrl: o.thumbnail_url,
rarityIndex: o.rarity_index,
})), })),
}; };
return outfit; return outfit;

View file

@ -59,7 +59,7 @@ const loadAllPetTypes = (db) => async () => {
return entities; return entities;
}; };
const buildItemsLoader = (db) => const buildItemLoader = (db) =>
new DataLoader(async (ids) => { new DataLoader(async (ids) => {
const qs = ids.map((_) => "?").join(","); const qs = ids.map((_) => "?").join(",");
const [rows, _] = await db.execute( const [rows, _] = await db.execute(
@ -72,7 +72,7 @@ const buildItemsLoader = (db) =>
return ids.map( return ids.map(
(id) => (id) =>
entitiesById.get(id) || new Error(`could not find item with ID: ${id}`) entitiesById.get(String(id)) || new Error(`could not find item with ID: ${id}`)
); );
}); });
@ -89,7 +89,7 @@ const buildItemTranslationLoader = (db) =>
return itemIds.map( return itemIds.map(
(itemId) => (itemId) =>
entitiesByItemId.get(itemId) || entitiesByItemId.get(String(itemId)) ||
new Error(`could not find translation for item ${itemId}`) new Error(`could not find translation for item ${itemId}`)
); );
}); });
@ -352,7 +352,7 @@ function buildLoaders(db) {
loaders.loadAllPetTypes = loadAllPetTypes(db); loaders.loadAllPetTypes = loadAllPetTypes(db);
loaders.colorTranslationLoader = buildColorTranslationLoader(db); loaders.colorTranslationLoader = buildColorTranslationLoader(db);
loaders.itemLoader = buildItemsLoader(db); loaders.itemLoader = buildItemLoader(db);
loaders.itemTranslationLoader = buildItemTranslationLoader(db); loaders.itemTranslationLoader = buildItemTranslationLoader(db);
loaders.itemSearchLoader = buildItemSearchLoader(db); loaders.itemSearchLoader = buildItemSearchLoader(db);
loaders.itemSearchToFitLoader = buildItemSearchToFitLoader(db); loaders.itemSearchToFitLoader = buildItemSearchToFitLoader(db);

View file

@ -56,6 +56,12 @@ describe("Outfit", () => {
"31856", "31856",
], ],
], ],
Array [
"SELECT * FROM pet_states WHERE id IN (?)",
Array [
"3951",
],
],
Array [ Array [
"SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?,?,?,?) AND locale = \\"en\\"", "SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?,?,?,?) AND locale = \\"en\\"",
Array [ Array [
@ -72,12 +78,6 @@ describe("Outfit", () => {
"56398", "56398",
], ],
], ],
Array [
"SELECT * FROM pet_states WHERE id IN (?)",
Array [
"3951",
],
],
Array [ Array [
"SELECT * FROM pet_types WHERE id IN (?)", "SELECT * FROM pet_types WHERE id IN (?)",
Array [ Array [

View file

@ -18,6 +18,11 @@ describe("Pet", () => {
pose pose
items { items {
id id
name
description
thumbnailUrl
rarityIndex
isNc
} }
} }
} }

View file

@ -9,28 +9,68 @@ Object {
}, },
"items": Array [ "items": Array [
Object { Object {
"description": "What does this ball actually do?",
"id": "37229", "id": "37229",
"isNc": false,
"name": "Magic Ball Table",
"rarityIndex": 101,
"thumbnailUrl": "http://images.neopets.com/items/gif_magicball_table.gif",
}, },
Object { Object {
"description": "Dont forget to wish upon a star.",
"id": "37375", "id": "37375",
"isNc": false,
"name": "Moon and Stars Background",
"rarityIndex": 75,
"thumbnailUrl": "http://images.neopets.com/items/bg_moonstars.gif",
}, },
Object { Object {
"description": "Hide your face and hair so no one can recognise you.",
"id": "38911", "id": "38911",
"isNc": false,
"name": "Zafara Agent Hood",
"rarityIndex": 92,
"thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_hood.gif",
}, },
Object { Object {
"description": "This robe is great for being stealthy.",
"id": "38912", "id": "38912",
"isNc": false,
"name": "Zafara Agent Robe",
"rarityIndex": 90,
"thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_robe.gif",
}, },
Object { Object {
"description": "Dont leave any trace that you were there with these gloves.",
"id": "38913", "id": "38913",
"isNc": false,
"name": "Zafara Agent Gloves",
"rarityIndex": 88,
"thumbnailUrl": "http://images.neopets.com/items/clo_zafara_agent_gloves.gif",
}, },
Object { Object {
"description": "These leaves almost look magical with their gentle glow.",
"id": "43014", "id": "43014",
"isNc": false,
"name": "Green Leaf String Lights",
"rarityIndex": 80,
"thumbnailUrl": "http://images.neopets.com/items/toy_stringlight_illleaf.gif",
}, },
Object { Object {
"description": "This jewelled staff shines with a magical light.",
"id": "43397", "id": "43397",
"isNc": true,
"name": "Jewelled Staff",
"rarityIndex": 500,
"thumbnailUrl": "http://images.neopets.com/items/mall_staff_jewelled.gif",
}, },
Object { Object {
"description": "Even the announcers of the Altador Cup celebrate. This was given out by the Advent Calendar in Y11.",
"id": "48313", "id": "48313",
"isNc": false,
"name": "Altador Cup Brooch",
"rarityIndex": 101,
"thumbnailUrl": "http://images.neopets.com/items/clo_altcuplogo_brooch.gif",
}, },
], ],
"pose": "SAD_MASC", "pose": "SAD_MASC",

View file

@ -571,6 +571,14 @@ Object {
"id": "1", "id": "1",
}, },
}, },
Object {
"color": Object {
"id": "110",
},
"species": Object {
"id": "1",
},
},
Object { Object {
"color": Object { "color": Object {
"id": "-1", "id": "-1",