]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/hooks/tracker.js
respond to whispers
[alttp.git] / resources / js / hooks / tracker.js
index be5a52f063604aad4eaeb7d88dcd650df2c4f87d..d0fb6b8e028e400bb4f0f3287eeebb3df6e5a1f4 100644 (file)
@@ -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 <context.Provider value={value}>
                {children}
        </context.Provider>;