diff --git a/src/App.tsx b/src/App.tsx index 6534161..a3c6355 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,9 @@ -import { useState } from 'react' import { BrowserRouter, Routes, Route } from 'react-router-dom' +import { initialState } from './util/stateSetters'; +import { useState } from 'react' + import Gameboard from './components/Gameboard/Gameboard' import GameConstructor from './components/GameConstructor'; -import { initialState } from './util/stateSetters'; import './App.css' function App() { @@ -13,7 +14,7 @@ function App() {

SPLENDOR

- {/* @ts-ignore */} + {/* @ts-ignore */}a } /> {/* @ts-ignore */} } /> diff --git a/src/components/Card/Card.tsx b/src/components/Card/Card.tsx index 582fcc2..339475a 100644 --- a/src/components/Card/Card.tsx +++ b/src/components/Card/Card.tsx @@ -1,6 +1,7 @@ import { v4 } from 'uuid'; import { CardData, StateProps } from '../../util/types'; -import { tooExpensive, buyCard } from '../Player/ActionMethods'; +import { buyCardActions } from '../Player/ActionMethods'; +const { buyCard, tooExpensive } = buyCardActions; interface CardProps extends StateProps { data: CardData diff --git a/src/components/Gameboard/Gameboard.tsx b/src/components/Gameboard/Gameboard.tsx index b2e198a..7e8a4e6 100644 --- a/src/components/Gameboard/Gameboard.tsx +++ b/src/components/Gameboard/Gameboard.tsx @@ -2,14 +2,15 @@ import { AppState, PlayerData, ResourceCost, SetActionType, StateProps } from '../../util/types'; import { setStateBuyCard, setStateGetChips, setStateReserveCard } from '../../util/stateSetters'; import { useCallback, useEffect, useState } from 'react'; -import Nobles from './Nobles'; +import { getChipsActions } from '../Player/ActionMethods'; +const { validateChips } = getChipsActions; // components +import Nobles from './Nobles'; 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'; import SelectionView from '../Resources/SelectionView'; export default function Gameboard({ state, setState }: StateProps) { diff --git a/src/components/Player/ActionMethods.ts b/src/components/Player/ActionMethods.ts deleted file mode 100644 index b06cc57..0000000 --- a/src/components/Player/ActionMethods.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { AppState, CardData, PlayerData, setStateType } from "../../util/types"; -import { turnOrderUtil } from "../../util/turnOrderUtil"; -import { initialActions } from "../../util/stateSetters"; -import { useCurrentPlayer } from "../../util/useCurrentPlayer"; - -// GET CHIPS ACTION HANDLERS -export const validateChips = (state: AppState): boolean => { - if (!state.actions.getChips.active || !state.actions.getChips.selection) return false; - - const selection = state.actions.getChips.selection; - - if (selection.length === 0 || selection.length > 3) return false; - const unique = new Set(selection); - - if (selection.length === 3 && selection.length > unique.size) return false; - - let globalResourceCopy = { ...state.gameboard.tradingResources } - - for (let item of selection) { - for (let key of Object.keys(globalResourceCopy)) { - if (item === key) { - globalResourceCopy[key] -= 1; - } - } - } - - for (let value of Object.values(globalResourceCopy)) { - if (value < 0) return false; - } - - return true; -} - -export const getChips = (state: AppState, setState: setStateType) => { - let targetPlayer = useCurrentPlayer(state); - - setState((prev) => { - if (!targetPlayer) return prev; - - const { newPlayers, roundIncrement } = turnOrderUtil(state, targetPlayer); - const idx = newPlayers.indexOf(targetPlayer); - const resources = state.actions.getChips.selection; - let newResources = prev.gameboard.tradingResources; - - if (resources) { - for (let value of resources) { - - // update player inventory - for (let each in newPlayers[idx].inventory) { - if (value === each) { - newPlayers[idx].inventory[value] += 1; - } - } - - // update globally available resources - for (let each in newResources) { - if (value === each) { - newResources[value] -= 1; - } - } - } - } - - return { - ...prev, - round: (roundIncrement ? prev.round + 1 : prev.round), - gameboard: { - ...prev.gameboard, - tradingResources: newResources - }, - players: newPlayers, - actions: initialActions - } - }) -} - -// BUY CARDS ACTION HANDLERS -export const tooExpensive = (card: CardData, state: AppState): boolean => { - const currentPlayer = useCurrentPlayer(state); - if (!currentPlayer) return true; - - for (let [gemType, cost] of Object.entries(card.resourceCost)) { - for (let [heldResource, quantity] of Object.entries(currentPlayer.inventory)) { - if (gemType === heldResource && quantity < cost) { - return true; - } - } - } - - return false; -} - -export const buyCard = (card: CardData, state: AppState, setState: setStateType) => { - /** - * functionality: adds target card's data to current player's collection of cards, - * removes the card from the active state of the gameboard, replaces it with - * a new card in the correct tier, and runs turn order utility - * - * @param card -> the target card, @param state -> current app state - */ - - const currentPlayer = useCurrentPlayer(state); - if (!currentPlayer) return; - - setState((prev: AppState) => { - let updatedPlayer = { - ...currentPlayer, - cards: [ - ...currentPlayer.cards, - card - ] - } - - return { - ...prev - } - }) -} - -export const reserveCard = () => { - return; -} \ No newline at end of file diff --git a/src/components/Player/ActionMethods/buyCardActions.ts b/src/components/Player/ActionMethods/buyCardActions.ts new file mode 100644 index 0000000..cfbea74 --- /dev/null +++ b/src/components/Player/ActionMethods/buyCardActions.ts @@ -0,0 +1,44 @@ +import { AppState, CardData, setStateType } from "../../../util/types"; +import { useCurrentPlayer } from "../../../util/useCurrentPlayer"; + +export const tooExpensive = (card: CardData, state: AppState): boolean => { + const currentPlayer = useCurrentPlayer(state); + if (!currentPlayer) return true; + + for (let [gemType, cost] of Object.entries(card.resourceCost)) { + for (let [heldResource, quantity] of Object.entries(currentPlayer.inventory)) { + if (gemType === heldResource && quantity < cost) { + return true; + } + } + } + + return false; +} + +export const buyCard = (card: CardData, state: AppState, setState: setStateType) => { + /** + * functionality: adds target card's data to current player's collection of cards, + * removes the card from the active state of the gameboard, replaces it with + * a new card in the correct tier, and runs turn order utility + * + * @param card -> the target card, @param state -> current app state + */ + + const currentPlayer = useCurrentPlayer(state); + if (!currentPlayer) return; + + setState((prev: AppState) => { + let updatedPlayer = { + ...currentPlayer, + cards: [ + ...currentPlayer.cards, + card + ] + } + + return { + ...prev + } + }) +} \ No newline at end of file diff --git a/src/components/Player/ActionMethods/buyCardsActions.ts b/src/components/Player/ActionMethods/buyCardsActions.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/components/Player/ActionMethods/getChipsActions.ts b/src/components/Player/ActionMethods/getChipsActions.ts index a0ed07a..15717f8 100644 --- a/src/components/Player/ActionMethods/getChipsActions.ts +++ b/src/components/Player/ActionMethods/getChipsActions.ts @@ -1,5 +1,6 @@ import { AppState, setStateType } from '../../../util/types'; import { useCurrentPlayer } from '../../../util/useCurrentPlayer'; +// @ts-ignore import { turnOrderUtil } from '../../../util/turnOrderUtil'; import { initialActions } from "../../../util/stateSetters"; diff --git a/src/components/Player/ActionMethods/index.ts b/src/components/Player/ActionMethods/index.ts index e69de29..71855ea 100644 --- a/src/components/Player/ActionMethods/index.ts +++ b/src/components/Player/ActionMethods/index.ts @@ -0,0 +1,3 @@ +export * as getChipsActions from './getChipsActions'; +export * as buyCardActions from './buyCardActions'; +export * as reserveCardActions from './reserveCardActions'; \ No newline at end of file diff --git a/src/components/Player/ActionMethods/reserveCardActions.ts b/src/components/Player/ActionMethods/reserveCardActions.ts new file mode 100644 index 0000000..cd01c2f --- /dev/null +++ b/src/components/Player/ActionMethods/reserveCardActions.ts @@ -0,0 +1,3 @@ +export const reserveCard = () => { + return; +} \ No newline at end of file diff --git a/src/components/Resources/AvailableChips.tsx b/src/components/Resources/AvailableChips.tsx index 7a0d9bc..3106d8a 100644 --- a/src/components/Resources/AvailableChips.tsx +++ b/src/components/Resources/AvailableChips.tsx @@ -3,7 +3,7 @@ import { useEffect } from "react"; import { v4 } from "uuid"; import "./AvailableChips.css" import { setStateGetChips } from "../../util/stateSetters"; -import { validateChips } from "../Player/ActionMethods"; +// import { validateChips } from "../Player/ActionMethods"; interface ResourceProps extends StateProps { liftSelection: (value: keyof ResourceCost) => void diff --git a/src/components/Resources/SelectionView.tsx b/src/components/Resources/SelectionView.tsx index c359a35..20a7522 100644 --- a/src/components/Resources/SelectionView.tsx +++ b/src/components/Resources/SelectionView.tsx @@ -30,9 +30,5 @@ export default function SelectionView({ state, setState }: StateProps) { }) }, [state]) - return ( -
- { view } -
- ) + return view } \ No newline at end of file diff --git a/src/components/Resources/ViewHTML.tsx b/src/components/Resources/ViewHTML.tsx index d42a3f4..f424c9d 100644 --- a/src/components/Resources/ViewHTML.tsx +++ b/src/components/Resources/ViewHTML.tsx @@ -2,7 +2,8 @@ import { v4 } from "uuid"; import { useEffect, useState } from "react"; import { setStateGetChips } from "../../util/stateSetters"; import { ResourceCost, StateProps } from "../../util/types"; -import { getChips } from "../Player/ActionMethods"; +import { getChipsActions } from "../Player/ActionMethods"; +const { getChips } = getChipsActions; export const GetChipsHTML = ({ state, setState }: StateProps) => { const [prompt, setPrompt] = useState(""); diff --git a/src/util/TurnOrderUtil.ts b/src/util/TurnOrderUtil.ts index 64af43f..f895ec8 100644 --- a/src/util/TurnOrderUtil.ts +++ b/src/util/TurnOrderUtil.ts @@ -5,7 +5,6 @@ export const turnOrderUtil = (prev: AppState, dynamic: PlayerData) => { const newPlayers = prev.players; for (let each of newPlayers) { - console.log(each); if (each.id === dynamic.id) { each.turnActive = false; } else if (each.id === dynamic.id + 1) { diff --git a/src/util/index.ts b/src/util/index.ts deleted file mode 100644 index 66c32f9..0000000 --- a/src/util/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -import initializeBoard from './initializeBoard'; -import * as stateSetters from './stateSetters'; -import { turnOrderUtil } from './turnOrderUtil'; -import * as APPTYPES from './types'; -import { useCurrentPlayer } from './useCurrentPlayer'; - -export default class UTIL { - initializeBoard = initializeBoard - turnOrderUtil = turnOrderUtil - useCurrentPlayer = useCurrentPlayer - stateSetters = stateSetters - APPTYPES = APPTYPES -} \ No newline at end of file