fix: Correct PnL calculation by using betAmount in net profit computation
Some checks failed
Deploy to Production / test (push) Failing after 34s

- Add betAmount field to BlackjackState to track the base bet
- Fix finishPlayerTurns: multiply hand.bet by state.betAmount for actual money bets
- Fix GameServer: roundPayouts are already gross payouts (not multipliers)
- Update cumulative PnL calculation to correctly subtract actual bet amount
- Add betAmount support to riggedState test helper
This commit is contained in:
syntaxbullet
2026-04-06 14:50:40 +02:00
parent f09cbe6939
commit 06c3891045
4 changed files with 13 additions and 8 deletions

View File

@@ -196,10 +196,10 @@ function finishPlayerTurns(state: BlackjackState): BlackjackState {
// Then calculate PnL based on resolved hands
const roundPayout = calculateRoundPayouts({ [id]: { ...seat, hands: resolvedHands } });
const roundPnl = roundPayout[id] ?? 0;
const roundPayoutMoney = 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;
const roundBetTotal = seat.hands.reduce((sum, h) => sum + (h.bet * state.betAmount), 0);
const roundNetPnl = roundPayoutMoney ? roundPayoutMoney - roundBetTotal : -roundBetTotal;
resolvedSeats[id] = {
...seat,
@@ -361,7 +361,8 @@ export const blackjackPlugin: GamePlugin<BlackjackState, BlackjackAction> = {
maxPlayers: 6,
manualStart: true,
createInitialState(players: string[], _options?: Record<string, unknown>): BlackjackState {
createInitialState(players: string[], options?: Record<string, unknown>): BlackjackState {
const betAmount = typeof options?.betAmount === 'number' ? options.betAmount : 0;
const seats: Record<string, PlayerSeat> = {};
for (const pid of players) {
seats[pid] = {
@@ -380,6 +381,7 @@ export const blackjackPlugin: GamePlugin<BlackjackState, BlackjackAction> = {
activePlayerIndex: -1,
phase: "betting",
roundNumber: 1,
betAmount,
};
},