X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fhooks%2Ftracker.js;h=d0fb6b8e028e400bb4f0f3287eeebb3df6e5a1f4;hb=e0925d5b97ab0804222195eb4231c63b33703942;hp=be5a52f063604aad4eaeb7d88dcd650df2c4f87d;hpb=b5a50d74cf042fa7fc874d8184dc37ae20bb74dd;p=alttp.git diff --git a/resources/js/hooks/tracker.js b/resources/js/hooks/tracker.js index be5a52f..d0fb6b8 100644 --- a/resources/js/hooks/tracker.js +++ b/resources/js/hooks/tracker.js @@ -1,7 +1,14 @@ import PropTypes from 'prop-types'; import React from 'react'; -import { CONFIG, DUNGEONS, makeEmptyState } from '../helpers/tracker'; +import { + CONFIG, + DUNGEONS, + applyLogic, + configureDungeons, + makeEmptyState, + mergeStates, +} from '../helpers/tracker'; const context = React.createContext({}); @@ -10,35 +17,66 @@ export const useTracker = () => React.useContext(context); export const TrackerProvider = ({ children }) => { const [config, setConfig] = React.useState(CONFIG); const [state, setState] = React.useState(makeEmptyState()); + const [autoState, setAutoState] = React.useState(makeEmptyState()); + const [manualState, setManualState] = React.useState(makeEmptyState()); const [dungeons, setDungeons] = React.useState(DUNGEONS); + const [logic, setLogic] = React.useState({}); + const [pins, setPins] = React.useState([]); - React.useEffect(() => { - const newDungeons = DUNGEONS.map(dungeon => { - const newDungeon = JSON.parse(JSON.stringify(dungeon)); - if (config.wildMap && dungeon.map) { - ++newDungeon.items; - } - if (config.wildCompass && dungeon.compass) { - ++newDungeon.items; - } - if (config.wildSmall) { - newDungeon.items += dungeon.sk; - } - if (config.wildBig && dungeon.bk && !dungeon.dropBk) { - ++newDungeon.items; - } - if (!config.bossShuffle && dungeon.boss) { - newDungeon.bosses = [dungeon.boss]; - } - return newDungeon; + const saveConfig = React.useCallback((values) => { + setConfig(s => { + const newConfig = { ...s, ...values }; + localStorage.setItem('tracker.config', JSON.stringify(newConfig)); + return newConfig; + }); + }, []); + + const addPin = React.useCallback((pin) => { + setPins(ps => { + const id = ps.length ? ps[ps.length - 1].id + 1 : 1; + return [...ps, { ...pin, id }]; }); + }, []); + + const removePin = React.useCallback((pin) => { + setPins(ps => ps.filter(p => p.id !== pin.id)); + }, []); + + React.useEffect(() => { + const savedConfig = localStorage.getItem('tracker.config'); + if (savedConfig) { + setConfig(c => ({ ...c, ...JSON.parse(savedConfig) })); + } + }, []); + + React.useEffect(() => { + const newDungeons = configureDungeons(config); setDungeons(newDungeons); }, [config]); - const value = React.useMemo(() => { - return { config, setConfig, dungeons, setState, state }; + React.useEffect(() => { + setState(mergeStates(autoState, manualState)); + }, [autoState, manualState]); + + React.useEffect(() => { + setLogic(applyLogic(config, dungeons, state)); }, [config, dungeons, state]); + const value = React.useMemo(() => { + return { + addPin, + config, + dungeons, + logic, + pins, + removePin, + saveConfig, + setAutoState, + setManualState, + state, + }; + }, [config, dungeons, logic, pins, state]); + return {children} ;