Skip to content

Commit

Permalink
Refactored handling of day end checks.
Browse files Browse the repository at this point in the history
Separated 'overdue loans' and 'scenarios due' checks into distinct methods for clearer logic flow. Updated localization for overdue loan messages.
  • Loading branch information
IllianiCBT committed Sep 14, 2024
1 parent 44d819a commit aa73b09
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 84 deletions.
5 changes: 4 additions & 1 deletion MekHQ/resources/mekhq/resources/CampaignGUI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ lblCargoSummary.text=<html><nobr><b>Cargo Summary:</b></nobr></html>;
lblFacilityCapacities.text=<html><nobr><b>Facility Capacities:</b></nobr></html>;
panLog.title=Daily Activity Log

dialogCheckDueScenarios.text=You must complete scenarios with a date of today or earlier before advancing the day.
dialogOverdueLoans.title=Overdue loans
dialogOverdueLoans.text=You must resolve overdue loans before advancing the day.

dialogCheckDueScenarios.title=Scenarios Must Be Completed
dialogCheckDueScenarios.text=You must complete scenarios with a date of today or earlier before advancing the day.

spareBonusPartExchange.text=Bonus Part Exchange Program
80 changes: 28 additions & 52 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@
*/
package mekhq.campaign;

import static mekhq.campaign.personnel.backgrounds.BackgroundsController.randomMercenaryCompanyNameGenerator;
import static mekhq.campaign.personnel.education.EducationController.getAcademy;
import static mekhq.campaign.personnel.turnoverAndRetention.RetirementDefectionTracker.Payout.isBreakingContract;
import static mekhq.campaign.unit.Unit.SITE_FACILITY_MAINTENANCE;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.swing.JOptionPane;

import megamek.client.generator.RandomGenderGenerator;
import megamek.client.generator.RandomNameGenerator;
import megamek.client.generator.RandomUnitGenerator;
Expand All @@ -54,11 +37,7 @@
import megamek.common.loaders.BLKFile;
import megamek.common.loaders.EntityLoadingException;
import megamek.common.loaders.EntitySavingException;
import megamek.common.options.GameOptions;
import megamek.common.options.IBasicOption;
import megamek.common.options.IOption;
import megamek.common.options.IOptionGroup;
import megamek.common.options.OptionsConstants;
import megamek.common.options.*;
import megamek.common.util.BuildingBlock;
import megamek.common.weapons.autocannons.ACWeapon;
import megamek.common.weapons.flamers.FlamerWeapon;
Expand All @@ -71,11 +50,7 @@
import mekhq.campaign.Quartermaster.PartAcquisitionResult;
import mekhq.campaign.againstTheBot.AtBConfiguration;
import mekhq.campaign.event.*;
import mekhq.campaign.finances.Accountant;
import mekhq.campaign.finances.CurrencyManager;
import mekhq.campaign.finances.Finances;
import mekhq.campaign.finances.Loan;
import mekhq.campaign.finances.Money;
import mekhq.campaign.finances.*;
import mekhq.campaign.finances.enums.TransactionType;
import mekhq.campaign.force.Force;
import mekhq.campaign.force.Lance;
Expand All @@ -90,12 +65,7 @@
import mekhq.campaign.market.ShoppingList;
import mekhq.campaign.market.unitMarket.AbstractUnitMarket;
import mekhq.campaign.market.unitMarket.DisabledUnitMarket;
import mekhq.campaign.mission.AtBContract;
import mekhq.campaign.mission.AtBDynamicScenario;
import mekhq.campaign.mission.AtBScenario;
import mekhq.campaign.mission.Contract;
import mekhq.campaign.mission.Mission;
import mekhq.campaign.mission.Scenario;
import mekhq.campaign.mission.*;
import mekhq.campaign.mission.atb.AtBScenarioFactory;
import mekhq.campaign.mission.enums.AtBLanceRole;
import mekhq.campaign.mission.enums.MissionStatus;
Expand All @@ -105,25 +75,15 @@
import mekhq.campaign.parts.equipment.AmmoBin;
import mekhq.campaign.parts.equipment.EquipmentPart;
import mekhq.campaign.parts.equipment.MissingEquipmentPart;
import mekhq.campaign.personnel.Bloodname;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.personnel.PersonnelOptions;
import mekhq.campaign.personnel.Skill;
import mekhq.campaign.personnel.SkillType;
import mekhq.campaign.personnel.SpecialAbility;
import mekhq.campaign.personnel.*;
import mekhq.campaign.personnel.autoAwards.AutoAwardsController;
import mekhq.campaign.personnel.death.AbstractDeath;
import mekhq.campaign.personnel.death.DisabledRandomDeath;
import mekhq.campaign.personnel.divorce.AbstractDivorce;
import mekhq.campaign.personnel.divorce.DisabledRandomDivorce;
import mekhq.campaign.personnel.education.Academy;
import mekhq.campaign.personnel.education.EducationController;
import mekhq.campaign.personnel.enums.FamilialRelationshipType;
import mekhq.campaign.personnel.enums.PersonnelRole;
import mekhq.campaign.personnel.enums.PersonnelStatus;
import mekhq.campaign.personnel.enums.Phenotype;
import mekhq.campaign.personnel.enums.PrisonerStatus;
import mekhq.campaign.personnel.enums.SplittingSurnameStyle;
import mekhq.campaign.personnel.enums.*;
import mekhq.campaign.personnel.generator.AbstractPersonnelGenerator;
import mekhq.campaign.personnel.generator.DefaultPersonnelGenerator;
import mekhq.campaign.personnel.generator.RandomPortraitGenerator;
Expand All @@ -136,21 +96,16 @@
import mekhq.campaign.personnel.ranks.Ranks;
import mekhq.campaign.personnel.turnoverAndRetention.Fatigue;
import mekhq.campaign.personnel.turnoverAndRetention.RetirementDefectionTracker;
import mekhq.campaign.rating.CamOpsReputation.ReputationController;
import mekhq.campaign.rating.FieldManualMercRevDragoonsRating;
import mekhq.campaign.rating.IUnitRating;
import mekhq.campaign.rating.UnitRatingMethod;
import mekhq.campaign.rating.CamOpsReputation.ReputationController;
import mekhq.campaign.storyarc.StoryArc;
import mekhq.campaign.stratcon.StratconContractInitializer;
import mekhq.campaign.stratcon.StratconRulesManager;
import mekhq.campaign.stratcon.StratconTrackState;
import mekhq.campaign.unit.CargoStatistics;
import mekhq.campaign.unit.CrewType;
import mekhq.campaign.unit.HangarStatistics;
import mekhq.campaign.unit.TestUnit;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.unit.UnitOrder;
import mekhq.campaign.unit.UnitTechProgression;
import mekhq.campaign.unit.*;
import mekhq.campaign.universe.*;
import mekhq.campaign.universe.Planet.PlanetaryEvent;
import mekhq.campaign.universe.PlanetarySystem.PlanetarySystemEvent;
Expand All @@ -171,6 +126,22 @@
import mekhq.service.mrms.MRMSService;
import mekhq.utilities.MHQXMLUtility;

import javax.swing.*;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import static mekhq.campaign.personnel.backgrounds.BackgroundsController.randomMercenaryCompanyNameGenerator;
import static mekhq.campaign.personnel.education.EducationController.getAcademy;
import static mekhq.campaign.personnel.turnoverAndRetention.RetirementDefectionTracker.Payout.isBreakingContract;
import static mekhq.campaign.unit.Unit.SITE_FACILITY_MAINTENANCE;

/**
* The main campaign class, keeps track of teams and units
*
Expand Down Expand Up @@ -7749,6 +7720,11 @@ public int checkTurnoverPrompt() {
options[0]);
}

/**
* Checks if there are any scenarios that are due based on the current date.
*
* @return true if there are scenarios due, false otherwise
*/
public boolean checkScenariosDue() {
return getActiveMissions(true).stream()
.flatMap(m -> m.getCurrentScenarios().stream())
Expand Down
101 changes: 70 additions & 31 deletions MekHQ/src/mekhq/gui/CampaignGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,9 @@ public void refreshCalendar() {
getFrame().setTitle(getCampaign().getTitle());
}

/**
* Refreshes the 'funds' display on the GUI.
*/
private void refreshFunds() {
Money funds = getCampaign().getFunds();
String inDebt = "";
Expand Down Expand Up @@ -2415,106 +2418,96 @@ public void undeployForce(Force f, boolean killSubs) {

// region Subscriptions
@Subscribe
public void handleDayEnding(DayEndingEvent evt) {
// first check for overdue loan payments - don't allow advancement until
// these are addressed
if (getCampaign().checkOverDueLoans()) {
refreshFunds();
// FIXME : Localize
JOptionPane.showMessageDialog(null, "You must resolve overdue loans before advancing the day",
"Overdue loans", JOptionPane.WARNING_MESSAGE);
evt.cancel();
public void handleDayEnding(DayEndingEvent dayEndingEvent) {
if (checkForOverdueLoans(dayEndingEvent)) {
return;
}

if (getCampaign().checkScenariosDue()) {
JOptionPane.showMessageDialog(null, getResourceMap().getString("dialogCheckDueScenarios.text"),
getResourceMap().getString("dialogCheckDueScenarios.title"), JOptionPane.WARNING_MESSAGE);
evt.cancel();
if (checkForDueScenarios(dayEndingEvent)) {
return;
}

if (new UnmaintainedUnitsNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new PregnantCombatantNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new PrisonersNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new UntreatedPersonnelNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new EndContractNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new NoCommanderNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new InvalidFactionNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new UnableToAffordJumpNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new InsufficientAstechsNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new InsufficientAstechTimeNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new InsufficientMedicsNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (getCampaign().getLocalDate()
.equals(getCampaign().getLocalDate().with(TemporalAdjusters.lastDayOfMonth()))) {
if (new UnableToAffordExpensesNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}
}

if (new UnableToAffordLoanPaymentNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (getCampaign().getCampaignOptions().isUseAtB()) {
if (new ShortDeploymentNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new UnresolvedStratConContactsNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}

if (new OutstandingScenariosNagDialog(getFrame(), getCampaign()).showDialog().isCancelled()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}
}
Expand All @@ -2529,15 +2522,15 @@ public void handleDayEnding(DayEndingEvent evt) {
case 0:
// the user launched the turnover dialog
if (!showRetirementDefectionDialog()) {
evt.cancel();
dayEndingEvent.cancel();
return;
}
case 1:
// the user picked 'Advance Day Regardless'
break;
case 2:
// the user canceled
evt.cancel();
dayEndingEvent.cancel();
return;
default:
throw new IllegalStateException(
Expand All @@ -2546,6 +2539,52 @@ public void handleDayEnding(DayEndingEvent evt) {
}
}

/**
* Checks if there are any due Scenarios.
* If the checkScenariosDue method of the Campaign associated with the given DayEndingEvent returns true, a dialog
* shows up informing the user of the due scenarios, and the DayEndingEvent is canceled.
*
* @param dayEndingEvent the DayEndingEvent being checked.
* @return {@code true} if there are due scenarios and {@code false} otherwise.
*/
private boolean checkForDueScenarios(DayEndingEvent dayEndingEvent) {
if (getCampaign().checkScenariosDue()) {
JOptionPane.showMessageDialog(null,
getResourceMap().getString("dialogCheckDueScenarios.text"),
getResourceMap().getString("dialogCheckDueScenarios.title"),
JOptionPane.WARNING_MESSAGE);

dayEndingEvent.cancel();

return true;
}
return false;
}

/**
* Checks if there are any overdue loans
* If the checkOverDueLoans method of the Campaign associated with the given DayEndingEvent returns true,
* the funds get refreshed, a dialog shows up informing the user of the overdue loans, and the DayEndingEvent is canceled.
*
* @param dayEndingEvent the DayEndingEvent being checked.
* @return {@code true} if there are overdue loans and {@code false} otherwise.
*/
private boolean checkForOverdueLoans(DayEndingEvent dayEndingEvent) {
if (getCampaign().checkOverDueLoans()) {
refreshFunds();

JOptionPane.showMessageDialog(null,
getResourceMap().getString("dialogOverdueLoans.text"),
getResourceMap().getString("dialogOverdueLoans.title"),
JOptionPane.WARNING_MESSAGE);

dayEndingEvent.cancel();

return true;
}
return false;
}

@Subscribe
public void handleNewDay(NewDayEvent evt) {
refreshCalendar();
Expand Down

0 comments on commit aa73b09

Please sign in to comment.