Skip to content

Commit

Permalink
Attach inventory to localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed Apr 26, 2024
1 parent ed7fb3b commit 64e970f
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 147 deletions.
10 changes: 0 additions & 10 deletions src/actions/Buy.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/components/DisplayWorld.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import useInteraction from '../hooks/useInteraction';
import useStats from '../hooks/useStats';
import useSave from '../hooks/useSave';
import { list } from '../actions/Load';
import * as Buy from '../actions/Buy';
import { ACTIONS } from '../Actions';
import { renderTemplate, keyAlias } from '../utils';
import { renderTemplate, keyAlias, parseInventory } from '../utils';

export default function DisplayWorld({
width, height,
Expand Down Expand Up @@ -75,7 +74,7 @@ export default function DisplayWorld({
const [category] = item.trim().split('\n', 1);
actions[category] = renderTemplate(item, { name }).slice(category.length + 1).trim();
});
const inventory = Buy.parse(actions);
const inventory = parseInventory(actions);
if (inventory.length > 0) {
actions[ACTIONS.BUY] = inventory;
}
Expand Down
28 changes: 15 additions & 13 deletions src/hooks/useEquipment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const RARITY_COLORS = [
'#f0f',
];

function useEquipmentFile(kind, spec, position, secondPosition=null) {
const { template='none', rarity=0, name='--' } = spec || {};
function useEquipmentFile(inventory, kind, id, position, secondPosition=null) {
const item = inventory[kind]?.find(item => item.id === id);
const { template='none', rarity=0, name='--' } = item || {};
const [data, setData] = useState(null);

useEffect(() => {
Expand Down Expand Up @@ -38,7 +39,7 @@ function useEquipmentFile(kind, spec, position, secondPosition=null) {
}
});
const data = {
...spec, kind, rarity, template, name, buffer, icon,
...item, kind, rarity, template, name, buffer, icon,
};
stats && stats.split('\n').forEach((line) => {
const [stat, value] = line.split(':');
Expand All @@ -47,13 +48,14 @@ function useEquipmentFile(kind, spec, position, secondPosition=null) {
return data;
})
.then(setData);
}, [kind, name]);
}, [kind, name, template]);

return data;
}


export default function useEquipment({
inventory,
hair=null,
head=null,
arms=null,
Expand All @@ -64,15 +66,15 @@ export default function useEquipment({
weapon=null,
shield=null,
}) {
const hairData = useEquipmentFile("hair", hair, [0, 2]);
const headData = useEquipmentFile("head", head, [0, 3]);
const armsData = useEquipmentFile("arms", arms, [1, 2], [1, 4]);
const bodyData = useEquipmentFile("body", body, [1, 3]);
const waistData = useEquipmentFile("waist", waist, [2, 2], [2, 4]);
const legsData = useEquipmentFile("legs", legs, [2, 3]);
const feetData = useEquipmentFile("feet", feet, [3, 2], [3, 4]);
const weaponData = useEquipmentFile("weapon", weapon, [1, 0]);
const shieldData = useEquipmentFile("shield", shield, [1, 5]);
const hairData = useEquipmentFile(inventory, "hair", hair, [0, 2]);
const headData = useEquipmentFile(inventory, "head", head, [0, 3]);
const armsData = useEquipmentFile(inventory, "arms", arms, [1, 2], [1, 4]);
const bodyData = useEquipmentFile(inventory, "body", body, [1, 3]);
const waistData = useEquipmentFile(inventory, "waist", waist, [2, 2], [2, 4]);
const legsData = useEquipmentFile(inventory, "legs", legs, [2, 3]);
const feetData = useEquipmentFile(inventory, "feet", feet, [3, 2], [3, 4]);
const weaponData = useEquipmentFile(inventory, "weapon", weapon, [1, 0]);
const shieldData = useEquipmentFile(inventory, "shield", shield, [1, 5]);

const [buffers, setBuffers] = useState(null);

Expand Down
65 changes: 37 additions & 28 deletions src/hooks/useEquipmentBuffers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ const EQUIPMENT_ORDER = [
'head', 'arms', 'shield', 'waist',
];

function farColumns(info1, info2) {
function label(data) {
const { kind='?', icon, stats: { A=0, D=0 }={} } = data;
function farColumns(inventory, equipment, kind1, kind2) {
function label(kind) {
const { icon, stats: { A=0, D=0 }={} } = (
inventory[kind]?.find(({ id }) => id === equipment[kind]) || {}
);
return `${ABBREVIATIONS[kind]} ${minifyNumbers(A || D)}`;
}
return (
label(info1 || {}).split('')
label(kind1).split('')
.concat(
['', '', '', '', '', '', ''],
label(info2 || {}).split('')
label(kind2).split('')
)
);
}

export default function useEquipmentBuffers(enabled, { width, height, keyMap }) {
const [buffers, setBuffers] = useState(null);

const { inventory, equipment, setEquipment } = useInventory();
const { buffers: _buffers, ...slots } = useEquipment(equipment);
const { inventory, equipment, equip } = useInventory('player');
const { buffers: _buffers } = useEquipment({ inventory, ...equipment });

// Main display
const [equipmentLayoutBuffer, setEquipmentLayoutBuffer] = useState(null);
Expand Down Expand Up @@ -65,16 +67,21 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
break;
case keyMap.select:
setSlotChoice(EQUIPMENT_ORDER[selectedSlot]);
const currentItem = inventory[EQUIPMENT_ORDER[selectedSlot]].findIndex(
({ id }) => id === equipment[EQUIPMENT_ORDER[selectedSlot]].id
const items = inventory[EQUIPMENT_ORDER[selectedSlot]];
if (!items) {
setScrollOffset(0);
return;
}
const currentItem = items.findIndex(
({ id }) => id === equipment[EQUIPMENT_ORDER[selectedSlot]]
);
setScrollOffset(currentItem);
setScrollOffset(currentItem + 1); // +1 because empty '--' is always prepended
break;
}
}
window.addEventListener('keydown', keydown);
return () => window.removeEventListener('keydown', keydown);
}, [enabled, slotChoice, selectedSlot]);
}, [enabled, slotChoice, selectedSlot, inventory, equipment]);

// Keybind equipment listing selection
useEffect(() => {
Expand All @@ -83,18 +90,18 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
switch (e.key) {
case keyMap.up:
setScrollOffset((offset) => {
if (offset === 0) return 0;
if (offset <= 0) return 0;
return offset - 1;
});
break;
case keyMap.down:
setScrollOffset((offset) => {
if (offset >= inventory[slotChoice].length - 3 + 2) return offset;
if (offset >= inventory[slotChoice].length) return offset;
return offset + 1;
});
break;
case keyMap.select:
setEquipment(slotChoice, inventory[slotChoice][scrollOffset].id);
equip(slotChoice, inventory[slotChoice][scrollOffset - 1]?.id || null);
setSlotChoice(null);
break;
case keyMap.cancel:
Expand All @@ -104,7 +111,7 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
}
window.addEventListener('keydown', keydown);
return () => window.removeEventListener('keydown', keydown);
}, [enabled, slotChoice, setEquipment]);
}, [enabled, slotChoice, equip, inventory, equipment]);

// Primary view
// Main equipment display, doesn't update unless equipped items change
Expand All @@ -115,12 +122,12 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
}
setEquipmentLayoutBuffer([
'', '', '', '',
farColumns(slots.weapon, slots.head),
farColumns(slots.body, slots.arms),
farColumns(slots.legs, slots.shield),
farColumns(slots.feet, slots.waist),
farColumns(inventory, equipment, 'weapon', 'head'),
farColumns(inventory, equipment, 'body', 'arms'),
farColumns(inventory, equipment, 'legs', 'shield'),
farColumns(inventory, equipment, 'feet', 'waist'),
]);
}, [enabled, slotChoice, slots.weapon, slots.head, slots.body, slots.arms, slots.legs, slots.shield, slots.feet, slots.waist]);
}, [enabled, slotChoice, inventory, equipment]);

// Based on selected slot, create the highlighter buffer
useEffect(() => {
Expand All @@ -141,9 +148,11 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
// Create equipment scroll buffer
useEffect(() => {
if (!enabled || !slotChoice) return;
const list = inventory[slotChoice].map(({ id, name }) => {
const list = [
{ id: null, name: '--' }
].concat(inventory[slotChoice]).map(({ id, name }) => {
let label = ` ${name}`;
if (id === equipment[slotChoice].id) {
if (id === equipment[slotChoice]) {
label = `*${label.slice(1)}`;
}
label = label.padEnd(width, ' ');
Expand All @@ -154,14 +163,14 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
setScrollSelectionBuffer(buffer.map(
(line, i) => i === 6 ? line : ''
));
}, [enabled, slotChoice, scrollOffset, inventory, height, width]);
}, [enabled, slotChoice, scrollOffset, inventory, equipment, height, width]);

useEffect(() => {
if (!enabled) {
setBuffers(null);
return;
}
if (!slotChoice) {
if (slotChoice === null) {
setBuffers([
equipmentLayoutBuffer && { fg: '#555', buffer: equipmentLayoutBuffer },
selectedSlotBuffer && { bg: '#888', fg: 'black', buffer: selectedSlotBuffer },
Expand All @@ -179,17 +188,17 @@ export default function useEquipmentBuffers(enabled, { width, height, keyMap })
'', '', '', '',
`${slotChoice[0].toUpperCase() + slotChoice.slice(1)}:`.padEnd(width - 5, ' ')
+
(inventory[slotChoice][scrollOffset]?.stats?.A !== undefined ? (
`Atk ${minifyNumbers(inventory[slotChoice][scrollOffset].stats?.A || 0)}`
(slotChoice === 'weapon' || inventory[slotChoice][scrollOffset - 1]?.stats?.A !== undefined ? (
`Atk ${minifyNumbers(inventory[slotChoice][scrollOffset - 1]?.stats?.A || 0)}`
) : (
`Def ${minifyNumbers(inventory[slotChoice][scrollOffset].stats?.D || 0)}`
`Def ${minifyNumbers(inventory[slotChoice][scrollOffset - 1]?.stats?.D || 0)}`
))
]},
scrollBuffer && { fg: '#c7c7c7', buffer: scrollBuffer },
scrollSelectionBuffer && { bg: '#aaa', fg: 'black', buffer: scrollSelectionBuffer },
].filter(Boolean));
}
}, [enabled, slotChoice, scrollOffset, scrollBuffer, scrollSelectionBuffer, equipmentLayoutBuffer, selectedSlotBuffer, inventory]);
}, [enabled, _buffers, slotChoice, scrollOffset, scrollBuffer, scrollSelectionBuffer, equipmentLayoutBuffer, selectedSlotBuffer, inventory]);

return buffers;
}
Loading

0 comments on commit 64e970f

Please sign in to comment.