Skip to content

Commit

Permalink
Use Sets for mastered items
Browse files Browse the repository at this point in the history
  • Loading branch information
DaPigGuy committed Feb 5, 2025
1 parent 1f7bf31 commit a79b98c
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 49 deletions.
25 changes: 11 additions & 14 deletions src/components/checklist/CategoryInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { produce } from "immer";
import PropTypes from "prop-types";
import { useStore } from "../../hooks/useStore";
import { foundersItems } from "../../utils/items";
Expand All @@ -21,20 +20,17 @@ const fancyCategoryNames = {
};

function CategoryItem({ name }) {
const { categoryItems, hideFounders } = useStore(state => ({
const {
itemsMastered,
partiallyMasteredItems,
categoryItems,
hideFounders
} = useStore(state => ({
itemsMastered: state.itemsMastered,
partiallyMasteredItems: state.partiallyMasteredItems,
categoryItems: state.items[name],
hideFounders: state.hideFounders
}));
const itemsMastered = useStore(state =>
state.itemsMastered.filter(item => categoryItems[item])
);
const partiallyMasteredItems = useStore(state =>
produce(state.partiallyMasteredItems, draftState => {
Object.keys(draftState).forEach(item => {
if (!categoryItems[item]) delete draftState[item];
});
})
);

let masteredCount = 0;
let masteredXP = 0;
Expand All @@ -46,12 +42,12 @@ function CategoryItem({ name }) {
if (
hideFounders &&
foundersItems.includes(itemName) &&
!itemsMastered.includes(itemName)
!itemsMastered.has(itemName)
)
return;
totalCount++;
totalXP += xpFromItem(item, name);
if (itemsMastered.includes(itemName)) {
if (itemsMastered.has(itemName)) {
masteredCount++;
masteredXP += xpFromItem(item, name);
} else if (partiallyMasteredItems[itemName]) {
Expand Down Expand Up @@ -92,3 +88,4 @@ CategoryItem.propTypes = {
};

export default CategoryItem;

6 changes: 3 additions & 3 deletions src/components/checklist/CategoryItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ function CategoryItem({ name, item }) {
} = useStore(state => ({
type: state.type,
masterItem: state.masterItem,
mastered: state.itemsMastered.includes(name),
mastered: state.itemsMastered.has(name),
masteryRankLocked: (item.mr || 0) > state.masteryRank,
partialRank: state.partiallyMasteredItems[name],
setPartiallyMasteredItem: state.setPartiallyMasteredItem,
hidden:
(state.hideMastered && state.itemsMastered.includes(name)) ||
(state.hideMastered && state.itemsMastered.has(name)) ||
(state.hideFounders &&
foundersItems.includes(name) &&
!state.itemsMastered.includes(name))
!state.itemsMastered.has(name))
}));
const [rankSelectToggled, setRankSelectToggled] = useState(false);

Expand Down
2 changes: 1 addition & 1 deletion src/components/checklist/Checklist.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function Checklist() {
!state.hideMastered ||
!Object.keys(state.items[category]).every(
item =>
state.itemsMastered.includes(item) ||
state.itemsMastered.has(item) ||
(state.hideFounders && foundersItems.includes(item))
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/checklist/planets/PlanetChecklist.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function PlanetChecklist() {
!Object.keys(nodes[planet]).every(id =>
state[
state.displayingSteelPath ? "steelPath" : "starChart"
].includes(id)
].has(id)
) ||
(planetsWithJunctions.includes(planet) &&
!state[
(state.displayingSteelPath
? "steelPath"
: "starChart") + "Junctions"
].includes(planet))
].has(planet))
);
})
);
Expand Down
6 changes: 3 additions & 3 deletions src/components/checklist/planets/PlanetInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ function PlanetInfo({ name }) {
const { nodesMastered, junctionMastered } = useStore(state => ({
nodesMastered: state[
state.displayingSteelPath ? "steelPath" : "starChart"
].filter(id => nodes[name][id]),
],
junctionMastered:
hasJunction &&
state[
(state.displayingSteelPath ? "steelPath" : "starChart") +
"Junctions"
].includes(name)
].has(name)
}));
Object.entries(nodes[name]).forEach(([id, node]) => {
totalCount++;
totalXP += node.xp ?? 0;
if (nodesMastered.includes(id)) {
if (nodesMastered.has(id)) {
masteredCount++;
masteredXP += node.xp ?? 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/checklist/planets/PlanetJunction.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ function PlanetJunction({ planet }) {
state[
(state.displayingSteelPath ? "steelPath" : "starChart") +
"Junctions"
].includes(planet),
].has(planet),
hidden:
state.hideMastered &&
state[
(state.displayingSteelPath ? "steelPath" : "starChart") +
"Junctions"
].includes(planet)
].has(planet)
}));
return hidden ? null : (
<div
Expand Down
4 changes: 2 additions & 2 deletions src/components/checklist/planets/PlanetNode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function PlanetNode({ id, node }) {
mastered:
state[
state.displayingSteelPath ? "steelPath" : "starChart"
].includes(id),
].has(id),
hidden:
state.hideMastered &&
state[
state.displayingSteelPath ? "steelPath" : "starChart"
].includes(id)
].has(id)
}));

return hidden ? null : (
Expand Down
43 changes: 21 additions & 22 deletions src/hooks/useStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export const useStore = createWithEqualityFn(
} = get();

const masteryBreakdown = {
STAR_CHART: junctionsToXP(starChartJunctions.length),
STEEL_PATH: junctionsToXP(steelPathJunctions.length),
STAR_CHART: junctionsToXP(starChartJunctions.size),
STEEL_PATH: junctionsToXP(steelPathJunctions.size),
RAILJACK_INTRINSICS: intrinsicsToXP(railjackIntrinsics),
DRIFTER_INTRINSICS: intrinsicsToXP(drifterIntrinsics)
};
Expand Down Expand Up @@ -243,7 +243,7 @@ export const useStore = createWithEqualityFn(
? 2
: 1;

if (itemsMastered.includes(itemName)) {
if (itemsMastered.has(itemName)) {
addItemXP(item);
itemsMasteredCount += additionalItemCount;

Expand All @@ -258,7 +258,7 @@ export const useStore = createWithEqualityFn(
if (
hideFounders &&
foundersItems.includes(itemName) &&
!itemsMastered.includes(itemName)
!itemsMastered.has(itemName)
)
return;
totalXP += xpFromItem(item, item.type);
Expand All @@ -268,11 +268,11 @@ export const useStore = createWithEqualityFn(
Object.entries(flattenedNodes).forEach(([id, node]) => {
if (node.xp) {
totalXP += node.xp * 2;
if (starChart.includes(id)) {
if (starChart.has(id)) {
xp += node.xp;
masteryBreakdown.STAR_CHART += node.xp;
}
if (steelPath.includes(id)) {
if (steelPath.has(id)) {
xp += node.xp;
masteryBreakdown.STEEL_PATH += node.xp;
}
Expand All @@ -289,7 +289,7 @@ export const useStore = createWithEqualityFn(
});
},

itemsMastered: [],
itemsMastered: new Set(),
setItemsMastered: itemsMastered => {
setMastered("itemsMastered", itemsMastered);
get().recalculateIngredients();
Expand All @@ -308,7 +308,7 @@ export const useStore = createWithEqualityFn(
i =>
!get().hideFounders ||
!foundersItems.includes(i) ||
get().itemsMastered.includes(i)
get().itemsMastered.has(i)
),
mastered
);
Expand All @@ -332,7 +332,7 @@ export const useStore = createWithEqualityFn(
},
setPartiallyMasteredItem: (name, rank, maxRank) => {
if (rank === maxRank) get().masterItem(name, true);
else if (get().itemsMastered.includes(name))
else if (get().itemsMastered.has(name))
get().masterItem(name, false);
rank = rank === maxRank || rank === 0 ? undefined : rank;

Expand Down Expand Up @@ -362,10 +362,10 @@ export const useStore = createWithEqualityFn(
displayingSteelPath: false,
setDisplayingSteelPath: displayingSteelPath =>
set({ displayingSteelPath }),
starChart: [],
starChartJunctions: [],
steelPath: [],
steelPathJunctions: [],
starChart: new Set(),
starChartJunctions: new Set(),
steelPath: new Set(),
steelPathJunctions: new Set(),
setNodesMastered: (nodesMastered, steelPath) =>
setMastered(steelPath ? "steelPath" : "starChart", nodesMastered),
masterNode: (id, steelPath, mastered) =>
Expand Down Expand Up @@ -454,7 +454,7 @@ export const useStore = createWithEqualityFn(
}

Object.entries(flattenedItems).forEach(([itemName, item]) => {
if (!itemsMastered.includes(itemName)) {
if (!itemsMastered.has(itemName)) {
if (
!partiallyMasteredItems[itemName] &&
recipes[itemName]
Expand Down Expand Up @@ -587,18 +587,17 @@ function setMastered(key, mastered) {
if (!mastered.includes(item)) mastered.push(item);
});

set(() => ({ [key]: mastered }));
set(() => ({ [key]: new Set(mastered) }));
get().recalculateMasteryRank();
}

function master(key, id, mastered) {
set(state =>
produce(state, draftState => {
if (mastered) {
if (!draftState[key].includes(id)) draftState[key].push(id);
draftState[key].add(id);
} else {
const index = draftState[key].indexOf(id);
if (index !== -1) draftState[key].splice(index, 1);
draftState[key].delete(id);
}
markMasteryChange(draftState, key, id, mastered);
})
Expand All @@ -611,14 +610,14 @@ function masterAll(key, all, mastered) {
set(state =>
produce(state, draftState => {
all.forEach(id => {
if (mastered && !draftState[key].includes(id)) {
draftState[key].push(id);
if (mastered && !draftState[key].has(id)) {
draftState[key].add(id);
markMasteryChange(draftState, key, id, mastered);
} else if (!mastered && draftState[key].includes(id)) {
} else if (!mastered && draftState[key].has(id)) {
markMasteryChange(draftState, key, id, mastered);
}
});
if (!mastered) draftState[key] = [];
if (!mastered) draftState[key] = new Set();
})
);
get().recalculateMasteryRank();
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React from "react";
import {createRoot} from "react-dom/client";
import App from "./App";
import "./index.css";
import { enableMapSet } from "immer";

enableMapSet();

createRoot(document.getElementById("root")).render(
<React.StrictMode>
Expand Down

0 comments on commit a79b98c

Please sign in to comment.