From b34787c3c97dca292b74b53c32df820c2b1e723e Mon Sep 17 00:00:00 2001 From: Jessica Mulein Date: Thu, 24 Oct 2024 16:59:18 +0000 Subject: [PATCH] fix last victory card bug --- src/game/dominion-lib.ts | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/game/dominion-lib.ts b/src/game/dominion-lib.ts index fdcfc70..928d5e0 100644 --- a/src/game/dominion-lib.ts +++ b/src/game/dominion-lib.ts @@ -236,32 +236,41 @@ export function updatePlayerField( const updatedGame = { ...game }; const player = { ...updatedGame.players[playerIndex] }; - if (field === 'victory' || field === 'turn' || field === 'mats' || field === 'newTurn') { - if (((player[field] as any)[subfield] || 0) + increment < 0) { - throw new NotEnoughSubfieldError(field, subfield); - } - (player[field] as any)[subfield] = Math.max( - ((player[field] as any)[subfield] || 0) + increment, - 0 - ); - } else { + // Check if the field is valid + if (field !== 'victory' && field !== 'turn' && field !== 'mats' && field !== 'newTurn') { throw new InvalidFieldError(field as string); } - updatedGame.players[playerIndex] = player; + // Check if the subfield decrement would go below 0 + if (((player[field] as any)[subfield] || 0) + increment < 0) { + throw new NotEnoughSubfieldError(field, subfield); + } - // update the supply if the field is a victory field - if ( + // Check if the supply decrement would go below 0 + const decrementSupply = field === 'victory' && - ['estates', 'duchies', 'provinces', 'colonies', 'curses'].includes(subfield) - ) { - (updatedGame.supply[subfield as keyof IGameSupply] as number) -= increment; + ['estates', 'duchies', 'provinces', 'colonies', 'curses'].includes(subfield); + if (decrementSupply) { const supplyCount = updatedGame.supply[subfield as keyof IGameSupply] as number; if (increment > 0 && supplyCount < increment) { throw new NotEnoughSupplyError(subfield as string); } } + + // Perform the actual updates + (player[field] as any)[subfield] = Math.max( + ((player[field] as any)[subfield] || 0) + increment, + 0 + ); + + updatedGame.players[playerIndex] = player; + + // Update the supply if the field is a victory field + if (decrementSupply) { + (updatedGame.supply[subfield as keyof IGameSupply] as number) -= increment; + } + return updatedGame; }