UpdateCowHealthJob.java

  1. package edu.ucsb.cs156.happiercows.jobs;

  2. import edu.ucsb.cs156.happiercows.entities.Commons;
  3. import edu.ucsb.cs156.happiercows.entities.User;
  4. import edu.ucsb.cs156.happiercows.entities.UserCommons;
  5. import edu.ucsb.cs156.happiercows.repositories.CommonsRepository;
  6. import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository;
  7. import edu.ucsb.cs156.happiercows.repositories.UserRepository;
  8. import edu.ucsb.cs156.happiercows.services.jobs.JobContext;
  9. import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer;
  10. import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy;
  11. import lombok.AllArgsConstructor;
  12. import lombok.Getter;

  13. @AllArgsConstructor
  14. public class UpdateCowHealthJob implements JobContextConsumer {

  15.     @Getter
  16.     private CommonsRepository commonsRepository;
  17.     @Getter
  18.     private UserCommonsRepository userCommonsRepository;
  19.     @Getter
  20.     private UserRepository userRepository;

  21.     @Override
  22.     public void accept(JobContext ctx) throws Exception {
  23.         ctx.log("Updating cow health...");

  24.         Iterable<Commons> allCommons = commonsRepository.findAll();

  25.         for (Commons commons : allCommons) {
  26.             ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", effectiveCapacity: " + commons.getEffectiveCapacity());
  27.             int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")"));

  28.             if (numUsers==0) {
  29.                 ctx.log("No users in this commons, skipping");
  30.                 continue;
  31.             }

  32.             int carryingCapacity = commons.getEffectiveCapacity();
  33.             Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId());

  34.             Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")"));

  35.             var isAboveCapacity = totalCows > carryingCapacity;
  36.             var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy();

  37.             for (UserCommons userCommons : allUserCommons) {
  38.                 User user = userCommons.getUser();
  39.                 var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commons, userCommons, totalCows);
  40.                 ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth());

  41.                 double oldHealth = userCommons.getCowHealth();
  42.                 userCommons.setCowHealth(newCowHealth);
  43.                 calculateCowDeaths(userCommons, ctx);

  44.                 ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth());
  45.                 userCommonsRepository.save(userCommons);
  46.             }
  47.         }

  48.         ctx.log("Cow health has been updated!");
  49.     }

  50.     // exposed for testing
  51.     public static double calculateNewCowHealthUsingStrategy(
  52.             CowHealthUpdateStrategy strategy,
  53.             Commons commons,
  54.             UserCommons userCommons,
  55.             int totalCows
  56.     ) {
  57.         var health = strategy.calculateNewCowHealth(commons, userCommons, totalCows);
  58.         return Math.max(0, Math.min(health, 100));
  59.     }

  60.     public static void calculateCowDeaths(UserCommons userCommons, JobContext ctx) {
  61.         if (userCommons.getCowHealth() == 0.0) {
  62.             userCommons.setCowDeaths(userCommons.getCowDeaths() + userCommons.getNumOfCows());
  63.             userCommons.setNumOfCows(0);
  64.             userCommons.setCowHealth(100.0);

  65.             ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." );
  66.         }
  67.     }
  68. }