Compare commits
No commits in common. "36f2d80d5ce61f63269cadeb60ea0677af10ba5b" and "2b8d45f6eca622d59858c0da59d0d152f02aab88" have entirely different histories.
36f2d80d5c
...
2b8d45f6ec
5 changed files with 5 additions and 54 deletions
|
@ -5,8 +5,3 @@ Content-Type: application/json
|
||||||
{
|
{
|
||||||
"betAmount": 1.01
|
"betAmount": 1.01
|
||||||
}
|
}
|
||||||
|
|
||||||
###
|
|
||||||
POST http://localhost:8080/blackjack/103/hit
|
|
||||||
Authorization: Bearer {{token}}
|
|
||||||
|
|
||||||
|
|
|
@ -6,12 +6,14 @@ 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.*;
|
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 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
|
||||||
|
@ -26,23 +28,6 @@ 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,7 +8,6 @@ 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;
|
||||||
|
@ -40,16 +39,13 @@ 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,7 +6,6 @@ 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
|
||||||
|
@ -21,11 +20,6 @@ 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);
|
||||||
|
@ -34,12 +28,10 @@ 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));
|
||||||
|
@ -51,16 +43,6 @@ 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()) {
|
||||||
|
@ -68,7 +50,6 @@ 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,5 +1,6 @@
|
||||||
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.*;
|
||||||
|
@ -29,11 +30,4 @@ 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