Compare commits
No commits in common. "v1.62.0" and "v1.61.1" have entirely different histories.
6 changed files with 43 additions and 214 deletions
|
@ -29,14 +29,14 @@ public class BlackJackService {
|
||||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||||
game.setUser(user);
|
game.setUser(user);
|
||||||
game.setBet(betAmount);
|
game.setBet(betAmount);
|
||||||
|
|
||||||
initializeDeck(game);
|
initializeDeck(game);
|
||||||
dealInitialCards(game);
|
dealInitialCards(game);
|
||||||
|
|
||||||
game.setState(getState(game));
|
game.setState(getState(game));
|
||||||
deductBetFromBalance(user, betAmount);
|
deductBetFromBalance(user, betAmount);
|
||||||
|
|
||||||
return processGameBasedOnState(game);
|
return blackJackGameRepository.save(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -44,11 +44,12 @@ public class BlackJackService {
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
dealCardToPlayer(game);
|
dealCardToPlayer(game);
|
||||||
|
|
||||||
updateGameStateAndBalance(game);
|
updateGameStateAndBalance(game);
|
||||||
|
|
||||||
return processGameBasedOnState(game);
|
return blackJackGameRepository.save(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -56,11 +57,11 @@ public class BlackJackService {
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
dealCardsToDealerUntilMinimumScore(game);
|
dealCardsToDealerUntilMinimumScore(game);
|
||||||
determineWinnerAndUpdateBalance(game);
|
determineWinnerAndUpdateBalance(game);
|
||||||
|
|
||||||
return processGameBasedOnState(game);
|
return blackJackGameRepository.save(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@ -68,107 +69,98 @@ public class BlackJackService {
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) {
|
if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = getUserWithFreshData(game.getUser());
|
UserEntity user = getUserWithFreshData(game.getUser());
|
||||||
BigDecimal additionalBet = game.getBet();
|
BigDecimal additionalBet = game.getBet();
|
||||||
|
|
||||||
deductBetFromBalance(user, additionalBet);
|
deductBetFromBalance(user, additionalBet);
|
||||||
game.setBet(game.getBet().add(additionalBet));
|
game.setBet(game.getBet().add(additionalBet));
|
||||||
|
|
||||||
dealCardToPlayer(game);
|
dealCardToPlayer(game);
|
||||||
updateGameStateAndBalance(game);
|
updateGameStateAndBalance(game);
|
||||||
|
|
||||||
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
||||||
return stand(game);
|
return stand(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public BlackJackGameEntity split(BlackJackGameEntity game) {
|
public BlackJackGameEntity split(BlackJackGameEntity game) {
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
||||||
game.getPlayerCards().size() != 2 ||
|
game.getPlayerCards().size() != 2 ||
|
||||||
game.isSplit() ||
|
game.isSplit() ||
|
||||||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserEntity user = getUserWithFreshData(game.getUser());
|
UserEntity user = getUserWithFreshData(game.getUser());
|
||||||
BigDecimal splitBet = game.getBet();
|
BigDecimal splitBet = game.getBet();
|
||||||
|
|
||||||
if (user.getBalance().compareTo(splitBet) < 0) {
|
if (user.getBalance().compareTo(splitBet) < 0) {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
deductBetFromBalance(user, splitBet);
|
deductBetFromBalance(user, splitBet);
|
||||||
game.setSplitBet(splitBet);
|
game.setSplitBet(splitBet);
|
||||||
game.setSplit(true);
|
game.setSplit(true);
|
||||||
|
|
||||||
CardEntity card = game.getPlayerCards().remove(1);
|
CardEntity card = game.getPlayerCards().remove(1);
|
||||||
card.setCardType(CardType.PLAYER_SPLIT);
|
card.setCardType(CardType.PLAYER_SPLIT);
|
||||||
game.getPlayerSplitCards().add(card);
|
game.getPlayerSplitCards().add(card);
|
||||||
|
|
||||||
dealCardToPlayer(game);
|
dealCardToPlayer(game);
|
||||||
dealCardToSplitHand(game);
|
dealCardToSplitHand(game);
|
||||||
|
|
||||||
return blackJackGameRepository.save(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BlackJackGameEntity processGameBasedOnState(BlackJackGameEntity game) {
|
|
||||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
|
||||||
this.blackJackGameRepository.delete(game);
|
|
||||||
return game;
|
|
||||||
}
|
|
||||||
|
|
||||||
return blackJackGameRepository.save(game);
|
return blackJackGameRepository.save(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) {
|
private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) {
|
||||||
return blackJackGameRepository.findById(game.getId()).orElse(game);
|
return blackJackGameRepository.findById(game.getId()).orElse(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserEntity getUserWithFreshData(UserEntity user) {
|
private UserEntity getUserWithFreshData(UserEntity user) {
|
||||||
return userRepository.findById(user.getId()).orElse(user);
|
return userRepository.findById(user.getId()).orElse(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealInitialCards(BlackJackGameEntity game) {
|
private void dealInitialCards(BlackJackGameEntity game) {
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
dealCardToPlayer(game);
|
dealCardToPlayer(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
dealCardToDealer(game);
|
dealCardToDealer(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealCardToPlayer(BlackJackGameEntity game) {
|
private void dealCardToPlayer(BlackJackGameEntity game) {
|
||||||
CardEntity card = drawCardFromDeck(game);
|
CardEntity card = drawCardFromDeck(game);
|
||||||
card.setCardType(CardType.PLAYER);
|
card.setCardType(CardType.PLAYER);
|
||||||
game.getPlayerCards().add(card);
|
game.getPlayerCards().add(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealCardToDealer(BlackJackGameEntity game) {
|
private void dealCardToDealer(BlackJackGameEntity game) {
|
||||||
CardEntity card = drawCardFromDeck(game);
|
CardEntity card = drawCardFromDeck(game);
|
||||||
card.setCardType(CardType.DEALER);
|
card.setCardType(CardType.DEALER);
|
||||||
game.getDealerCards().add(card);
|
game.getDealerCards().add(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealCardsToDealerUntilMinimumScore(BlackJackGameEntity game) {
|
private void dealCardsToDealerUntilMinimumScore(BlackJackGameEntity game) {
|
||||||
while (calculateHandValue(game.getDealerCards()) < 17) {
|
while (calculateHandValue(game.getDealerCards()) < 17) {
|
||||||
dealCardToDealer(game);
|
dealCardToDealer(game);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dealCardToSplitHand(BlackJackGameEntity game) {
|
private void dealCardToSplitHand(BlackJackGameEntity game) {
|
||||||
CardEntity card = drawCardFromDeck(game);
|
CardEntity card = drawCardFromDeck(game);
|
||||||
card.setCardType(CardType.PLAYER_SPLIT);
|
card.setCardType(CardType.PLAYER_SPLIT);
|
||||||
game.getPlayerSplitCards().add(card);
|
game.getPlayerSplitCards().add(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
||||||
if (game.isSplit()) {
|
if (game.isSplit()) {
|
||||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
||||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
||||||
|
|
||||||
if (mainHandValue > 21 && splitHandValue > 21) {
|
if (mainHandValue > 21 && splitHandValue > 21) {
|
||||||
game.setState(BlackJackState.PLAYER_LOST);
|
game.setState(BlackJackState.PLAYER_LOST);
|
||||||
updateUserBalance(game, false);
|
updateUserBalance(game, false);
|
||||||
|
@ -179,7 +171,7 @@ public class BlackJackService {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
game.setState(getState(game));
|
game.setState(getState(game));
|
||||||
|
|
||||||
if (game.getState() == BlackJackState.PLAYER_WON) {
|
if (game.getState() == BlackJackState.PLAYER_WON) {
|
||||||
updateUserBalance(game, true);
|
updateUserBalance(game, true);
|
||||||
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
||||||
|
@ -187,7 +179,7 @@ public class BlackJackService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
||||||
int playerValue = calculateHandValue(game.getPlayerCards());
|
int playerValue = calculateHandValue(game.getPlayerCards());
|
||||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||||
|
@ -203,7 +195,7 @@ public class BlackJackService {
|
||||||
updateUserBalance(game, false);
|
updateUserBalance(game, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deductBetFromBalance(UserEntity user, BigDecimal betAmount) {
|
private void deductBetFromBalance(UserEntity user, BigDecimal betAmount) {
|
||||||
user.setBalance(user.getBalance().subtract(betAmount));
|
user.setBalance(user.getBalance().subtract(betAmount));
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
@ -214,21 +206,21 @@ public class BlackJackService {
|
||||||
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 (game.isSplit()) {
|
||||||
totalBet = totalBet.add(game.getSplitBet());
|
totalBet = totalBet.add(game.getSplitBet());
|
||||||
|
|
||||||
if (isWin) {
|
if (isWin) {
|
||||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
||||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
||||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||||
|
|
||||||
if (mainHandValue <= 21 && (dealerValue > 21 || mainHandValue > dealerValue)) {
|
if (mainHandValue <= 21 && (dealerValue > 21 || mainHandValue > dealerValue)) {
|
||||||
balance = balance.add(game.getBet().multiply(BigDecimal.valueOf(2)));
|
balance = balance.add(game.getBet().multiply(BigDecimal.valueOf(2)));
|
||||||
} else if (mainHandValue == dealerValue) {
|
} else if (mainHandValue == dealerValue) {
|
||||||
balance = balance.add(game.getBet());
|
balance = balance.add(game.getBet());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (splitHandValue <= 21 && (dealerValue > 21 || splitHandValue > dealerValue)) {
|
if (splitHandValue <= 21 && (dealerValue > 21 || splitHandValue > dealerValue)) {
|
||||||
balance = balance.add(game.getSplitBet().multiply(BigDecimal.valueOf(2)));
|
balance = balance.add(game.getSplitBet().multiply(BigDecimal.valueOf(2)));
|
||||||
} else if (splitHandValue == dealerValue) {
|
} else if (splitHandValue == dealerValue) {
|
||||||
|
@ -244,7 +236,7 @@ public class BlackJackService {
|
||||||
balance = balance.add(totalBet);
|
balance = balance.add(totalBet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
user.setBalance(balance);
|
user.setBalance(balance);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
package de.szut.casino.dice;
|
|
||||||
|
|
||||||
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
|
||||||
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
|
||||||
import de.szut.casino.shared.service.BalanceService;
|
|
||||||
import de.szut.casino.user.UserEntity;
|
|
||||||
import de.szut.casino.user.UserService;
|
|
||||||
import jakarta.validation.Valid;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class DiceController {
|
|
||||||
private final UserService userService;
|
|
||||||
private final BalanceService balanceService;
|
|
||||||
private final DiceService diceService;
|
|
||||||
|
|
||||||
public DiceController(UserService userService, BalanceService balanceService, DiceService diceService) {
|
|
||||||
this.userService = userService;
|
|
||||||
this.balanceService = balanceService;
|
|
||||||
this.diceService = diceService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/dice")
|
|
||||||
public ResponseEntity<Object> rollDice(@RequestBody @Valid DiceDto diceDto) {
|
|
||||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
|
||||||
|
|
||||||
if (optionalUser.isEmpty()) {
|
|
||||||
throw new UserNotFoundException();
|
|
||||||
}
|
|
||||||
|
|
||||||
UserEntity user = optionalUser.get();
|
|
||||||
|
|
||||||
if (!this.balanceService.hasFunds(user, diceDto)) {
|
|
||||||
throw new InsufficientFundsException();
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseEntity.ok(diceService.play(user, diceDto));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
package de.szut.casino.dice;
|
|
||||||
|
|
||||||
import de.szut.casino.shared.dto.BetDto;
|
|
||||||
import jakarta.validation.constraints.DecimalMax;
|
|
||||||
import jakarta.validation.constraints.DecimalMin;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class DiceDto extends BetDto {
|
|
||||||
private boolean rollOver;
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@DecimalMin(value = "1.00")
|
|
||||||
@DecimalMax(value = "100")
|
|
||||||
private BigDecimal targetValue;
|
|
||||||
|
|
||||||
public DiceDto(BigDecimal betAmount, boolean rollOver, BigDecimal targetValue) {
|
|
||||||
super(betAmount);
|
|
||||||
this.rollOver = rollOver;
|
|
||||||
this.targetValue = targetValue;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
package de.szut.casino.dice;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
public class DiceResult {
|
|
||||||
private boolean win;
|
|
||||||
private BigDecimal payout;
|
|
||||||
private BigDecimal rolledValue;
|
|
||||||
|
|
||||||
public DiceResult(boolean win, BigDecimal payout, BigDecimal rolledValue) {
|
|
||||||
this.win = win;
|
|
||||||
this.payout = payout;
|
|
||||||
this.rolledValue = rolledValue;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,68 +0,0 @@
|
||||||
package de.szut.casino.dice;
|
|
||||||
|
|
||||||
import de.szut.casino.shared.service.BalanceService;
|
|
||||||
import de.szut.casino.user.UserEntity;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.RoundingMode;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class DiceService {
|
|
||||||
private static final int MAX_DICE_VALUE = 100;
|
|
||||||
private final Random random = new Random();
|
|
||||||
private final BalanceService balanceService;
|
|
||||||
|
|
||||||
public DiceService(BalanceService balanceService) {
|
|
||||||
this.balanceService = balanceService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DiceResult play(UserEntity user, DiceDto diceDto) {
|
|
||||||
balanceService.subtractFunds(user, diceDto.getBetAmount());
|
|
||||||
|
|
||||||
int rolledValue = random.nextInt(MAX_DICE_VALUE) + 1;
|
|
||||||
BigDecimal rolledValueDecimal = BigDecimal.valueOf(rolledValue);
|
|
||||||
|
|
||||||
BigDecimal targetValue = diceDto.getTargetValue();
|
|
||||||
boolean isRollOver = diceDto.isRollOver();
|
|
||||||
|
|
||||||
boolean winConditionMet = isWinConditionMet(rolledValueDecimal, targetValue, isRollOver);
|
|
||||||
|
|
||||||
if (!winConditionMet) {
|
|
||||||
return new DiceResult(false, BigDecimal.ZERO, rolledValueDecimal);
|
|
||||||
}
|
|
||||||
|
|
||||||
BigDecimal winChance = calculateWinChance(targetValue, isRollOver);
|
|
||||||
BigDecimal multiplier = calculateMultiplier(winChance);
|
|
||||||
|
|
||||||
BigDecimal payout = diceDto.getBetAmount().multiply(multiplier);
|
|
||||||
balanceService.addFunds(user, payout);
|
|
||||||
|
|
||||||
return new DiceResult(true, payout, rolledValueDecimal);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isWinConditionMet(BigDecimal rolledValue, BigDecimal targetValue, boolean isRollOver) {
|
|
||||||
if (isRollOver) {
|
|
||||||
return rolledValue.compareTo(targetValue) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return rolledValue.compareTo(targetValue) < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private BigDecimal calculateWinChance(BigDecimal targetValue, boolean isRollOver) {
|
|
||||||
if (isRollOver) {
|
|
||||||
return BigDecimal.valueOf(MAX_DICE_VALUE).subtract(targetValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
return targetValue.subtract(BigDecimal.ONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BigDecimal calculateMultiplier(BigDecimal winChance) {
|
|
||||||
if (winChance.compareTo(BigDecimal.ZERO) > 0) {
|
|
||||||
return BigDecimal.valueOf(MAX_DICE_VALUE - 1).divide(winChance, 4, RoundingMode.HALF_UP);
|
|
||||||
}
|
|
||||||
|
|
||||||
return BigDecimal.ZERO;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -87,10 +87,6 @@ export class RecoverPasswordComponent implements OnInit {
|
||||||
'Wenn ein Konto mit dieser E-Mail existiert, wird eine E-Mail mit weiteren Anweisungen gesendet.'
|
'Wenn ein Konto mit dieser E-Mail existiert, wird eine E-Mail mit weiteren Anweisungen gesendet.'
|
||||||
);
|
);
|
||||||
this.emailForm.reset();
|
this.emailForm.reset();
|
||||||
setTimeout(() => {
|
|
||||||
this.closeDialog.emit();
|
|
||||||
this.switchToLogin.emit();
|
|
||||||
}, 2000);
|
|
||||||
},
|
},
|
||||||
error: (err) => {
|
error: (err) => {
|
||||||
this.isLoading.set(false);
|
this.isLoading.set(false);
|
||||||
|
|
Reference in a new issue