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 = {
Item: {
name: async (item, _, { itemTranslationLoader }) => {
// Search queries pre-fill this!
if (item.name) return item.name;
const translation = await itemTranslationLoader.load(item.id);
name: async ({ id, name }, _, { itemTranslationLoader }) => {
if (name) return name;
const translation = await itemTranslationLoader.load(id);
return translation.name;
},
description: async (item, _, { itemTranslationLoader }) => {
const translation = await itemTranslationLoader.load(item.id);
description: async ({ id, description }, _, { itemTranslationLoader }) => {
if (description) return description;
const translation = await itemTranslationLoader.load(id);
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 (
item,
{ id },
{ speciesId, colorId },
{ petTypeBySpeciesAndColorLoader, itemSwfAssetLoader }
{ petTypeBySpeciesAndColorLoader, itemSwfAssetLoader, itemLoader }
) => {
const itemPromise = itemLoader.load(id);
const petType = await petTypeBySpeciesAndColorLoader.load({
speciesId: speciesId,
colorId: colorId,
});
const allSwfAssets = await itemSwfAssetLoader.load({
itemId: item.id,
itemId: id,
bodyId: petType.bodyId,
});
if (allSwfAssets.length === 0) {
// 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
@ -196,6 +211,7 @@ const resolvers = {
const swfAssets = allSwfAssets.filter((sa) => sa.url.endsWith(".swf"));
const restrictedZones = [];
const item = await itemPromise;
for (const [i, bit] of Array.from(item.zonesRestrict).entries()) {
if (bit === "1") {
const zone = { id: i + 1 };
@ -426,6 +442,10 @@ const resolvers = {
pose: getPoseFromPetData(petMetaData, customPetData),
items: Object.values(customPetData.object_info_registry).map((o) => ({
id: o.obj_info_id,
name: o.name,
description: o.description,
thumbnailUrl: o.thumbnail_url,
rarityIndex: o.rarity_index,
})),
};
return outfit;

View file

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

View file

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

View file

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

View file

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

View file

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