Skip to content

Commit

Permalink
Fix "Can't remove more than" error due to save game purchase code. (#…
Browse files Browse the repository at this point in the history
…12578)

* Fix "Can't remove more than player has of resource" due to save game purchase code.

This would happen for a joined player because end() wasn't being called on the delegate object.

Also some code cleanup.

* More cleanup.
  • Loading branch information
asvitkine committed May 12, 2024
1 parent c17681d commit 5146b2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PurchaseDelegate extends BaseTripleADelegate
Comparator.comparing(o -> (UnitType) o.getAnyResultKey(), new UnitTypeComparator());

private boolean needToInitialize = true;
@Getter private IntegerMap<ProductionRule> pendingProductionRules;
@Getter private @Nullable IntegerMap<ProductionRule> pendingProductionRules;

@Override
public void start() {
Expand Down Expand Up @@ -388,7 +388,8 @@ protected String removeFromPlayer(
return returnString.toString();
}

public void setPendingProductionRules(IntegerMap<ProductionRule> pendingProductionRules) {
public void setPendingProductionRules(
@Nullable IntegerMap<ProductionRule> pendingProductionRules) {
this.pendingProductionRules = pendingProductionRules;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import games.strategy.engine.data.Territory;
import games.strategy.engine.data.Unit;
import games.strategy.engine.data.UnitType;
import games.strategy.engine.data.properties.GameProperties;
import games.strategy.engine.delegate.IDelegate;
import games.strategy.triplea.Properties;
import games.strategy.triplea.UnitUtils;
Expand Down Expand Up @@ -162,18 +163,19 @@ public void performDone() {
}
// give a warning if the
// player tries to produce too much
if (Properties.getWW2V2(getData().getProperties())
|| Properties.getPlacementRestrictedByFactory(getData().getProperties())) {
final GameData data = getData();
final GamePlayer player = getCurrentPlayer();
final GameProperties properties = data.getProperties();
if (Properties.getWW2V2(properties) || Properties.getPlacementRestrictedByFactory(properties)) {
int totalProd = 0;
try (GameData.Unlocker ignored = getData().acquireReadLock()) {
for (final Territory t :
CollectionUtils.getMatches(
getData().getMap().getTerritories(),
Matches.territoryHasOwnedIsFactoryOrCanProduceUnits(getCurrentPlayer()))) {
final GameData data = getData();
try (GameData.Unlocker ignored = data.acquireReadLock()) {
final var predicate = Matches.territoryHasOwnedIsFactoryOrCanProduceUnits(player);
final var territories =
CollectionUtils.getMatches(data.getMap().getTerritories(), predicate);
for (final Territory t : CollectionUtils.getMatches(territories, predicate)) {
totalProd +=
UnitUtils.getProductionPotentialOfTerritory(
t.getUnits(), t, getCurrentPlayer(), data.getProperties(), true, true);
t.getUnits(), t, player, properties, true, true);
}
}
// sum production for all units except factories
Expand All @@ -187,7 +189,6 @@ public void performDone() {
}
}
}
final GamePlayer player = getCurrentPlayer();
final Collection<Unit> unitsNeedingFactory =
CollectionUtils.getMatches(player.getUnits(), Matches.unitIsNotConstruction());
if (!bid
Expand All @@ -210,6 +211,11 @@ public void performDone() {
}
}
}
// When closing the panel, clear the pending production.
final IDelegate delegate = data.getSequence().getStep().getDelegate();
if (delegate instanceof PurchaseDelegate) {
((PurchaseDelegate) delegate).setPendingProductionRules(null);
}
release();
}

Expand Down

0 comments on commit 5146b2c

Please sign in to comment.