Skip to content

Commit

Permalink
Build menu choices early rather than late
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed Apr 30, 2024
1 parent de18b77 commit 55e7934
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/actions/Sell.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const EQUIPMENT = {
waist: "Waist gear",
}

export function parse(kindList, { interaction, inventory }) {
return kindList.split(',').map((kind) => {
export function parse({ text }, { interaction, inventory }) {
return text.split(',').map((kind) => {
if (EQUIPMENT[kind] !== undefined) {
return { name: EQUIPMENT[kind], items: inventory[kind] || [] };
}
Expand Down
32 changes: 17 additions & 15 deletions src/components/DisplayMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,19 @@ export default function DisplayMenu({
return;
}

const items = ACTIONS_ORDER.filter((option) => target[option])
items.push(...Object.keys(target).filter((option) => {
if (items.includes(option)) return false;
return /^[A-Z]$/.test(option[0]);
}))
items.forEach((option, i) => {
if (typeof target[option] === 'string') {
items[i] = { name: option, text: target[option] };
} else if (typeof target[option] === 'object') {
items[i] = { name: option, items: target[option] };
}
});
// const items = ACTIONS_ORDER.filter((option) => target[option]);
const items = Object.entries(target)
.filter(([k]) => ACTIONS_ORDER.includes(k))
.sort(([k1], [k2]) => ACTIONS_ORDER.indexOf(k1) - ACTIONS_ORDER.indexOf(k2))
;
items.push(...Object.entries(target).filter(([k]) => {
if (items.find(([k2]) => k2 === k)) return false;
return /^[A-Z]$/.test(k[0]);
}));

setMenus([{
title: `→${target.sprite} ${target.label}`,
items,
items: items.map(([_, v]) => v),
}])
}, [`${target?.coordinates}`]); // fixme: target 'updates' when player inventory changes

Expand Down Expand Up @@ -126,7 +123,7 @@ export default function DisplayMenu({
case keyMap.use:
setSelected((selected) => {
const option = options[selected];
if (!option.items && !option.text) {
if (option.event) {
const _selected = isNaN(menus[0].selected) ? selected : menus[0].selected;
const topOption = menus[0].items[_selected];
const eventNames = { Load: 'load' };
Expand All @@ -135,7 +132,12 @@ export default function DisplayMenu({
{ detail: option }
);
setTimeout((event) => window.dispatchEvent(event), 0, event);
return selected;

// Bail now if nothing to display
// (I don't think this is in use yet)
if (!option.items && !option.text) {
return selected;
}
}
setMenus((menus) => {
menus[menus.length - 1].selected = selected;
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useSave.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const IGNORE_VALUES = [undefined, null];
export default function useSave({...vars}) {
useEffect(() => {
const saveHandler = (e) => {
const { name: slot=localStorage.getItem('latest') } = e.detail || {};
const { slot=localStorage.getItem('latest') } = e.detail || {};
// console.log("* Saving", slot);
localStorage.setItem('latest', slot);
Object.entries(vars).forEach(([key, [value, _]]) => {
Expand All @@ -20,7 +20,7 @@ export default function useSave({...vars}) {
});
useEffect(() => {
const loadHandler = (e) => {
const { name: slot=localStorage.getItem('latest') || "Hero" } = e.detail || {};
const { slot=localStorage.getItem('latest') || "Hero" } = e.detail || {};
// console.log("* Loading", slot);
localStorage.setItem('latest', slot);
Object.entries(vars).forEach(([key, [_, setter]]) => {
Expand Down
24 changes: 20 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,33 @@ export function parseInteraction(interaction, dataFileText, { name, inventory })
const actions = {...interaction};
items.forEach((item) => {
const [category] = item.trim().split('\n', 1);
actions[category] = renderTemplate(item, { name }).slice(category.length + 1).trim();
actions[category] = {
name: category,
text: renderTemplate(item, { name }).slice(category.length + 1).trim()
};
});
const theirInventory = parseInventory(actions);
if (theirInventory.length > 0) {
actions[ACTIONS.BUY] = theirInventory.map((item) => ({ ...item, buyer: 'player' }));
actions[ACTIONS.BUY] = {
name: ACTIONS.BUY,
items: theirInventory.map((item) => ({ ...item, buyer: 'player', event: 'Buy' })),
text: null,
};
}
if (actions.Save !== undefined) {
actions.Save.event = 'Save';
}
if (actions.Load !== undefined) {
actions.Load = Load.list().map((name) => ({ name }));
Object.assign(actions.Load, {
items: Load.list().map((slot) => ({ slot, name: slot, event: 'Load' })),
text: null,
});
}
if (actions.Sell !== undefined) {
actions.Sell = Sell.parse(actions.Sell, { interaction, inventory });
Object.assign(actions.Sell, {
items: Sell.parse(actions.Sell, { interaction, inventory }),
text: null,
});
}
return actions;
}
Expand Down

0 comments on commit 55e7934

Please sign in to comment.