refactor: remove unused blackjack split funcionality
All checks were successful
CI / Get Changed Files (pull_request) Successful in 8s
CI / eslint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 54s
CI / Docker backend validation (pull_request) Successful in 1m1s
All checks were successful
CI / Get Changed Files (pull_request) Successful in 8s
CI / eslint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 54s
CI / Docker backend validation (pull_request) Successful in 1m1s
This commit is contained in:
parent
9859e60173
commit
c277b89ffc
4 changed files with 11 additions and 105 deletions
|
@ -57,13 +57,6 @@ public class BlackJackGameController {
|
||||||
return ResponseEntity.ok(blackJackService.doubleDown(game));
|
return ResponseEntity.ok(blackJackService.doubleDown(game));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/blackjack/{id}/split")
|
|
||||||
public ResponseEntity<Object> split(@PathVariable Long id) {
|
|
||||||
BlackJackGameEntity game = getBlackJackGame(id);
|
|
||||||
|
|
||||||
return ResponseEntity.ok(blackJackService.split(game));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/blackjack/start")
|
@PostMapping("/blackjack/start")
|
||||||
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid BetDto betDto) {
|
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid BetDto betDto) {
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||||
|
|
|
@ -51,15 +51,4 @@ public class BlackJackGameEntity {
|
||||||
@JsonManagedReference
|
@JsonManagedReference
|
||||||
@SQLRestriction("card_type = 'DEALER'")
|
@SQLRestriction("card_type = 'DEALER'")
|
||||||
private List<CardEntity> dealerCards = new ArrayList<>();
|
private List<CardEntity> dealerCards = new ArrayList<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
|
||||||
@JsonManagedReference
|
|
||||||
@SQLRestriction("card_type = 'PLAYER_SPLIT'")
|
|
||||||
private List<CardEntity> playerSplitCards = new ArrayList<>();
|
|
||||||
|
|
||||||
@Column(name = "split_bet")
|
|
||||||
private BigDecimal splitBet;
|
|
||||||
|
|
||||||
@Column(name = "is_split")
|
|
||||||
private boolean isSplit;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package de.szut.casino.blackjack;
|
||||||
|
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
import de.szut.casino.user.UserRepository;
|
import de.szut.casino.user.UserRepository;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -24,7 +24,6 @@ public class BlackJackService {
|
||||||
return blackJackGameRepository.findById(id).orElse(null);
|
return blackJackGameRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
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);
|
||||||
|
@ -85,36 +84,6 @@ public class BlackJackService {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public BlackJackGameEntity split(BlackJackGameEntity game) {
|
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
|
||||||
game.getPlayerCards().size() != 2 ||
|
|
||||||
game.isSplit() ||
|
|
||||||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
|
||||||
return game;
|
|
||||||
}
|
|
||||||
|
|
||||||
UserEntity user = getUserWithFreshData(game.getUser());
|
|
||||||
BigDecimal splitBet = game.getBet();
|
|
||||||
|
|
||||||
if (user.getBalance().compareTo(splitBet) < 0) {
|
|
||||||
return game;
|
|
||||||
}
|
|
||||||
|
|
||||||
deductBetFromBalance(user, splitBet);
|
|
||||||
game.setSplitBet(splitBet);
|
|
||||||
game.setSplit(true);
|
|
||||||
|
|
||||||
CardEntity card = game.getPlayerCards().remove(1);
|
|
||||||
card.setCardType(CardType.PLAYER_SPLIT);
|
|
||||||
game.getPlayerSplitCards().add(card);
|
|
||||||
|
|
||||||
dealCardToPlayer(game);
|
|
||||||
dealCardToSplitHand(game);
|
|
||||||
|
|
||||||
return processGameBasedOnState(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BlackJackGameEntity processGameBasedOnState(BlackJackGameEntity game) {
|
private BlackJackGameEntity processGameBasedOnState(BlackJackGameEntity game) {
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||||
this.blackJackGameRepository.delete(game);
|
this.blackJackGameRepository.delete(game);
|
||||||
|
@ -154,26 +123,7 @@ public class BlackJackService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealCardToSplitHand(BlackJackGameEntity game) {
|
|
||||||
CardEntity card = drawCardFromDeck(game);
|
|
||||||
card.setCardType(CardType.PLAYER_SPLIT);
|
|
||||||
game.getPlayerSplitCards().add(card);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
||||||
if (game.isSplit()) {
|
|
||||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
|
||||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
|
||||||
|
|
||||||
if (mainHandValue > 21 && splitHandValue > 21) {
|
|
||||||
game.setState(BlackJackState.PLAYER_LOST);
|
|
||||||
updateUserBalance(game, false);
|
|
||||||
} else if (mainHandValue <= 21 && splitHandValue <= 21) {
|
|
||||||
game.setState(BlackJackState.IN_PROGRESS);
|
|
||||||
} else {
|
|
||||||
game.setState(BlackJackState.IN_PROGRESS);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
game.setState(getState(game));
|
game.setState(getState(game));
|
||||||
|
|
||||||
if (game.getState() == BlackJackState.PLAYER_WON) {
|
if (game.getState() == BlackJackState.PLAYER_WON) {
|
||||||
|
@ -182,7 +132,6 @@ public class BlackJackService {
|
||||||
updateUserBalance(game, false);
|
updateUserBalance(game, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
||||||
int playerValue = calculateHandValue(game.getPlayerCards());
|
int playerValue = calculateHandValue(game.getPlayerCards());
|
||||||
|
@ -205,41 +154,16 @@ public class BlackJackService {
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
||||||
UserEntity user = getUserWithFreshData(game.getUser());
|
UserEntity user = getUserWithFreshData(game.getUser());
|
||||||
BigDecimal totalBet = game.getBet();
|
BigDecimal totalBet = game.getBet();
|
||||||
BigDecimal balance = user.getBalance();
|
BigDecimal balance = user.getBalance();
|
||||||
|
|
||||||
if (game.isSplit()) {
|
|
||||||
totalBet = totalBet.add(game.getSplitBet());
|
|
||||||
|
|
||||||
if (isWin) {
|
|
||||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
|
||||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
|
||||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
|
||||||
|
|
||||||
if (mainHandValue <= 21 && (dealerValue > 21 || mainHandValue > dealerValue)) {
|
|
||||||
balance = balance.add(game.getBet().multiply(BigDecimal.valueOf(2)));
|
|
||||||
} else if (mainHandValue == dealerValue) {
|
|
||||||
balance = balance.add(game.getBet());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (splitHandValue <= 21 && (dealerValue > 21 || splitHandValue > dealerValue)) {
|
|
||||||
balance = balance.add(game.getSplitBet().multiply(BigDecimal.valueOf(2)));
|
|
||||||
} else if (splitHandValue == dealerValue) {
|
|
||||||
balance = balance.add(game.getSplitBet());
|
|
||||||
}
|
|
||||||
} else if (game.getState() == BlackJackState.DRAW) {
|
|
||||||
balance = balance.add(totalBet);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (isWin) {
|
if (isWin) {
|
||||||
balance = balance.add(totalBet.multiply(BigDecimal.valueOf(2)));
|
balance = balance.add(totalBet.multiply(BigDecimal.valueOf(2)));
|
||||||
} else if (game.getState() == BlackJackState.DRAW) {
|
} else if (game.getState() == BlackJackState.DRAW) {
|
||||||
balance = balance.add(totalBet);
|
balance = balance.add(totalBet);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
user.setBalance(balance);
|
user.setBalance(balance);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
|
@ -36,5 +36,5 @@ public class CardEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CardType {
|
enum CardType {
|
||||||
DECK, PLAYER, DEALER, PLAYER_SPLIT
|
DECK, PLAYER, DEALER
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue