Compare commits
No commits in common. "v1.55.0" and "v1.54.0" have entirely different histories.
6 changed files with 1 additions and 121 deletions
|
@ -1,6 +0,0 @@
|
|||
package de.szut.casino.coinflip;
|
||||
|
||||
public enum CoinSide {
|
||||
HEAD,
|
||||
TAILS;
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package de.szut.casino.coinflip;
|
||||
|
||||
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 CoinflipController {
|
||||
private final UserService userService;
|
||||
private final BalanceService balanceService;
|
||||
private final CoinflipService coinflipService;
|
||||
|
||||
public CoinflipController(UserService userService, BalanceService balanceService, CoinflipService coinflipService) {
|
||||
this.userService = userService;
|
||||
this.balanceService = balanceService;
|
||||
this.coinflipService = coinflipService;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/coinflip")
|
||||
public ResponseEntity<Object> coinFlip(@RequestBody @Valid CoinflipDto coinflipDto) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, coinflipDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(coinflipService.play(user, coinflipDto));
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
package de.szut.casino.coinflip;
|
||||
|
||||
import de.szut.casino.shared.dto.BetDto;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class CoinflipDto extends BetDto {
|
||||
@NotNull(message = "chosen side cannot be null")
|
||||
private CoinSide coinSide;
|
||||
|
||||
public CoinflipDto(BigDecimal betAmount, CoinSide coinSide) {
|
||||
super(betAmount);
|
||||
this.coinSide = coinSide;
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package de.szut.casino.coinflip;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Setter
|
||||
@Getter
|
||||
public class CoinflipResult {
|
||||
private boolean isWin;
|
||||
private BigDecimal payout;
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package de.szut.casino.coinflip;
|
||||
|
||||
import de.szut.casino.shared.service.BalanceService;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class CoinflipService {
|
||||
private final Random random = new Random();
|
||||
private final BalanceService balanceService;
|
||||
|
||||
public CoinflipService(BalanceService balanceService) {
|
||||
this.balanceService = balanceService;
|
||||
}
|
||||
|
||||
public CoinflipResult play(UserEntity user, CoinflipDto coinflipDto) {
|
||||
this.balanceService.subtractFunds(user, coinflipDto.getBetAmount());
|
||||
|
||||
CoinflipResult coinflipResult = new CoinflipResult(false, BigDecimal.ZERO);
|
||||
CoinSide coinSide = this.random.nextBoolean() ? CoinSide.HEAD : CoinSide.TAILS;
|
||||
if (coinSide == coinflipDto.getCoinSide()) {
|
||||
coinflipResult.setWin(true);
|
||||
|
||||
BigDecimal payout = coinflipDto.getBetAmount().multiply(BigDecimal.TWO);
|
||||
this.balanceService.addFunds(user, payout);
|
||||
coinflipResult.setPayout(payout);
|
||||
}
|
||||
|
||||
return coinflipResult;
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ import java.math.BigDecimal;
|
|||
|
||||
@Service
|
||||
public class BalanceService {
|
||||
private final UserRepository userRepository;
|
||||
private UserRepository userRepository;
|
||||
|
||||
public BalanceService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
|
|
Reference in a new issue