refactor(lottery): improve code structure and readability
Some checks failed
CI / Get Changed Files (pull_request) Successful in 7s
CI / eslint (pull_request) Successful in 38s
CI / Docker frontend validation (pull_request) Successful in 50s
CI / oxlint (pull_request) Successful in 27s
CI / prettier (pull_request) Failing after 28s
CI / Docker backend validation (pull_request) Successful in 1m23s
CI / Checkstyle Main (pull_request) Successful in 1m20s
CI / test-build (pull_request) Successful in 40s
Some checks failed
CI / Get Changed Files (pull_request) Successful in 7s
CI / eslint (pull_request) Successful in 38s
CI / Docker frontend validation (pull_request) Successful in 50s
CI / oxlint (pull_request) Successful in 27s
CI / prettier (pull_request) Failing after 28s
CI / Docker backend validation (pull_request) Successful in 1m23s
CI / Checkstyle Main (pull_request) Successful in 1m20s
CI / test-build (pull_request) Successful in 40s
This commit is contained in:
parent
a3f34e960b
commit
7aefe67aa0
5 changed files with 200 additions and 140 deletions
|
@ -1,91 +1,91 @@
|
|||
package de.szut.casino.lootboxes;
|
||||
|
||||
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
||||
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||
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.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class LootBoxController {
|
||||
private final LootBoxRepository lootBoxRepository;
|
||||
private final UserService userService;
|
||||
private final LootBoxService lootBoxService;
|
||||
|
||||
public LootBoxController(LootBoxRepository lootBoxRepository, UserService userService, LootBoxService lootBoxService) {
|
||||
this.lootBoxRepository = lootBoxRepository;
|
||||
this.userService = userService;
|
||||
this.lootBoxService = lootBoxService;
|
||||
}
|
||||
|
||||
@GetMapping("/lootboxes")
|
||||
public List<LootBoxEntity> getAllLootBoxes() {
|
||||
return lootBoxRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/lootboxes/{id}")
|
||||
public ResponseEntity<Object> purchaseLootBox(@PathVariable Long id) {
|
||||
Optional<LootBoxEntity> optionalLootBox = lootBoxRepository.findById(id);
|
||||
if (optionalLootBox.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
LootBoxEntity lootBox = optionalLootBox.get();
|
||||
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
|
||||
if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
|
||||
RewardEntity reward = lootBoxService.determineReward(lootBox);
|
||||
lootBoxService.handleBalance(user, lootBox, reward);
|
||||
|
||||
return ResponseEntity.ok(reward);
|
||||
}
|
||||
|
||||
@PostMapping("/lootboxes")
|
||||
public ResponseEntity<Object> createLootbox(@RequestBody @Valid CreateLootBoxDto createLootBoxDto) {
|
||||
List<RewardEntity> rewardEntities = new ArrayList<>();
|
||||
|
||||
for (CreateRewardDto createRewardDto : createLootBoxDto.getRewards()) {
|
||||
rewardEntities.add(new RewardEntity(createRewardDto.getValue(), createRewardDto.getProbability()));
|
||||
}
|
||||
|
||||
LootBoxEntity lootBoxEntity = new LootBoxEntity(
|
||||
createLootBoxDto.getName(),
|
||||
createLootBoxDto.getPrice(),
|
||||
rewardEntities
|
||||
);
|
||||
|
||||
this.lootBoxRepository.save(lootBoxEntity);
|
||||
|
||||
return ResponseEntity.ok(lootBoxEntity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/lootboxes/{id}")
|
||||
public ResponseEntity<Object> deleteLootbox(@PathVariable Long id) {
|
||||
Optional<LootBoxEntity> optionalLootBox = lootBoxRepository.findById(id);
|
||||
if (optionalLootBox.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
LootBoxEntity lootBox = optionalLootBox.get();
|
||||
lootBoxRepository.delete(lootBox);
|
||||
|
||||
return ResponseEntity.ok(Collections.singletonMap("message", "successfully deleted lootbox"));
|
||||
}
|
||||
|
||||
}
|
||||
package de.szut.casino.lootboxes;
|
||||
|
||||
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
||||
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||
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.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class LootBoxController {
|
||||
private final LootBoxRepository lootBoxRepository;
|
||||
private final UserService userService;
|
||||
private final LootBoxService lootBoxService;
|
||||
|
||||
public LootBoxController(LootBoxRepository lootBoxRepository, UserService userService, LootBoxService lootBoxService) {
|
||||
this.lootBoxRepository = lootBoxRepository;
|
||||
this.userService = userService;
|
||||
this.lootBoxService = lootBoxService;
|
||||
}
|
||||
|
||||
@GetMapping("/lootboxes")
|
||||
public List<LootBoxEntity> getAllLootBoxes() {
|
||||
return lootBoxRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/lootboxes/{id}")
|
||||
public ResponseEntity<Object> purchaseLootBox(@PathVariable Long id) {
|
||||
Optional<LootBoxEntity> optionalLootBox = lootBoxRepository.findById(id);
|
||||
if (optionalLootBox.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
LootBoxEntity lootBox = optionalLootBox.get();
|
||||
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
|
||||
if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) {
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
|
||||
RewardEntity reward = lootBoxService.determineReward(lootBox);
|
||||
lootBoxService.handleBalance(user, lootBox, reward);
|
||||
|
||||
return ResponseEntity.ok(reward);
|
||||
}
|
||||
|
||||
@PostMapping("/lootboxes")
|
||||
public ResponseEntity<Object> createLootbox(@RequestBody @Valid CreateLootBoxDto createLootBoxDto) {
|
||||
List<RewardEntity> rewardEntities = new ArrayList<>();
|
||||
|
||||
for (CreateRewardDto createRewardDto : createLootBoxDto.getRewards()) {
|
||||
rewardEntities.add(new RewardEntity(createRewardDto.getValue(), createRewardDto.getProbability()));
|
||||
}
|
||||
|
||||
LootBoxEntity lootBoxEntity = new LootBoxEntity(
|
||||
createLootBoxDto.getName(),
|
||||
createLootBoxDto.getPrice(),
|
||||
rewardEntities
|
||||
);
|
||||
|
||||
this.lootBoxRepository.save(lootBoxEntity);
|
||||
|
||||
return ResponseEntity.ok(lootBoxEntity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/lootboxes/{id}")
|
||||
public ResponseEntity<Object> deleteLootbox(@PathVariable Long id) {
|
||||
Optional<LootBoxEntity> optionalLootBox = lootBoxRepository.findById(id);
|
||||
if (optionalLootBox.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
LootBoxEntity lootBox = optionalLootBox.get();
|
||||
lootBoxRepository.delete(lootBox);
|
||||
|
||||
return ResponseEntity.ok(Collections.singletonMap("message", "successfully deleted lootbox"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,40 +1,40 @@
|
|||
package de.szut.casino.lootboxes;
|
||||
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Service
|
||||
public class LootBoxService {
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public LootBoxService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public boolean hasSufficientBalance(UserEntity user, BigDecimal price) {
|
||||
return user.getBalance().compareTo(price) < 0;
|
||||
}
|
||||
|
||||
public RewardEntity determineReward(LootBoxEntity lootBox) {
|
||||
double randomValue = Math.random();
|
||||
BigDecimal cumulativeProbability = BigDecimal.ZERO;
|
||||
|
||||
for (RewardEntity reward : lootBox.getRewards()) {
|
||||
cumulativeProbability = cumulativeProbability.add(reward.getProbability());
|
||||
if (randomValue <= cumulativeProbability.doubleValue()) {
|
||||
return reward;
|
||||
}
|
||||
}
|
||||
|
||||
return lootBox.getRewards().getLast();
|
||||
}
|
||||
|
||||
public void handleBalance(UserEntity user, LootBoxEntity lootBox, RewardEntity reward) {
|
||||
user.setBalance(user.getBalance().subtract(lootBox.getPrice()));
|
||||
user.setBalance(user.getBalance().add(reward.getValue()));
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
package de.szut.casino.lootboxes;
|
||||
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Service
|
||||
public class LootBoxService {
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public LootBoxService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public boolean hasSufficientBalance(UserEntity user, BigDecimal price) {
|
||||
return user.getBalance().compareTo(price) < 0;
|
||||
}
|
||||
|
||||
public RewardEntity determineReward(LootBoxEntity lootBox) {
|
||||
double randomValue = Math.random();
|
||||
BigDecimal cumulativeProbability = BigDecimal.ZERO;
|
||||
|
||||
for (RewardEntity reward : lootBox.getRewards()) {
|
||||
cumulativeProbability = cumulativeProbability.add(reward.getProbability());
|
||||
if (randomValue <= cumulativeProbability.doubleValue()) {
|
||||
return reward;
|
||||
}
|
||||
}
|
||||
|
||||
return lootBox.getRewards().getLast();
|
||||
}
|
||||
|
||||
public void handleBalance(UserEntity user, LootBoxEntity lootBox, RewardEntity reward) {
|
||||
user.setBalance(user.getBalance().subtract(lootBox.getPrice()));
|
||||
user.setBalance(user.getBalance().add(reward.getValue()));
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue