Skip to content

Commit

Permalink
Add crude save/load
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed Apr 24, 2024
1 parent 8ab3e3f commit 71bdfe5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 19 deletions.
1 change: 0 additions & 1 deletion public/Bard/bard.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ Save
"I'll sing about you forever, ${name}."
---
Load
"Whose tale do you wish to hear?"
6 changes: 6 additions & 0 deletions src/Actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export const ACTIONS_ORDER = [
'Bribe',
'Fight',
];
export const TARGETED_ACTIONS = [
'Buy',
'Sell',
'Bribe',
'Fight',
];

export const ACTIONS = Object.fromEntries(ACTIONS_ORDER.map((action) => [
action.toUpperCase(),
Expand Down
49 changes: 41 additions & 8 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import WorldDisplay from './components/displays/WorldDisplay';
import MenuDisplay from './components/displays/MenuDisplay';
import Analysis from './components/Analysis';
import useAnalyzer from './hooks/useAnalyzer';
import { ACTIONS, ACTIONS_ORDER } from './Actions';
import useSave from './hooks/useSave';
import { ACTIONS, TARGETED_ACTIONS, ACTIONS_ORDER } from './Actions';

const START_WORLD = 'Terra Montans.txt'
const START_Y = 17;
Expand All @@ -15,11 +15,12 @@ const START_X = 35;
const VIEWPORT_WIDTH = 16;
const VIEWPORT_HEIGHT = 8;

let START_SAVE_SLOT = localStorage.getItem('latest');
if (!START_SAVE_SLOT) {
START_SAVE_SLOT = 'Hero';
localStorage.setItem('latest', START_SAVE_SLOT);
}
// let START_SAVE_SLOT = localStorage.getItem('latest');
// if (!START_SAVE_SLOT) {
// START_SAVE_SLOT = 'Hero';
// localStorage.setItem('latest', START_SAVE_SLOT);
// }
const START_SAVE_SLOT = 'Hero2';


export default function App({
Expand Down Expand Up @@ -51,15 +52,47 @@ export default function App({
destination: [destination, setDestination],
});

useEffect(() => {
const loadHandler = (e) => {
// const newSaveSlot = e.detail;
// setSaveSlot(newSaveSlot);
// console.log('Load event', e.detail);
const loadEvent = new CustomEvent('load', { detail: e.detail });
window.dispatchEvent(loadEvent);
};
window.addEventListener('Load', loadHandler);
return () => window.removeEventListener('Load', loadHandler);
}, []);

useEffect(() => {
if (!interaction) {
setActiveOptions(ACTIONS_ORDER);
setActiveOptions(ACTIONS_ORDER.filter((option) => !TARGETED_ACTIONS.includes(option)));
return;
};
const newOptions = Object.keys(interaction).filter((option) => /^[A-Z]$/.test(option[0]));
const newOptions = [];
ACTIONS_ORDER.forEach((option) => {
if (interaction[option]) {
newOptions.push(option);
}
});
newOptions.push(...Object.keys(interaction).filter((option) => {
if (newOptions.includes(option)) return false;
if (option === ACTIONS.BUY) return true;
return /^[A-Z]$/.test(option[0]);
}));
setActiveOptions(newOptions);
}, [interaction]);

// // React to a slot change event
// useEffect(() => {
// const saveSlotHandler = (e) => {
// const newSaveSlot = e.detail;
// setSaveSlot(newSaveSlot);
// };
// window.addEventListener('ChangeSlot', saveSlotHandler);
// return () => window.removeEventListener('ChangeSlot', saveSlotHandler);
// }, []);

// React to a destination event
useEffect(() => {
const destinationHandler = (e) => {
Expand Down
18 changes: 16 additions & 2 deletions src/actions/Load.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const IGNORE = [undefined, null];

export function load(slot, vars) {
const loadHandler = () => {
const loadHandler = (e) => {
const specificSlot = e.detail?.name;
console.log('specificSlot', specificSlot);
Object.entries(vars).forEach(([key, [_, setter]]) => {
const v = JSON.parse(localStorage.getItem(`${slot}/${key}`));
const v = JSON.parse(localStorage.getItem(`${specificSlot || slot}/${key}`));
if (!IGNORE.includes(v)) {
setter(v);
}
Expand All @@ -12,3 +14,15 @@ export function load(slot, vars) {
window.addEventListener('load', loadHandler);
return () => window.removeEventListener('load', loadHandler);
}

export function list() {
const slotNames = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const [name] = key.split('/', 1);
if (name !== 'latest' && !slotNames.includes(name)) {
slotNames.push(name);
}
}
return slotNames;
}
24 changes: 18 additions & 6 deletions src/components/displays/MenuDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useSave from '../../hooks/useSave';
import { minifyNumbers, bufferize } from '../../utils';

const OPTION_KEYS = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const SUB_MENU_CHOICES = [ACTIONS.BUY, "Load"];

export default function MenuDisplay({
saveSlot,
Expand Down Expand Up @@ -39,7 +40,15 @@ export default function MenuDisplay({
});

useEffect(() => {
if (!targetData || ![ACTIONS.BUY].includes(activeChoice)) {
if (activeChoice === "Save") {
const event = new CustomEvent("Save");
window.dispatchEvent(event);
}
}, [activeChoice]);

useEffect(() => {
// console.log("Active choice", activeChoice, targetData);
if (!targetData || !SUB_MENU_CHOICES.includes(activeChoice)) {
setSubOptions(null);
return;
}
Expand All @@ -50,9 +59,9 @@ export default function MenuDisplay({
useEffect(() => {
if (
started === null
|| !targetData
|| !targetData[activeChoice]
|| !targetData?.[activeChoice]
|| typeof targetData[activeChoice] !== 'string'
|| targetData[activeChoice].length === 0
) {
setScrollOffset(0);
setScrollBuffer(null);
Expand Down Expand Up @@ -129,7 +138,7 @@ export default function MenuDisplay({

window.addEventListener('keydown', keydown);
return () => window.removeEventListener('keydown', keydown);
}, [target, targetData, scrollBuffer, activeChoice, subOptions]);
}, [target, targetData, scrollBuffer, activeChoice, options, subOptions]);

useEffect(() => {
const keydown = (e) => {
Expand All @@ -150,7 +159,7 @@ export default function MenuDisplay({

window.addEventListener('keydown', keydown);
return () => window.removeEventListener('keydown', keydown);
}, [target, selected]);
}, [target, selected, subSelected, subOptions, activeChoice]);

useEffect(() => {
if (!target) {
Expand Down Expand Up @@ -229,7 +238,10 @@ export default function MenuDisplay({
if (i === subSelected){
label = ` ${label.split(':')[1]}`;
}
return label.slice(0, -1) + minifyNumbers(spec.stats.A || spec.stats.D)
if (spec.stats === undefined) {
return label;
}
return label.slice(0, -1) + minifyNumbers(spec.stats?.A || spec.stats?.D || 0)
}),
]}
),
Expand Down
8 changes: 6 additions & 2 deletions src/components/displays/WorldDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import useLocation from '../../hooks/useLocation';
import useInteraction from '../../hooks/useInteraction';
import { ACTIONS } from '../../Actions';
import * as Buy from '../../actions/Buy';
import * as Load from '../../actions/Load';
import { renderTemplate, keyAlias } from '../../utils';

export default function WorldDisplay({
Expand Down Expand Up @@ -59,20 +60,23 @@ export default function WorldDisplay({
const actions = {...interaction};
items.forEach((item) => {
const [category] = item.trim().split('\n', 1);
actions[category] = renderTemplate(item, { name: "Hero" }).slice(category.length + 1).trim();
actions[category] = renderTemplate(item, { name: saveSlot }).slice(category.length + 1).trim();
});
const inventory = Buy.parse(actions);
if (inventory.length > 0) {
actions[ACTIONS.BUY] = inventory;
}
if (actions.Load !== undefined) {
actions["Load"] = Load.list().map((name) => ({ name }));
}
return actions;
})
.then((actions) => {
const event = new CustomEvent('interaction', { detail: actions });
window.dispatchEvent(event);
})
}
}, [interaction]);
}, [interaction, saveSlot]);

useEffect(() => {
if (!target) {
Expand Down

0 comments on commit 71bdfe5

Please sign in to comment.