| 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 runUpdateJobInGame( | |
| 97 | Game game, | |
| 98 | GamePlus gamePlus, | |
| 99 | GamePlusBuilderService gamePlusBuilderService, | |
| 100 | GameRepository gameRepository, | |
| 101 | FarmerRepository farmerRepository, | |
| 102 | CowHerdRepository cowHerdRepository, | |
| 103 | JobContext ctx | |
| 104 | ) { | |
| 105 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 106 | "Game " + game.getName() | |
| 107 | + ", degradationRate: " + game.getDegradationRate() | |
| 108 | + ", effectiveCapacity: " + gamePlus.getEffectiveCapacity() | |
| 109 | ); | |
| 110 | ||
| 111 | int numUsers = gameRepository.getNumUsers(game.getId()) | |
| 112 |
1
1. lambda$runUpdateJobInGame$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$0 → KILLED |
.orElseThrow(() -> new RuntimeException( |
| 113 | "Error calling getNumUsers(" + game.getId() + ")" | |
| 114 | )); | |
| 115 | ||
| 116 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
if (numUsers == 0) { |
| 117 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("No users in this game, skipping"); |
| 118 | return; | |
| 119 | } | |
| 120 | ||
| 121 | int carryingCapacity = gamePlus.getEffectiveCapacity(); | |
| 122 | ||
| 123 | Iterable<Farmer> allFarmer = | |
| 124 | farmerRepository.findByGameId(game.getId()); | |
| 125 | ||
| 126 | Integer totalCows = gameRepository.getNumCows(game.getId()) | |
| 127 |
1
1. lambda$runUpdateJobInGame$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInGame$1 → KILLED |
.orElseThrow(() -> new RuntimeException( |
| 128 | "Error calling getNumCows(" + game.getId() + ")" | |
| 129 | )); | |
| 130 | ||
| 131 |
2
1. runUpdateJobInGame : changed conditional boundary → KILLED 2. runUpdateJobInGame : negated conditional → KILLED |
var isAboveCapacity = totalCows > carryingCapacity; |
| 132 | ||
| 133 |
1
1. runUpdateJobInGame : negated conditional → KILLED |
var cowHealthUpdateStrategy = isAboveCapacity |
| 134 | ? game.getAboveCapacityHealthUpdateStrategy() | |
| 135 | : game.getBelowCapacityHealthUpdateStrategy(); | |
| 136 | ||
| 137 | for (Farmer farmer : allFarmer) { | |
| 138 | User user = farmer.getUser(); | |
| 139 | ||
| 140 | // Reuse the existing GamePlus for farmer health. | |
| 141 | var newCowHealth = calculateNewCowHealthUsingStrategy( | |
| 142 | cowHealthUpdateStrategy, | |
| 143 | gamePlus, | |
| 144 | farmer, | |
| 145 | totalCows | |
| 146 | ); | |
| 147 | ||
| 148 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 149 | "User: " + user.getFullName() | |
| 150 | + ", numCows: " + farmer.getNumOfCows() | |
| 151 | + ", cowHealth: " + farmer.getCowHealth() | |
| 152 | ); | |
| 153 | ||
| 154 | double oldHealth = farmer.getCowHealth(); | |
| 155 | ||
| 156 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED |
farmer.setCowHealth(newCowHealth); |
| 157 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED |
calculateCowDeaths(farmer, ctx); |
| 158 | ||
| 159 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 160 | " old cow health: " + oldHealth | |
| 161 | + ", new cow health: " + farmer.getCowHealth() | |
| 162 | ); | |
| 163 | ||
| 164 | farmerRepository.save(farmer); | |
| 165 | ||
| 166 | for (CowHerd herd : | |
| 167 | cowHerdRepository.findByFarmer_Id_UserIdAndFarmer_Id_GameId( | |
| 168 | farmer.getUserId(), | |
| 169 | game.getId() | |
| 170 | ) | |
| 171 | ) { | |
| 172 | Farmer herdFarmer = Farmer.builder() | |
| 173 | .cowHealth(herd.getCowHealth()) | |
| 174 | .build(); | |
| 175 | ||
| 176 | // Reuse GamePlus instead of rebuilding it per herd. | |
| 177 | double newHerdHealth = | |
| 178 | calculateNewCowHealthUsingStrategy( | |
| 179 | cowHealthUpdateStrategy, | |
| 180 | gamePlus, | |
| 181 | herdFarmer, | |
| 182 | totalCows | |
| 183 | ); | |
| 184 | ||
| 185 |
1
1. runUpdateJobInGame : removed call to edu/ucsb/cs156/happiercows/entities/CowHerd::setCowHealth → KILLED |
herd.setCowHealth(newHerdHealth); |
| 186 | cowHerdRepository.save(herd); | |
| 187 | } | |
| 188 | } | |
| 189 | } | |
| 190 | } | |
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 |
|
| 105 |
1.1 |
|
| 112 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 127 |
1.1 |
|
| 131 |
1.1 2.2 |
|
| 133 |
1.1 |
|
| 148 |
1.1 |
|
| 156 |
1.1 |
|
| 157 |
1.1 |
|
| 159 |
1.1 |
|
| 185 |
1.1 |