Skip to content

Commit 1366fde

Browse files
authored
Fix saving games from history. (#10497)
Fix saving games from history. This got broken by my recent history changes. Also a few clean ups.
1 parent 17146bc commit 1366fde

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

game-app/game-core/src/main/java/games/strategy/engine/history/History.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.ArrayList;
1010
import java.util.Enumeration;
1111
import java.util.List;
12+
import java.util.Optional;
1213
import javax.swing.tree.DefaultMutableTreeNode;
1314
import javax.swing.tree.DefaultTreeModel;
1415

@@ -131,12 +132,15 @@ public synchronized void gotoNode(final HistoryNode node) {
131132
* removes all changes that occurred after this node.
132133
*/
133134
public synchronized void removeAllHistoryAfterNode(final HistoryNode removeAfterNode) {
134-
gotoNode(removeAfterNode);
135135
assertCorrectThread();
136+
if (!seekingEnabled) {
137+
nextChangeIndex = changes.size();
138+
seekingEnabled = true;
139+
}
140+
gotoNode(getNearestLeafAtOrBefore(removeAfterNode).orElse((HistoryNode) getRoot()));
136141
try (GameData.Unlocker ignored = gameData.acquireWriteLock()) {
137-
final int lastChange = getLastChange(removeAfterNode) + 1;
138-
while (changes.size() > lastChange) {
139-
changes.remove(lastChange);
142+
if (changes.size() > nextChangeIndex) {
143+
changes.subList(nextChangeIndex, changes.size()).clear();
140144
}
141145
final Enumeration<?> enumeration =
142146
((DefaultMutableTreeNode) this.getRoot()).preorderEnumeration();
@@ -147,20 +151,27 @@ public synchronized void removeAllHistoryAfterNode(final HistoryNode removeAfter
147151
final HistoryNode node = (HistoryNode) enumeration.nextElement();
148152
if (node instanceof IndexedHistoryNode) {
149153
final int index = ((IndexedHistoryNode) node).getChangeStartIndex();
150-
if (index >= lastChange) {
154+
if (index >= nextChangeIndex) {
151155
startRemoving = true;
152156
}
153157
if (startRemoving) {
154158
nodesToRemove.add(node);
155159
}
156160
}
157161
}
158-
while (!nodesToRemove.isEmpty()) {
159-
removeNodeFromParent(nodesToRemove.remove(0));
162+
for (HistoryNode node : nodesToRemove) {
163+
removeNodeFromParent(node);
160164
}
161165
}
162166
}
163167

168+
public Optional<HistoryNode> getNearestLeafAtOrBefore(HistoryNode node) {
169+
if (node.isLeaf()) {
170+
return Optional.of(node);
171+
}
172+
return Optional.ofNullable((HistoryNode) node.getPreviousLeaf());
173+
}
174+
164175
synchronized void changeAdded(final Change change) {
165176
changes.add(change);
166177
if (seekingEnabled && nextChangeIndex == changes.size()) {

game-app/game-core/src/main/java/games/strategy/triplea/ui/TripleAFrame.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,6 @@ public void actionPerformed(final ActionEvent ae) {
18811881
Injections.getInstance().getEngineVersion())
18821882
.orElse(null);
18831883
if (datacopy != null) {
1884-
datacopy.getHistory().gotoNode(historyPanel.getCurrentPopupNode());
18851884
datacopy
18861885
.getHistory()
18871886
.removeAllHistoryAfterNode(historyPanel.getCurrentPopupNode());
@@ -1901,14 +1900,11 @@ public void actionPerformed(final ActionEvent ae) {
19011900
datacopy.getSequence().getStep(0).getDisplayName();
19021901
GamePlayer currentPlayer =
19031902
datacopy.getSequence().getStep(0).getPlayerId();
1903+
int roundOffset = datacopy.getSequence().getRoundOffset();
19041904
while (enumeration.hasMoreElements()) {
19051905
final HistoryNode node = (HistoryNode) enumeration.nextElement();
19061906
if (node instanceof Round) {
1907-
round =
1908-
Math.max(
1909-
0,
1910-
((Round) node).getRoundNo()
1911-
- datacopy.getSequence().getRoundOffset());
1907+
round = Math.max(0, ((Round) node).getRoundNo() - roundOffset);
19121908
currentPlayer = null;
19131909
stepDisplayName = node.getTitle();
19141910
} else if (node instanceof Step) {

game-app/game-core/src/main/java/games/strategy/triplea/ui/history/HistoryPanel.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,8 @@ private void gotoNode(final HistoryNode node) {
275275
// If this is not a leaf node, set the game state to the last leaf node before it. This way,
276276
// selecting something like "Round 3" shows the state at the start of the round, which ensures
277277
// chronological order when moving up/down the nodes using arrow keys even if some are expanded.
278-
if (!node.isLeaf()) {
279-
final TreeNode leaf = node.getPreviousLeaf();
280-
// If no previous leaf, select the root node.
281-
data.getHistory().gotoNode((HistoryNode) Optional.ofNullable(leaf).orElse(node.getRoot()));
282-
} else {
283-
data.getHistory().gotoNode(node);
284-
}
278+
Optional<HistoryNode> target = data.getHistory().getNearestLeafAtOrBefore(node);
279+
data.getHistory().gotoNode(target.orElse((HistoryNode) node.getRoot()));
285280
}
286281

287282
public HistoryNode getCurrentNode() {

0 commit comments

Comments
 (0)