| 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.CowHerd; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.GameRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 10 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 11 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 12 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 13 | import edu.ucsb.cs156.happiercows.services.GamePlusBuilderService; | |
| 14 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.CowHerdRepository; | |
| 16 | import lombok.AllArgsConstructor; | |
| 17 | import lombok.Getter; | |
| 18 | ||
| 19 | ||
| 20 | @AllArgsConstructor | |
| 21 | public class UpdateCowHealthJob implements JobContextConsumer { | |
| 22 | ||
| 23 | @Getter | |
| 24 | private GameRepository gameRepository; | |
| 25 | @Getter | |
| 26 | private FarmerRepository farmerRepository; | |
| 27 | @Getter | |
| 28 | private UserRepository userRepository; | |
| 29 | @Getter | |
| 30 | private GamePlusBuilderService gamePlusBuilderService; | |
| 31 | @Getter | |
| 32 | private CowHerdRepository cowHerdRepository; | |
| 33 | ||
| 34 | @Override | |
| 35 | public void accept(JobContext ctx) throws Exception { | |
| 36 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Updating cow health..."); |
| 37 | ||
| 38 | ||
| 39 | Iterable<Game> allGame = gameRepository.findAll(); | |
| 40 | Iterable<GamePlus> allGamePlus = gamePlusBuilderService.convertToGamePlus(allGame); | |
| 41 | ||
| 42 | for (GamePlus gamePlus : allGamePlus) { | |
| 43 | ||
| 44 | ||
| 45 | Game game = gamePlus.getGame(); | |
| 46 | ||
| 47 |
1
1. accept : negated conditional → KILLED |
if (!GameGate.shouldProcess(game, gameRepository, ctx)) { |
| 48 | continue; | |
| 49 | } | |
| 50 | ||
| 51 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInGame → KILLED |
runUpdateJobInGame(game, gamePlus, gamePlusBuilderService, |
| 52 | gameRepository, farmerRepository, cowHerdRepository, ctx); | |
| 53 | ||
| 54 | } | |
| 55 | ||
| 56 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Cow health has been updated!"); |
| 57 | } | |
| 58 | ||
| 59 | // exposed for testing | |
| 60 | public static double calculateNewCowHealthUsingStrategy( | |
| 61 | CowHealthUpdateStrategy strategy, | |
| 62 | GamePlus gamePlus, | |
| 63 | Farmer farmer, | |
| 64 | int totalCows | |
| 65 | ) { | |
| 66 | var health = strategy.calculateNewCowHealth(gamePlus, farmer, totalCows); | |
| 67 |
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)); |
| 68 | } | |
| 69 | ||
| 70 | public static void calculateCowDeaths(Farmer farmer, JobContext ctx) { | |
| 71 |
1
1. calculateCowDeaths : negated conditional → KILLED |
if (farmer.getCowHealth() == 0.0) { |
| 72 |
2
1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED 2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowDeaths → KILLED |
farmer.setCowDeaths(farmer.getCowDeaths() + farmer.getNumOfCows()); |
| 73 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED |
farmer.setNumOfCows(0); |
| 74 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED |
farmer.setCowHealth(100.0); |
| 75 | ||
| 76 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(" " + farmer.getCowDeaths() + " cows for this user died." ); |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | public static void calculateHerdDeaths(CowHerd herd, Farmer farmer, JobContext ctx) { | |
| 81 |
3
1. calculateHerdDeaths : changed conditional boundary → KILLED 2. calculateHerdDeaths : negated conditional → KILLED 3. calculateHerdDeaths : negated conditional → KILLED |
if (herd.getCowHealth() == 0.0 && herd.getNumOfCows() > 0) { |
| 82 | int deadCows = herd.getNumOfCows(); | |
| 83 | ||
| 84 |
2
1. calculateHerdDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowDeaths → KILLED 2. calculateHerdDeaths : Replaced integer addition with subtraction → KILLED |
farmer.setCowDeaths(farmer.getCowDeaths() + deadCows); |
| 85 |
2
1. calculateHerdDeaths : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setNumOfCows → KILLED 2. calculateHerdDeaths : Replaced integer subtraction with addition → KILLED |
farmer.setNumOfCows(farmer.getNumOfCows() - deadCows); |
| 86 |
1
1. calculateHerdDeaths : removed call to edu/ucsb/cs156/happiercows/entities/CowHerd::setNumOfCows → KILLED |
herd.setNumOfCows(0); |
| 87 | ||
| 88 |
1
1. calculateHerdDeaths : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(" " + deadCows + " cows from herd " + herd.getId() + " died."); |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | ||
| 93 | public static void runUpdateJobInGame(Game game, GamePlus gamePlus, GamePlusBuilderService gamePlusBuilderService, GameRepository gameRepository, FarmerRepository farmerRepository, CowHerdRepository cowHerdRepository, JobContext ctx) { | |
| 94 |
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()); |
| 95 | ||
| 96 |
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() + ")")); |
| 97 | ||
| 98 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
if (numUsers==0) { |
| 99 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("No users in this game, skipping"); |
| 100 | return; | |
| 101 | } | |
| 102 | ||
| 103 | int carryingCapacity = gamePlus.getEffectiveCapacity(); | |
| 104 | Iterable<Farmer> allFarmer = farmerRepository.findByGameId(game.getId()); | |
| 105 | ||
| 106 |
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() + ")")); |
| 107 | ||
| 108 |
2
1. runUpdateJobInGame : negated conditional → KILLED 2. runUpdateJobInGame : changed conditional boundary → KILLED |
var isAboveCapacity = totalCows > carryingCapacity; |
| 109 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
var cowHealthUpdateStrategy = isAboveCapacity ? game.getAboveCapacityHealthUpdateStrategy() : game.getBelowCapacityHealthUpdateStrategy(); |
| 110 | ||
| 111 | for (Farmer farmer : allFarmer) { | |
| 112 | User user = farmer.getUser(); | |
| 113 | ||
| 114 | var herds = cowHerdRepository.findByFarmer_Id_UserIdAndFarmer_Id_GameId( | |
| 115 | farmer.getUserId(), game.getId()); | |
| 116 | ||
| 117 | var newCowHealth = calculateNewCowHealthUsingStrategy( | |
| 118 | cowHealthUpdateStrategy, | |
| 119 | gamePlusBuilderService.toGamePlus(game), | |
| 120 | farmer, | |
| 121 | totalCows); | |
| 122 | ||
| 123 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + farmer.getNumOfCows() |
| 124 | + ", cowHealth: " + farmer.getCowHealth()); | |
| 125 | ||
| 126 | double oldHealth = farmer.getCowHealth(); | |
| 127 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED |
farmer.setCowHealth(newCowHealth); |
| 128 | ||
| 129 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
if (herds.iterator().hasNext()) { |
| 130 | for (CowHerd herd : herds) { | |
| 131 | Farmer herdFarmer = Farmer.builder() | |
| 132 | .cowHealth(herd.getCowHealth()) | |
| 133 | .build(); | |
| 134 | ||
| 135 | double newHerdHealth = calculateNewCowHealthUsingStrategy( | |
| 136 | cowHealthUpdateStrategy, | |
| 137 | gamePlusBuilderService.toGamePlus(game), | |
| 138 | herdFarmer, | |
| 139 | totalCows); | |
| 140 | ||
| 141 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/CowHerd::setCowHealth → KILLED |
herd.setCowHealth(newHerdHealth); |
| 142 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateHerdDeaths → KILLED |
calculateHerdDeaths(herd, farmer, ctx); |
| 143 | cowHerdRepository.save(herd); | |
| 144 | } | |
| 145 | } else { | |
| 146 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED |
calculateCowDeaths(farmer, ctx); |
| 147 | } | |
| 148 | ||
| 149 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(" old cow health: " + oldHealth |
| 150 | + ", new cow health: " + farmer.getCowHealth()); | |
| 151 | ||
| 152 | farmerRepository.save(farmer); | |
| 153 | } | |
| 154 | ||
| 155 | } | |
| 156 | ||
| 157 | } | |
Mutations | ||
| 36 |
1.1 |
|
| 47 |
1.1 |
|
| 51 |
1.1 |
|
| 56 |
1.1 |
|
| 67 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 2.2 |
|
| 73 |
1.1 |
|
| 74 |
1.1 |
|
| 76 |
1.1 |
|
| 81 |
1.1 2.2 3.3 |
|
| 84 |
1.1 2.2 |
|
| 85 |
1.1 2.2 |
|
| 86 |
1.1 |
|
| 88 |
1.1 |
|
| 94 |
1.1 |
|
| 96 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |
|
| 106 |
1.1 |
|
| 108 |
1.1 2.2 |
|
| 109 |
1.1 |
|
| 123 |
1.1 |
|
| 127 |
1.1 |
|
| 129 |
1.1 |
|
| 141 |
1.1 |
|
| 142 |
1.1 |
|
| 146 |
1.1 |
|
| 149 |
1.1 |