Skip to content

Commit 8b506e0

Browse files
authored
Merge pull request #6364 from IllianiCBT/infirmaryNag
Updated Untreated Injuries Nag Dialog to Account for AutoInfirmary
2 parents 0f7ece6 + d778e87 commit 8b506e0

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed

MekHQ/src/mekhq/gui/dialog/nagDialogs/NagController.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ public static boolean triggerDailyNags(Campaign campaign) {
9898
}
9999

100100
final List<Person> activePersonnel = campaign.getActivePersonnel(false);
101+
final CampaignOptions campaignOptions = campaign.getCampaignOptions();
102+
final int doctorCapacity = campaignOptions.getMaximumPatients();
101103

102104
// Untreated personnel
103-
if (UntreatedPersonnelNagDialog.checkNag(activePersonnel)) {
105+
if (UntreatedPersonnelNagDialog.checkNag(activePersonnel, doctorCapacity)) {
104106
UntreatedPersonnelNagDialog untreatedPersonnelNagDialog = new UntreatedPersonnelNagDialog(campaign);
105107
if (untreatedPersonnelNagDialog.wasAdvanceDayCanceled()) {
106108
return true;
@@ -135,7 +137,6 @@ public static boolean triggerDailyNags(Campaign campaign) {
135137

136138
// Unmaintained Units
137139
final Collection<Unit> units = campaign.getUnits();
138-
final CampaignOptions campaignOptions = campaign.getCampaignOptions();
139140
final boolean isCheckMaintenance = campaignOptions.isCheckMaintenance();
140141

141142
if (UnmaintainedUnitsNagDialog.checkNag(units, isCheckMaintenance)) {

MekHQ/src/mekhq/gui/dialog/nagDialogs/UntreatedPersonnelNagDialog.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
*/
2828
package mekhq.gui.dialog.nagDialogs;
2929

30+
import static mekhq.gui.dialog.nagDialogs.nagLogic.UntreatedPersonnelNagLogic.campaignHasUntreatedInjuries;
31+
32+
import java.util.List;
33+
3034
import mekhq.MHQConstants;
3135
import mekhq.MekHQ;
3236
import mekhq.campaign.Campaign;
3337
import mekhq.campaign.personnel.Person;
3438
import mekhq.gui.baseComponents.AbstractMHQNagDialog;
3539

36-
import java.util.List;
37-
38-
import static mekhq.gui.dialog.nagDialogs.nagLogic.UntreatedPersonnelNagLogic.campaignHasUntreatedInjuries;
39-
4040
/**
4141
* A nag dialog that alerts the user about untreated injuries within the campaign's personnel.
4242
*
@@ -76,13 +76,14 @@ public UntreatedPersonnelNagDialog(Campaign campaign) {
7676
* </ul>
7777
*
7878
* @param activePersonnel A {@link List} of active personnel in the campaign.
79-
* @return {@code true} if the nag dialog should be displayed due to untreated injuries,
80-
* {@code false} otherwise.
79+
* @param doctorCapacity The maximum number of patients each doctor can medicate.
80+
*
81+
* @return {@code true} if the nag dialog should be displayed due to untreated injuries, {@code false} otherwise.
8182
*/
82-
public static boolean checkNag(List<Person> activePersonnel) {
83+
public static boolean checkNag(List<Person> activePersonnel, int doctorCapacity) {
8384
final String NAG_KEY = MHQConstants.NAG_UNTREATED_PERSONNEL;
8485

8586
return !MekHQ.getMHQOptions().getNagDialogIgnore(NAG_KEY)
86-
&& campaignHasUntreatedInjuries(activePersonnel);
87+
&& campaignHasUntreatedInjuries(activePersonnel, doctorCapacity);
8788
}
8889
}

MekHQ/src/mekhq/gui/dialog/nagDialogs/nagLogic/UntreatedPersonnelNagLogic.java

+43-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
*/
2828
package mekhq.gui.dialog.nagDialogs.nagLogic;
2929

30-
import mekhq.campaign.personnel.Person;
31-
3230
import java.util.List;
3331

32+
import mekhq.MekHQ;
33+
import mekhq.campaign.personnel.Person;
34+
3435
public class UntreatedPersonnelNagLogic {
3536
/**
3637
* Determines whether the campaign has any personnel with untreated injuries.
@@ -46,12 +47,51 @@ public class UntreatedPersonnelNagLogic {
4647
* @param activePersonnel A {@link List} of active personnel in the campaign.
4748
* @return {@code true} if there are untreated injuries among the personnel, {@code false} otherwise.
4849
*/
49-
public static boolean campaignHasUntreatedInjuries(List<Person> activePersonnel) {
50+
public static boolean campaignHasUntreatedInjuries(List<Person> activePersonnel, int doctorCapacity) {
51+
// if we're automatically optimizing medical assignments, we only want to advance day if there are more
52+
// patients than doctor capacity
53+
if (MekHQ.getMHQOptions().getNewDayOptimizeMedicalAssignments()) {
54+
return checkDoctorCapacity(activePersonnel, doctorCapacity);
55+
}
56+
57+
// Otherwise, we only need to find the first unassigned patient.
5058
for (Person person : activePersonnel) {
5159
if (person.needsFixing() && person.getDoctorId() == null) {
5260
return true;
5361
}
5462
}
63+
5564
return false;
5665
}
66+
67+
/**
68+
* Checks if the current doctor capacity is sufficient to handle the patients needing attention.
69+
*
70+
* <p>This method iterates through a list of active personnel to count the number of patients
71+
* (individuals who need fixing) and available doctor capacity. The available doctor capacity
72+
* is calculated by multiplying the number of doctors by their individual capacity. The method
73+
* returns whether the number of patients exceeds the calculated doctor capacity.</p>
74+
*
75+
* @param activePersonnel a list of {@link Person} objects representing the active personnel,
76+
* including both patients and doctors.
77+
* @param doctorCapacity the number of patients a single doctor can handle.
78+
* @return {@code true} if the number of patients exceeds the total doctor capacity,
79+
* {@code false} otherwise.
80+
*/
81+
private static boolean checkDoctorCapacity(List<Person> activePersonnel, int doctorCapacity) {
82+
int patients = 0;
83+
int doctors = 0;
84+
85+
for (Person person : activePersonnel) {
86+
if (person.needsFixing()) {
87+
patients++;
88+
}
89+
90+
if (person.isDoctor()) {
91+
doctors += doctorCapacity;
92+
}
93+
}
94+
95+
return patients > doctors;
96+
}
5797
}

MekHQ/unittests/mekhq/gui/dialog/nagDialogs/nagLogic/UntreatedPersonnelNagLogicTest.java

+28-9
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@
2727
*/
2828
package mekhq.gui.dialog.nagDialogs.nagLogic;
2929

30+
import static mekhq.campaign.personnel.SkillType.S_DOCTOR;
31+
import static mekhq.campaign.personnel.enums.PersonnelRole.DOCTOR;
32+
import static mekhq.gui.dialog.nagDialogs.nagLogic.UntreatedPersonnelNagLogic.campaignHasUntreatedInjuries;
33+
import static org.junit.jupiter.api.Assertions.assertFalse;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
import static org.mockito.Mockito.mock;
36+
37+
import java.util.List;
38+
3039
import megamek.common.EquipmentType;
3140
import megamek.logging.MMLogger;
41+
import mekhq.MekHQ;
3242
import mekhq.campaign.Campaign;
3343
import mekhq.campaign.personnel.Person;
3444
import mekhq.campaign.personnel.SkillType;
@@ -39,13 +49,6 @@
3949
import org.junit.jupiter.api.BeforeEach;
4050
import org.junit.jupiter.api.Test;
4151

42-
import java.util.List;
43-
44-
import static mekhq.gui.dialog.nagDialogs.nagLogic.UntreatedPersonnelNagLogic.campaignHasUntreatedInjuries;
45-
import static org.junit.jupiter.api.Assertions.assertFalse;
46-
import static org.junit.jupiter.api.Assertions.assertTrue;
47-
import static org.mockito.Mockito.mock;
48-
4952
/**
5053
* This class contains test methods for the {@link UntreatedPersonnelNagDialog} class.
5154
* It tests the different combinations of untreated personnel and verifies the behavior of the
@@ -55,6 +58,7 @@ class UntreatedPersonnelNagLogicTest {
5558
Campaign campaign;
5659
Person injuredPerson;
5760
Person uninjuredPerson;
61+
Person doctor;
5862

5963
/**
6064
* Sets up the necessary dependencies and configurations before running the test methods.
@@ -81,18 +85,33 @@ public void init() {
8185
injuredPerson = new Person(campaign);
8286
injuredPerson.setHits(1);
8387
uninjuredPerson = new Person(campaign);
88+
doctor = new Person(campaign);
89+
doctor.addSkill(S_DOCTOR, 5, 0);
90+
doctor.setPrimaryRole(campaign, DOCTOR);
8491
}
8592

8693
// In the following tests the isUntreatedInjury() method is called, and its response is checked
8794
// against expected behavior
8895

8996
@Test
9097
public void isUntreatedInjuryTest() {
91-
assertTrue(campaignHasUntreatedInjuries(List.of(injuredPerson)));
98+
assertTrue(campaignHasUntreatedInjuries(List.of(injuredPerson), 0));
9299
}
93100

94101
@Test
95102
public void isNoUntreatedInjuryTest() {
96-
assertFalse(campaignHasUntreatedInjuries(List.of(uninjuredPerson)));
103+
assertFalse(campaignHasUntreatedInjuries(List.of(uninjuredPerson), 0));
104+
}
105+
106+
@Test
107+
public void isAboveDoctorThresholdTest() {
108+
MekHQ.getMHQOptions().setNewDayOptimizeMedicalAssignments(true);
109+
assertTrue(campaignHasUntreatedInjuries(List.of(injuredPerson), 0));
110+
}
111+
112+
@Test
113+
public void isBelowDoctorThresholdTest() {
114+
MekHQ.getMHQOptions().setNewDayOptimizeMedicalAssignments(true);
115+
assertFalse(campaignHasUntreatedInjuries(List.of(injuredPerson, doctor), 25));
97116
}
98117
}

0 commit comments

Comments
 (0)