Skip to content

Commit

Permalink
Save games should save pending purchase selections #11819 (#12084)
Browse files Browse the repository at this point in the history
* Save games should save pending purchase selections #11819

* implementation using PurchaseDelegate

Signed-off-by: Marek Kurej <[email protected]>

* fixing indent & comments

Signed-off-by: Marek Kurej <[email protected]>

---------

Signed-off-by: Marek Kurej <[email protected]>
  • Loading branch information
zemjak authored Nov 24, 2023
1 parent 35a0c70 commit 3bd2d4a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import games.strategy.triplea.TripleA;
import games.strategy.triplea.delegate.AbstractMoveDelegate;
import games.strategy.triplea.delegate.PoliticsDelegate;
import games.strategy.triplea.delegate.PurchaseDelegate;
import games.strategy.triplea.delegate.TechTracker;
import games.strategy.triplea.delegate.TechnologyDelegate;
import games.strategy.triplea.delegate.battle.BattleDelegate;
Expand Down Expand Up @@ -491,6 +492,11 @@ public PoliticsDelegate getPoliticsDelegate() {
return (PoliticsDelegate) findDelegate("politics");
}

@Override
public PurchaseDelegate getPurchaseDelegate() {
return (PurchaseDelegate) findDelegate("purchase");
}

private IDelegate findDelegate(final String delegateName) {
final IDelegate delegate = this.getDelegate(delegateName);
if (delegate == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import games.strategy.engine.data.properties.GameProperties;
import games.strategy.triplea.delegate.AbstractMoveDelegate;
import games.strategy.triplea.delegate.PoliticsDelegate;
import games.strategy.triplea.delegate.PurchaseDelegate;
import games.strategy.triplea.delegate.TechTracker;
import games.strategy.triplea.delegate.TechnologyDelegate;
import games.strategy.triplea.delegate.battle.BattleDelegate;
Expand Down Expand Up @@ -70,6 +71,8 @@ public interface GameState {

PoliticsDelegate getPoliticsDelegate();

PurchaseDelegate getPurchaseDelegate();

AbstractMoveDelegate getMoveDelegate();

TechnologyDelegate getTechDelegate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class PurchaseDelegate extends BaseTripleADelegate
Comparator.comparing(o -> (UnitType) o.getAnyResultKey(), new UnitTypeComparator());

private boolean needToInitialize = true;
private IntegerMap<ProductionRule> pendingProductionRules;

@Override
public void start() {
Expand Down Expand Up @@ -102,6 +103,7 @@ public void start() {
@Override
public void end() {
super.end();
pendingProductionRules = null;
needToInitialize = true;
}

Expand All @@ -110,6 +112,7 @@ public Serializable saveState() {
final PurchaseExtendedDelegateState state = new PurchaseExtendedDelegateState();
state.superState = super.saveState();
state.needToInitialize = needToInitialize;
state.pendingProductionRules = pendingProductionRules;
return state;
}

Expand All @@ -118,6 +121,7 @@ public void loadState(final Serializable state) {
final PurchaseExtendedDelegateState s = (PurchaseExtendedDelegateState) state;
super.loadState(s.superState);
needToInitialize = s.needToInitialize;
pendingProductionRules = s.pendingProductionRules;
}

@Override
Expand Down Expand Up @@ -382,6 +386,14 @@ protected String removeFromPlayer(
return returnString.toString();
}

public IntegerMap<ProductionRule> getPendingProductionRules() {
return pendingProductionRules;
}

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

@Override
public void setHasPostedTurnSummary(final boolean hasPostedTurnSummary) {
// nothing for now
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package games.strategy.triplea.delegate;

import games.strategy.engine.data.ProductionRule;
import java.io.Serializable;
import org.triplea.java.collections.IntegerMap;

class PurchaseExtendedDelegateState implements Serializable {
private static final long serialVersionUID = 2326864364534284490L;

Serializable superState;
// add other variables here:
boolean needToInitialize;
IntegerMap<ProductionRule> pendingProductionRules;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import games.strategy.triplea.UnitUtils;
import games.strategy.triplea.attachments.RulesAttachment;
import games.strategy.triplea.delegate.Matches;
import games.strategy.triplea.delegate.PurchaseDelegate;
import games.strategy.triplea.formatter.MyFormatter;
import games.strategy.triplea.ui.panels.map.MapPanel;
import games.strategy.triplea.util.UnitSeparator;
Expand Down Expand Up @@ -53,6 +54,10 @@ public class PurchasePanel extends ActionPanel {
public void actionPerformed(final ActionEvent e) {
final GamePlayer player = getCurrentPlayer();
final GameData data = getData();
final PurchaseDelegate purchaseDelegate = data.getPurchaseDelegate();

// Restore pending production that was loaded from the save game.
purchase = purchaseDelegate.getPendingProductionRules();
purchase =
TabbedProductionPanel.getProduction(
player,
Expand All @@ -61,6 +66,10 @@ public void actionPerformed(final ActionEvent e) {
bid,
purchase,
getMap().getUiContext());

// Keeping actualized pending production in PurchaseDelegate for later saving game.
purchaseDelegate.setPendingProductionRules(purchase);

purchasedUnits.setUnitsFromProductionRuleMap(purchase, player);
if (purchase.totalValues() == 0) {
purchasedLabel.setText("");
Expand Down

0 comments on commit 3bd2d4a

Please sign in to comment.