Compare commits
3 commits
Author | SHA1 | Date | |
---|---|---|---|
2651e34bf1 |
|||
|
e9e2eba46f | ||
|
c277b89ffc |
4 changed files with 11 additions and 104 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;
|
||||||
|
@ -85,36 +85,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,33 +124,13 @@ 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()) {
|
game.setState(getState(game));
|
||||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
|
||||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
|
||||||
|
|
||||||
if (mainHandValue > 21 && splitHandValue > 21) {
|
if (game.getState() == BlackJackState.PLAYER_WON) {
|
||||||
game.setState(BlackJackState.PLAYER_LOST);
|
updateUserBalance(game, true);
|
||||||
updateUserBalance(game, false);
|
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
||||||
} else if (mainHandValue <= 21 && splitHandValue <= 21) {
|
updateUserBalance(game, false);
|
||||||
game.setState(BlackJackState.IN_PROGRESS);
|
|
||||||
} else {
|
|
||||||
game.setState(BlackJackState.IN_PROGRESS);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
game.setState(getState(game));
|
|
||||||
|
|
||||||
if (game.getState() == BlackJackState.PLAYER_WON) {
|
|
||||||
updateUserBalance(game, true);
|
|
||||||
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
|
||||||
updateUserBalance(game, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,40 +155,15 @@ 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()) {
|
if (isWin) {
|
||||||
totalBet = totalBet.add(game.getSplitBet());
|
balance = balance.add(totalBet.multiply(BigDecimal.valueOf(2)));
|
||||||
|
} else if (game.getState() == BlackJackState.DRAW) {
|
||||||
if (isWin) {
|
balance = balance.add(totalBet);
|
||||||
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) {
|
|
||||||
balance = balance.add(totalBet.multiply(BigDecimal.valueOf(2)));
|
|
||||||
} else if (game.getState() == BlackJackState.DRAW) {
|
|
||||||
balance = balance.add(totalBet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user.setBalance(balance);
|
user.setBalance(balance);
|
||||||
|
|
|
@ -36,5 +36,5 @@ public class CardEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CardType {
|
enum CardType {
|
||||||
DECK, PLAYER, DEALER, PLAYER_SPLIT
|
DECK, PLAYER, DEALER
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue