fix: delete orhpaned blackjack games #207

Merged
ptran merged 2 commits from fix-delete-completed-blackjack-games into main 2025-05-21 07:54:02 +00:00
Showing only changes of commit 1dfdedee91 - Show all commits

View file

@ -35,8 +35,8 @@ public class BlackJackService {
game.setState(getState(game)); game.setState(getState(game));
deductBetFromBalance(user, betAmount); deductBetFromBalance(user, betAmount);
return blackJackGameRepository.save(game); return processGameBasedOnState(game);
} }
@Transactional @Transactional
@ -44,12 +44,11 @@ public class BlackJackService {
if (game.getState() != BlackJackState.IN_PROGRESS) { if (game.getState() != BlackJackState.IN_PROGRESS) {
return game; return game;
} }
dealCardToPlayer(game); dealCardToPlayer(game);
updateGameStateAndBalance(game); updateGameStateAndBalance(game);
return blackJackGameRepository.save(game); return processGameBasedOnState(game);
} }
@Transactional @Transactional
@ -57,11 +56,11 @@ public class BlackJackService {
if (game.getState() != BlackJackState.IN_PROGRESS) { if (game.getState() != BlackJackState.IN_PROGRESS) {
return game; return game;
} }
dealCardsToDealerUntilMinimumScore(game); dealCardsToDealerUntilMinimumScore(game);
determineWinnerAndUpdateBalance(game); determineWinnerAndUpdateBalance(game);
return blackJackGameRepository.save(game); return processGameBasedOnState(game);
} }
@Transactional @Transactional
@ -69,50 +68,59 @@ public class BlackJackService {
if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) { if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) {
return game; return game;
} }
UserEntity user = getUserWithFreshData(game.getUser()); UserEntity user = getUserWithFreshData(game.getUser());
BigDecimal additionalBet = game.getBet(); BigDecimal additionalBet = game.getBet();
deductBetFromBalance(user, additionalBet); deductBetFromBalance(user, additionalBet);
game.setBet(game.getBet().add(additionalBet)); game.setBet(game.getBet().add(additionalBet));
dealCardToPlayer(game); dealCardToPlayer(game);
updateGameStateAndBalance(game); updateGameStateAndBalance(game);
if (game.getState() == BlackJackState.IN_PROGRESS) { if (game.getState() == BlackJackState.IN_PROGRESS) {
return stand(game); return stand(game);
} }
return game; return game;
} }
@Transactional @Transactional
public BlackJackGameEntity split(BlackJackGameEntity game) { public BlackJackGameEntity split(BlackJackGameEntity game) {
if (game.getState() != BlackJackState.IN_PROGRESS || if (game.getState() != BlackJackState.IN_PROGRESS ||
game.getPlayerCards().size() != 2 || game.getPlayerCards().size() != 2 ||
game.isSplit() || game.isSplit() ||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) { !game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
return game; return game;
} }
UserEntity user = getUserWithFreshData(game.getUser()); UserEntity user = getUserWithFreshData(game.getUser());
BigDecimal splitBet = game.getBet(); BigDecimal splitBet = game.getBet();
if (user.getBalance().compareTo(splitBet) < 0) { if (user.getBalance().compareTo(splitBet) < 0) {
return game; return game;
} }
deductBetFromBalance(user, splitBet); deductBetFromBalance(user, splitBet);
game.setSplitBet(splitBet); game.setSplitBet(splitBet);
game.setSplit(true); game.setSplit(true);
CardEntity card = game.getPlayerCards().remove(1); CardEntity card = game.getPlayerCards().remove(1);
card.setCardType(CardType.PLAYER_SPLIT); card.setCardType(CardType.PLAYER_SPLIT);
game.getPlayerSplitCards().add(card); game.getPlayerSplitCards().add(card);
dealCardToPlayer(game); dealCardToPlayer(game);
dealCardToSplitHand(game); dealCardToSplitHand(game);
return blackJackGameRepository.save(game);
}
private BlackJackGameEntity processGameBasedOnState(BlackJackGameEntity game) {
if (game.getState() != BlackJackState.IN_PROGRESS) {
this.blackJackGameRepository.delete(game);
return game;
}
return blackJackGameRepository.save(game); return blackJackGameRepository.save(game);
} }