fix: fix wrong reward getting returned, refactor to service
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
	
		
	
		
			All checks were successful
		
		
	
	
This commit is contained in:
		
					parent
					
						
							
								b963595ab4
							
						
					
				
			
			
				commit
				
					
						948240ba1e
					
				
			
		
					 4 changed files with 48 additions and 30 deletions
				
			
		| 
						 | 
					@ -4,6 +4,6 @@ Content-Type: application/json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
###
 | 
					###
 | 
				
			||||||
 | 
					
 | 
				
			||||||
POST http://localhost:8080/lootboxes/1
 | 
					POST http://localhost:8080/lootboxes/2
 | 
				
			||||||
Authorization: Bearer {{token}}
 | 
					Authorization: Bearer {{token}}
 | 
				
			||||||
Content-Type: application/json
 | 
					Content-Type: application/json
 | 
				
			||||||
| 
						 | 
					@ -41,7 +41,6 @@ public class CasinoApplication {
 | 
				
			||||||
                premiumLootBox.setPrice(new BigDecimal("5"));
 | 
					                premiumLootBox.setPrice(new BigDecimal("5"));
 | 
				
			||||||
                premiumLootBox.setRewards(new ArrayList<>()); // Initialize the list
 | 
					                premiumLootBox.setRewards(new ArrayList<>()); // Initialize the list
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // Save the loot boxes first
 | 
					 | 
				
			||||||
                lootBoxRepository.saveAll(Arrays.asList(basicLootBox, premiumLootBox));
 | 
					                lootBoxRepository.saveAll(Arrays.asList(basicLootBox, premiumLootBox));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                RewardEntity commonReward = new RewardEntity();
 | 
					                RewardEntity commonReward = new RewardEntity();
 | 
				
			||||||
| 
						 | 
					@ -68,13 +67,11 @@ public class CasinoApplication {
 | 
				
			||||||
                legendaryReward.setValue(new BigDecimal("15.00"));
 | 
					                legendaryReward.setValue(new BigDecimal("15.00"));
 | 
				
			||||||
                legendaryReward.setProbability(new BigDecimal("0.10"));
 | 
					                legendaryReward.setProbability(new BigDecimal("0.10"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // Save all rewards
 | 
					 | 
				
			||||||
                rewardRepository.saveAll(Arrays.asList(
 | 
					                rewardRepository.saveAll(Arrays.asList(
 | 
				
			||||||
                        commonReward, rareReward, epicReward,
 | 
					                        commonReward, rareReward, epicReward,
 | 
				
			||||||
                        premiumCommon, premiumRare, legendaryReward
 | 
					                        premiumCommon, premiumRare, legendaryReward
 | 
				
			||||||
                ));
 | 
					                ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // Add relationships from the owning side (LootBox)
 | 
					 | 
				
			||||||
                basicLootBox.getRewards().add(commonReward);
 | 
					                basicLootBox.getRewards().add(commonReward);
 | 
				
			||||||
                basicLootBox.getRewards().add(premiumRare);
 | 
					                basicLootBox.getRewards().add(premiumRare);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -82,7 +79,6 @@ public class CasinoApplication {
 | 
				
			||||||
                premiumLootBox.getRewards().add(premiumRare);
 | 
					                premiumLootBox.getRewards().add(premiumRare);
 | 
				
			||||||
                premiumLootBox.getRewards().add(legendaryReward);
 | 
					                premiumLootBox.getRewards().add(legendaryReward);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // Update the loot boxes to save the relationships
 | 
					 | 
				
			||||||
                lootBoxRepository.saveAll(Arrays.asList(basicLootBox, premiumLootBox));
 | 
					                lootBoxRepository.saveAll(Arrays.asList(basicLootBox, premiumLootBox));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                System.out.println("Initial LootBoxes and rewards created successfully");
 | 
					                System.out.println("Initial LootBoxes and rewards created successfully");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,6 @@ import de.szut.casino.user.UserService;
 | 
				
			||||||
import org.springframework.http.ResponseEntity;
 | 
					import org.springframework.http.ResponseEntity;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.math.BigDecimal;
 | 
					 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
| 
						 | 
					@ -15,13 +14,13 @@ import java.util.Optional;
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
public class LootBoxController {
 | 
					public class LootBoxController {
 | 
				
			||||||
    private final LootBoxRepository lootBoxRepository;
 | 
					    private final LootBoxRepository lootBoxRepository;
 | 
				
			||||||
    private final UserRepository userRepository;
 | 
					 | 
				
			||||||
    private final UserService userService;
 | 
					    private final UserService userService;
 | 
				
			||||||
 | 
					    private final LootBoxService lootBoxService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public LootBoxController(LootBoxRepository lootBoxRepository, UserRepository userRepository, UserService userService) {
 | 
					    public LootBoxController(LootBoxRepository lootBoxRepository, UserRepository userRepository, UserService userService, LootBoxService lootBoxService) {
 | 
				
			||||||
        this.lootBoxRepository = lootBoxRepository;
 | 
					        this.lootBoxRepository = lootBoxRepository;
 | 
				
			||||||
        this.userRepository = userRepository;
 | 
					 | 
				
			||||||
        this.userService = userService;
 | 
					        this.userService = userService;
 | 
				
			||||||
 | 
					        this.lootBoxService = lootBoxService;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @GetMapping("/lootboxes")
 | 
					    @GetMapping("/lootboxes")
 | 
				
			||||||
| 
						 | 
					@ -37,7 +36,6 @@ public class LootBoxController {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        LootBoxEntity lootBox = optionalLootBox.get();
 | 
					        LootBoxEntity lootBox = optionalLootBox.get();
 | 
				
			||||||
        RewardEntity reward = determineReward(lootBox);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
 | 
					        Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
 | 
				
			||||||
        if (optionalUser.isEmpty()) {
 | 
					        if (optionalUser.isEmpty()) {
 | 
				
			||||||
| 
						 | 
					@ -46,31 +44,15 @@ public class LootBoxController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        UserEntity user = optionalUser.get();
 | 
					        UserEntity user = optionalUser.get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (user.getBalance().compareTo(lootBox.getPrice()) < 0) {
 | 
					        if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) {
 | 
				
			||||||
            Map<String, String> errorResponse = new HashMap<>();
 | 
					            Map<String, String> errorResponse = new HashMap<>();
 | 
				
			||||||
            errorResponse.put("error", "Insufficient balance");
 | 
					            errorResponse.put("error", "Insufficient balance");
 | 
				
			||||||
            return ResponseEntity.badRequest().body(errorResponse);
 | 
					            return ResponseEntity.badRequest().body(errorResponse);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        user.setBalance(user.getBalance().subtract(lootBox.getPrice()));
 | 
					        RewardEntity reward = lootBoxService.determineReward(lootBox);
 | 
				
			||||||
        user.setBalance(user.getBalance().add(reward.getValue()));
 | 
					        lootBoxService.handleBalance(user, lootBox, reward);
 | 
				
			||||||
        userRepository.save(user);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return ResponseEntity.ok(determineReward(lootBox));
 | 
					        return ResponseEntity.ok(reward);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    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();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in a new issue