From f6ce03a8dcb58009e68c32cee1e46b09813482be Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sun, 24 Jul 2022 12:56:11 -0500 Subject: [PATCH] chip selection validator works as expected --- src/App.tsx | 2 +- src/{util => components}/GameConstructor.tsx | 2 +- src/components/Gameboard/Gameboard.tsx | 18 ++++++----- src/components/Player/ActionMethods.ts | 34 ++++++++++++++++++-- src/components/Player/Player.tsx | 31 ++++++++---------- src/components/Resources/AvailableChips.tsx | 7 ++++ src/util/TurnOrderUtil.ts | 2 +- 7 files changed, 64 insertions(+), 32 deletions(-) rename src/{util => components}/GameConstructor.tsx (98%) diff --git a/src/App.tsx b/src/App.tsx index b8a5d75..6534161 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,7 @@ import { useState } from 'react' import { BrowserRouter, Routes, Route } from 'react-router-dom' import Gameboard from './components/Gameboard/Gameboard' -import GameConstructor from './util/GameConstructor'; +import GameConstructor from './components/GameConstructor'; import { initialState } from './util/stateSetters'; import './App.css' diff --git a/src/util/GameConstructor.tsx b/src/components/GameConstructor.tsx similarity index 98% rename from src/util/GameConstructor.tsx rename to src/components/GameConstructor.tsx index d110c7b..1b4a3eb 100644 --- a/src/util/GameConstructor.tsx +++ b/src/components/GameConstructor.tsx @@ -1,6 +1,6 @@ import { useEffect, useState } from "react" import { useNavigate } from "react-router-dom" -import { CardData, NobleData, PlayerData, StateProps } from "./types"; +import { CardData, NobleData, PlayerData, StateProps } from "../util/types"; interface InputState { playerOne: PlayerInput diff --git a/src/components/Gameboard/Gameboard.tsx b/src/components/Gameboard/Gameboard.tsx index 16a338a..3cefb8e 100644 --- a/src/components/Gameboard/Gameboard.tsx +++ b/src/components/Gameboard/Gameboard.tsx @@ -9,6 +9,7 @@ import initializeBoard from '../../util/initializeBoard'; import AvailableChips from '../Resources/AvailableChips'; import AllPlayers from '../Player/AllPlayers'; import CardRow from '../Card/CardRow'; +import { validateChips } from '../Player/ActionMethods'; export default function Gameboard({ state, setState }: StateProps) { const [view, setView] = useState(
Loading...
); @@ -23,18 +24,24 @@ export default function Gameboard({ state, setState }: StateProps) { let newSelection = prev.actions.getChips.selection; newSelection?.push(value); - return { + let newState = { ...prev, actions: { ...state.actions, getChips: { active: true, - selection: newSelection + selection: newSelection, + valid: false } } } + + const result = validateChips(newState); + newState.actions.getChips.valid = result; + + return newState; }) - }, []); + }, [state]); const setActionState = useCallback((value: SetActionType, player: PlayerData) => { if (!player?.turnActive) return; @@ -84,11 +91,6 @@ export default function Gameboard({ state, setState }: StateProps) { } }, [state]); - // DEBUG - useEffect(() => { - console.log(state); - }, [state]) - // render return view } \ No newline at end of file diff --git a/src/components/Player/ActionMethods.ts b/src/components/Player/ActionMethods.ts index 31c03df..ebe1b8d 100644 --- a/src/components/Player/ActionMethods.ts +++ b/src/components/Player/ActionMethods.ts @@ -1,11 +1,11 @@ import { AppState, PlayerData, ResourceCost, setStateType } from "../../util/types"; -import { TurnOrderUtil } from "../../util/TurnOrderUtil"; +import { turnOrderUtil } from "../../util/TurnOrderUtil"; -export const getChips = (resource: string | ArrayIs {player.starter || "not"} round starter
{/* Dynamic data from state */} - {dynamic?.turnActive ?{prompt}
:...
} +{dynamic?.turnActive ? prompt : '...'}
diff --git a/src/components/Resources/AvailableChips.tsx b/src/components/Resources/AvailableChips.tsx index 32c0a79..fd8c441 100644 --- a/src/components/Resources/AvailableChips.tsx +++ b/src/components/Resources/AvailableChips.tsx @@ -2,6 +2,7 @@ import { ResourceCost, StateProps } from "../../util/types"; import { useEffect } from "react"; import { v4 } from "uuid"; import "./AvailableChips.css" +import { setStateGetChips } from "../../util/stateSetters"; interface ResourceProps extends StateProps { liftSelection: (value: keyof ResourceCost) => void @@ -14,6 +15,11 @@ export default function AvailableChips({ state, setState, liftSelection }: Resou return (Your selection is {state.actions.getChips.valid || "not"} valid
} + { + state.actions.getChips.active && + state.actions.getChips.selection?.map((each) =>{each}
) + } { Object.keys(state.gameboard.tradingResources).map((key: string | keyof ResourceCost) => { return ( @@ -25,6 +31,7 @@ export default function AvailableChips({ state, setState, liftSelection }: Resou ) }) } +