Skip to content

Commit

Permalink
Don't reset purchase selection on purchase error. (#11818)
Browse files Browse the repository at this point in the history
For example, if you tried to buy too many of a unit. Previously, your full purchase selection will be cleared. Now, it will just bring you back to the purchase screen after showing error, keeping the previous selections.
  • Loading branch information
asvitkine authored Jul 26, 2023
1 parent c345ed7 commit e2b8b6c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,9 @@ public String purchase(final IntegerMap<ProductionRule> productionRules) {

final int allowedBuild = maxBuilt - currentlyBuilt;
if (allowedBuild - quantity < 0) {
return "May only build "
+ allowedBuild
+ " of "
+ type.getName()
+ " this turn, may only build "
+ maxBuilt
+ " total";
return String.format(
"May only build %s of %s this turn, may only build %s total",
allowedBuild, type.getName(), maxBuilt);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void start(final String name) {
if (GameStep.isTechStep(name)) {
tech();
} else if (GameStep.isPurchaseOrBidStep(name)) {
purchase(GameStepPropertiesHelper.isBid(getGameData()));
purchase(GameStepPropertiesHelper.isBid(getGameData()), false);
if (!GameStepPropertiesHelper.isBid(getGameData())) {
ui.waitForMoveForumPoster(this.getGamePlayer(), getPlayerBridge());
// TODO only do forum post if there is a combat
Expand Down Expand Up @@ -414,7 +414,7 @@ private boolean canUnitsFight() {
return !(unitsCantFight.isEmpty() || ui.getOkToLetUnitsDie(unitsCantFight));
}

private void purchase(final boolean bid) {
private void purchase(final boolean bid, final boolean keepCurrentPurchase) {
if (getPlayerBridge().isGameOver()) {
return;
}
Expand Down Expand Up @@ -464,15 +464,15 @@ private void purchase(final boolean bid) {
if (error != null) {
ui.notifyError(error);
// don't give up, keep going
purchase(bid);
purchase(bid, true);
}
}
}
}
if (isOnlyRepairIfDisabled) {
return;
}
final IntegerMap<ProductionRule> prod = ui.getProduction(gamePlayer, bid);
final IntegerMap<ProductionRule> prod = ui.getProduction(gamePlayer, bid, keepCurrentPurchase);
if (prod == null) {
return;
}
Expand All @@ -492,7 +492,7 @@ private void purchase(final boolean bid) {
if (purchaseError != null) {
ui.notifyError(purchaseError);
// don't give up, keep going
purchase(bid);
purchase(bid, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public void changeToRepair(final GamePlayer gamePlayer) {
changeTo(gamePlayer, repairPanel);
}

public void changeToProduce(final GamePlayer gamePlayer) {
public void changeToProduce(final GamePlayer gamePlayer, final boolean keepCurrentPurchase) {
purchasePanel.setKeepCurrentPurchase(keepCurrentPurchase);
changeTo(gamePlayer, purchasePanel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import lombok.Setter;
import org.triplea.java.collections.CollectionUtils;
import org.triplea.java.collections.IntegerMap;
import org.triplea.swing.SwingComponents;
Expand All @@ -43,6 +44,7 @@ public class PurchasePanel extends ActionPanel {
private final SimpleUnitPanel purchasedUnits;
private final JLabel purchasedLabel = createIndentedLabel();
private final JButton buyButton;
@Setter private boolean keepCurrentPurchase;

private final AbstractAction purchaseAction =
new AbstractAction("Buy") {
Expand Down Expand Up @@ -86,7 +88,13 @@ public PurchasePanel(final GameData data, final MapPanel map) {
@Override
public void display(final GamePlayer gamePlayer) {
super.display(gamePlayer);
purchase = new IntegerMap<>();
// If keepCurrentPurchase is true, we're trying after showing an error to the user about their
// current selection. Don't clear everything and let the user correct it instead.
if (keepCurrentPurchase) {
keepCurrentPurchase = false;
} else {
purchase = new IntegerMap<>();
}
SwingUtilities.invokeLater(
() -> {
removeAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,10 @@ public void setStatusWarningMessage(final String msg) {
bottomBar.setStatus(msg, mapPanel.getWarningImage());
}

public IntegerMap<ProductionRule> getProduction(final GamePlayer player, final boolean bid) {
public IntegerMap<ProductionRule> getProduction(
final GamePlayer player, final boolean bid, final boolean keepCurrentPurchase) {
messageAndDialogThreadPool.waitForAll();
actionButtons.changeToProduce(player);
actionButtons.changeToProduce(player, keepCurrentPurchase);
return actionButtons.waitForPurchase(bid);
}

Expand Down

0 comments on commit e2b8b6c

Please sign in to comment.