diff --git a/src/components/Player/ActionMethods/buyCard.test.tsx b/src/components/Player/ActionMethods/buyCard.test.tsx index ff8ef30..d838987 100644 --- a/src/components/Player/ActionMethods/buyCard.test.tsx +++ b/src/components/Player/ActionMethods/buyCard.test.tsx @@ -2,10 +2,11 @@ import { expensiveCard, midGameCardOne, midGameCardTwo, midGameState, mockPlayer import { buyCard, tooExpensive } from './buyCardActions'; import getTotalBuyingPower from '../../../util/getTotalBuyingPower'; import { useCurrentPlayer } from '../../../util/useCurrentPlayer'; -import { AppState, PlayerData } from '../../../util/types'; +import { AppState, CardData, PlayerData, ResourceCost } from '../../../util/types'; import { test, expect, describe, vi, afterEach } from 'vitest'; import { renderHook } from "@testing-library/react"; import React, { useState } from 'react'; +import { turnOrderUtil } from '../../../util/turnOrderUtil'; afterEach(() => { vi.restoreAllMocks(); @@ -37,7 +38,7 @@ describe('buy cards', () => { ] } - const totalBuyingPower = getTotalBuyingPower(modifiedState); + const totalBuyingPower = getTotalBuyingPower(mockPlayerOne); const expectedValue = { ruby: 3, @@ -50,25 +51,6 @@ describe('buy cards', () => { expect(totalBuyingPower).toStrictEqual(expectedValue); }) - - test('use state', () => { - const { result } = renderHook(() => { - const [state, setState] = useState('me'); - setState('you'); - return state; - }) - - expect(result.current).toBe('you'); - }) - - test('buyCard and updateResources', () => { - /** - * actions in test: - * player triggers "buy card" action - * corresponding chips come out of player's hand - * - */ - }) }) // describe('get chips', () => {}) diff --git a/src/components/Player/ActionMethods/buyCardActions.ts b/src/components/Player/ActionMethods/buyCardActions.ts index ac4a920..11f47b5 100644 --- a/src/components/Player/ActionMethods/buyCardActions.ts +++ b/src/components/Player/ActionMethods/buyCardActions.ts @@ -2,12 +2,13 @@ import { turnOrderUtil } from "../../../util/turnOrderUtil"; import { AppState, CardData, ResourceCost, setStateType } from "../../../util/types"; import { useCurrentPlayer } from "../../../util/useCurrentPlayer"; import getTotalBuyingPower from "../../../util/getTotalBuyingPower"; +import { initialActions } from "../../../util/stateSetters"; 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)) { - let totalBuyingPower = getTotalBuyingPower(state); + let totalBuyingPower = getTotalBuyingPower(currentPlayer); for (let [heldResource, quantity] of Object.entries(totalBuyingPower)) { if (gemType === heldResource && quantity < cost) { return true; @@ -28,6 +29,7 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData) const updatedPlayer = newPlayers[idx]; const cardCost = card.resourceCost; + const playerBuyingPower = getTotalBuyingPower(currentPlayer); const newPlayerInventory = updatedPlayer.inventory; const newResourcePool = prev.gameboard.tradingResources; @@ -40,10 +42,14 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData) if (!adjustedCost || !adjustedInventoryValue || !adjustedResourcePoolValue) continue; + const buyingPowerDifference = playerBuyingPower[typedKey] - adjustedInventoryValue; + adjustedCost -= buyingPowerDifference; + while (adjustedCost > 0) { - adjustedCost--; adjustedInventoryValue--; adjustedResourcePoolValue++; + + adjustedCost--; } newPlayerInventory[typedKey] = adjustedInventoryValue; @@ -58,6 +64,7 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData) ...prev, players: newPlayers, round: (roundIncrement ? prev.round + 1 : prev.round), + actions: initialActions, gameboard: { ...prev.gameboard, tradingResources: newResourcePool diff --git a/src/components/Player/Player.tsx b/src/components/Player/Player.tsx index 84ec0d6..ccd0629 100644 --- a/src/components/Player/Player.tsx +++ b/src/components/Player/Player.tsx @@ -27,6 +27,11 @@ export default function Player({ player, state, setState }: PlayerProps) {

{data.gemValue} card

{data.points + " points" || null}

+ { + Object.entries(data.resourceCost).map(([key, value]) => { + return value > 0 &&

{key}: {value}

+ }) + }
) }) diff --git a/src/util/getTotalBuyingPower.ts b/src/util/getTotalBuyingPower.ts index eba6519..34575e4 100644 --- a/src/util/getTotalBuyingPower.ts +++ b/src/util/getTotalBuyingPower.ts @@ -1,9 +1,6 @@ -import { AppState, CardData, ResourceCost } from "./types"; -import { useCurrentPlayer } from "./useCurrentPlayer"; +import { PlayerData, ResourceCost } from "./types"; -export default function getTotalBuyingPower(state: AppState) { - const currentPlayer = useCurrentPlayer(state); - +export default function getTotalBuyingPower(currentPlayer: PlayerData) { let totalBuyingPower = { ruby: 0, sapphire: 0,