accounts for permanent resources
This commit is contained in:
@@ -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', () => {})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -27,6 +27,11 @@ export default function Player({ player, state, setState }: PlayerProps) {
|
||||
<div key={v4()} className="mini-card" style={{backgroundColor: 'white'}}>
|
||||
<p>{data.gemValue} card</p>
|
||||
<p>{data.points + " points" || null}</p>
|
||||
{
|
||||
Object.entries(data.resourceCost).map(([key, value]) => {
|
||||
return value > 0 && <p key={v4()}>{key}: {value}</p>
|
||||
})
|
||||
}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { AppState, CardData, ResourceCost } from "./types";
|
||||
import { useCurrentPlayer } from "./useCurrentPlayer";
|
||||
|
||||
export default function getTotalBuyingPower(state: AppState) {
|
||||
const currentPlayer = useCurrentPlayer(state);
|
||||
import { PlayerData, ResourceCost } from "./types";
|
||||
|
||||
export default function getTotalBuyingPower(currentPlayer: PlayerData) {
|
||||
let totalBuyingPower = {
|
||||
ruby: 0,
|
||||
sapphire: 0,
|
||||
|
||||
Reference in New Issue
Block a user