Merge pull request 'feat(blackjack): add hit endpoint for blackjack game (CAS-51)' (!97) from add-hitting into main
Some checks failed
Release / Release (push) Has been cancelled
Some checks failed
Release / Release (push) Has been cancelled
Reviewed-on: #97 Reviewed-by: Phan Huy Tran <ptran@noreply.localhost>
This commit is contained in:
commit
36f2d80d5c
5 changed files with 54 additions and 5 deletions
|
@ -5,3 +5,8 @@ Content-Type: application/json
|
||||||
{
|
{
|
||||||
"betAmount": 1.01
|
"betAmount": 1.01
|
||||||
}
|
}
|
||||||
|
|
||||||
|
###
|
||||||
|
POST http://localhost:8080/blackjack/103/hit
|
||||||
|
Authorization: Bearer {{token}}
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,12 @@ import de.szut.casino.user.UserService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -28,6 +26,23 @@ public class BlackJackGameController {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/blackjack/{id}/hit")
|
||||||
|
public ResponseEntity<Object> hit(@PathVariable Long id, @RequestHeader("Authorization") String token) {
|
||||||
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
||||||
|
if (optionalUser.isEmpty()) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
UserEntity user = optionalUser.get();
|
||||||
|
BlackJackGameEntity game = blackJackService.getBlackJackGame(id);
|
||||||
|
if (game == null || !Objects.equals(game.getUserId(), user.getId())) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(blackJackService.hit(game));
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/blackjack/start")
|
@PostMapping("/blackjack/start")
|
||||||
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid CreateBlackJackGameDto createBlackJackGameDto, @RequestHeader("Authorization") String token) {
|
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid CreateBlackJackGameDto createBlackJackGameDto, @RequestHeader("Authorization") String token) {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||||
|
|
|
@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.hibernate.annotations.SQLRestriction;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -39,13 +40,16 @@ public class BlackJackGameEntity {
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
|
@SQLRestriction("card_type = 'DECK'")
|
||||||
private List<CardEntity> deck = new ArrayList<>();
|
private List<CardEntity> deck = new ArrayList<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@JsonManagedReference
|
@JsonManagedReference
|
||||||
|
@SQLRestriction("card_type = 'PLAYER'")
|
||||||
private List<CardEntity> playerCards = new ArrayList<>();
|
private List<CardEntity> playerCards = new ArrayList<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@JsonManagedReference
|
@JsonManagedReference
|
||||||
|
@SQLRestriction("card_type = 'DEALER'")
|
||||||
private List<CardEntity> dealerCards = new ArrayList<>();
|
private List<CardEntity> dealerCards = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -20,6 +21,11 @@ public class BlackJackService {
|
||||||
|
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
|
|
||||||
|
public BlackJackGameEntity getBlackJackGame(Long id) {
|
||||||
|
Optional<BlackJackGameEntity> optionalBlackJackGame = blackJackGameRepository.findById(id);
|
||||||
|
return optionalBlackJackGame.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
|
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
|
||||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||||
game.setUser(user);
|
game.setUser(user);
|
||||||
|
@ -28,10 +34,12 @@ public class BlackJackService {
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
CardEntity playerCard = drawCardFromDeck(game);
|
CardEntity playerCard = drawCardFromDeck(game);
|
||||||
|
playerCard.setCardType(CardType.PLAYER);
|
||||||
game.getPlayerCards().add(playerCard);
|
game.getPlayerCards().add(playerCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
CardEntity dealerCard = drawCardFromDeck(game);
|
CardEntity dealerCard = drawCardFromDeck(game);
|
||||||
|
dealerCard.setCardType(CardType.DEALER);
|
||||||
game.getDealerCards().add(dealerCard);
|
game.getDealerCards().add(dealerCard);
|
||||||
|
|
||||||
game.setState(getState(game));
|
game.setState(getState(game));
|
||||||
|
@ -43,6 +51,16 @@ public class BlackJackService {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BlackJackGameEntity hit(BlackJackGameEntity game) {
|
||||||
|
CardEntity drawnCard = drawCardFromDeck(game);
|
||||||
|
drawnCard.setCardType(CardType.PLAYER);
|
||||||
|
game.getPlayerCards().add(drawnCard);
|
||||||
|
|
||||||
|
game.setState(getState(game));
|
||||||
|
|
||||||
|
return blackJackGameRepository.save(game);
|
||||||
|
}
|
||||||
|
|
||||||
private void initializeDeck(BlackJackGameEntity game) {
|
private void initializeDeck(BlackJackGameEntity game) {
|
||||||
for (Suit suit : Suit.values()) {
|
for (Suit suit : Suit.values()) {
|
||||||
for (Rank rank : Rank.values()) {
|
for (Rank rank : Rank.values()) {
|
||||||
|
@ -50,6 +68,7 @@ public class BlackJackService {
|
||||||
card.setGame(game);
|
card.setGame(game);
|
||||||
card.setSuit(suit);
|
card.setSuit(suit);
|
||||||
card.setRank(rank);
|
card.setRank(rank);
|
||||||
|
card.setCardType(CardType.DECK);
|
||||||
game.getDeck().add(card);
|
game.getDeck().add(card);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.szut.casino.blackjack;
|
package de.szut.casino.blackjack;
|
||||||
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
@ -30,4 +29,11 @@ public class CardEntity {
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Rank rank;
|
private Rank rank;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
private CardType cardType;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum CardType {
|
||||||
|
DECK, PLAYER, DEALER
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue