fix: delete orhpaned blackjack games #207
1 changed files with 30 additions and 22 deletions
|
@ -35,8 +35,8 @@ public class BlackJackService {
|
|||
|
||||
game.setState(getState(game));
|
||||
deductBetFromBalance(user, betAmount);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
|
||||
return processGameBasedOnState(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -44,12 +44,11 @@ public class BlackJackService {
|
|||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
||||
dealCardToPlayer(game);
|
||||
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
|
||||
return processGameBasedOnState(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -57,11 +56,11 @@ public class BlackJackService {
|
|||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
||||
dealCardsToDealerUntilMinimumScore(game);
|
||||
determineWinnerAndUpdateBalance(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
|
||||
return processGameBasedOnState(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -69,50 +68,59 @@ public class BlackJackService {
|
|||
if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal additionalBet = game.getBet();
|
||||
|
||||
|
||||
deductBetFromBalance(user, additionalBet);
|
||||
game.setBet(game.getBet().add(additionalBet));
|
||||
|
||||
|
||||
dealCardToPlayer(game);
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
|
||||
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
||||
return stand(game);
|
||||
}
|
||||
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity split(BlackJackGameEntity game) {
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
||||
game.getPlayerCards().size() != 2 ||
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
||||
game.getPlayerCards().size() != 2 ||
|
||||
game.isSplit() ||
|
||||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal splitBet = game.getBet();
|
||||
|
||||
|
||||
if (user.getBalance().compareTo(splitBet) < 0) {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
||||
deductBetFromBalance(user, splitBet);
|
||||
game.setSplitBet(splitBet);
|
||||
game.setSplit(true);
|
||||
|
||||
|
||||
CardEntity card = game.getPlayerCards().remove(1);
|
||||
card.setCardType(CardType.PLAYER_SPLIT);
|
||||
game.getPlayerSplitCards().add(card);
|
||||
|
||||
|
||||
dealCardToPlayer(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);
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue