Compare commits
6 commits
2b8d45f6ec
...
36f2d80d5c
Author | SHA1 | Date | |
---|---|---|---|
36f2d80d5c | |||
|
7a7d24c8ea | ||
500a76dd7a | |||
59562e5b64 | |||
bd5539bb42 | |||
ed5960877d |
5 changed files with 54 additions and 5 deletions
|
@ -5,3 +5,8 @@ Content-Type: application/json
|
|||
{
|
||||
"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 lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
|
@ -28,6 +26,23 @@ public class BlackJackGameController {
|
|||
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")
|
||||
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid CreateBlackJackGameDto createBlackJackGameDto, @RequestHeader("Authorization") String token) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||
|
|
|
@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
|
|||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.SQLRestriction;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
|
@ -39,13 +40,16 @@ public class BlackJackGameEntity {
|
|||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JsonIgnore
|
||||
@SQLRestriction("card_type = 'DECK'")
|
||||
private List<CardEntity> deck = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JsonManagedReference
|
||||
@SQLRestriction("card_type = 'PLAYER'")
|
||||
private List<CardEntity> playerCards = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JsonManagedReference
|
||||
@SQLRestriction("card_type = 'DEALER'")
|
||||
private List<CardEntity> dealerCards = new ArrayList<>();
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
|
@ -20,6 +21,11 @@ public class BlackJackService {
|
|||
|
||||
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) {
|
||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||
game.setUser(user);
|
||||
|
@ -28,10 +34,12 @@ public class BlackJackService {
|
|||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
CardEntity playerCard = drawCardFromDeck(game);
|
||||
playerCard.setCardType(CardType.PLAYER);
|
||||
game.getPlayerCards().add(playerCard);
|
||||
}
|
||||
|
||||
CardEntity dealerCard = drawCardFromDeck(game);
|
||||
dealerCard.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(dealerCard);
|
||||
|
||||
game.setState(getState(game));
|
||||
|
@ -43,6 +51,16 @@ public class BlackJackService {
|
|||
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) {
|
||||
for (Suit suit : Suit.values()) {
|
||||
for (Rank rank : Rank.values()) {
|
||||
|
@ -50,6 +68,7 @@ public class BlackJackService {
|
|||
card.setGame(game);
|
||||
card.setSuit(suit);
|
||||
card.setRank(rank);
|
||||
card.setCardType(CardType.DECK);
|
||||
game.getDeck().add(card);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
@ -30,4 +29,11 @@ public class CardEntity {
|
|||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Rank rank;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private CardType cardType;
|
||||
}
|
||||
|
||||
enum CardType {
|
||||
DECK, PLAYER, DEALER
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue