Skip to content

Commit

Permalink
Merge pull request #5089 from IllianiCBT/nag_nextJumpBug
Browse files Browse the repository at this point in the history
Added Empty Jump Path Check `UnableToAffordJumpNagDialog`
  • Loading branch information
HammerGS authored Oct 21, 2024
2 parents 86eda2f + 912bd3f commit 732f6fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import mekhq.MHQConstants;
import mekhq.MekHQ;
import mekhq.campaign.Campaign;
import mekhq.campaign.CurrentLocation;
import mekhq.campaign.JumpPath;
import mekhq.campaign.finances.Money;
import mekhq.gui.baseComponents.AbstractMHQNagDialog;

import javax.swing.*;
import java.util.Objects;

/**
* This class represents a nag dialog displayed when the campaign can't afford its next jump
Expand All @@ -45,7 +48,11 @@ static boolean isUnableToAffordNextJump (Campaign campaign) {
Money currentFunds = campaign.getFunds();
Money nextJumpCost = getNextJumpCost(campaign);

return currentFunds.isLessThan(nextJumpCost);
if (nextJumpCost.isZero()) {
return false;
} else {
return currentFunds.isLessThan(nextJumpCost);
}
}

/**
Expand All @@ -59,6 +66,17 @@ static boolean isUnableToAffordNextJump (Campaign campaign) {
* @return the cost of the next jump for the campaign
*/
static Money getNextJumpCost(Campaign campaign) {
CurrentLocation location = campaign.getLocation();
JumpPath jumpPath = location.getJumpPath();

if (jumpPath == null) {
return Money.zero();
}

if (Objects.equals(jumpPath.getLastSystem(), location.getCurrentSystem())) {
return Money.zero();
}

boolean isContractPayBasedOnToeUnitsValue = campaign.getCampaignOptions().isEquipmentContractBase();

return campaign.calculateCostPerJump(true, isContractPayBasedOnToeUnitsValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

import mekhq.campaign.Campaign;
import mekhq.campaign.CampaignOptions;
import mekhq.campaign.CurrentLocation;
import mekhq.campaign.JumpPath;
import mekhq.campaign.finances.Money;
import mekhq.campaign.universe.PlanetarySystem;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -38,7 +41,6 @@
class UnableToAffordJumpNagDialogTest {
// Mock objects for the tests
private Campaign campaign;
private CampaignOptions options;

/**
* Test setup for each test, runs before each test.
Expand All @@ -48,10 +50,21 @@ class UnableToAffordJumpNagDialogTest {
void init() {
// Initialize the mock objects
campaign = mock(Campaign.class);
options = mock(CampaignOptions.class);
CampaignOptions options = mock(CampaignOptions.class);
CurrentLocation location = mock(CurrentLocation.class);
JumpPath jumpPath = mock(JumpPath.class);


// Stubs
when(campaign.getCampaignOptions()).thenReturn(options);

PlanetarySystem originSystem = mock(PlanetarySystem.class);
PlanetarySystem destinationSystem = mock(PlanetarySystem.class);

jumpPath.addSystem(destinationSystem);
when(campaign.getLocation()).thenReturn(location);
when(location.getCurrentSystem()).thenReturn(originSystem);
when(location.getJumpPath()).thenReturn(jumpPath);
}

// In the following tests the canAffordNextJump() method is called, and its response is checked
Expand All @@ -72,4 +85,4 @@ void cannotAffordNextJump() {

assertTrue(isUnableToAffordNextJump(campaign));
}
}
}

0 comments on commit 732f6fb

Please sign in to comment.