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