| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.GamePlus; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.Farmer; | |
| 7 | import edu.ucsb.cs156.happiercows.entities.Herd; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.GameRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 10 | import edu.ucsb.cs156.happiercows.repositories.HerdRepository; | |
| 11 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 12 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 13 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 14 | import edu.ucsb.cs156.happiercows.services.GamePlusBuilderService; | |
| 15 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy; | |
| 16 | import java.util.function.DoubleSupplier; | |
| 17 | ||
| 18 | import lombok.AllArgsConstructor; | |
| 19 | import lombok.Getter; | |
| 20 | ||
| 21 | @AllArgsConstructor | |
| 22 | public class UpdateCowHealthJob implements JobContextConsumer { | |
| 23 | ||
| 24 | @Getter | |
| 25 | private GameRepository gameRepository; | |
| 26 | @Getter | |
| 27 | private FarmerRepository farmerRepository; | |
| 28 | @Getter | |
| 29 | private UserRepository userRepository; | |
| 30 | @Getter | |
| 31 | private GamePlusBuilderService gamePlusBuilderService; | |
| 32 | @Getter | |
| 33 | private HerdRepository herdRepository; | |
| 34 | // Source of random numbers in [0, 1) for cow deaths; replaced in tests. | |
| 35 | @Getter | |
| 36 | private DoubleSupplier random; | |
| 37 | ||
| 38 | @Override | |
| 39 | public void accept(JobContext ctx) throws Exception { | |
| 40 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Updating cow health..."); |
| 41 | ||
| 42 | ||
| 43 | Iterable<Game> allGame = gameRepository.findAll(); | |
| 44 | Iterable<GamePlus> allGamePlus = gamePlusBuilderService.convertToGamePlus(allGame); | |
| 45 | ||
| 46 | for (GamePlus gamePlus : allGamePlus) { | |
| 47 | ||
| 48 | ||
| 49 | Game game = gamePlus.getGame(); | |
| 50 | ||
| 51 |
1
1. accept : negated conditional → KILLED |
if (!GameGate.shouldProcess(game, gameRepository, ctx)) { |
| 52 | continue; | |
| 53 | } | |
| 54 | ||
| 55 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInGame → KILLED |
runUpdateJobInGame(game, gamePlus, gamePlusBuilderService, gameRepository, farmerRepository, herdRepository, random, ctx); |
| 56 | ||
| 57 | } | |
| 58 | ||
| 59 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Cow health has been updated!"); |
| 60 | } | |
| 61 | ||
| 62 | // Cows in a herd below this health may die (see docs/gamePlay.md). | |
| 63 | public static final double DEATH_HEALTH_THRESHOLD = 30.0; | |
| 64 | // Chance that each cow in a herd below the threshold dies during one update. | |
| 65 | public static final double DEATH_CHANCE_PER_COW = 0.01; | |
| 66 | ||
| 67 | // exposed for testing | |
| 68 | public static double calculateNewCowHealthUsingStrategy( | |
| 69 | CowHealthUpdateStrategy strategy, | |
| 70 | GamePlus gamePlus, | |
| 71 | double cowHealth, | |
| 72 | int totalCows | |
| 73 | ) { | |
| 74 | var health = strategy.calculateNewCowHealth(gamePlus, cowHealth, totalCows); | |
| 75 |
1
1. calculateNewCowHealthUsingStrategy : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED |
return Math.max(0, Math.min(health, 100)); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Removes the cows that die in this herd during one update, and returns how | |
| 80 | * many died. A herd at 0 health loses all of its cows. A herd below | |
| 81 | * DEATH_HEALTH_THRESHOLD loses each cow with chance DEATH_CHANCE_PER_COW, | |
| 82 | * using random (a source of numbers in [0, 1)) so tests can control it. | |
| 83 | */ | |
| 84 | public static int calculateCowDeaths(Herd herd, DoubleSupplier random) { | |
| 85 | int deaths = 0; | |
| 86 |
1
1. calculateCowDeaths : negated conditional → KILLED |
if (herd.getHealth() == 0.0) { |
| 87 | deaths = herd.getNumCows(); | |
| 88 |
2
1. calculateCowDeaths : changed conditional boundary → KILLED 2. calculateCowDeaths : negated conditional → KILLED |
} else if (herd.getHealth() < DEATH_HEALTH_THRESHOLD) { |
| 89 |
2
1. calculateCowDeaths : negated conditional → KILLED 2. calculateCowDeaths : changed conditional boundary → KILLED |
for (int i = 0; i < herd.getNumCows(); i++) { |
| 90 |
2
1. calculateCowDeaths : negated conditional → KILLED 2. calculateCowDeaths : changed conditional boundary → KILLED |
if (random.getAsDouble() < DEATH_CHANCE_PER_COW) { |
| 91 |
1
1. calculateCowDeaths : Changed increment from 1 to -1 → KILLED |
deaths++; |
| 92 | } | |
| 93 | } | |
| 94 | } | |
| 95 |
2
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Herd::setNumCows → KILLED 2. calculateCowDeaths : Replaced integer subtraction with addition → KILLED |
herd.setNumCows(herd.getNumCows() - deaths); |
| 96 |
1
1. calculateCowDeaths : replaced int return with 0 for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED |
return deaths; |
| 97 | } | |
| 98 | ||
| 99 | public static void runUpdateJobInGame(Game game, GamePlus gamePlus, GamePlusBuilderService gamePlusBuilderService, | |
| 100 | GameRepository gameRepository, FarmerRepository farmerRepository, HerdRepository herdRepository, | |
| 101 | DoubleSupplier random, JobContext ctx) { | |
| 102 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Game " + game.getName() + ", degradationRate: " + game.getDegradationRate() + ", effectiveCapacity: " + gamePlus.getEffectiveCapacity()); |
| 103 | ||
| 104 |
1
1. lambda$runUpdateJobInGame$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$0 → KILLED |
int numUsers = gameRepository.getNumUsers(game.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + game.getId() + ")")); |
| 105 | ||
| 106 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
if (numUsers == 0) { |
| 107 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("No users in this game, skipping"); |
| 108 | return; | |
| 109 | } | |
| 110 | ||
| 111 | int carryingCapacity = gamePlus.getEffectiveCapacity(); | |
| 112 | Iterable<Farmer> allFarmer = farmerRepository.findByGameId(game.getId()); | |
| 113 | ||
| 114 |
1
1. lambda$runUpdateJobInGame$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$1 → KILLED |
Integer totalCows = gameRepository.getNumCows(game.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + game.getId() + ")")); |
| 115 | ||
| 116 |
2
1. runUpdateJobInGame : changed conditional boundary → KILLED 2. runUpdateJobInGame : negated conditional → KILLED |
var isAboveCapacity = totalCows > carryingCapacity; |
| 117 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
var cowHealthUpdateStrategy = isAboveCapacity ? game.getAboveCapacityHealthUpdateStrategy() : game.getBelowCapacityHealthUpdateStrategy(); |
| 118 | GamePlus currentGamePlus = gamePlusBuilderService.toGamePlus(game); | |
| 119 | ||
| 120 | for (Farmer farmer : allFarmer) { | |
| 121 | User user = farmer.getUser(); | |
| 122 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + farmer.getNumOfCows() + ", cowHealth: " + farmer.getCowHealth()); |
| 123 | ||
| 124 | int survivingCows = 0; | |
| 125 | double totalHealth = 0; | |
| 126 | for (Herd herd : herdRepository.findByGameIdAndUserId(game.getId(), farmer.getUserId())) { | |
| 127 | double oldHealth = herd.getHealth(); | |
| 128 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Herd::setHealth → KILLED |
herd.setHealth(calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, currentGamePlus, oldHealth, totalCows)); |
| 129 | int deaths = calculateCowDeaths(herd, random); | |
| 130 |
2
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowDeaths → KILLED 2. runUpdateJobInGame : Replaced integer addition with subtraction → KILLED |
farmer.setCowDeaths(farmer.getCowDeaths() + deaths); |
| 131 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(" Herd #" + herd.getHerdNumber() + ": old health: " + oldHealth + ", new health: " + herd.getHealth() |
| 132 | + ", cows died: " + deaths + ", cows left: " + herd.getNumCows()); | |
| 133 | ||
| 134 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
if (herd.getNumCows() == 0) { |
| 135 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/repositories/HerdRepository::delete → KILLED |
herdRepository.delete(herd); |
| 136 | } else { | |
| 137 | herdRepository.save(herd); | |
| 138 |
1
1. runUpdateJobInGame : Replaced integer addition with subtraction → KILLED |
survivingCows += herd.getNumCows(); |
| 139 |
2
1. runUpdateJobInGame : Replaced double multiplication with division → KILLED 2. runUpdateJobInGame : Replaced double addition with subtraction → KILLED |
totalHealth += herd.getHealth() * herd.getNumCows(); |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 143 | // Keep the farmer's totals in step with their herds: total cows and | |
| 144 | // average health weighted by cow count (100 when they have no cows, | |
| 145 | // the health a newly bought herd starts at). | |
| 146 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED |
farmer.setNumOfCows(survivingCows); |
| 147 |
4
1. runUpdateJobInGame : Replaced double division with multiplication → KILLED 2. runUpdateJobInGame : changed conditional boundary → KILLED 3. runUpdateJobInGame : negated conditional → KILLED 4. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED |
farmer.setCowHealth(survivingCows > 0 ? totalHealth / survivingCows : 100.0); |
| 148 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(" new numCows: " + farmer.getNumOfCows() + ", new cowHealth: " + farmer.getCowHealth()); |
| 149 | farmerRepository.save(farmer); | |
| 150 | } | |
| 151 | } | |
| 152 | } | |
Mutations | ||
| 40 |
1.1 |
|
| 51 |
1.1 |
|
| 55 |
1.1 |
|
| 59 |
1.1 |
|
| 75 |
1.1 |
|
| 86 |
1.1 |
|
| 88 |
1.1 2.2 |
|
| 89 |
1.1 2.2 |
|
| 90 |
1.1 2.2 |
|
| 91 |
1.1 |
|
| 95 |
1.1 2.2 |
|
| 96 |
1.1 |
|
| 102 |
1.1 |
|
| 104 |
1.1 |
|
| 106 |
1.1 |
|
| 107 |
1.1 |
|
| 114 |
1.1 |
|
| 116 |
1.1 2.2 |
|
| 117 |
1.1 |
|
| 122 |
1.1 |
|
| 128 |
1.1 |
|
| 130 |
1.1 2.2 |
|
| 131 |
1.1 |
|
| 134 |
1.1 |
|
| 135 |
1.1 |
|
| 138 |
1.1 |
|
| 139 |
1.1 2.2 |
|
| 146 |
1.1 |
|
| 147 |
1.1 2.2 3.3 4.4 |
|
| 148 |
1.1 |