to do: bug, points assigned to wrong player in setStateGetNoble. trigger endgame action

This commit is contained in:
2022-08-06 19:22:35 -05:00
parent adc653abbf
commit afc3e9ac47
4 changed files with 25 additions and 54 deletions

View File

@@ -10,10 +10,6 @@ import ResumeGame from './components/ResumeGame';
function App() { function App() {
const [state, setState] = useState(initialState); const [state, setState] = useState(initialState);
useEffect(() => {
return;
}, [state]);
return ( return (
<div className="App"> <div className="App">
<h1>SPLENDOR</h1> <h1>SPLENDOR</h1>

View File

@@ -49,6 +49,12 @@ export default function Gameboard({ state, setState }: StateProps) {
useEffect(() => { useEffect(() => {
setCardRows(state); setCardRows(state);
for (let player of state.players) {
if (player.points >= 15) {
console.log('trigger endgame');
}
}
}, [state]) }, [state])
// displays state of board if data is populated, otherwise points to game constructor // displays state of board if data is populated, otherwise points to game constructor

View File

@@ -1,57 +1,18 @@
import { v4 } from "uuid"; import { v4 } from "uuid";
import { NobleData, PlayerData, ResourceCost } from "../../util/types"; import { NobleData, ResourceCost } from "../../util/types";
import { StateProps } from "../../util/propTypes"; import { StateProps } from "../../util/propTypes";
import "./Nobles.css" import "./Nobles.css"
import getTotalBuyingPower from "../../util/getTotalBuyingPower";
import { useCurrentPlayer } from "../../util/useCurrentPlayer";
import { useEffect } from "react";
export default function Nobles({ state, setState }: StateProps) { export default function Nobles({ state }: StateProps) {
const removeNoble = (noble: NobleData) => { if (!state.gameboard.nobles.length) {
console.log(noble); return (
setState((prev) => { <div className="nobles-panel">
return { <strong>NOBLES</strong>
...prev, <p>All nobles have been acquired!</p>
gameboard: { </div>
...prev.gameboard, )
nobles: prev.gameboard.nobles.filter((each) => each.nobleid !== noble.nobleid)
}
}
})
} }
const canPickUpNoble = (player: PlayerData, noble: NobleData): boolean => {
const nobleCost = noble.resourceCost;
const totalBuyingPower = getTotalBuyingPower(player);
const playerInventory = player.inventory;
for (let key of Object.keys(totalBuyingPower)) {
const typedKey = key as keyof ResourceCost;
let coinValue = playerInventory[typedKey] || 0;
if (!noble.resourceCost[typedKey]) continue;
// @ts-ignore
if ((totalBuyingPower[typedKey] - coinValue) >= noble.resourceCost[typedKey]) {
continue;
} else {
return false;
}
}
return true;
}
// useEffect(() => {
// const currentPlayer = useCurrentPlayer(state);
// if (!currentPlayer) return;
// for (let each of state.gameboard.nobles) {
// console.log(`${currentPlayer.name} can pick up noble ${state.gameboard.nobles.indexOf(each) + 1}? ${canPickUpNoble(currentPlayer, each) ? "yes" : "no"}`)
// }
// }, [state])
return ( return (
<div className="nobles-panel"> <div className="nobles-panel">
<strong>NOBLES</strong> <strong>NOBLES</strong>

View File

@@ -2,7 +2,8 @@ import { turnOrderUtil } from "../../../util/turnOrderUtil";
import { AppState, CardData, FullDeck, ResourceCost, setStateType } from "../../../util/types"; import { AppState, CardData, FullDeck, 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"; import { initialActions, setStateGetNoble } from "../../../util/stateSetters";
import { canPickUpNoble } from "../../../util/canPickUpNoble";
export const tooExpensive = (card: CardData, state: AppState): boolean => { export const tooExpensive = (card: CardData, state: AppState): boolean => {
const currentPlayer = useCurrentPlayer(state); const currentPlayer = useCurrentPlayer(state);
@@ -92,5 +93,12 @@ export const buyCard = (state: AppState, setState: setStateType, card: CardData)
} }
} }
} }
}) });
for (let each of state.gameboard.nobles) {
if (canPickUpNoble(currentPlayer, each)) {
console.log(`${currentPlayer.name} can pick up noble ${state.gameboard.nobles.indexOf(each)}`);
setState((prev) => setStateGetNoble(prev, each));
}
}
} }