]> git.localhorst.tv Git - alttp.git/blob - resources/js/hooks/tracker.js
tracker medallion controler
[alttp.git] / resources / js / hooks / tracker.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3
4 import { CONFIG, DUNGEONS, makeEmptyState } from '../helpers/tracker';
5
6 const context = React.createContext({});
7
8 export const useTracker = () => React.useContext(context);
9
10 export const TrackerProvider = ({ children }) => {
11         const [config, setConfig] = React.useState(CONFIG);
12         const [state, setState] = React.useState(makeEmptyState());
13         const [dungeons, setDungeons] = React.useState(DUNGEONS);
14
15         React.useEffect(() => {
16                 const newDungeons = DUNGEONS.map(dungeon => {
17                         const newDungeon = JSON.parse(JSON.stringify(dungeon));
18                         if (config.wildMap && dungeon.map) {
19                                 ++newDungeon.items;
20                         }
21                         if (config.wildCompass && dungeon.compass) {
22                                 ++newDungeon.items;
23                         }
24                         if (config.wildSmall) {
25                                 newDungeon.items += dungeon.sk;
26                         }
27                         if (config.wildBig && dungeon.bk && !dungeon.dropBk) {
28                                 ++newDungeon.items;
29                         }
30                         if (!config.bossShuffle && dungeon.boss) {
31                                 newDungeon.bosses = [dungeon.boss];
32                         }
33                         return newDungeon;
34                 });
35                 setDungeons(newDungeons);
36         }, [config]);
37
38         const value = React.useMemo(() => {
39                 return { config, setConfig, dungeons, setState, state };
40         }, [config, dungeons, state]);
41
42         return <context.Provider value={value}>
43                 {children}
44         </context.Provider>;
45 };
46
47 TrackerProvider.propTypes = {
48         children: PropTypes.node,
49 };