defined logic for picking up noble, adding to score
This commit is contained in:
22
src/util/canPickUpNoble.ts
Normal file
22
src/util/canPickUpNoble.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import getTotalBuyingPower from "./getTotalBuyingPower";
|
||||
import { NobleData, PlayerData, ResourceCost } from "./types";
|
||||
|
||||
export const canPickUpNoble = (player: PlayerData, noble: NobleData) => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return noble;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { AppState, CardData, NobleData, PlayerData, ResourceCost } from "./types";
|
||||
import CardDeck from '../data/cards.json';
|
||||
import { useCurrentPlayer } from "./useCurrentPlayer";
|
||||
|
||||
export const initialActions = {
|
||||
buyCard: { active: false },
|
||||
@@ -77,3 +78,29 @@ export const setStateReserveCard = (prev: AppState) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const setStateGetNoble = (prev: AppState, noble: NobleData) => {
|
||||
const currentPlayer = useCurrentPlayer(prev);
|
||||
if (!currentPlayer) return prev;
|
||||
|
||||
const updatedPlayer = {
|
||||
...currentPlayer,
|
||||
nobles: [...currentPlayer.nobles, noble],
|
||||
points: currentPlayer.points + 3
|
||||
}
|
||||
|
||||
const idx = prev.players.indexOf(currentPlayer);
|
||||
const newPlayers = prev.players;
|
||||
newPlayers[idx] = updatedPlayer;
|
||||
|
||||
const newNobles = prev.gameboard.nobles.filter((x: NobleData) => x.resourceCost !== noble.resourceCost);
|
||||
|
||||
return {
|
||||
...prev,
|
||||
players: newPlayers,
|
||||
gameboard: {
|
||||
...prev.gameboard,
|
||||
nobles: newNobles
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user