Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix purchase persisting on max built error. #12670

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import games.strategy.triplea.attachments.TechAbilityAttachment;
import games.strategy.triplea.attachments.TerritoryAttachment;
import games.strategy.triplea.attachments.TriggerAttachment;
import games.strategy.triplea.attachments.UnitAttachment;
import games.strategy.triplea.attachments.UnitTypeComparator;
import games.strategy.triplea.delegate.remote.IAbstractForumPosterDelegate;
import games.strategy.triplea.delegate.remote.IPurchaseDelegate;
Expand Down Expand Up @@ -174,8 +173,7 @@ protected boolean canAfford(final IntegerMap<Resource> costs, final GamePlayer p
if (!(next instanceof Resource)) {
final UnitType type = (UnitType) next;
final int quantity = results.getInt(type);
final UnitAttachment ua = type.getUnitAttachment();
final int maxBuilt = ua.getMaxBuiltPerPlayer();
final int maxBuilt = type.getUnitAttachment().getMaxBuiltPerPlayer();
if (maxBuilt == 0) {
return "May not build any of this unit right now: " + type.getName();
} else if (maxBuilt > 0) {
Expand All @@ -184,13 +182,13 @@ protected boolean canAfford(final IntegerMap<Resource> costs, final GamePlayer p

final Predicate<Unit> unitTypeOwnedBy =
Matches.unitIsOfType(type).and(Matches.unitIsOwnedBy(player));
final Collection<Territory> allTerrs = getData().getMap().getTerritories();
for (final Territory t : allTerrs) {
for (final Territory t : getData().getMap().getTerritories()) {
currentlyBuilt += t.getUnitCollection().countMatches(unitTypeOwnedBy);
}

final int allowedBuild = maxBuilt - currentlyBuilt;
if (allowedBuild - quantity < 0) {
// Use Math.max(0, ...) to avoid negative if existing count exceeds limit.
final int allowedBuild = Math.max(0, maxBuilt - currentlyBuilt);
if (quantity > allowedBuild) {
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 @@ -39,7 +39,7 @@ public class PurchasePanel extends ActionPanel {
private static final String BUY = "Buy...";
private static final String CHANGE = "Change...";

private IntegerMap<ProductionRule> purchase;
private IntegerMap<ProductionRule> purchase = new IntegerMap<>();
private boolean bid;
private final SimpleUnitPanel purchasedPreviousRoundsUnits;
private final JLabel purchasedPreviousRoundsLabel;
Expand All @@ -61,10 +61,10 @@ public void actionPerformed(final ActionEvent e) {
// Use the delegate from the step, since it may not actually be named 'purchase'.
final IDelegate delegate = data.getSequence().getStep().getDelegate();
if (delegate instanceof PurchaseDelegate) {
purchase = ((PurchaseDelegate) delegate).getPendingProductionRules();
}
if (purchase == null) {
purchase = new IntegerMap<>();
final var savedPurchase = ((PurchaseDelegate) delegate).getPendingProductionRules();
if (savedPurchase != null) {
purchase = savedPurchase;
}
}

purchase =
Expand Down Expand Up @@ -112,7 +112,7 @@ public void display(final GamePlayer gamePlayer) {
if (keepCurrentPurchase) {
keepCurrentPurchase = false;
} else {
purchase = new IntegerMap<>();
purchase.clear();
}
SwingUtilities.invokeLater(
() -> {
Expand Down