refactor: Extract game creation logic to service
All checks were successful
All checks were successful
This commit is contained in:
parent
1b9bc90920
commit
ca87c684b1
2 changed files with 37 additions and 27 deletions
|
@ -2,11 +2,9 @@ package de.szut.casino.blackjack;
|
||||||
|
|
||||||
import de.szut.casino.blackjack.dto.CreateBlackJackGameDto;
|
import de.szut.casino.blackjack.dto.CreateBlackJackGameDto;
|
||||||
import de.szut.casino.user.UserEntity;
|
import de.szut.casino.user.UserEntity;
|
||||||
import de.szut.casino.user.UserRepository;
|
|
||||||
import de.szut.casino.user.UserService;
|
import de.szut.casino.user.UserService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
@ -24,14 +22,10 @@ public class BlackJackGameController {
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final BlackJackService blackJackService;
|
private final BlackJackService blackJackService;
|
||||||
private final BlackJackGameRepository blackJackGameRepository;
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
|
|
||||||
public BlackJackGameController(UserService userService, BlackJackService blackJackService, BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
|
public BlackJackGameController(UserService userService, BlackJackService blackJackService) {
|
||||||
this.userService = userService;
|
|
||||||
this.blackJackService = blackJackService;
|
this.blackJackService = blackJackService;
|
||||||
this.blackJackGameRepository = blackJackGameRepository;
|
this.userService = userService;
|
||||||
this.userRepository = userRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/blackjack/start")
|
@PostMapping("/blackjack/start")
|
||||||
|
@ -58,23 +52,6 @@ public class BlackJackGameController {
|
||||||
return ResponseEntity.badRequest().body(errorResponse);
|
return ResponseEntity.badRequest().body(errorResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betAmount));
|
||||||
game.setUser(user);
|
|
||||||
game.setBet(betAmount);
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
|
||||||
CardEntity playerCard = blackJackService.createRandomCard(game);
|
|
||||||
game.getPlayerCards().add(playerCard);
|
|
||||||
}
|
|
||||||
|
|
||||||
CardEntity dealerCard = blackJackService.createRandomCard(game);
|
|
||||||
game.getDealerCards().add(dealerCard);
|
|
||||||
|
|
||||||
user.setBalance(user.getBalance().subtract(betAmount));
|
|
||||||
|
|
||||||
userRepository.save(user);
|
|
||||||
blackJackGameRepository.save(game);
|
|
||||||
|
|
||||||
return ResponseEntity.ok(game);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,46 @@
|
||||||
package de.szut.casino.blackjack;
|
package de.szut.casino.blackjack;
|
||||||
|
|
||||||
|
import de.szut.casino.user.UserEntity;
|
||||||
|
import de.szut.casino.user.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class BlackJackService {
|
public class BlackJackService {
|
||||||
|
private final BlackJackGameRepository blackJackGameRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
public BlackJackService(BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
|
||||||
|
this.blackJackGameRepository = blackJackGameRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
|
|
||||||
public CardEntity createRandomCard(BlackJackGameEntity game) {
|
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
|
||||||
|
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||||
|
game.setUser(user);
|
||||||
|
game.setBet(betAmount);
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
CardEntity playerCard = createRandomCard(game);
|
||||||
|
game.getPlayerCards().add(playerCard);
|
||||||
|
}
|
||||||
|
|
||||||
|
CardEntity dealerCard = createRandomCard(game);
|
||||||
|
game.getDealerCards().add(dealerCard);
|
||||||
|
|
||||||
|
user.setBalance(user.getBalance().subtract(betAmount));
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
blackJackGameRepository.save(game);
|
||||||
|
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
|
||||||
|
private CardEntity createRandomCard(BlackJackGameEntity game) {
|
||||||
CardEntity card = new CardEntity();
|
CardEntity card = new CardEntity();
|
||||||
card.setGame(game);
|
card.setGame(game);
|
||||||
card.setSuit(Suit.values()[random.nextInt(Suit.values().length)]);
|
card.setSuit(Suit.values()[random.nextInt(Suit.values().length)]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue