style: format code
All checks were successful
CI / Get Changed Files (pull_request) Successful in 9s
CI / eslint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 1m6s
CI / Docker backend validation (pull_request) Successful in 1m3s
All checks were successful
CI / Get Changed Files (pull_request) Successful in 9s
CI / eslint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 1m6s
CI / Docker backend validation (pull_request) Successful in 1m3s
This commit is contained in:
parent
1dfdedee91
commit
1931a02369
1 changed files with 22 additions and 22 deletions
|
@ -29,10 +29,10 @@ public class BlackJackService {
|
|||
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||
game.setUser(user);
|
||||
game.setBet(betAmount);
|
||||
|
||||
|
||||
initializeDeck(game);
|
||||
dealInitialCards(game);
|
||||
|
||||
|
||||
game.setState(getState(game));
|
||||
deductBetFromBalance(user, betAmount);
|
||||
|
||||
|
@ -88,9 +88,9 @@ public class BlackJackService {
|
|||
@Transactional
|
||||
public BlackJackGameEntity split(BlackJackGameEntity game) {
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
||||
game.getPlayerCards().size() != 2 ||
|
||||
game.isSplit() ||
|
||||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
||||
game.getPlayerCards().size() != 2 ||
|
||||
game.isSplit() ||
|
||||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
@ -127,48 +127,48 @@ public class BlackJackService {
|
|||
private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) {
|
||||
return blackJackGameRepository.findById(game.getId()).orElse(game);
|
||||
}
|
||||
|
||||
|
||||
private UserEntity getUserWithFreshData(UserEntity user) {
|
||||
return userRepository.findById(user.getId()).orElse(user);
|
||||
}
|
||||
|
||||
|
||||
private void dealInitialCards(BlackJackGameEntity game) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
dealCardToPlayer(game);
|
||||
}
|
||||
|
||||
|
||||
dealCardToDealer(game);
|
||||
}
|
||||
|
||||
|
||||
private void dealCardToPlayer(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.PLAYER);
|
||||
game.getPlayerCards().add(card);
|
||||
}
|
||||
|
||||
|
||||
private void dealCardToDealer(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(card);
|
||||
}
|
||||
|
||||
|
||||
private void dealCardsToDealerUntilMinimumScore(BlackJackGameEntity game) {
|
||||
while (calculateHandValue(game.getDealerCards()) < 17) {
|
||||
dealCardToDealer(game);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void dealCardToSplitHand(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.PLAYER_SPLIT);
|
||||
game.getPlayerSplitCards().add(card);
|
||||
}
|
||||
|
||||
|
||||
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
||||
if (game.isSplit()) {
|
||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
||||
|
||||
|
||||
if (mainHandValue > 21 && splitHandValue > 21) {
|
||||
game.setState(BlackJackState.PLAYER_LOST);
|
||||
updateUserBalance(game, false);
|
||||
|
@ -179,7 +179,7 @@ public class BlackJackService {
|
|||
}
|
||||
} else {
|
||||
game.setState(getState(game));
|
||||
|
||||
|
||||
if (game.getState() == BlackJackState.PLAYER_WON) {
|
||||
updateUserBalance(game, true);
|
||||
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
||||
|
@ -187,7 +187,7 @@ public class BlackJackService {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
||||
int playerValue = calculateHandValue(game.getPlayerCards());
|
||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||
|
@ -203,7 +203,7 @@ public class BlackJackService {
|
|||
updateUserBalance(game, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void deductBetFromBalance(UserEntity user, BigDecimal betAmount) {
|
||||
user.setBalance(user.getBalance().subtract(betAmount));
|
||||
userRepository.save(user);
|
||||
|
@ -214,21 +214,21 @@ public class BlackJackService {
|
|||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal totalBet = game.getBet();
|
||||
BigDecimal balance = user.getBalance();
|
||||
|
||||
|
||||
if (game.isSplit()) {
|
||||
totalBet = totalBet.add(game.getSplitBet());
|
||||
|
||||
|
||||
if (isWin) {
|
||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||
|
||||
|
||||
if (mainHandValue <= 21 && (dealerValue > 21 || mainHandValue > dealerValue)) {
|
||||
balance = balance.add(game.getBet().multiply(BigDecimal.valueOf(2)));
|
||||
} else if (mainHandValue == dealerValue) {
|
||||
balance = balance.add(game.getBet());
|
||||
}
|
||||
|
||||
|
||||
if (splitHandValue <= 21 && (dealerValue > 21 || splitHandValue > dealerValue)) {
|
||||
balance = balance.add(game.getSplitBet().multiply(BigDecimal.valueOf(2)));
|
||||
} else if (splitHandValue == dealerValue) {
|
||||
|
@ -244,7 +244,7 @@ public class BlackJackService {
|
|||
balance = balance.add(totalBet);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
user.setBalance(balance);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
|
Reference in a new issue