2020-06-05 23:56:42 -07:00
|
|
|
import React from "react";
|
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-23 12:47:06 -07:00
|
|
|
const { wornItemIds, speciesId, colorId, pose } = outfitState;
|
2020-05-02 13:40:37 -07:00
|
|
|
|
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
2020-05-19 14:48:54 -07:00
|
|
|
query OutfitAppearance(
|
2020-05-02 22:32:08 -07:00
|
|
|
$wornItemIds: [ID!]!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
2020-05-23 12:47:06 -07:00
|
|
|
$pose: Pose!
|
2020-05-02 22:32:08 -07:00
|
|
|
) {
|
2020-05-23 12:47:06 -07:00
|
|
|
petAppearance(speciesId: $speciesId, colorId: $colorId, pose: $pose) {
|
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,
|
2020-05-23 12:47:06 -07:00
|
|
|
pose,
|
2020-05-02 22:32:08 -07:00
|
|
|
},
|
2020-07-22 22:07:45 -07:00
|
|
|
skip: speciesId == null || colorId == null || pose == null,
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-05 23:56:42 -07:00
|
|
|
const itemAppearances = React.useMemo(
|
|
|
|
() => (data?.items || []).map((i) => i.appearanceOn),
|
|
|
|
[data]
|
|
|
|
);
|
|
|
|
const visibleLayers = React.useMemo(
|
|
|
|
() => getVisibleLayers(data?.petAppearance, itemAppearances),
|
|
|
|
[data, 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
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-05-02 13:40:37 -07:00
|
|
|
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
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-05-02 17:22:46 -07:00
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|