oops, restricting the same zone is not conflict!

This commit is contained in:
Matt Dunn-Rankin 2020-04-26 23:26:17 -07:00
parent 06fc2715de
commit 7d4cedb146

View file

@ -218,28 +218,31 @@ function findItemConflicts(itemIdToAdd, state, apolloClient) {
if (!itemToAdd.appearanceOn) { if (!itemToAdd.appearanceOn) {
return []; return [];
} }
const itemToAddZoneIds = [
...itemToAdd.appearanceOn.layers.map((l) => l.zone.id),
...itemToAdd.appearanceOn.restrictedZones.map((z) => z.id),
];
const wornItems = Array.from(wornItemIds).map((id) => const wornItems = Array.from(wornItemIds).map((id) =>
items.find((i) => i.id === id) items.find((i) => i.id === id)
); );
const itemToAddZoneSets = getItemZones(itemToAdd);
const conflictingIds = []; const conflictingIds = [];
for (const wornItem of wornItems) { for (const wornItem of wornItems) {
if (!wornItem.appearanceOn) { if (!wornItem.appearanceOn) {
continue; continue;
} }
const wornItemZoneIds = [
...wornItem.appearanceOn.layers.map((l) => l.zone.id),
...wornItem.appearanceOn.restrictedZones.map((z) => z.id),
];
const hasConflict = wornItemZoneIds.some((zid) => const wornItemZoneSets = getItemZones(wornItem);
itemToAddZoneIds.includes(zid)
const itemsConflict =
setsIntersect(
itemToAddZoneSets.occupies,
wornItemZoneSets.occupiesOrRestricts
) ||
setsIntersect(
wornItemZoneSets.occupies,
itemToAddZoneSets.occupiesOrRestricts
); );
if (hasConflict) {
if (itemsConflict) {
conflictingIds.push(wornItem.id); conflictingIds.push(wornItem.id);
} }
} }
@ -247,6 +250,22 @@ function findItemConflicts(itemIdToAdd, state, apolloClient) {
return conflictingIds; return conflictingIds;
} }
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;
}
// TODO: Get this out of here, tbh... // TODO: Get this out of here, tbh...
function getZonesAndItems(itemsById, wornItemIds, closetedItemIds) { function getZonesAndItems(itemsById, wornItemIds, closetedItemIds) {
const wornItems = wornItemIds.map((id) => itemsById[id]).filter((i) => i); const wornItems = wornItemIds.map((id) => itemsById[id]).filter((i) => i);