diff --git a/backend/requests/blackjack.http b/backend/requests/blackjack.http index 1ac2def..0b8b261 100644 --- a/backend/requests/blackjack.http +++ b/backend/requests/blackjack.http @@ -7,6 +7,6 @@ Content-Type: application/json } ### -POST http://localhost:8080/blackjack/202/hit +POST http://localhost:8080/blackjack/54/hit Authorization: Bearer {{token}} diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java index 58505eb..774e0c1 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java @@ -46,7 +46,7 @@ public class BlackJackGameController { return ResponseEntity.badRequest().body(errorResponse); } - return ResponseEntity.ok(blackJackService.hit(game)); + return ResponseEntity.ok(blackJackService.hit(game, user)); } @PostMapping("/blackjack/start") diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java index 62b8859..693c055 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java @@ -42,14 +42,7 @@ public class BlackJackService { dealerCard.setCardType(CardType.DEALER); game.getDealerCards().add(dealerCard); - BlackJackState state = getState(game); - if (state == BlackJackState.PLAYER_BLACKJACK) { - BigDecimal blackjackWinnings = betAmount.multiply(new BigDecimal("1.5")); - user.setBalance(user.getBalance().add(blackjackWinnings)); - } else { - user.setBalance(user.getBalance().subtract(betAmount)); - } - + BlackJackState state = handleState(game, user); game.setState(state); userRepository.save(user); @@ -58,12 +51,12 @@ public class BlackJackService { return game; } - public BlackJackGameEntity hit(BlackJackGameEntity game) { + public BlackJackGameEntity hit(BlackJackGameEntity game, UserEntity user) { CardEntity drawnCard = drawCardFromDeck(game); drawnCard.setCardType(CardType.PLAYER); game.getPlayerCards().add(drawnCard); - game.setState(getState(game)); + game.setState(handleState(game, user)); return blackJackGameRepository.save(game); } @@ -91,12 +84,25 @@ public class BlackJackService { return game.getDeck().removeFirst(); } - private BlackJackState getState(BlackJackGameEntity game) { + private BlackJackState handleState(BlackJackGameEntity game, UserEntity user) { int playerHandValue = calculateHandValue(game.getPlayerCards()); if (playerHandValue == 21) { - return BlackJackState.PLAYER_BLACKJACK; + CardEntity hole = drawCardFromDeck(game); + hole.setCardType(CardType.DEALER); + game.getDealerCards().add(hole); + + int dealerHandValue = calculateHandValue(game.getDealerCards()); + + if (dealerHandValue == 21) { + return BlackJackState.STANDOFF; + } else { + BigDecimal blackjackWinnings = game.getBet().multiply(new BigDecimal("1.5")); + user.setBalance(user.getBalance().add(blackjackWinnings)); + return BlackJackState.PLAYER_BLACKJACK; + } } else if (playerHandValue > 21) { + user.setBalance(user.getBalance().subtract(game.getBet())); return BlackJackState.PLAYER_LOST; } diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackState.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackState.java index e866bd9..f896058 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackState.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackState.java @@ -4,4 +4,5 @@ public enum BlackJackState { IN_PROGRESS, PLAYER_BLACKJACK, PLAYER_LOST, + STANDOFF, } diff --git a/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java b/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java index 5520b58..3b6903a 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java +++ b/backend/src/main/java/de/szut/casino/blackjack/CardEntity.java @@ -31,6 +31,7 @@ public class CardEntity { private Rank rank; @Enumerated(EnumType.STRING) + @JsonIgnore private CardType cardType; }