Compare commits
No commits in common. "v1.69.2" and "v1.69.1" have entirely different histories.
4 changed files with 67 additions and 82 deletions
|
@ -4,8 +4,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import jakarta.validation.constraints.Positive;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
|
@ -11,27 +11,28 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class BlackJackService {
|
public class BlackJackService {
|
||||||
private final BlackJackGameRepository blackJackGameRepository;
|
private final BlackJackGameRepository blackJackGameRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
private final Random random;
|
||||||
private final BalanceService balanceService;
|
private final BalanceService balanceService;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final DeckService deckService;
|
|
||||||
|
|
||||||
public BlackJackService(
|
public BlackJackService(
|
||||||
BlackJackGameRepository blackJackGameRepository,
|
BlackJackGameRepository blackJackGameRepository,
|
||||||
UserRepository userRepository,
|
UserRepository userRepository,
|
||||||
|
Random random,
|
||||||
BalanceService balanceService,
|
BalanceService balanceService,
|
||||||
UserService userService,
|
UserService userService
|
||||||
DeckService deckService
|
|
||||||
) {
|
) {
|
||||||
this.blackJackGameRepository = blackJackGameRepository;
|
this.blackJackGameRepository = blackJackGameRepository;
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
|
this.random = random;
|
||||||
this.balanceService = balanceService;
|
this.balanceService = balanceService;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.deckService = deckService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlackJackGameEntity getBlackJackGame(Long id) {
|
public BlackJackGameEntity getBlackJackGame(Long id) {
|
||||||
|
@ -52,8 +53,8 @@ public class BlackJackService {
|
||||||
game.setUser(user);
|
game.setUser(user);
|
||||||
game.setBet(betDto.getBetAmount());
|
game.setBet(betDto.getBetAmount());
|
||||||
|
|
||||||
this.deckService.initializeDeck(game);
|
initializeDeck(game);
|
||||||
this.deckService.dealInitialCards(game);
|
dealInitialCards(game);
|
||||||
|
|
||||||
game.setState(getState(game));
|
game.setState(getState(game));
|
||||||
|
|
||||||
|
@ -66,7 +67,7 @@ public class BlackJackService {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.deckService.dealCardToPlayer(game);
|
dealCardToPlayer(game);
|
||||||
updateGameStateAndBalance(game);
|
updateGameStateAndBalance(game);
|
||||||
|
|
||||||
return processGameBasedOnState(game);
|
return processGameBasedOnState(game);
|
||||||
|
@ -90,14 +91,14 @@ public class BlackJackService {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = game.getUser();
|
UserEntity user = getUserWithFreshData(game.getUser());
|
||||||
BigDecimal additionalBet = game.getBet();
|
BigDecimal additionalBet = game.getBet();
|
||||||
|
|
||||||
this.balanceService.subtractFunds(user, additionalBet);
|
this.balanceService.subtractFunds(user, additionalBet);
|
||||||
|
|
||||||
game.setBet(game.getBet().add(additionalBet));
|
game.setBet(game.getBet().add(additionalBet));
|
||||||
|
|
||||||
this.deckService.dealCardToPlayer(game);
|
dealCardToPlayer(game);
|
||||||
updateGameStateAndBalance(game);
|
updateGameStateAndBalance(game);
|
||||||
|
|
||||||
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
||||||
|
@ -116,6 +117,36 @@ public class BlackJackService {
|
||||||
return blackJackGameRepository.save(game);
|
return blackJackGameRepository.save(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 updateGameStateAndBalance(BlackJackGameEntity game) {
|
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
||||||
game.setState(getState(game));
|
game.setState(getState(game));
|
||||||
|
|
||||||
|
@ -143,7 +174,7 @@ public class BlackJackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
||||||
UserEntity user = game.getUser();
|
UserEntity user = getUserWithFreshData(game.getUser());
|
||||||
BigDecimal totalBet = game.getBet();
|
BigDecimal totalBet = game.getBet();
|
||||||
BigDecimal balance = user.getBalance();
|
BigDecimal balance = user.getBalance();
|
||||||
|
|
||||||
|
@ -157,11 +188,34 @@ public class BlackJackService {
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void initializeDeck(BlackJackGameEntity game) {
|
||||||
|
for (Suit suit : Suit.values()) {
|
||||||
|
for (Rank rank : Rank.values()) {
|
||||||
|
CardEntity card = new CardEntity();
|
||||||
|
card.setGame(game);
|
||||||
|
card.setSuit(suit);
|
||||||
|
card.setRank(rank);
|
||||||
|
card.setCardType(CardType.DECK);
|
||||||
|
game.getDeck().add(card);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
java.util.Collections.shuffle(game.getDeck(), random);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CardEntity drawCardFromDeck(BlackJackGameEntity game) {
|
||||||
|
if (game.getDeck().isEmpty()) {
|
||||||
|
throw new IllegalStateException("Deck is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
return game.getDeck().removeFirst();
|
||||||
|
}
|
||||||
|
|
||||||
private BlackJackState getState(BlackJackGameEntity game) {
|
private BlackJackState getState(BlackJackGameEntity game) {
|
||||||
int playerHandValue = calculateHandValue(game.getPlayerCards());
|
int playerHandValue = calculateHandValue(game.getPlayerCards());
|
||||||
|
|
||||||
if (playerHandValue == 21) {
|
if (playerHandValue == 21) {
|
||||||
CardEntity hole = this.deckService.drawCardFromDeck(game);
|
CardEntity hole = drawCardFromDeck(game);
|
||||||
hole.setCardType(CardType.DEALER);
|
hole.setCardType(CardType.DEALER);
|
||||||
game.getDealerCards().add(hole);
|
game.getDealerCards().add(hole);
|
||||||
|
|
||||||
|
@ -171,7 +225,7 @@ public class BlackJackService {
|
||||||
return BlackJackState.DRAW;
|
return BlackJackState.DRAW;
|
||||||
} else {
|
} else {
|
||||||
BigDecimal blackjackWinnings = game.getBet().multiply(new BigDecimal("1.5"));
|
BigDecimal blackjackWinnings = game.getBet().multiply(new BigDecimal("1.5"));
|
||||||
UserEntity user = game.getUser();
|
UserEntity user = getUserWithFreshData(game.getUser());
|
||||||
user.setBalance(user.getBalance().add(blackjackWinnings));
|
user.setBalance(user.getBalance().add(blackjackWinnings));
|
||||||
return BlackJackState.PLAYER_BLACKJACK;
|
return BlackJackState.PLAYER_BLACKJACK;
|
||||||
}
|
}
|
||||||
|
@ -199,12 +253,4 @@ public class BlackJackService {
|
||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealCardsToDealerUntilMinimumScore(BlackJackGameEntity game) {
|
|
||||||
while (calculateHandValue(game.getDealerCards()) < 17) {
|
|
||||||
this.deckService.dealCardToDealer(game);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,57 +0,0 @@
|
||||||
package de.szut.casino.blackjack;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class DeckService {
|
|
||||||
private final Random random;
|
|
||||||
|
|
||||||
public DeckService(Random random) {
|
|
||||||
this.random = random;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeDeck(BlackJackGameEntity game) {
|
|
||||||
for (Suit suit : Suit.values()) {
|
|
||||||
for (Rank rank : Rank.values()) {
|
|
||||||
CardEntity card = new CardEntity();
|
|
||||||
card.setGame(game);
|
|
||||||
card.setSuit(suit);
|
|
||||||
card.setRank(rank);
|
|
||||||
card.setCardType(CardType.DECK);
|
|
||||||
game.getDeck().add(card);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
java.util.Collections.shuffle(game.getDeck(), random);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CardEntity drawCardFromDeck(BlackJackGameEntity game) {
|
|
||||||
if (game.getDeck().isEmpty()) {
|
|
||||||
throw new IllegalStateException("Deck is empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
return game.getDeck().removeFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dealInitialCards(BlackJackGameEntity game) {
|
|
||||||
for (int i = 0; i < 2; i++) {
|
|
||||||
dealCardToPlayer(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
dealCardToDealer(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dealCardToPlayer(BlackJackGameEntity game) {
|
|
||||||
CardEntity card = drawCardFromDeck(game);
|
|
||||||
card.setCardType(CardType.PLAYER);
|
|
||||||
game.getPlayerCards().add(card);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dealCardToDealer(BlackJackGameEntity game) {
|
|
||||||
CardEntity card = drawCardFromDeck(game);
|
|
||||||
card.setCardType(CardType.DEALER);
|
|
||||||
game.getDealerCards().add(card);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,9 +16,6 @@ public class UserEntity {
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Version
|
|
||||||
private Long version;
|
|
||||||
|
|
||||||
@Column(unique = true)
|
@Column(unique = true)
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
@ -47,6 +44,7 @@ public class UserEntity {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
this.verificationToken = verificationToken;
|
this.verificationToken = verificationToken;
|
||||||
|
this.provider = AuthProvider.LOCAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity(String email, String username, AuthProvider provider, String providerId, BigDecimal balance) {
|
public UserEntity(String email, String username, AuthProvider provider, String providerId, BigDecimal balance) {
|
||||||
|
|
Reference in a new issue