to do: rendering problem after buy card, remove purchased card from gameboard

This commit is contained in:
2022-08-05 18:55:07 -05:00
parent 9abd8b2667
commit c7138038fa
5 changed files with 157 additions and 205 deletions

View File

@@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { v4 } from 'uuid'; import { v4 } from 'uuid';
import { CardProps } from '../../util/propTypes'; import { CardProps } from '../../util/propTypes';
import { ResourceCost } from '../../util/types'; import { ResourceCost } from '../../util/types';

View File

@@ -1,141 +0,0 @@
import { expensiveCard, midGameCardOne, midGameCardTwo, midGameState, mockPlayerOne, mockPlayerTwo, mockState, playerTwoMidGame } from '../../../util/testUtils';
import { buyCard, tooExpensive } from './buyCardActions';
import getTotalBuyingPower from '../../../util/getTotalBuyingPower';
import { useCurrentPlayer } from '../../../util/useCurrentPlayer';
import { AppState, PlayerData } from '../../../util/types';
import { test, expect, describe } from 'vitest';
import { renderHook } from "@testing-library/react";
import { useState } from 'react';
import { initialState } from '../../../util/stateSetters';
describe('buy cards', () => {
test('detects unaffordable cards', () => {
const result = tooExpensive(expensiveCard, mockState);
expect(result).toBeTruthy();
})
test('calculates total buying power', () => {
let modifiedState = {
...mockState,
players: [
{
...mockPlayerOne,
inventory: {
ruby: 3,
sapphire: 3,
emerald: 3,
onyx: 3,
diamond: 3,
gold: 3
},
cards: [expensiveCard]
},
mockPlayerTwo
]
}
const totalBuyingPower = getTotalBuyingPower(modifiedState);
const expectedValue = {
ruby: 3,
sapphire: 3,
emerald: 3,
onyx: 3,
diamond: 4,
gold: 3
}
expect(totalBuyingPower).toStrictEqual(expectedValue);
})
test('buyCard and updateResources', () => {
const { result } = renderHook(() => {
const [state, setState] = useState(midGameState);
return { state, setState }
})
const { state, setState } = result.current;
const currentPlayer = useCurrentPlayer(state);
if (!currentPlayer) return;
/**
* actions in test:
* midGameState :: playerOneMidGame, playerTwoMidGame
* playerOneMidGame => buy midGameCardTwo
* playerTwoMidGame => buy midGameCardOne
*/
let P1UPDATED = state.players.filter((p: PlayerData) => p.name === "Player One")[0];
let P2UPDATED = state.players.filter((p: PlayerData) => p.name === "Player Two")[0];
// playerOne receives midGameCardTwo and pays three diamonds back to resource pool
P1UPDATED = {
...P1UPDATED,
cards: [...P1UPDATED.cards, midGameCardTwo],
turnActive: false,
inventory: {
...P1UPDATED.inventory,
diamond: 0
}
}
P2UPDATED = { ...P2UPDATED, turnActive: true }
const moveOneExpectedState: AppState = {
...state,
players: [P1UPDATED, P2UPDATED],
gameboard: {
...state.gameboard,
tradingResources: {
...state.gameboard.tradingResources,
diamond: 4
}
}
}
// first player action
// @ts-ignore
const newState = buyCard(state, setState, midGameCardTwo);
expect(newState).toStrictEqual(moveOneExpectedState);
// playerTwo receives midGameCardOne and pays four rubies back to resource pool
P2UPDATED = {
...P2UPDATED,
cards: [...P2UPDATED.cards, midGameCardOne],
turnActive: false,
inventory: {
...P2UPDATED.inventory,
ruby: 0
}
}
P1UPDATED = { ...P1UPDATED, turnActive: true }
const moveTwoExpectedState: AppState = {
...moveOneExpectedState,
players: [P1UPDATED, P2UPDATED],
gameboard: {
...moveOneExpectedState.gameboard,
tradingResources: {
...moveOneExpectedState.gameboard.tradingResources,
ruby: 4
}
}
}
expect(() => {
if (!newState) throw Error();
}).not.toThrowError();
if (newState) {
// @ts-ignore
const finalState = buyCard(newState, setState, midGameCardOne);
expect(finalState).toStrictEqual(moveTwoExpectedState);
}
})
})
// describe('get chips', () => {})
// describe('reserve card', () => {})

View File

@@ -0,0 +1,75 @@
import { expensiveCard, midGameCardOne, midGameCardTwo, midGameState, mockPlayerOne, mockPlayerTwo, mockState } from '../../../util/testUtils';
import { buyCard, tooExpensive } from './buyCardActions';
import getTotalBuyingPower from '../../../util/getTotalBuyingPower';
import { useCurrentPlayer } from '../../../util/useCurrentPlayer';
import { AppState, PlayerData } from '../../../util/types';
import { test, expect, describe, vi, afterEach } from 'vitest';
import { renderHook } from "@testing-library/react";
import React, { useState } from 'react';
afterEach(() => {
vi.restoreAllMocks();
})
describe('buy cards', () => {
test('detects unaffordable cards', () => {
const result = tooExpensive(expensiveCard, mockState);
expect(result).toBeTruthy();
})
test('calculates total buying power', () => {
let modifiedState = {
...mockState,
players: [
{
...mockPlayerOne,
inventory: {
ruby: 3,
sapphire: 3,
emerald: 3,
onyx: 3,
diamond: 3,
gold: 3
},
cards: [expensiveCard]
},
mockPlayerTwo
]
}
const totalBuyingPower = getTotalBuyingPower(modifiedState);
const expectedValue = {
ruby: 3,
sapphire: 3,
emerald: 3,
onyx: 3,
diamond: 4,
gold: 3
}
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('reserve card', () => {})

View File

@@ -4,31 +4,6 @@ import { AppState, CardData, ResourceCost, setStateType } from "../../../util/ty
import { useCurrentPlayer } from "../../../util/useCurrentPlayer"; import { useCurrentPlayer } from "../../../util/useCurrentPlayer";
import getTotalBuyingPower from "../../../util/getTotalBuyingPower"; import getTotalBuyingPower from "../../../util/getTotalBuyingPower";
// export const _getTotalBuyingPower = (state: AppState) => {
// const currentPlayer = useCurrentPlayer(state);
// let totalBuyingPower = {
// ruby: 0,
// sapphire: 0,
// emerald: 0,
// diamond: 0,
// onyx: 0,
// gold: 0,
// }
// if (!currentPlayer) return totalBuyingPower;
// for (let [key,quantity] of Object.entries(currentPlayer.inventory)) {
// totalBuyingPower[key as keyof ResourceCost] += quantity;
// }
// for (let each of currentPlayer.cards) {
// totalBuyingPower[each.gemValue as keyof ResourceCost] += 1;
// }
// return totalBuyingPower;
// }
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;
@@ -44,51 +19,94 @@ export const tooExpensive = (card: CardData, state: AppState): boolean => {
return false; return false;
} }
export const updateResources = (state: AppState, card: CardData) => { export const buyCard = (state: AppState, setState: setStateType, card: CardData) => {
console.log('updateResources called'); const currentPlayer = useCurrentPlayer(state);
let currentPlayer = useCurrentPlayer(state); if (!currentPlayer) return;
let newTradingResources = state.gameboard.tradingResources;
let updatedPlayer = currentPlayer;
const totalBuyingPower = getTotalBuyingPower(state);
let difference = 0; setState(() => {
for (let [key, value] of Object.entries(card.resourceCost)) { const { newPlayers, roundIncrement } = turnOrderUtil(state, currentPlayer);
if (value < 1) continue; const idx = newPlayers.indexOf(currentPlayer);
if (value !== totalBuyingPower[key as keyof ResourceCost]) { const updatedPlayer = newPlayers[idx];
difference += Math.abs(totalBuyingPower[key as keyof ResourceCost] - value);
const cardCost = card.resourceCost;
const newPlayerInventory = updatedPlayer.inventory;
const newResourcePool = state.gameboard.tradingResources;
for (let key of Object.keys(cardCost)) {
const typedKey = key as keyof ResourceCost;
let adjustedCost = cardCost[typedKey];
let adjustedInventoryValue = newPlayerInventory[typedKey];
let adjustedResourcePoolValue = newResourcePool[typedKey];
if (!adjustedCost || !adjustedInventoryValue || !adjustedResourcePoolValue) continue;
while (adjustedCost > 0) {
adjustedCost--;
adjustedInventoryValue--;
adjustedResourcePoolValue++;
}
newPlayerInventory[typedKey] = adjustedInventoryValue;
newResourcePool[typedKey] = adjustedResourcePoolValue;
} }
}
return { newTradingResources, updatedPlayer } updatedPlayer.inventory = newPlayerInventory;
newPlayers[idx] = updatedPlayer;
state.gameboard.tradingResources = newResourcePool;
roundIncrement && (state.round = state.round + 1);
state.players = newPlayers;
return state;
})
} }
export const buyCard = (state: AppState, setState: setStateType, card: CardData) => { // export const updateResources = (state: AppState, card: CardData) => {
let currentPlayer = useCurrentPlayer(state); // console.log('updateResources called');
if (!currentPlayer) return; // let currentPlayer = useCurrentPlayer(state);
const { newPlayers, roundIncrement } = turnOrderUtil(state, currentPlayer); // let newTradingResources = state.gameboard.tradingResources;
// let updatedPlayer = currentPlayer;
// const totalBuyingPower = getTotalBuyingPower(state);
console.log('cleared to setstate'); // let difference = 0;
// for (let [key, value] of Object.entries(card.resourceCost)) {
// if (value < 1) continue;
// if (value !== totalBuyingPower[key as keyof ResourceCost]) {
// difference += Math.abs(totalBuyingPower[key as keyof ResourceCost] - value);
// }
// }
// return { newTradingResources, updatedPlayer }
// }
// export const buyCard = (state: AppState, setState: setStateType, card: CardData) => {
// let currentPlayer = useCurrentPlayer(state);
// if (!currentPlayer) return;
// const { newPlayers, roundIncrement } = turnOrderUtil(state, currentPlayer);
// console.log('cleared to setstate');
setState((prev: AppState) => { // setState((prev: AppState) => {
if (!currentPlayer) return prev; // if (!currentPlayer) return prev;
const { newTradingResources, updatedPlayer } = updateResources(state, card); // const { newTradingResources, updatedPlayer } = updateResources(state, card);
const idx = newPlayers.indexOf(currentPlayer); // const idx = newPlayers.indexOf(currentPlayer);
updatedPlayer && (newPlayers[idx] = updatedPlayer); // updatedPlayer && (newPlayers[idx] = updatedPlayer);
return { // return {
...prev, // ...prev,
gameboard: { // gameboard: {
...prev.gameboard, // ...prev.gameboard,
cardRows: prev.gameboard.cardRows, // cardRows: prev.gameboard.cardRows,
tradingResources: newTradingResources // tradingResources: newTradingResources
}, // },
round: (roundIncrement ? prev.round + 1 : prev.round), // round: (roundIncrement ? prev.round + 1 : prev.round),
players: newPlayers, // players: newPlayers,
actions: initialActions // actions: initialActions
} // }
}) // })
// for testing? // // for testing?
return state; // return state;
} // }

View File

@@ -33,7 +33,6 @@ const setNobles = (state: AppState, setState: setStateType) => {
const setResources = (state: AppState) => { const setResources = (state: AppState) => {
let newResources = state.gameboard.tradingResources; let newResources = state.gameboard.tradingResources;
console.log(state.players.length);
switch (state.players.length) { switch (state.players.length) {
case 2: case 2:
for (let [key, value] of Object.entries(newResources)) { for (let [key, value] of Object.entries(newResources)) {