player's spent resources are allocated back to global resource pool
This commit is contained in:
@@ -26,8 +26,6 @@ export const buyCard = (card: CardData, state: AppState, setState: setStateType)
|
||||
* @param card -> the target card, @param state -> current app state
|
||||
*/
|
||||
|
||||
console.log('called')
|
||||
|
||||
let currentPlayer = useCurrentPlayer(state);
|
||||
console.log(currentPlayer);
|
||||
|
||||
@@ -35,19 +33,27 @@ export const buyCard = (card: CardData, state: AppState, setState: setStateType)
|
||||
if (!currentPlayer) return prev;
|
||||
|
||||
const { newPlayers, roundIncrement } = turnOrderUtil(prev, currentPlayer);
|
||||
let newInventory = currentPlayer.inventory;
|
||||
|
||||
let newPlayerInventory = currentPlayer.inventory;
|
||||
let newResourcePool = prev.gameboard.tradingResources;
|
||||
|
||||
for (let [gem, cost] of Object.entries(card.resourceCost)) {
|
||||
if (cost < 1) continue;
|
||||
|
||||
let resourceToReplenish = newResourcePool[gem as keyof ResourceCost];
|
||||
let newInventoryValue = newPlayerInventory[gem as keyof ResourceCost];
|
||||
if (!newInventoryValue || !resourceToReplenish) continue;
|
||||
|
||||
let i = cost;
|
||||
let newInventoryValue = newInventory[gem as keyof ResourceCost];
|
||||
if (!newInventoryValue) continue;
|
||||
|
||||
while (i > 0) {
|
||||
newInventoryValue--;
|
||||
i--;
|
||||
}
|
||||
newInventory[gem as keyof ResourceCost] = newInventoryValue;
|
||||
|
||||
resourceToReplenish += cost;
|
||||
|
||||
newPlayerInventory[gem as keyof ResourceCost] = newInventoryValue;
|
||||
newResourcePool[gem as keyof ResourceCost] = resourceToReplenish;
|
||||
}
|
||||
|
||||
let updatedPlayer: PlayerData = {
|
||||
@@ -56,7 +62,7 @@ export const buyCard = (card: CardData, state: AppState, setState: setStateType)
|
||||
...currentPlayer.cards,
|
||||
card
|
||||
],
|
||||
inventory: newInventory
|
||||
inventory: newPlayerInventory
|
||||
}
|
||||
|
||||
let newScore = updatedPlayer.points;
|
||||
@@ -71,10 +77,12 @@ export const buyCard = (card: CardData, state: AppState, setState: setStateType)
|
||||
|
||||
return {
|
||||
...prev,
|
||||
gameboard: {
|
||||
...prev.gameboard,
|
||||
tradingResources: prev.gameboard.tradingResources
|
||||
},
|
||||
round: (roundIncrement ? prev.round + 1 : prev.round),
|
||||
players: newPlayers
|
||||
}
|
||||
})
|
||||
|
||||
console.log(state);
|
||||
}
|
||||
@@ -6,16 +6,20 @@ import { initialActions } from "../../../util/stateSetters";
|
||||
|
||||
export const validateChips = (state: AppState): boolean => {
|
||||
if (!state.actions.getChips.active || !state.actions.getChips.selection) return false;
|
||||
|
||||
const selection = state.actions.getChips.selection;
|
||||
|
||||
if (selection.length === 0 || selection.length > 3) return false;
|
||||
const unique = new Set(selection);
|
||||
|
||||
// requires a selection to be made, and less than three chips to be selected
|
||||
if (selection.length === 0 || selection.length > 3) return false;
|
||||
|
||||
// ensures correct handling of duplicate chips
|
||||
const unique = new Set(selection);
|
||||
if (selection.length === 3 && selection.length > unique.size) return false;
|
||||
|
||||
let globalResourceCopy = { ...state.gameboard.tradingResources }
|
||||
// allows only one gold chip to be collected at a time
|
||||
if (selection.includes('gold') && unique.size > 1) return false;
|
||||
|
||||
// ensures the player cannot collect unavailable resources
|
||||
let globalResourceCopy = { ...state.gameboard.tradingResources }
|
||||
for (let item of selection) {
|
||||
for (let key of Object.keys(globalResourceCopy)) {
|
||||
if (item === key) {
|
||||
|
||||
Reference in New Issue
Block a user