accounts for permanent resources

This commit is contained in:
2022-08-05 20:28:07 -05:00
parent e0ad965ac2
commit 9c067172bf
4 changed files with 19 additions and 28 deletions

View File

@@ -2,10 +2,11 @@ import { expensiveCard, midGameCardOne, midGameCardTwo, midGameState, mockPlayer
import { buyCard, tooExpensive } from './buyCardActions'; import { buyCard, tooExpensive } from './buyCardActions';
import getTotalBuyingPower from '../../../util/getTotalBuyingPower'; import getTotalBuyingPower from '../../../util/getTotalBuyingPower';
import { useCurrentPlayer } from '../../../util/useCurrentPlayer'; 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 { test, expect, describe, vi, afterEach } from 'vitest';
import { renderHook } from "@testing-library/react"; import { renderHook } from "@testing-library/react";
import React, { useState } from 'react'; import React, { useState } from 'react';
import { turnOrderUtil } from '../../../util/turnOrderUtil';
afterEach(() => { afterEach(() => {
vi.restoreAllMocks(); vi.restoreAllMocks();
@@ -37,7 +38,7 @@ describe('buy cards', () => {
] ]
} }
const totalBuyingPower = getTotalBuyingPower(modifiedState); const totalBuyingPower = getTotalBuyingPower(mockPlayerOne);
const expectedValue = { const expectedValue = {
ruby: 3, ruby: 3,
@@ -50,25 +51,6 @@ describe('buy cards', () => {
expect(totalBuyingPower).toStrictEqual(expectedValue); 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', () => {}) // describe('get chips', () => {})

View File

@@ -2,12 +2,13 @@ import { turnOrderUtil } from "../../../util/turnOrderUtil";
import { AppState, CardData, ResourceCost, setStateType } from "../../../util/types"; import { AppState, CardData, ResourceCost, setStateType } from "../../../util/types";
import { useCurrentPlayer } from "../../../util/useCurrentPlayer"; import { useCurrentPlayer } from "../../../util/useCurrentPlayer";
import getTotalBuyingPower from "../../../util/getTotalBuyingPower"; import getTotalBuyingPower from "../../../util/getTotalBuyingPower";
import { initialActions } from "../../../util/stateSetters";
export const tooExpensive = (card: CardData, state: AppState): boolean => { export const tooExpensive = (card: CardData, state: AppState): boolean => {
const currentPlayer = useCurrentPlayer(state); const currentPlayer = useCurrentPlayer(state);
if (!currentPlayer) return true; if (!currentPlayer) return true;
for (let [gemType, cost] of Object.entries(card.resourceCost)) { 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)) { for (let [heldResource, quantity] of Object.entries(totalBuyingPower)) {
if (gemType === heldResource && quantity < cost) { if (gemType === heldResource && quantity < cost) {
return true; return true;
@@ -28,6 +29,7 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData)
const updatedPlayer = newPlayers[idx]; const updatedPlayer = newPlayers[idx];
const cardCost = card.resourceCost; const cardCost = card.resourceCost;
const playerBuyingPower = getTotalBuyingPower(currentPlayer);
const newPlayerInventory = updatedPlayer.inventory; const newPlayerInventory = updatedPlayer.inventory;
const newResourcePool = prev.gameboard.tradingResources; const newResourcePool = prev.gameboard.tradingResources;
@@ -40,10 +42,14 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData)
if (!adjustedCost || !adjustedInventoryValue || !adjustedResourcePoolValue) continue; if (!adjustedCost || !adjustedInventoryValue || !adjustedResourcePoolValue) continue;
const buyingPowerDifference = playerBuyingPower[typedKey] - adjustedInventoryValue;
adjustedCost -= buyingPowerDifference;
while (adjustedCost > 0) { while (adjustedCost > 0) {
adjustedCost--;
adjustedInventoryValue--; adjustedInventoryValue--;
adjustedResourcePoolValue++; adjustedResourcePoolValue++;
adjustedCost--;
} }
newPlayerInventory[typedKey] = adjustedInventoryValue; newPlayerInventory[typedKey] = adjustedInventoryValue;
@@ -58,6 +64,7 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData)
...prev, ...prev,
players: newPlayers, players: newPlayers,
round: (roundIncrement ? prev.round + 1 : prev.round), round: (roundIncrement ? prev.round + 1 : prev.round),
actions: initialActions,
gameboard: { gameboard: {
...prev.gameboard, ...prev.gameboard,
tradingResources: newResourcePool tradingResources: newResourcePool

View File

@@ -27,6 +27,11 @@ export default function Player({ player, state, setState }: PlayerProps) {
<div key={v4()} className="mini-card" style={{backgroundColor: 'white'}}> <div key={v4()} className="mini-card" style={{backgroundColor: 'white'}}>
<p>{data.gemValue} card</p> <p>{data.gemValue} card</p>
<p>{data.points + " points" || null}</p> <p>{data.points + " points" || null}</p>
{
Object.entries(data.resourceCost).map(([key, value]) => {
return value > 0 && <p key={v4()}>{key}: {value}</p>
})
}
</div> </div>
) )
}) })

View File

@@ -1,9 +1,6 @@
import { AppState, CardData, ResourceCost } from "./types"; import { PlayerData, ResourceCost } from "./types";
import { useCurrentPlayer } from "./useCurrentPlayer";
export default function getTotalBuyingPower(state: AppState) { export default function getTotalBuyingPower(currentPlayer: PlayerData) {
const currentPlayer = useCurrentPlayer(state);
let totalBuyingPower = { let totalBuyingPower = {
ruby: 0, ruby: 0,
sapphire: 0, sapphire: 0,