Add cumulative PnL tracking to Blackjack game
Some checks failed
Deploy to Production / test (push) Failing after 32s
Some checks failed
Deploy to Production / test (push) Failing after 32s
- Added cumulativePnl field to PlayerSeat and PlayerSeatView types - Added myCumulativePnl to PlayerView for UI display - Track net profit/loss across rounds in the game state - Update round result banner to show both round net and total balance - Add player seat PnL indicator with color coding (green/red) - Preserve cumulativePnl when players stay seated through rounds - Initialize new players with cumulativePnl = 0 - Added comprehensive tests for cumulative PnL tracking
This commit is contained in:
@@ -191,10 +191,21 @@ function finishPlayerTurns(state: BlackjackState): BlackjackState {
|
||||
|
||||
const resolvedSeats: Record<string, PlayerSeat> = {};
|
||||
for (const [id, seat] of Object.entries(state.seats)) {
|
||||
// First resolve the hands
|
||||
const resolvedHands = seat.hands.map(h => resolveHand(h, dealerHand));
|
||||
|
||||
// Then calculate PnL based on resolved hands
|
||||
const roundPayout = calculateRoundPayouts({ [id]: { ...seat, hands: resolvedHands } });
|
||||
const roundPnl = roundPayout[id] ?? 0;
|
||||
// Subtract the total bet amount to get net profit/loss
|
||||
const roundBetTotal = seat.hands.reduce((sum, h) => sum + h.bet, 0);
|
||||
const roundNetPnl = roundPayout[id] ? roundPayout[id] - roundBetTotal : -roundBetTotal;
|
||||
|
||||
resolvedSeats[id] = {
|
||||
...seat,
|
||||
activeHandIndex: -1,
|
||||
hands: seat.hands.map(h => resolveHand(h, dealerHand)),
|
||||
hands: resolvedHands,
|
||||
cumulativePnl: (seat.cumulativePnl ?? 0) + roundNetPnl,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -262,6 +273,7 @@ function toSeatView(seat: PlayerSeat): PlayerSeatView {
|
||||
hands: seat.hands.map(toHandView),
|
||||
activeHandIndex: seat.activeHandIndex,
|
||||
hasBet: seat.hasBet,
|
||||
cumulativePnl: seat.cumulativePnl,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -292,6 +304,7 @@ function dealRound(state: BlackjackState): BlackjackState {
|
||||
}],
|
||||
activeHandIndex: -1,
|
||||
hasBet: true,
|
||||
cumulativePnl: state.seats[pid]?.cumulativePnl ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -355,6 +368,7 @@ export const blackjackPlugin: GamePlugin<BlackjackState, BlackjackAction> = {
|
||||
hands: [],
|
||||
activeHandIndex: -1,
|
||||
hasBet: false,
|
||||
cumulativePnl: 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -439,6 +453,7 @@ export const blackjackPlugin: GamePlugin<BlackjackState, BlackjackAction> = {
|
||||
canSplit: isMyTurn && myActiveHand !== null && canSplitHand(myActiveHand, mySeat!.hands.length),
|
||||
canDoubleDown: isMyTurn && myActiveHand !== null && canDoubleHand(myActiveHand),
|
||||
roundNumber: state.roundNumber,
|
||||
myCumulativePnl: mySeat?.cumulativePnl ?? 0,
|
||||
};
|
||||
},
|
||||
|
||||
@@ -502,10 +517,12 @@ function handlePlaceBet(state: BlackjackState, playerId: string): GameResult<Bla
|
||||
|
||||
const resetSeats: Record<string, PlayerSeat> = {};
|
||||
for (const pid of state.turnOrder) {
|
||||
// Preserve cumulativePnl from previous round
|
||||
resetSeats[pid] = {
|
||||
hands: [],
|
||||
activeHandIndex: -1,
|
||||
hasBet: false,
|
||||
cumulativePnl: state.seats[pid]?.cumulativePnl ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -764,6 +781,7 @@ function handleSitDown(state: BlackjackState, playerId: string): GameResult<Blac
|
||||
hands: [],
|
||||
activeHandIndex: -1,
|
||||
hasBet: false,
|
||||
cumulativePnl: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user