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";
|
2020-07-31 23:10:34 -07:00
|
|
|
import { useQuery } from "@apollo/client";
|
2020-05-02 13:40:37 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* useOutfitAppearance downloads the outfit's appearance data, and returns
|
|
|
|
* visibleLayers for rendering.
|
|
|
|
*/
|
|
|
|
export default function useOutfitAppearance(outfitState) {
|
2020-08-28 22:58:39 -07:00
|
|
|
const { wornItemIds, speciesId, colorId, pose, appearanceId } = outfitState;
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
// We split this query out from the other one, so that we can HTTP cache it.
|
|
|
|
//
|
|
|
|
// While Apollo gives us fine-grained caching during the page session, we can
|
|
|
|
// only HTTP a full query at a time.
|
|
|
|
//
|
|
|
|
// This is a minor optimization with respect to keeping the user's cache
|
|
|
|
// populated with their favorite species/color combinations. Once we start
|
|
|
|
// caching the items by body instead of species/color, this could make color
|
|
|
|
// changes really snappy!
|
|
|
|
//
|
|
|
|
// The larger optimization is that this enables the CDN to edge-cache the
|
|
|
|
// most popular species/color combinations, for very fast previews on the
|
|
|
|
// HomePage. At time of writing, Vercel isn't actually edge-caching these, I
|
|
|
|
// assume because our traffic isn't enough - so let's keep an eye on this!
|
|
|
|
const { loading: loading1, error: error1, data: data1 } = useQuery(
|
2020-08-28 22:58:39 -07:00
|
|
|
appearanceId == null
|
|
|
|
? gql`
|
|
|
|
query OutfitPetAppearance(
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
$pose: Pose!
|
|
|
|
) {
|
|
|
|
petAppearance(
|
|
|
|
speciesId: $speciesId
|
|
|
|
colorId: $colorId
|
|
|
|
pose: $pose
|
|
|
|
) {
|
|
|
|
...PetAppearanceForOutfitPreview
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${petAppearanceFragment}
|
|
|
|
`
|
|
|
|
: gql`
|
|
|
|
query OutfitPetAppearanceById($appearanceId: ID!) {
|
|
|
|
petAppearance: petAppearanceById(id: $appearanceId) {
|
|
|
|
...PetAppearanceForOutfitPreview
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${petAppearanceFragment}
|
|
|
|
`,
|
2020-07-22 23:08:28 -07:00
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
pose,
|
2020-08-28 22:58:39 -07:00
|
|
|
appearanceId,
|
2020-07-22 23:08:28 -07:00
|
|
|
},
|
2021-03-14 07:16:01 -07:00
|
|
|
skip:
|
|
|
|
speciesId == null ||
|
|
|
|
colorId == null ||
|
|
|
|
(pose == null && appearanceId == null),
|
2020-07-22 23:08:28 -07:00
|
|
|
}
|
|
|
|
);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
const { loading: loading2, error: error2, data: data2 } = useQuery(
|
|
|
|
gql`
|
|
|
|
query OutfitItemsAppearance(
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
$wornItemIds: [ID!]!
|
|
|
|
) {
|
2020-05-02 13:40:37 -07:00
|
|
|
items(ids: $wornItemIds) {
|
|
|
|
id
|
2021-02-10 13:47:02 -08:00
|
|
|
name # HACK: This is for HTML5 detection UI in OutfitControls!
|
2021-02-09 20:28:03 -08:00
|
|
|
appearance: 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 22:32:08 -07:00
|
|
|
variables: {
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
2020-07-22 23:08:28 -07:00
|
|
|
wornItemIds,
|
2020-05-02 22:32:08 -07:00
|
|
|
},
|
2020-07-22 23:08:28 -07:00
|
|
|
skip: speciesId == null || colorId == null || wornItemIds.length === 0,
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-02-09 20:28:03 -08:00
|
|
|
const petAppearance = data1?.petAppearance;
|
2021-02-10 13:51:59 -08:00
|
|
|
const items = data2?.items;
|
2020-06-05 23:56:42 -07:00
|
|
|
const itemAppearances = React.useMemo(
|
2021-02-09 20:28:03 -08:00
|
|
|
() => (items || []).map((i) => i.appearance),
|
|
|
|
[items]
|
2020-06-05 23:56:42 -07:00
|
|
|
);
|
|
|
|
const visibleLayers = React.useMemo(
|
2021-02-09 20:28:03 -08:00
|
|
|
() => getVisibleLayers(petAppearance, itemAppearances),
|
|
|
|
[petAppearance, itemAppearances]
|
2020-06-05 23:56:42 -07:00
|
|
|
);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2021-02-09 20:28:03 -08:00
|
|
|
const bodyId = petAppearance?.bodyId;
|
2020-08-01 14:30:10 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
return {
|
|
|
|
loading: loading1 || loading2,
|
|
|
|
error: error1 || error2,
|
2021-02-09 20:28:03 -08:00
|
|
|
petAppearance,
|
2021-02-10 13:51:59 -08:00
|
|
|
items: items || [],
|
2021-02-09 20:28:03 -08:00
|
|
|
itemAppearances,
|
2020-07-22 23:08:28 -07:00
|
|
|
visibleLayers,
|
2020-08-01 14:30:10 -07:00
|
|
|
bodyId,
|
2020-07-22 23:08:28 -07:00
|
|
|
};
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2021-03-13 07:21:30 -08:00
|
|
|
const petLayers = petAppearance.layers.map((l) => ({ ...l, source: "pet" }));
|
|
|
|
|
2020-08-01 01:35:27 -07:00
|
|
|
const itemLayers = validItemAppearances
|
|
|
|
.map((a) => a.layers)
|
|
|
|
.flat()
|
2021-03-13 01:08:48 -08:00
|
|
|
.map((l) => ({ ...l, source: "item" }));
|
2021-03-13 00:51:13 -08:00
|
|
|
|
2020-08-01 01:35:27 -07:00
|
|
|
let allLayers = [...petLayers, ...itemLayers];
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2021-03-13 07:21:30 -08:00
|
|
|
const itemRestrictedZoneIds = new Set(
|
|
|
|
validItemAppearances
|
|
|
|
.map((a) => a.restrictedZones)
|
|
|
|
.flat()
|
|
|
|
.map((z) => z.id)
|
|
|
|
);
|
|
|
|
const petOccupiedZoneIds = new Set(petLayers.map((l) => l.zone.id));
|
|
|
|
const petRestrictedZoneIds = new Set(
|
|
|
|
petAppearance.restrictedZones.map((z) => z.id)
|
|
|
|
);
|
|
|
|
const petOccupiedOrRestrictedZoneIds = new Set([
|
|
|
|
...petOccupiedZoneIds,
|
2020-08-31 19:23:56 -07:00
|
|
|
...petRestrictedZoneIds,
|
|
|
|
]);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2021-03-13 07:21:30 -08:00
|
|
|
const visibleLayers = allLayers.filter((layer) => {
|
|
|
|
// When an item restricts a zone, it hides pet layers of the same zone.
|
|
|
|
// We use this to e.g. make a hat hide a hair ruff.
|
|
|
|
//
|
|
|
|
// NOTE: Items' restricted layers also affect what items you can wear at
|
|
|
|
// the same time. We don't enforce anything about that here, and
|
|
|
|
// instead assume that the input by this point is valid!
|
|
|
|
if (layer.source === "pet" && itemRestrictedZoneIds.has(layer.zone.id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a pet appearance restricts or occupies a zone, it makes items
|
|
|
|
// that occupy the zone incompatible, but *only* if the item is
|
|
|
|
// body-specific. We use this to disallow UCs from wearing certain
|
|
|
|
// body-specific Biology Effects, Statics, etc, while still allowing
|
|
|
|
// non-body-specific items in those zones! (I think this happens for some
|
|
|
|
// Invisible pet stuff, too?)
|
|
|
|
//
|
|
|
|
// NOTE: This can result in both pet layers and items occupying the same
|
|
|
|
// zone, like Static! That's correct, and the item layer should be
|
|
|
|
// on top! (Here, we implement it by placing item layers second in
|
|
|
|
// the list, and depending on JS sort stability, and *then* depending
|
|
|
|
// on the UI to respect that ordering when rendering them by depth.
|
|
|
|
// Not great! 😅)
|
|
|
|
//
|
|
|
|
// TODO: Hiding the layer is the *old* behavior. Move this way deeper in
|
|
|
|
// the code to prevent these items from showing up in the first
|
|
|
|
// place!
|
|
|
|
if (
|
|
|
|
layer.source === "item" &&
|
|
|
|
layer.bodyId !== "0" &&
|
|
|
|
petOccupiedOrRestrictedZoneIds.has(layer.zone.id)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2020-05-02 13:40:37 -07:00
|
|
|
visibleLayers.sort((a, b) => a.zone.depth - b.zone.depth);
|
|
|
|
|
|
|
|
return visibleLayers;
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:26:05 -08:00
|
|
|
export const itemAppearanceFragmentForGetVisibleLayers = gql`
|
|
|
|
fragment ItemAppearanceForGetVisibleLayers on ItemAppearance {
|
|
|
|
id
|
|
|
|
layers {
|
|
|
|
id
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth @client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-05-02 13:40:37 -07:00
|
|
|
export const itemAppearanceFragment = gql`
|
2020-05-02 17:22:46 -07:00
|
|
|
fragment ItemAppearanceForOutfitPreview on ItemAppearance {
|
2020-08-28 00:10:00 -07:00
|
|
|
id
|
2020-05-02 13:40:37 -07:00
|
|
|
layers {
|
|
|
|
id
|
2020-08-14 22:09:52 -07:00
|
|
|
remoteId # HACK: This is for Support tools, but other views don't need it
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-09-22 03:53:48 -07:00
|
|
|
canvasMovieLibraryUrl
|
2020-05-02 13:40:37 -07:00
|
|
|
imageUrl(size: SIZE_600)
|
2020-08-01 22:54:30 -07:00
|
|
|
swfUrl # HACK: This is for Support tools, but other views don't need it
|
2021-03-12 04:01:35 -08:00
|
|
|
knownGlitches # HACK: This is for Support tools, but other views don't need it
|
2020-08-01 14:12:57 -07:00
|
|
|
bodyId
|
2020-05-02 13:40:37 -07:00
|
|
|
zone {
|
2020-08-19 17:50:05 -07:00
|
|
|
label @client # HACK: This is for Support tools, but other views don't need it
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 00:26:05 -08:00
|
|
|
...ItemAppearanceForGetVisibleLayers
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
2021-01-04 00:26:05 -08:00
|
|
|
|
|
|
|
${itemAppearanceFragmentForGetVisibleLayers}
|
2020-05-02 13:40:37 -07:00
|
|
|
`;
|
2020-05-02 17:22:46 -07:00
|
|
|
|
2021-01-04 00:10:35 -08:00
|
|
|
export const petAppearanceFragmentForGetVisibleLayers = gql`
|
|
|
|
fragment PetAppearanceForGetVisibleLayers on PetAppearance {
|
2020-05-02 22:41:01 -07:00
|
|
|
id
|
2020-05-02 17:22:46 -07:00
|
|
|
layers {
|
|
|
|
id
|
|
|
|
zone {
|
|
|
|
id
|
2020-08-19 17:50:05 -07:00
|
|
|
depth @client
|
2020-05-02 17:22:46 -07:00
|
|
|
}
|
2020-08-31 23:18:30 -07:00
|
|
|
}
|
|
|
|
restrictedZones {
|
|
|
|
id
|
2020-05-02 17:22:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
2021-01-04 00:10:35 -08:00
|
|
|
|
|
|
|
export const petAppearanceFragment = gql`
|
|
|
|
fragment PetAppearanceForOutfitPreview on PetAppearance {
|
|
|
|
id
|
|
|
|
bodyId
|
|
|
|
layers {
|
|
|
|
id
|
|
|
|
svgUrl
|
|
|
|
canvasMovieLibraryUrl
|
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
}
|
|
|
|
...PetAppearanceForGetVisibleLayers
|
|
|
|
}
|
|
|
|
|
|
|
|
${petAppearanceFragmentForGetVisibleLayers}
|
|
|
|
`;
|