From 4f5adf9cbd0e0922d94d5047200e21440a49aa58 Mon Sep 17 00:00:00 2001 From: IllianiCBT Date: Thu, 9 Jan 2025 13:01:54 -0600 Subject: [PATCH 1/2] Refactored maternity leave handling logic. Moved maternity leave status changes from Campaign to AbstractProcreation for better encapsulation and logic centralization. Ensured proper status updates when pregnancies end and added safeguards for returning personnel to active duty. --- MekHQ/src/mekhq/campaign/Campaign.java | 21 ---------------- .../procreation/AbstractProcreation.java | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/MekHQ/src/mekhq/campaign/Campaign.java b/MekHQ/src/mekhq/campaign/Campaign.java index dfa601762f..4c0dea9015 100644 --- a/MekHQ/src/mekhq/campaign/Campaign.java +++ b/MekHQ/src/mekhq/campaign/Campaign.java @@ -4291,27 +4291,6 @@ private void processWeeklyRelationshipEvents(Person person) { getDivorce().processNewWeek(this, getLocalDate(), person, false); getMarriage().processNewWeek(this, getLocalDate(), person, false); getProcreation().processNewWeek(this, getLocalDate(), person); - - if (person.getGender().isFemale()) { - if (campaignOptions.isUseMaternityLeave()) { - if ((person.isPregnant()) - && (person.getStatus().isActive()) - && (person.getDueDate().minusWeeks(20).isAfter(currentDay.minusDays(1)))) { - - person.changeStatus(this, currentDay, PersonnelStatus.ON_MATERNITY_LEAVE); - } - - List children = person.getGenealogy().getChildren(); - - if ((person.getStatus().isOnMaternityLeave()) && (!children.isEmpty())) { - LocalDate lastChildBirthDate = getYoungestChildDateOfBirth(children); - - if (currentDay.isAfter(lastChildBirthDate)) { - person.changeStatus(this, getLocalDate(), PersonnelStatus.ACTIVE); - } - } - } - } } } diff --git a/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java b/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java index f7789ef3aa..4b0c218e12 100644 --- a/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java +++ b/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java @@ -393,6 +393,11 @@ public void birth(final Campaign campaign, final LocalDate today, final Person m // Cleanup Data removePregnancy(mother); + + // Return from Maternity leave + if (mother.getStatus().isOnMaternityLeave()) { + mother.changeStatus(campaign, today, PersonnelStatus.ACTIVE); + } } /** @@ -556,7 +561,26 @@ public void processNewWeek(final Campaign campaign, final LocalDate today, final // They give birth if the due date has passed if ((today.isAfter(person.getDueDate())) || (today.isEqual(person.getDueDate()))) { birth(campaign, today, person); + + if (person.getStatus().isOnMaternityLeave() && !person.isPregnant()) { + person.changeStatus(campaign, today, PersonnelStatus.ACTIVE); + } + + return; } + + if (campaign.getCampaignOptions().isUseMaternityLeave()) { + if (person.getStatus().isActive() + && (person.getDueDate().minusWeeks(20).isAfter(today.minusDays(1)))) { + person.changeStatus(campaign, today, PersonnelStatus.ON_MATERNITY_LEAVE); + } + } + + // This is added as insurance, in case we ever miss returning the character to active duty + if (person.getStatus().isOnMaternityLeave() && !person.isPregnant()) { + person.changeStatus(campaign, today, PersonnelStatus.ACTIVE); + } + return; } From 3f0eecf4e679687e784318299009719ded235996 Mon Sep 17 00:00:00 2001 From: IllianiCBT Date: Thu, 9 Jan 2025 13:26:07 -0600 Subject: [PATCH 2/2] `Removed redundant maternity leave status checks` The code no longer redundantly resets maternity leave status for non-pregnant characters, as this logic was already handled elsewhere. Added unit tests to ensure correct behavior for various scenarios, such as different genders and pregnancy states. This cleanup improves clarity and prevents unnecessary status updates. --- .../procreation/AbstractProcreation.java | 9 ------ .../procreation/AbstractProcreationTest.java | 28 ++++++++++++++++--- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java b/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java index 4b0c218e12..04ae85dd39 100644 --- a/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java +++ b/MekHQ/src/mekhq/campaign/personnel/procreation/AbstractProcreation.java @@ -562,10 +562,6 @@ public void processNewWeek(final Campaign campaign, final LocalDate today, final if ((today.isAfter(person.getDueDate())) || (today.isEqual(person.getDueDate()))) { birth(campaign, today, person); - if (person.getStatus().isOnMaternityLeave() && !person.isPregnant()) { - person.changeStatus(campaign, today, PersonnelStatus.ACTIVE); - } - return; } @@ -576,11 +572,6 @@ public void processNewWeek(final Campaign campaign, final LocalDate today, final } } - // This is added as insurance, in case we ever miss returning the character to active duty - if (person.getStatus().isOnMaternityLeave() && !person.isPregnant()) { - person.changeStatus(campaign, today, PersonnelStatus.ACTIVE); - } - return; } diff --git a/MekHQ/unittests/mekhq/campaign/personnel/procreation/AbstractProcreationTest.java b/MekHQ/unittests/mekhq/campaign/personnel/procreation/AbstractProcreationTest.java index 7cdafedfae..879ce8d811 100644 --- a/MekHQ/unittests/mekhq/campaign/personnel/procreation/AbstractProcreationTest.java +++ b/MekHQ/unittests/mekhq/campaign/personnel/procreation/AbstractProcreationTest.java @@ -445,27 +445,47 @@ public void testProcessPregnancyComplications() { } //endregion Pregnancy Complications - //region New Day + //region Process New Week + @Test - public void testProcessNewWeek() { + public void testProcessNewWeek_ForNonPregnantMale() { doCallRealMethod().when(mockProcreation).processNewWeek(any(), any(), any()); - doNothing().when(mockProcreation).birth(any(), any(), any()); final Person mockPerson = mock(Person.class); - when(mockPerson.getGender()).thenReturn(Gender.MALE); mockProcreation.processNewWeek(mockCampaign, LocalDate.ofYearDay(3025, 1), mockPerson); verify(mockPerson, never()).isPregnant(); verify(mockProcreation, never()).randomlyProcreates(any(), any()); + } + + @Test + public void testProcessNewWeek_ForPregnantFemale() { + doCallRealMethod().when(mockProcreation).processNewWeek(any(), any(), any()); + + final Person mockPerson = mock(Person.class); when(mockPerson.getGender()).thenReturn(Gender.FEMALE); when(mockPerson.isPregnant()).thenReturn(true); when(mockPerson.getDueDate()).thenReturn(LocalDate.ofYearDay(3025, 2)); + mockProcreation.processNewWeek(mockCampaign, LocalDate.ofYearDay(3025, 1), mockPerson); + verify(mockProcreation, never()).birth(any(), any(), any()); verify(mockProcreation, never()).randomlyProcreates(any(), any()); + } + + @Test + public void testProcessNewWeek_ForPregnantFemaleWithDueDate() { + doCallRealMethod().when(mockProcreation).processNewWeek(any(), any(), any()); + doNothing().when(mockProcreation).birth(any(), any(), any()); + final Person mockPerson = mock(Person.class); + + // Ensure proper stubbing + when(mockPerson.getGender()).thenReturn(Gender.FEMALE); + when(mockPerson.isPregnant()).thenReturn(true); when(mockPerson.getDueDate()).thenReturn(LocalDate.ofYearDay(3025, 1)); + mockProcreation.processNewWeek(mockCampaign, LocalDate.ofYearDay(3025, 1), mockPerson); verify(mockProcreation, times(1)).birth(any(), any(), any()); verify(mockProcreation, never()).randomlyProcreates(any(), any());