1 | package edu.ucsb.cs156.happiercows.jobs; | |
2 | ||
3 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
4 | import edu.ucsb.cs156.happiercows.entities.User; | |
5 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
6 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
7 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
8 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
9 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
10 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
11 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy; | |
12 | import lombok.AllArgsConstructor; | |
13 | import lombok.Getter; | |
14 | ||
15 | @AllArgsConstructor | |
16 | public class UpdateCowHealthJob implements JobContextConsumer { | |
17 | ||
18 | @Getter | |
19 | private CommonsRepository commonsRepository; | |
20 | @Getter | |
21 | private UserCommonsRepository userCommonsRepository; | |
22 | @Getter | |
23 | private UserRepository userRepository; | |
24 | ||
25 | @Override | |
26 | public void accept(JobContext ctx) throws Exception { | |
27 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Updating cow health..."); |
28 | ||
29 | Iterable<Commons> allCommons = commonsRepository.findAll(); | |
30 | ||
31 | for (Commons commons : allCommons) { | |
32 |
1
1. accept : negated conditional → KILLED |
if (!commons.gameInProgress()) { |
33 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Skipping Cow Health Update at Commons: " + commons.getName() + " because game is not in progress"); |
34 | continue; | |
35 | } | |
36 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", carryingCapacity: " + commons.getCarryingCapacity()); |
37 |
1
1. lambda$accept$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$0 → KILLED |
int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")")); |
38 | ||
39 |
1
1. accept : negated conditional → KILLED |
if (numUsers==0) { |
40 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("No users in this commons, skipping"); |
41 | continue; | |
42 | } | |
43 | ||
44 | int effectiveCapacity = Commons.computeEffectiveCapacity(commons, commonsRepository); | |
45 | Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId()); | |
46 | ||
47 |
1
1. lambda$accept$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$1 → KILLED |
Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")")); |
48 | ||
49 |
2
1. accept : changed conditional boundary → KILLED 2. accept : negated conditional → KILLED |
var isAboveCapacity = totalCows > effectiveCapacity; |
50 |
1
1. accept : negated conditional → KILLED |
var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy(); |
51 | ||
52 | for (UserCommons userCommons : allUserCommons) { | |
53 | User user = userCommons.getUser(); | |
54 | var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commons, commonsRepository, userCommons, totalCows); | |
55 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth()); |
56 | ||
57 | double oldHealth = userCommons.getCowHealth(); | |
58 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(newCowHealth); |
59 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED |
calculateCowDeaths(userCommons, ctx); |
60 | ||
61 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth()); |
62 | userCommonsRepository.save(userCommons); | |
63 | } | |
64 | } | |
65 | ||
66 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Cow health has been updated!"); |
67 | } | |
68 | ||
69 | // exposed for testing | |
70 | public static double calculateNewCowHealthUsingStrategy( | |
71 | CowHealthUpdateStrategy strategy, | |
72 | Commons commons, | |
73 | CommonsRepository commonsRepository, | |
74 | UserCommons userCommons, | |
75 | int totalCows | |
76 | ) { | |
77 | var health = strategy.calculateNewCowHealth(commons, commonsRepository, userCommons, totalCows); | |
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(UserCommons userCommons, JobContext ctx) { | |
82 |
1
1. calculateCowDeaths : negated conditional → KILLED |
if (userCommons.getCowHealth() == 0.0) { |
83 |
2
1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED 2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowDeaths → KILLED |
userCommons.setCowDeaths(userCommons.getCowDeaths() + userCommons.getNumOfCows()); |
84 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED |
userCommons.setNumOfCows(0); |
85 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(100.0); |
86 | ||
87 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." ); |
88 | } | |
89 | } | |
90 | } | |
Mutations | ||
27 |
1.1 |
|
32 |
1.1 |
|
33 |
1.1 |
|
36 |
1.1 |
|
37 |
1.1 |
|
39 |
1.1 |
|
40 |
1.1 |
|
47 |
1.1 |
|
49 |
1.1 2.2 |
|
50 |
1.1 |
|
55 |
1.1 |
|
58 |
1.1 |
|
59 |
1.1 |
|
61 |
1.1 |
|
66 |
1.1 |
|
78 |
1.1 |
|
82 |
1.1 |
|
83 |
1.1 2.2 |
|
84 |
1.1 |
|
85 |
1.1 |
|
87 |
1.1 |