| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | ||
| 4 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 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 lombok.AllArgsConstructor; | |
| 15 | import lombok.Getter; | |
| 16 | ||
| 17 | import java.util.Optional; | |
| 18 | ||
| 19 | @AllArgsConstructor | |
| 20 | public class SetCowHealthJob implements JobContextConsumer { | |
| 21 | ||
| 22 | private long gameID; | |
| 23 | private double newCowHealth; | |
| 24 | ||
| 25 | @Getter | |
| 26 | private GameRepository gameRepository; | |
| 27 | @Getter | |
| 28 | private FarmerRepository farmerRepository; | |
| 29 | @Getter | |
| 30 | private UserRepository userRepository; | |
| 31 | @Getter | |
| 32 | private HerdRepository herdRepository; | |
| 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("Setting cow health..."); |
| 37 | ||
| 38 | Optional<Game> game = gameRepository.findById(gameID); | |
| 39 | ||
| 40 | ||
| 41 |
1
1. accept : negated conditional → KILLED |
if (game.isPresent()) { |
| 42 |
1
1. accept : negated conditional → KILLED |
if (!GameGate.shouldProcess(game.get(), gameRepository, ctx)) { |
| 43 | return; | |
| 44 | } | |
| 45 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Game " + game.get().getName()); |
| 46 | ||
| 47 | Iterable<Farmer> allFarmer = farmerRepository.findByGameId(game.get().getId()); | |
| 48 | ||
| 49 | for (Farmer farmer : allFarmer) { | |
| 50 | User user = farmer.getUser(); | |
| 51 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + farmer.getNumOfCows() + ", cowHealth: " + farmer.getCowHealth()); |
| 52 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(" old cow health: " + farmer.getCowHealth() + ", new cow health: " + newCowHealth); |
| 53 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/entities/Farmer::setCowHealth → KILLED |
farmer.setCowHealth(newCowHealth); |
| 54 | farmerRepository.save(farmer); | |
| 55 | for (Herd herd : herdRepository.findByGameIdAndUserId(game.get().getId(), farmer.getUserId())) { | |
| 56 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/entities/Herd::setHealth → KILLED |
herd.setHealth(newCowHealth); |
| 57 | herdRepository.save(herd); | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Cow health has been set!"); |
| 62 | } else { | |
| 63 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(String.format("No game found for id %d", gameID)); |
| 64 | } | |
| 65 | ||
| 66 | } | |
| 67 | } | |
Mutations | ||
| 36 |
1.1 |
|
| 41 |
1.1 |
|
| 42 |
1.1 |
|
| 45 |
1.1 |
|
| 51 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 56 |
1.1 |
|
| 61 |
1.1 |
|
| 63 |
1.1 |