Skip to content

Commit

Permalink
Merge pull request #4820 from IllianiCBT/autoAwards_posthumousAwardBug
Browse files Browse the repository at this point in the history
Refactored Personnel Filtering and Added Last Mission Date Check
  • Loading branch information
IllianiCBT authored Sep 13, 2024
2 parents 831b222 + 8f193be commit b5d7044
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ lblAwardBonusStyle.text=Award Bonuses
lblAwardBonusStyle.toolTipText=Toggle XP and/or Edge bonuses from awards.
chkEnableAutoAwards.text=Track Award Eligibility
chkEnableAutoAwards.toolTipText=Enable the automatic tracking of award eligibility.
chkIssuePosthumousAwards.text=Issue Posthumous Awards
chkIssuePosthumousAwards.text=Issue Posthumous Awards (AtB Only)
chkIssuePosthumousAwards.toolTipText=Enabling this setting will qualify personnel for awards even if they are dead (excludes formation-based kill awards).
chkIssueBestAwardOnly.text=Only Issue Best Award
chkIssueBestAwardOnly.toolTipText=Activating this setting will disqualify personnel from receiving an award if they qualify for a higher-tier award of the same type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import mekhq.campaign.personnel.Award;
import mekhq.campaign.personnel.AwardsFactory;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.personnel.enums.PersonnelRole;
import mekhq.gui.dialog.AutoAwardsDialog;

import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -245,38 +245,88 @@ public void PostGraduationController(Campaign campaign, List<UUID> personnel, Ha
* Builds a list of personnel autoAwards should process
*/
private List<UUID> getPersonnel() {
// Get whether to issue posthumous awards from campaign options.
boolean issuePosthumous = campaign.getCampaignOptions().isIssuePosthumousAwards();

List<UUID> personnel = campaign.getPersonnel()
.stream()
.filter(person ->
((!person.getStatus().isDepartedUnit()) && (!person.getPrisonerStatus().isCurrentPrisoner()) && (!person.hasRole(PersonnelRole.DEPENDENT)))
|| ((issuePosthumous) && (person.getStatus().isDead()) && (filterOutPersonnel(person)))
)
.map(Person::getId)
.collect(Collectors.toList());
// Get the last mission end date.
LocalDate lastMissionEndDate = getLastMissionEndDate();

// Fetch all personnel in the campaign.
Collection<Person> rawPersonnel = campaign.getPersonnel();
// Create a list to store Ids of qualifying personnel.
List<UUID> personnel = new ArrayList<>();

// Iterate through each person in the rawPersonnel collection.
for (Person person : rawPersonnel) {
// Check conditions of the person being active, not currently a prisoner, and not a civilian.
// If all conditions are met, add their ID to the list.
if ((person.getStatus().isActive())
&& (!person.getPrisonerStatus().isCurrentPrisoner())
&& (!person.getPrimaryRole().isCivilian())) {
personnel.add(person.getId());
}

// Check the conditions of the person being dead, the issuePosthumous flag being true,
// the person not currently being a prisoner, and not a civilian.
// If all conditions are met, further check if the person's date of death
// is after or equal to the last mission's end date.
// If all these conditions are met, add their ID to the list.
if (person.getStatus().isDead()
&& (issuePosthumous)
&& (!person.getPrisonerStatus().isCurrentPrisoner())
&& (!person.getPrimaryRole().isCivilian())) {
if (person.getDateOfDeath().isAfter(lastMissionEndDate) || person.getDateOfDeath().equals(lastMissionEndDate)) {
personnel.add(person.getId());
}
}
}

// Log the details of the personnel list.
logger.debug("Personnel {}", personnel);

// Return the list of qualifying personnel IDs.
return personnel;
}

/**
* Filters out personnel based on specific criteria.
* Returns the last mission end date based on the campaign's completed AtBContracts.
*
* @param person the person to be filtered
* @return true if the person should be filtered out, false otherwise
* @return the date of the last mission end, or today's date if no completed contracts exist
*/
private boolean filterOutPersonnel(Person person) {
return !((person.getPrisonerStatus().isCurrentPrisoner()) || (person.hasRole(PersonnelRole.DEPENDENT)))
&&
!((person.getStatus().isRetired())
|| (person.getStatus().isResigned())
|| (person.getStatus().isSacked())
|| (person.getStatus().isDeserted())
|| (person.getStatus().isDefected())
|| (person.getStatus().isMissing())
|| (person.getStatus().isLeft()));
private LocalDate getLastMissionEndDate() {
// Get the current date from the campaign object.
LocalDate today = campaign.getLocalDate();

// Get the list of completed contracts from the campaign object.
List<AtBContract> completedContracts = campaign.getCompletedAtBContracts();

// If there are no completed contracts, return the current date.
if (completedContracts.isEmpty()) {
return today;
}

// Initialize a variable to hold the ending date of the last contract.
LocalDate lastContractEndingDate = null;

// Loop through each contract in the list of completed contracts.
for (AtBContract contract : completedContracts) {
// Get the ending date of the current contract.
LocalDate endingDate = contract.getEndingDate();

// If this is the first iteration, assign the ending date of the current contract to lastContractEndingDate and continue to the next iteration.
if (lastContractEndingDate == null) {
lastContractEndingDate = endingDate;
continue;
}

// If the ending date of the current contract is after the lastContractEndingDate, update lastContractEndingDate.
if (endingDate.isAfter(lastContractEndingDate)) {
lastContractEndingDate = endingDate;
}
}

// Return the ending date of the last contract.
return lastContractEndingDate;
}

/**
Expand Down

0 comments on commit b5d7044

Please sign in to comment.