Merge pull request 'refactor: refactor blackjack service' (!231) from refactor-blackjack into main
Reviewed-on: #231 Reviewed-by: Constantin Simonis <constantin@simonis.lol> Reviewed-by: Jan K9f <jan@kjan.email>
This commit is contained in:
commit
b34b12cfc7
2 changed files with 31 additions and 25 deletions
|
@ -1,10 +1,7 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
||||
import de.szut.casino.exceptionHandling.exceptions.UserBlackJackGameMismatchException;
|
||||
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||
import de.szut.casino.shared.dto.BetDto;
|
||||
import de.szut.casino.shared.service.BalanceService;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserService;
|
||||
import jakarta.validation.Valid;
|
||||
|
@ -13,18 +10,15 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class BlackJackGameController {
|
||||
|
||||
private final BalanceService balanceService;
|
||||
private final UserService userService;
|
||||
private final BlackJackService blackJackService;
|
||||
|
||||
public BlackJackGameController(BalanceService balanceService, UserService userService, BlackJackService blackJackService) {
|
||||
this.balanceService = balanceService;
|
||||
public BlackJackGameController(UserService userService, BlackJackService blackJackService) {
|
||||
this.blackJackService = blackJackService;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
@ -59,13 +53,7 @@ public class BlackJackGameController {
|
|||
|
||||
@PostMapping("/blackjack/start")
|
||||
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid BetDto betDto) {
|
||||
UserEntity user = this.userService.getCurrentUser();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, betDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount()));
|
||||
return ResponseEntity.ok(blackJackService.createBlackJackGame(betDto));
|
||||
}
|
||||
|
||||
private BlackJackGameEntity getBlackJackGame(Long gameId) {
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
||||
import de.szut.casino.shared.dto.BetDto;
|
||||
import de.szut.casino.shared.service.BalanceService;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import de.szut.casino.user.UserService;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -13,11 +17,22 @@ import java.util.Random;
|
|||
public class BlackJackService {
|
||||
private final BlackJackGameRepository blackJackGameRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final Random random = new Random();
|
||||
private final Random random;
|
||||
private final BalanceService balanceService;
|
||||
private final UserService userService;
|
||||
|
||||
public BlackJackService(BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
|
||||
public BlackJackService(
|
||||
BlackJackGameRepository blackJackGameRepository,
|
||||
UserRepository userRepository,
|
||||
Random random,
|
||||
BalanceService balanceService,
|
||||
UserService userService
|
||||
) {
|
||||
this.blackJackGameRepository = blackJackGameRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.random = random;
|
||||
this.balanceService = balanceService;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
public BlackJackGameEntity getBlackJackGame(Long id) {
|
||||
|
@ -25,16 +40,23 @@ public class BlackJackService {
|
|||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
|
||||
public BlackJackGameEntity createBlackJackGame(BetDto betDto) {
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, betDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
|
||||
this.balanceService.subtractFunds(user, betDto.getBetAmount());
|
||||
|
||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||
game.setUser(user);
|
||||
game.setBet(betAmount);
|
||||
game.setBet(betDto.getBetAmount());
|
||||
|
||||
initializeDeck(game);
|
||||
dealInitialCards(game);
|
||||
|
||||
game.setState(getState(game));
|
||||
deductBetFromBalance(user, betAmount);
|
||||
|
||||
return processGameBasedOnState(game);
|
||||
}
|
||||
|
@ -72,7 +94,8 @@ public class BlackJackService {
|
|||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal additionalBet = game.getBet();
|
||||
|
||||
deductBetFromBalance(user, additionalBet);
|
||||
this.balanceService.subtractFunds(user, additionalBet);
|
||||
|
||||
game.setBet(game.getBet().add(additionalBet));
|
||||
|
||||
dealCardToPlayer(game);
|
||||
|
@ -150,11 +173,6 @@ public class BlackJackService {
|
|||
}
|
||||
}
|
||||
|
||||
private void deductBetFromBalance(UserEntity user, BigDecimal betAmount) {
|
||||
user.setBalance(user.getBalance().subtract(betAmount));
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal totalBet = game.getBet();
|
||||
|
|
Reference in a new issue