All checks were successful
Build docs / build-docs (pull_request) Successful in 29s
Label PRs based on size / Check PR size (pull_request) Successful in 27s
CI / Get Changed Files (pull_request) Successful in 34s
CI / Backend Tests (pull_request) Has been skipped
CI / eslint (pull_request) Has been skipped
Pull Request Labeler / labeler (pull_request_target) Successful in 4s
CI / Checkstyle Main (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Playwright (pull_request) Has been skipped
Claude PR Review / claude-code (pull_request) Successful in 1m41s
35 lines
No EOL
1.2 KiB
Java
35 lines
No EOL
1.2 KiB
Java
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;
|
|
private final BalanceService balanceService;
|
|
|
|
public CoinflipService(BalanceService balanceService, Random random) {
|
|
this.balanceService = balanceService;
|
|
this.random = random;
|
|
}
|
|
|
|
public CoinflipResult play(UserEntity user, CoinflipDto coinflipDto) {
|
|
this.balanceService.subtractFunds(user, coinflipDto.getBetAmount());
|
|
|
|
CoinSide coinSide = this.random.nextBoolean() ? CoinSide.HEAD : CoinSide.TAILS;
|
|
CoinflipResult coinflipResult = new CoinflipResult(false, BigDecimal.ZERO, coinSide);
|
|
if (coinSide == coinflipDto.getCoinSide()) {
|
|
coinflipResult.setWin(true);
|
|
|
|
BigDecimal payout = coinflipDto.getBetAmount().multiply(BigDecimal.TWO);
|
|
this.balanceService.addFunds(user, payout);
|
|
coinflipResult.setPayout(payout);
|
|
}
|
|
|
|
return coinflipResult;
|
|
}
|
|
} |