]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/tracker/Map.js
clean up manual tracking
[alttp.git] / resources / js / components / tracker / Map.js
index b2685f873d6776814e00db66ff2cc6c225d51b03..cfae29c4b4a4875e0c71e087b7f7456ee511d014 100644 (file)
@@ -3,12 +3,20 @@ import React from 'react';
 import { useTranslation } from 'react-i18next';
 
 import {
+       addDungeonCheck,
        clearAll,
-       decrement,
+       completeDungeonChecks,
+       countRemainingLocations,
+       getDungeonClearedItems,
+       getDungeonRemainingItems,
+       hasClearedLocations,
        hasDungeonBoss,
-       increment,
+       hasDungeonPrize,
        isDungeonCleared,
-       toggleBoolean,
+       removeDungeonCheck,
+       resetDungeonChecks,
+       setBossDefeated,
+       setPrizeAcquired,
        unclearAll,
 } from '../../helpers/tracker';
 import { useTracker } from '../../hooks/tracker';
@@ -637,8 +645,8 @@ const Location = ({ number, l, size }) => {
        >
                <title>{t(`tracker.location.${l.id}`)}</title>
                <rect className="box" x="0" y="0" />
-               {number && l.cleared < l.total ?
-                       <text className="text" x="0" y="0">{Math.max(0, l.total - l.cleared)}</text>
+               {number && l.remaining ?
+                       <text className="text" x="0" y="0">{l.remaining}</text>
                : null}
        </g>;
 };
@@ -649,8 +657,8 @@ Location.propTypes = {
                id: PropTypes.string,
                x: PropTypes.number,
                y: PropTypes.number,
-               cleared: PropTypes.number,
-               total: PropTypes.number,
+               number: PropTypes.number,
+               remaining: PropTypes.number,
                status: PropTypes.string,
                handlePrimary: PropTypes.func,
                handleSecondary: PropTypes.func,
@@ -682,8 +690,7 @@ const Map = () => {
 
        const mapDungeon = React.useCallback(dungeon => {
                const definition = dungeons.find(d => d.id === dungeon.id);
-               const cleared = state[`${dungeon.id}-checks`] || 0;
-               const total = definition.items;
+               const remaining = getDungeonRemainingItems(state, definition);
                let status = 'available';
                if (isDungeonCleared(state, definition)) {
                        status = 'cleared';
@@ -691,65 +698,74 @@ const Map = () => {
                return {
                        ...dungeon,
                        status,
-                       cleared,
-                       total,
+                       remaining,
                        handlePrimary: () => {
-                               if (['ct', 'gt'].includes(dungeon.id) && cleared === total) {
-                                       if (hasDungeonBoss(state, dungeon)) {
-                                               // reset
-                                               setState(s => ({
-                                                       ...s,
-                                                       [`${dungeon.id}-checks`]: 0,
-                                                       [`${dungeon.id}-boss-defeated`]: false,
-                                               }));
-                                       } else {
-                                               setState(toggleBoolean(`${dungeon.id}-boss-defeated`));
+                               if (getDungeonRemainingItems(state, definition)) {
+                                       setState(addDungeonCheck(definition));
+                               } else if (
+                                       !hasDungeonBoss(state, definition) || !hasDungeonPrize(state, definition)
+                               ) {
+                                       if (definition.boss) {
+                                               setState(setBossDefeated(definition, true));
+                                       }
+                                       if (definition.prize) {
+                                               setState(setPrizeAcquired(definition, true));
                                        }
                                } else {
-                                       setState(increment(`${dungeon.id}-checks`, total));
+                                       setState(resetDungeonChecks(definition));
+                                       if (definition.boss) {
+                                               setState(setBossDefeated(definition, false));
+                                       }
+                                       if (definition.prize) {
+                                               setState(setPrizeAcquired(definition, false));
+                                       }
                                }
                        },
                        handleSecondary: () => {
-                               if (['ct', 'gt'].includes(dungeon.id) &&
-                                       (hasDungeonBoss(state, dungeon) || !cleared)
-                               ) {
-                                       if (hasDungeonBoss(state, dungeon)) {
-                                               setState(toggleBoolean(`${dungeon.id}-boss-defeated`));
-                                       } else {
-                                               setState(s => ({
-                                                       ...s,
-                                                       [`${dungeon.id}-checks`]: total,
-                                                       [`${dungeon.id}-boss-defeated`]: true,
-                                               }));
+                               if (isDungeonCleared(state, definition)) {
+                                       if (definition.items) {
+                                               setState(removeDungeonCheck(definition));
                                        }
+                                       if (definition.boss) {
+                                               setState(setBossDefeated(definition, false));
+                                       }
+                                       if (definition.prize) {
+                                               setState(setPrizeAcquired(definition, false));
+                                       }
+                               } else if (getDungeonClearedItems(state, definition)) {
+                                       setState(removeDungeonCheck(definition));
                                } else {
-                                       setState(decrement(`${dungeon.id}-checks`, total));
+                                       setState(completeDungeonChecks(definition));
+                                       if (definition.boss) {
+                                               setState(setBossDefeated(definition, true));
+                                       }
+                                       if (definition.prize) {
+                                               setState(setPrizeAcquired(definition, true));
+                                       }
                                }
                        },
                };
        }, [dungeons, setState, state]);
 
        const mapLocation = React.useCallback(loc => {
-               const cleared = loc.checks.reduce((acc, cur) => state[cur] ? acc + 1 : acc, 0);
-               const total = loc.checks.length;
+               const remaining = countRemainingLocations(state, loc.checks);
                let status = 'available';
-               if (cleared === total) {
+               if (hasClearedLocations(state, loc.checks)) {
                        status = 'cleared';
                }
                return {
                        ...loc,
-                       cleared,
-                       total,
+                       remaining,
                        status,
                        handlePrimary: () => {
-                               if (cleared < total) {
+                               if (remaining) {
                                        setState(clearAll(loc.checks));
                                } else {
                                        setState(unclearAll(loc.checks));
                                }
                        },
                        handleSecondary: () => {
-                               if (cleared < total) {
+                               if (remaining) {
                                        setState(clearAll(loc.checks));
                                } else {
                                        setState(unclearAll(loc.checks));