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 de.szut.casino.user.UserEntity;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Positive;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
|
|
@ -11,27 +11,28 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class BlackJackService {
|
||||
private final BlackJackGameRepository blackJackGameRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final Random random;
|
||||
private final BalanceService balanceService;
|
||||
private final UserService userService;
|
||||
private final DeckService deckService;
|
||||
|
||||
public BlackJackService(
|
||||
BlackJackGameRepository blackJackGameRepository,
|
||||
UserRepository userRepository,
|
||||
Random random,
|
||||
BalanceService balanceService,
|
||||
UserService userService,
|
||||
DeckService deckService
|
||||
UserService userService
|
||||
) {
|
||||
this.blackJackGameRepository = blackJackGameRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.random = random;
|
||||
this.balanceService = balanceService;
|
||||
this.userService = userService;
|
||||
this.deckService = deckService;
|
||||
}
|
||||
|
||||
public BlackJackGameEntity getBlackJackGame(Long id) {
|
||||
|
@ -52,8 +53,8 @@ public class BlackJackService {
|
|||
game.setUser(user);
|
||||
game.setBet(betDto.getBetAmount());
|
||||
|
||||
this.deckService.initializeDeck(game);
|
||||
this.deckService.dealInitialCards(game);
|
||||
initializeDeck(game);
|
||||
dealInitialCards(game);
|
||||
|
||||
game.setState(getState(game));
|
||||
|
||||
|
@ -66,7 +67,7 @@ public class BlackJackService {
|
|||
return game;
|
||||
}
|
||||
|
||||
this.deckService.dealCardToPlayer(game);
|
||||
dealCardToPlayer(game);
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
return processGameBasedOnState(game);
|
||||
|
@ -90,14 +91,14 @@ public class BlackJackService {
|
|||
return game;
|
||||
}
|
||||
|
||||
UserEntity user = game.getUser();
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal additionalBet = game.getBet();
|
||||
|
||||
this.balanceService.subtractFunds(user, additionalBet);
|
||||
|
||||
game.setBet(game.getBet().add(additionalBet));
|
||||
|
||||
this.deckService.dealCardToPlayer(game);
|
||||
dealCardToPlayer(game);
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
||||
|
@ -116,6 +117,36 @@ public class BlackJackService {
|
|||
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) {
|
||||
game.setState(getState(game));
|
||||
|
||||
|
@ -143,7 +174,7 @@ public class BlackJackService {
|
|||
}
|
||||
|
||||
protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
||||
UserEntity user = game.getUser();
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal totalBet = game.getBet();
|
||||
BigDecimal balance = user.getBalance();
|
||||
|
||||
|
@ -157,11 +188,34 @@ public class BlackJackService {
|
|||
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) {
|
||||
int playerHandValue = calculateHandValue(game.getPlayerCards());
|
||||
|
||||
if (playerHandValue == 21) {
|
||||
CardEntity hole = this.deckService.drawCardFromDeck(game);
|
||||
CardEntity hole = drawCardFromDeck(game);
|
||||
hole.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(hole);
|
||||
|
||||
|
@ -171,7 +225,7 @@ public class BlackJackService {
|
|||
return BlackJackState.DRAW;
|
||||
} else {
|
||||
BigDecimal blackjackWinnings = game.getBet().multiply(new BigDecimal("1.5"));
|
||||
UserEntity user = game.getUser();
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
user.setBalance(user.getBalance().add(blackjackWinnings));
|
||||
return BlackJackState.PLAYER_BLACKJACK;
|
||||
}
|
||||
|
@ -199,12 +253,4 @@ public class BlackJackService {
|
|||
|
||||
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
|
||||
private Long id;
|
||||
|
||||
@Version
|
||||
private Long version;
|
||||
|
||||
@Column(unique = true)
|
||||
private String email;
|
||||
|
||||
|
@ -47,6 +44,7 @@ public class UserEntity {
|
|||
this.password = password;
|
||||
this.balance = balance;
|
||||
this.verificationToken = verificationToken;
|
||||
this.provider = AuthProvider.LOCAL;
|
||||
}
|
||||
|
||||
public UserEntity(String email, String username, AuthProvider provider, String providerId, BigDecimal balance) {
|
||||
|
|
Reference in a new issue