diff --git a/src/components/Card/Card.tsx b/src/components/Card/Card.tsx index 8cec869..582fcc2 100644 --- a/src/components/Card/Card.tsx +++ b/src/components/Card/Card.tsx @@ -1,6 +1,6 @@ import { v4 } from 'uuid'; import { CardData, StateProps } from '../../util/types'; -import { tooExpensive } from '../Player/ActionMethods'; +import { tooExpensive, buyCard } from '../Player/ActionMethods'; interface CardProps extends StateProps { data: CardData @@ -19,7 +19,14 @@ export default function Card({ data, state, setState }: CardProps) { return (data.resourceCost[key] > 0) &&
{key}: {data.resourceCost[key]}
}) } - { state.actions.buyCard.active && } + { state.actions.buyCard.active && + + } ) diff --git a/src/components/Player/ActionMethods.ts b/src/components/Player/ActionMethods.ts index 3a73a5f..b06cc57 100644 --- a/src/components/Player/ActionMethods.ts +++ b/src/components/Player/ActionMethods.ts @@ -1,5 +1,5 @@ import { AppState, CardData, PlayerData, setStateType } from "../../util/types"; -import { turnOrderUtil } from "../../util/TurnOrderUtil"; +import { turnOrderUtil } from "../../util/turnOrderUtil"; import { initialActions } from "../../util/stateSetters"; import { useCurrentPlayer } from "../../util/useCurrentPlayer"; @@ -90,8 +90,31 @@ export const tooExpensive = (card: CardData, state: AppState): boolean => { return false; } -export const buyCard = () => { - return; +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 = () => { diff --git a/src/components/Player/ActionMethods/buyCardsActions.ts b/src/components/Player/ActionMethods/buyCardsActions.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/components/Player/ActionMethods/getChipsActions.ts b/src/components/Player/ActionMethods/getChipsActions.ts new file mode 100644 index 0000000..a0ed07a --- /dev/null +++ b/src/components/Player/ActionMethods/getChipsActions.ts @@ -0,0 +1,74 @@ +import { AppState, setStateType } from '../../../util/types'; +import { useCurrentPlayer } from '../../../util/useCurrentPlayer'; +import { turnOrderUtil } from '../../../util/turnOrderUtil'; +import { initialActions } from "../../../util/stateSetters"; + +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 + } + }) +} \ No newline at end of file diff --git a/src/components/Player/ActionMethods/index.ts b/src/components/Player/ActionMethods/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/util/index.ts b/src/util/index.ts new file mode 100644 index 0000000..66c32f9 --- /dev/null +++ b/src/util/index.ts @@ -0,0 +1,13 @@ +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