2020-05-02 13:40:37 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/react-hooks";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useOutfitAppearance downloads the outfit's appearance data, and returns
|
|
|
|
* visibleLayers for rendering.
|
|
|
|
*/
|
|
|
|
export default function useOutfitAppearance(outfitState) {
|
2020-05-02 22:32:08 -07:00
|
|
|
const {
|
|
|
|
wornItemIds,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
emotion,
|
|
|
|
genderPresentation,
|
|
|
|
} = outfitState;
|
2020-05-02 13:40:37 -07:00
|
|
|
|
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
2020-05-02 22:32:08 -07:00
|
|
|
query(
|
|
|
|
$wornItemIds: [ID!]!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
$emotion: Emotion!
|
|
|
|
$genderPresentation: GenderPresentation!
|
|
|
|
) {
|
|
|
|
petAppearance(
|
|
|
|
speciesId: $speciesId
|
|
|
|
colorId: $colorId
|
|
|
|
emotion: $emotion
|
|
|
|
genderPresentation: $genderPresentation
|
|
|
|
) {
|
2020-05-02 17:22:46 -07:00
|
|
|
...PetAppearanceForOutfitPreview
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
items(ids: $wornItemIds) {
|
|
|
|
id
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
2020-05-02 17:22:46 -07:00
|
|
|
...ItemAppearanceForOutfitPreview
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${itemAppearanceFragment}
|
2020-05-02 17:22:46 -07:00
|
|
|
${petAppearanceFragment}
|
2020-05-02 13:40:37 -07:00
|
|
|
`,
|
|
|
|
{
|
2020-05-02 22:32:08 -07:00
|
|
|
variables: {
|
|
|
|
wornItemIds,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
emotion,
|
|
|
|
genderPresentation,
|
|
|
|
},
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-05-02 21:04:54 -07:00
|
|
|
const itemAppearances = (data?.items || []).map((i) => i.appearanceOn);
|
|
|
|
const visibleLayers = getVisibleLayers(data?.petAppearance, itemAppearances);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
|
|
|
return { loading, error, visibleLayers };
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:04:54 -07:00
|
|
|
export function getVisibleLayers(petAppearance, itemAppearances) {
|
|
|
|
if (!petAppearance) {
|
2020-05-02 13:40:37 -07:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:32:08 -07:00
|
|
|
const validItemAppearances = itemAppearances.filter((a) => a);
|
|
|
|
|
|
|
|
const allAppearances = [petAppearance, ...validItemAppearances];
|
2020-05-02 13:40:37 -07:00
|
|
|
let allLayers = allAppearances.map((a) => a.layers).flat();
|
|
|
|
|
|
|
|
// Clean up our data a bit, by ensuring only one layer per zone. This
|
|
|
|
// shouldn't happen in theory, but sometimes our database doesn't clean up
|
|
|
|
// after itself correctly :(
|
|
|
|
allLayers = allLayers.filter((l, i) => {
|
|
|
|
return allLayers.findIndex((l2) => l2.zone.id === l.zone.id) === i;
|
|
|
|
});
|
|
|
|
|
2020-05-02 22:32:08 -07:00
|
|
|
const allRestrictedZoneIds = validItemAppearances
|
2020-05-02 13:40:37 -07:00
|
|
|
.map((l) => l.restrictedZones)
|
|
|
|
.flat()
|
|
|
|
.map((z) => z.id);
|
|
|
|
|
|
|
|
const visibleLayers = allLayers.filter(
|
|
|
|
(l) => !allRestrictedZoneIds.includes(l.zone.id)
|
|
|
|
);
|
|
|
|
visibleLayers.sort((a, b) => a.zone.depth - b.zone.depth);
|
|
|
|
|
|
|
|
return visibleLayers;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const itemAppearanceFragment = gql`
|
2020-05-02 17:22:46 -07:00
|
|
|
fragment ItemAppearanceForOutfitPreview on ItemAppearance {
|
2020-05-02 13:40:37 -07:00
|
|
|
layers {
|
|
|
|
id
|
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
2020-05-02 17:22:46 -07:00
|
|
|
|
|
|
|
export const petAppearanceFragment = gql`
|
|
|
|
fragment PetAppearanceForOutfitPreview on PetAppearance {
|
2020-05-02 22:41:01 -07:00
|
|
|
id
|
2020-05-02 17:22:46 -07:00
|
|
|
layers {
|
|
|
|
id
|
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|