2020-04-21 20:46:53 -07:00
|
|
|
import React from "react";
|
2020-04-24 18:39:38 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import produce, { enableMapSet } from "immer";
|
2020-07-31 23:10:34 -07:00
|
|
|
import { useQuery, useApolloClient } from "@apollo/client";
|
2021-01-04 22:29:39 -08:00
|
|
|
import { useParams } from "react-router-dom";
|
2020-04-21 20:46:53 -07:00
|
|
|
|
2020-07-22 21:29:57 -07:00
|
|
|
import { itemAppearanceFragment } from "../components/useOutfitAppearance";
|
2020-04-21 20:46:53 -07:00
|
|
|
|
2020-04-24 18:39:38 -07:00
|
|
|
enableMapSet();
|
2020-04-21 20:46:53 -07:00
|
|
|
|
2020-08-05 00:25:25 -07:00
|
|
|
export const OutfitStateContext = React.createContext(null);
|
|
|
|
|
2020-04-24 18:39:38 -07:00
|
|
|
function useOutfitState() {
|
|
|
|
const apolloClient = useApolloClient();
|
2021-04-15 17:17:53 -07:00
|
|
|
const urlOutfitState = useParseOutfitUrl();
|
|
|
|
const [localOutfitState, dispatchToOutfit] = React.useReducer(
|
2020-04-24 19:16:24 -07:00
|
|
|
outfitStateReducer(apolloClient),
|
2021-04-15 17:17:53 -07:00
|
|
|
urlOutfitState
|
2020-04-24 19:16:24 -07:00
|
|
|
);
|
2020-04-21 20:46:53 -07:00
|
|
|
|
2021-01-04 22:29:39 -08:00
|
|
|
// If there's an outfit ID (i.e. we're on /outfits/:id), load basic data
|
|
|
|
// about the outfit. We'll use it to initialize the local state.
|
2021-01-04 22:39:12 -08:00
|
|
|
const {
|
|
|
|
loading: outfitLoading,
|
|
|
|
error: outfitError,
|
|
|
|
data: outfitData,
|
|
|
|
} = useQuery(
|
2021-01-04 22:29:39 -08:00
|
|
|
gql`
|
|
|
|
query OutfitStateSavedOutfit($id: ID!) {
|
|
|
|
outfit(id: $id) {
|
|
|
|
id
|
|
|
|
name
|
2021-01-17 08:04:21 -08:00
|
|
|
creator {
|
|
|
|
id
|
|
|
|
}
|
2021-01-04 22:29:39 -08:00
|
|
|
petAppearance {
|
2021-01-04 22:43:33 -08:00
|
|
|
id
|
2021-01-04 22:29:39 -08:00
|
|
|
species {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
color {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
pose
|
|
|
|
}
|
|
|
|
wornItems {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
closetedItems {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: Consider pre-loading some fields, instead of doing them in
|
|
|
|
# follow-up queries?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{
|
2021-04-15 17:17:53 -07:00
|
|
|
variables: { id: urlOutfitState.id },
|
|
|
|
skip: urlOutfitState.id == null,
|
2021-01-04 22:39:12 -08:00
|
|
|
returnPartialData: true,
|
2021-01-04 22:29:39 -08:00
|
|
|
onCompleted: (outfitData) => {
|
|
|
|
dispatchToOutfit({
|
|
|
|
type: "reset",
|
2021-04-15 17:17:53 -07:00
|
|
|
newState: getOutfitStateFromOutfitData(outfitData.outfit),
|
2021-01-04 22:29:39 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
const creator = outfitData?.outfit?.creator;
|
|
|
|
|
|
|
|
const savedOutfitState = getOutfitStateFromOutfitData(outfitData?.outfit);
|
|
|
|
|
|
|
|
// Choose which customization state to use. We want it to match the outfit in
|
|
|
|
// the URL immediately, without having to wait for any effects, to avoid race
|
|
|
|
// conditions!
|
2021-01-08 03:19:57 -08:00
|
|
|
//
|
2021-04-15 17:17:53 -07:00
|
|
|
// The reducer is generally the main source of truth for live changes!
|
|
|
|
//
|
|
|
|
// But if:
|
|
|
|
// - it's not initialized yet (e.g. the first frame of navigating to an
|
|
|
|
// outfit from Your Outfits), or
|
|
|
|
// - it's for a different outfit than the URL says (e.g. clicking Back
|
|
|
|
// or Forward to switch between saved outfits),
|
|
|
|
//
|
|
|
|
// Then use saved outfit data or the URL query string instead, because that's
|
|
|
|
// a better representation of the outfit in the URL. (If the saved outfit
|
|
|
|
// data isn't loaded yet, then this will be a customization state with
|
|
|
|
// partial data, and that's okay.)
|
|
|
|
let outfitState;
|
|
|
|
if (urlOutfitState.id === localOutfitState.id) {
|
|
|
|
// Use the reducer state: they're both for the same saved outfit, or both
|
|
|
|
// for an unsaved outfit (null === null).
|
|
|
|
outfitState = localOutfitState;
|
|
|
|
} else if (urlOutfitState.id && urlOutfitState.id === savedOutfitState.id) {
|
|
|
|
// Use the saved outfit state: it's for the saved outfit the URL points to.
|
|
|
|
outfitState = savedOutfitState;
|
|
|
|
} else {
|
|
|
|
// Use the URL state: it's more up-to-date than any of the others. (Worst
|
|
|
|
// case, it's empty except for ID, which is fine while the saved outfit
|
|
|
|
// data loads!)
|
|
|
|
outfitState = urlOutfitState;
|
|
|
|
}
|
2021-01-08 03:19:57 -08:00
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
// When unpacking the customization state, we call `Array.from` on our item
|
|
|
|
// IDs. It's more convenient to manage them as a Set in state, but most
|
|
|
|
// callers will find it more convenient to access them as arrays! e.g. for
|
|
|
|
// `.map()`.
|
2021-04-20 01:50:42 -07:00
|
|
|
const { id, name, speciesId, colorId, pose, appearanceId } = outfitState;
|
2021-04-15 17:17:53 -07:00
|
|
|
const wornItemIds = Array.from(outfitState.wornItemIds);
|
|
|
|
const closetedItemIds = Array.from(outfitState.closetedItemIds);
|
|
|
|
const allItemIds = [...wornItemIds, ...closetedItemIds];
|
2021-01-04 22:39:12 -08:00
|
|
|
|
2021-01-04 22:29:39 -08:00
|
|
|
const {
|
|
|
|
loading: itemsLoading,
|
|
|
|
error: itemsError,
|
|
|
|
data: itemsData,
|
|
|
|
} = useQuery(
|
2020-04-24 21:17:03 -07:00
|
|
|
gql`
|
2020-05-19 14:57:25 -07:00
|
|
|
query OutfitStateItems(
|
|
|
|
$allItemIds: [ID!]!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
) {
|
2020-04-24 21:17:03 -07:00
|
|
|
items(ids: $allItemIds) {
|
|
|
|
# TODO: De-dupe this from SearchPanel?
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
2020-08-31 23:27:21 -07:00
|
|
|
isNc
|
2020-11-08 15:13:30 -08:00
|
|
|
isPb
|
2020-09-12 20:02:56 -07:00
|
|
|
currentUserOwnsThis
|
|
|
|
currentUserWantsThis
|
2020-04-24 21:17:03 -07:00
|
|
|
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
|
|
|
# This enables us to quickly show the item when the user clicks it!
|
2020-05-02 17:22:46 -07:00
|
|
|
...ItemAppearanceForOutfitPreview
|
2020-04-24 21:17:03 -07:00
|
|
|
|
|
|
|
# This is used to group items by zone, and to detect conflicts when
|
|
|
|
# wearing a new item.
|
|
|
|
layers {
|
|
|
|
zone {
|
|
|
|
id
|
2020-08-19 17:50:05 -07:00
|
|
|
label @client
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-01 01:18:24 -07:00
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
label @client
|
|
|
|
isCommonlyUsedByItems @client
|
|
|
|
}
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 15:18:59 -07:00
|
|
|
|
|
|
|
# NOTE: We skip this query if items is empty for perf reasons. If
|
|
|
|
# you're adding more fields, consider changing that condition!
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
|
|
|
${itemAppearanceFragment}
|
|
|
|
`,
|
2020-05-19 14:57:25 -07:00
|
|
|
{
|
|
|
|
variables: { allItemIds, speciesId, colorId },
|
2021-01-21 14:57:21 -08:00
|
|
|
context: { sendAuth: true },
|
2021-01-04 22:29:39 -08:00
|
|
|
// Skip if this outfit has no items, as an optimization; or if we don't
|
|
|
|
// have the species/color ID loaded yet because we're waiting on the
|
|
|
|
// saved outfit to load.
|
|
|
|
skip: allItemIds.length === 0 || speciesId == null || colorId == null,
|
2020-05-19 14:57:25 -07:00
|
|
|
}
|
2020-04-21 20:46:53 -07:00
|
|
|
);
|
|
|
|
|
2021-01-04 22:29:39 -08:00
|
|
|
const resultItems = itemsData?.items || [];
|
2020-09-24 07:07:05 -07:00
|
|
|
|
|
|
|
// Okay, time for some big perf hacks! Lower down in the app, we use
|
|
|
|
// React.memo to avoid re-rendering Item components if the items haven't
|
|
|
|
// updated. In simpler cases, we just make the component take the individual
|
|
|
|
// item fields as props... but items are complex and that makes it annoying
|
|
|
|
// :p Instead, we do these tricks to reuse physical item objects if they're
|
|
|
|
// still deep-equal to the previous version. This is because React.memo uses
|
|
|
|
// object identity to compare its props, so now when it checks whether
|
|
|
|
// `oldItem === newItem`, the answer will be `true`, unless the item really
|
|
|
|
// _did_ change!
|
|
|
|
const [cachedItemObjects, setCachedItemObjects] = React.useState([]);
|
|
|
|
let items = resultItems.map((item) => {
|
|
|
|
const cachedItemObject = cachedItemObjects.find((i) => i.id === item.id);
|
|
|
|
if (
|
|
|
|
cachedItemObject &&
|
|
|
|
JSON.stringify(cachedItemObject) === JSON.stringify(item)
|
|
|
|
) {
|
|
|
|
return cachedItemObject;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
if (
|
|
|
|
items.length === cachedItemObjects.length &&
|
|
|
|
items.every((_, index) => items[index] === cachedItemObjects[index])
|
|
|
|
) {
|
|
|
|
// Even reuse the entire array if none of the items changed!
|
|
|
|
items = cachedItemObjects;
|
|
|
|
}
|
|
|
|
React.useEffect(() => {
|
|
|
|
setCachedItemObjects(items);
|
|
|
|
}, [items, setCachedItemObjects]);
|
|
|
|
|
2020-04-24 21:17:03 -07:00
|
|
|
const itemsById = {};
|
|
|
|
for (const item of items) {
|
|
|
|
itemsById[item.id] = item;
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:55:12 -07:00
|
|
|
const zonesAndItems = getZonesAndItems(
|
|
|
|
itemsById,
|
|
|
|
wornItemIds,
|
|
|
|
closetedItemIds
|
2020-04-21 20:46:53 -07:00
|
|
|
);
|
2020-09-01 17:17:45 -07:00
|
|
|
const incompatibleItems = items
|
|
|
|
.filter((i) => i.appearanceOn.layers.length === 0)
|
|
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
2020-04-21 20:46:53 -07:00
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
const url = buildOutfitUrl(outfitState);
|
2020-04-30 00:45:01 -07:00
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
const outfitStateWithExtras = {
|
2021-04-20 01:50:42 -07:00
|
|
|
id,
|
2021-01-17 08:04:21 -08:00
|
|
|
creator,
|
2020-04-24 19:16:24 -07:00
|
|
|
zonesAndItems,
|
2020-09-01 17:17:45 -07:00
|
|
|
incompatibleItems,
|
2020-04-24 23:29:26 -07:00
|
|
|
name,
|
2020-04-24 19:16:24 -07:00
|
|
|
wornItemIds,
|
2020-04-25 07:22:03 -07:00
|
|
|
closetedItemIds,
|
2020-04-24 19:16:24 -07:00
|
|
|
allItemIds,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
2020-05-23 12:47:06 -07:00
|
|
|
pose,
|
2020-08-28 22:58:39 -07:00
|
|
|
appearanceId,
|
2020-04-30 00:45:01 -07:00
|
|
|
url,
|
2021-04-22 02:35:59 -07:00
|
|
|
savedOutfitState,
|
2020-04-24 19:16:24 -07:00
|
|
|
};
|
2020-04-24 18:39:38 -07:00
|
|
|
|
2020-05-10 01:28:39 -07:00
|
|
|
// Keep the URL up-to-date. (We don't listen to it, though 😅)
|
2020-04-30 00:45:01 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
window.history.replaceState(null, "", url);
|
|
|
|
}, [url]);
|
|
|
|
|
2021-01-04 22:29:39 -08:00
|
|
|
return {
|
|
|
|
loading: outfitLoading || itemsLoading,
|
|
|
|
error: outfitError || itemsError,
|
2021-04-15 17:17:53 -07:00
|
|
|
outfitState: outfitStateWithExtras,
|
2021-01-04 22:29:39 -08:00
|
|
|
dispatchToOutfit,
|
|
|
|
};
|
2020-04-24 18:39:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const outfitStateReducer = (apolloClient) => (baseState, action) => {
|
|
|
|
switch (action.type) {
|
2020-04-24 23:29:26 -07:00
|
|
|
case "rename":
|
2021-04-15 17:17:53 -07:00
|
|
|
return produce(baseState, (state) => {
|
|
|
|
state.name = action.outfitName;
|
|
|
|
});
|
2020-05-10 00:21:04 -07:00
|
|
|
case "setSpeciesAndColor":
|
2021-04-15 17:17:53 -07:00
|
|
|
return produce(baseState, (state) => {
|
|
|
|
state.speciesId = action.speciesId;
|
|
|
|
state.colorId = action.colorId;
|
|
|
|
state.pose = action.pose;
|
|
|
|
state.appearanceId = null;
|
|
|
|
});
|
2020-04-24 18:39:38 -07:00
|
|
|
case "wearItem":
|
|
|
|
return produce(baseState, (state) => {
|
2020-04-24 19:16:24 -07:00
|
|
|
const { wornItemIds, closetedItemIds } = state;
|
2020-09-02 00:53:35 -07:00
|
|
|
const { itemId, itemIdsToReconsider = [] } = action;
|
2020-04-24 18:39:38 -07:00
|
|
|
|
|
|
|
// Move conflicting items to the closet.
|
|
|
|
//
|
|
|
|
// We do this by looking them up in the Apollo Cache, which is going to
|
|
|
|
// include the relevant item data because the `useOutfitState` hook
|
|
|
|
// queries for it!
|
|
|
|
//
|
|
|
|
// (It could be possible to mess up the timing by taking an action
|
|
|
|
// while worn items are still partially loading, but I think it would
|
|
|
|
// require a pretty weird action sequence to make that happen... like,
|
|
|
|
// doing a search and it loads before the worn item data does? Anyway,
|
|
|
|
// Apollo will throw in that case, which should just essentially reject
|
|
|
|
// the action.)
|
2020-05-19 15:14:12 -07:00
|
|
|
let conflictingIds;
|
|
|
|
try {
|
|
|
|
conflictingIds = findItemConflicts(itemId, state, apolloClient);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 18:39:38 -07:00
|
|
|
for (const conflictingId of conflictingIds) {
|
|
|
|
wornItemIds.delete(conflictingId);
|
|
|
|
closetedItemIds.add(conflictingId);
|
|
|
|
}
|
|
|
|
|
2020-04-24 20:19:26 -07:00
|
|
|
// Move this item from the closet to the worn set.
|
|
|
|
closetedItemIds.delete(itemId);
|
2020-04-24 18:39:38 -07:00
|
|
|
wornItemIds.add(itemId);
|
2020-09-02 00:53:35 -07:00
|
|
|
|
|
|
|
reconsiderItems(itemIdsToReconsider, state, apolloClient);
|
2020-04-24 18:39:38 -07:00
|
|
|
});
|
2020-04-24 20:19:26 -07:00
|
|
|
case "unwearItem":
|
|
|
|
return produce(baseState, (state) => {
|
|
|
|
const { wornItemIds, closetedItemIds } = state;
|
2020-09-02 00:53:35 -07:00
|
|
|
const { itemId, itemIdsToReconsider = [] } = action;
|
2020-04-24 20:19:26 -07:00
|
|
|
|
|
|
|
// Move this item from the worn set to the closet.
|
|
|
|
wornItemIds.delete(itemId);
|
|
|
|
closetedItemIds.add(itemId);
|
2020-09-02 00:53:35 -07:00
|
|
|
|
|
|
|
reconsiderItems(itemIdsToReconsider, state, apolloClient);
|
2020-04-24 20:19:26 -07:00
|
|
|
});
|
2020-04-25 00:22:49 -07:00
|
|
|
case "removeItem":
|
|
|
|
return produce(baseState, (state) => {
|
|
|
|
const { wornItemIds, closetedItemIds } = state;
|
2020-09-02 00:53:35 -07:00
|
|
|
const { itemId, itemIdsToReconsider = [] } = action;
|
2020-04-25 00:22:49 -07:00
|
|
|
|
|
|
|
// Remove this item from both the worn set and the closet.
|
|
|
|
wornItemIds.delete(itemId);
|
|
|
|
closetedItemIds.delete(itemId);
|
2020-09-02 00:53:35 -07:00
|
|
|
|
|
|
|
reconsiderItems(itemIdsToReconsider, state, apolloClient);
|
2020-04-25 00:22:49 -07:00
|
|
|
});
|
2020-05-02 22:20:17 -07:00
|
|
|
case "setPose":
|
2021-04-15 17:17:53 -07:00
|
|
|
return produce(baseState, (state) => {
|
|
|
|
state.pose = action.pose;
|
2020-08-28 22:58:39 -07:00
|
|
|
// Usually only the `pose` is specified, but `PosePickerSupport` can
|
|
|
|
// also specify a corresponding `appearanceId`, to get even more
|
|
|
|
// particular about which version of the pose to show if more than one.
|
2021-04-15 17:17:53 -07:00
|
|
|
state.appearanceId = action.appearanceId || null;
|
2020-05-02 22:32:08 -07:00
|
|
|
});
|
2021-04-15 17:17:53 -07:00
|
|
|
case "reset":
|
|
|
|
return action.newState;
|
2020-04-24 18:39:38 -07:00
|
|
|
default:
|
2020-04-25 04:33:05 -07:00
|
|
|
throw new Error(`unexpected action ${JSON.stringify(action)}`);
|
2020-04-24 18:39:38 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
const EMPTY_CUSTOMIZATION_STATE = {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
speciesId: null,
|
|
|
|
colorId: null,
|
|
|
|
pose: null,
|
|
|
|
appearanceId: null,
|
|
|
|
wornItemIds: [],
|
|
|
|
closetedItemIds: [],
|
|
|
|
};
|
|
|
|
|
2021-01-04 22:29:39 -08:00
|
|
|
function useParseOutfitUrl() {
|
|
|
|
const { id } = useParams();
|
|
|
|
|
2021-01-17 07:43:08 -08:00
|
|
|
// For the /outfits/:id page, ignore the query string, and just wait for the
|
|
|
|
// outfit data to load in!
|
|
|
|
if (id != null) {
|
|
|
|
return {
|
2021-04-15 17:17:53 -07:00
|
|
|
...EMPTY_CUSTOMIZATION_STATE,
|
2021-01-17 07:43:08 -08:00
|
|
|
id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, parse the query string, and fill in default values for anything
|
|
|
|
// not specified.
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
2020-05-10 01:28:39 -07:00
|
|
|
return {
|
2021-04-15 17:17:53 -07:00
|
|
|
id: null,
|
2020-05-10 01:28:39 -07:00
|
|
|
name: urlParams.get("name"),
|
2021-01-17 07:43:08 -08:00
|
|
|
speciesId: urlParams.get("species") || "1",
|
|
|
|
colorId: urlParams.get("color") || "8",
|
2020-05-23 12:47:06 -07:00
|
|
|
pose: urlParams.get("pose") || "HAPPY_FEM",
|
2020-08-28 22:58:39 -07:00
|
|
|
appearanceId: urlParams.get("state") || null,
|
2020-05-11 21:19:34 -07:00
|
|
|
wornItemIds: new Set(urlParams.getAll("objects[]")),
|
|
|
|
closetedItemIds: new Set(urlParams.getAll("closet[]")),
|
2020-05-10 01:28:39 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
function getOutfitStateFromOutfitData(outfit) {
|
|
|
|
if (!outfit) {
|
|
|
|
return EMPTY_CUSTOMIZATION_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: outfit.id,
|
|
|
|
name: outfit.name,
|
|
|
|
// Note that these fields are intentionally null if loading, rather than
|
|
|
|
// falling back to a default appearance like Blue Acara.
|
|
|
|
speciesId: outfit.petAppearance?.species?.id,
|
|
|
|
colorId: outfit.petAppearance?.color?.id,
|
|
|
|
pose: outfit.petAppearance?.pose,
|
|
|
|
// Whereas the items are more convenient to just leave as empty lists!
|
2021-04-16 23:49:10 -07:00
|
|
|
wornItemIds: new Set((outfit.wornItems || []).map((item) => item.id)),
|
|
|
|
closetedItemIds: new Set(
|
|
|
|
(outfit.closetedItems || []).map((item) => item.id)
|
|
|
|
),
|
2021-04-15 17:17:53 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:39:38 -07:00
|
|
|
function findItemConflicts(itemIdToAdd, state, apolloClient) {
|
|
|
|
const { wornItemIds, speciesId, colorId } = state;
|
|
|
|
|
|
|
|
const { items } = apolloClient.readQuery({
|
|
|
|
query: gql`
|
2020-05-19 14:48:54 -07:00
|
|
|
query OutfitStateItemConflicts(
|
|
|
|
$itemIds: [ID!]!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
) {
|
2020-04-24 18:39:38 -07:00
|
|
|
items(ids: $itemIds) {
|
|
|
|
id
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
|
|
|
layers {
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 22:12:05 -07:00
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
2020-04-24 18:39:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
itemIds: [itemIdToAdd, ...wornItemIds],
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const itemToAdd = items.find((i) => i.id === itemIdToAdd);
|
2020-04-25 04:46:40 -07:00
|
|
|
if (!itemToAdd.appearanceOn) {
|
|
|
|
return [];
|
|
|
|
}
|
2020-04-24 18:39:38 -07:00
|
|
|
const wornItems = Array.from(wornItemIds).map((id) =>
|
|
|
|
items.find((i) => i.id === id)
|
|
|
|
);
|
|
|
|
|
2020-04-26 23:26:17 -07:00
|
|
|
const itemToAddZoneSets = getItemZones(itemToAdd);
|
|
|
|
|
2020-04-24 18:39:38 -07:00
|
|
|
const conflictingIds = [];
|
|
|
|
for (const wornItem of wornItems) {
|
2020-04-25 04:46:40 -07:00
|
|
|
if (!wornItem.appearanceOn) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-26 23:26:17 -07:00
|
|
|
|
|
|
|
const wornItemZoneSets = getItemZones(wornItem);
|
|
|
|
|
|
|
|
const itemsConflict =
|
|
|
|
setsIntersect(
|
|
|
|
itemToAddZoneSets.occupies,
|
|
|
|
wornItemZoneSets.occupiesOrRestricts
|
|
|
|
) ||
|
|
|
|
setsIntersect(
|
|
|
|
wornItemZoneSets.occupies,
|
|
|
|
itemToAddZoneSets.occupiesOrRestricts
|
|
|
|
);
|
|
|
|
|
|
|
|
if (itemsConflict) {
|
2020-04-24 18:39:38 -07:00
|
|
|
conflictingIds.push(wornItem.id);
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 14:55:12 -07:00
|
|
|
|
2020-04-24 18:39:38 -07:00
|
|
|
return conflictingIds;
|
2020-04-22 14:55:12 -07:00
|
|
|
}
|
|
|
|
|
2020-04-26 23:26:17 -07:00
|
|
|
function getItemZones(item) {
|
|
|
|
const occupies = new Set(item.appearanceOn.layers.map((l) => l.zone.id));
|
|
|
|
const restricts = new Set(item.appearanceOn.restrictedZones.map((z) => z.id));
|
|
|
|
const occupiesOrRestricts = new Set([...occupies, ...restricts]);
|
|
|
|
return { occupies, occupiesOrRestricts };
|
|
|
|
}
|
|
|
|
|
|
|
|
function setsIntersect(a, b) {
|
|
|
|
for (const el of a) {
|
|
|
|
if (b.has(el)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-02 00:53:35 -07:00
|
|
|
/**
|
|
|
|
* Try to add these items back to the outfit, if there would be no conflicts.
|
|
|
|
* We use this in Search to try to restore these items after the user makes
|
|
|
|
* changes, e.g., after they try on another Background we want to restore the
|
|
|
|
* previous one!
|
|
|
|
*
|
|
|
|
* This mutates state.wornItemIds directly, on the assumption that we're in an
|
|
|
|
* immer block, in which case mutation is the simplest API!
|
|
|
|
*/
|
|
|
|
function reconsiderItems(itemIdsToReconsider, state, apolloClient) {
|
|
|
|
for (const itemIdToReconsider of itemIdsToReconsider) {
|
|
|
|
const conflictingIds = findItemConflicts(
|
|
|
|
itemIdToReconsider,
|
|
|
|
state,
|
|
|
|
apolloClient
|
|
|
|
);
|
|
|
|
if (conflictingIds.length === 0) {
|
|
|
|
state.wornItemIds.add(itemIdToReconsider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:16:24 -07:00
|
|
|
// TODO: Get this out of here, tbh...
|
2020-04-22 14:55:12 -07:00
|
|
|
function getZonesAndItems(itemsById, wornItemIds, closetedItemIds) {
|
|
|
|
const wornItems = wornItemIds.map((id) => itemsById[id]).filter((i) => i);
|
|
|
|
const closetedItems = closetedItemIds
|
|
|
|
.map((id) => itemsById[id])
|
|
|
|
.filter((i) => i);
|
|
|
|
|
2020-04-25 00:46:25 -07:00
|
|
|
// We use zone label here, rather than ID, because some zones have the same
|
|
|
|
// label and we *want* to over-simplify that in this UI. (e.g. there are
|
|
|
|
// multiple Hat zones, and some items occupy different ones, but mostly let's
|
|
|
|
// just group them and if they don't conflict then all the better!)
|
2020-04-21 20:46:53 -07:00
|
|
|
const allItems = [...wornItems, ...closetedItems];
|
2020-04-25 00:46:25 -07:00
|
|
|
const itemsByZoneLabel = new Map();
|
2020-04-24 19:16:24 -07:00
|
|
|
for (const item of allItems) {
|
2020-04-25 04:38:55 -07:00
|
|
|
if (!item.appearanceOn) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:16:24 -07:00
|
|
|
for (const layer of item.appearanceOn.layers) {
|
2020-04-25 00:46:25 -07:00
|
|
|
const zoneLabel = layer.zone.label;
|
2020-04-24 19:16:24 -07:00
|
|
|
|
2020-04-25 00:46:25 -07:00
|
|
|
if (!itemsByZoneLabel.has(zoneLabel)) {
|
|
|
|
itemsByZoneLabel.set(zoneLabel, []);
|
2020-04-24 19:16:24 -07:00
|
|
|
}
|
2020-04-25 00:46:25 -07:00
|
|
|
itemsByZoneLabel.get(zoneLabel).push(item);
|
2020-04-24 19:16:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 03:53:24 -07:00
|
|
|
let zonesAndItems = Array.from(itemsByZoneLabel.entries()).map(
|
2020-04-25 00:46:25 -07:00
|
|
|
([zoneLabel, items]) => ({
|
|
|
|
zoneLabel: zoneLabel,
|
2020-04-24 19:16:24 -07:00
|
|
|
items: [...items].sort((a, b) => a.name.localeCompare(b.name)),
|
|
|
|
})
|
|
|
|
);
|
2020-04-25 00:46:25 -07:00
|
|
|
zonesAndItems.sort((a, b) => a.zoneLabel.localeCompare(b.zoneLabel));
|
2020-04-21 20:46:53 -07:00
|
|
|
|
2020-09-01 03:53:24 -07:00
|
|
|
// As one last step, try to remove zone groups that aren't helpful.
|
|
|
|
const groupsWithConflicts = zonesAndItems.filter(
|
|
|
|
({ items }) => items.length > 1
|
|
|
|
);
|
|
|
|
const itemIdsWithConflicts = new Set(
|
|
|
|
groupsWithConflicts
|
|
|
|
.map(({ items }) => items)
|
|
|
|
.flat()
|
|
|
|
.map((item) => item.id)
|
|
|
|
);
|
|
|
|
const itemIdsWeHaveSeen = new Set();
|
|
|
|
zonesAndItems = zonesAndItems.filter(({ items }) => {
|
|
|
|
// We need all groups with more than one item. If there's only one, we get
|
|
|
|
// to think harder :)
|
|
|
|
if (items.length > 1) {
|
|
|
|
items.forEach((item) => itemIdsWeHaveSeen.add(item.id));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = items[0];
|
|
|
|
|
|
|
|
// Has the item been seen a group we kept, or an upcoming group with
|
|
|
|
// multiple conflicting items? If so, skip this group. If not, keep it.
|
|
|
|
if (itemIdsWeHaveSeen.has(item.id) || itemIdsWithConflicts.has(item.id)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
itemIdsWeHaveSeen.add(item.id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-22 14:55:12 -07:00
|
|
|
return zonesAndItems;
|
2020-04-21 20:46:53 -07:00
|
|
|
}
|
|
|
|
|
2021-04-15 17:17:53 -07:00
|
|
|
function buildOutfitUrl(outfitState) {
|
2021-04-22 02:35:59 -07:00
|
|
|
const { id } = outfitState;
|
|
|
|
|
|
|
|
const { origin, pathname } = window.location;
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
return origin + `/outfits/${id}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return origin + pathname + "?" + buildOutfitQueryString(outfitState);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildOutfitQueryString(outfitState) {
|
2020-05-02 22:20:17 -07:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
2020-05-23 12:47:06 -07:00
|
|
|
pose,
|
2020-08-28 22:58:39 -07:00
|
|
|
appearanceId,
|
2020-05-02 22:20:17 -07:00
|
|
|
wornItemIds,
|
|
|
|
closetedItemIds,
|
2021-04-15 17:17:53 -07:00
|
|
|
} = outfitState;
|
2020-04-30 00:45:01 -07:00
|
|
|
|
2020-05-10 00:21:04 -07:00
|
|
|
const params = new URLSearchParams({
|
|
|
|
name: name || "",
|
|
|
|
species: speciesId,
|
|
|
|
color: colorId,
|
2020-05-23 12:47:06 -07:00
|
|
|
pose,
|
2020-05-10 00:21:04 -07:00
|
|
|
});
|
2020-04-30 00:45:01 -07:00
|
|
|
for (const itemId of wornItemIds) {
|
|
|
|
params.append("objects[]", itemId);
|
|
|
|
}
|
|
|
|
for (const itemId of closetedItemIds) {
|
|
|
|
params.append("closet[]", itemId);
|
|
|
|
}
|
2020-08-28 22:58:39 -07:00
|
|
|
if (appearanceId != null) {
|
|
|
|
// `state` is an old name for compatibility with old-style DTI URLs. It
|
|
|
|
// refers to "PetState", the database table name for pet appearances.
|
|
|
|
params.append("state", appearanceId);
|
|
|
|
}
|
2020-04-30 00:45:01 -07:00
|
|
|
|
2021-04-22 02:35:59 -07:00
|
|
|
return params.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the two given outfit states represent identical customizations.
|
|
|
|
*/
|
|
|
|
export function outfitStatesAreEqual(a, b) {
|
|
|
|
return buildOutfitQueryString(a) === buildOutfitQueryString(b);
|
2020-04-30 00:45:01 -07:00
|
|
|
}
|
|
|
|
|
2020-04-21 20:46:53 -07:00
|
|
|
export default useOutfitState;
|