Skip to content

Commit

Permalink
Fix saving games from history. (#10497)
Browse files Browse the repository at this point in the history
Fix saving games from history. This got broken by my recent history changes.

Also a few clean ups.
  • Loading branch information
asvitkine authored May 23, 2022
1 parent 17146bc commit 1366fde
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

Expand Down Expand Up @@ -131,12 +132,15 @@ public synchronized void gotoNode(final HistoryNode node) {
* removes all changes that occurred after this node.
*/
public synchronized void removeAllHistoryAfterNode(final HistoryNode removeAfterNode) {
gotoNode(removeAfterNode);
assertCorrectThread();
if (!seekingEnabled) {
nextChangeIndex = changes.size();
seekingEnabled = true;
}
gotoNode(getNearestLeafAtOrBefore(removeAfterNode).orElse((HistoryNode) getRoot()));
try (GameData.Unlocker ignored = gameData.acquireWriteLock()) {
final int lastChange = getLastChange(removeAfterNode) + 1;
while (changes.size() > lastChange) {
changes.remove(lastChange);
if (changes.size() > nextChangeIndex) {
changes.subList(nextChangeIndex, changes.size()).clear();
}
final Enumeration<?> enumeration =
((DefaultMutableTreeNode) this.getRoot()).preorderEnumeration();
Expand All @@ -147,20 +151,27 @@ public synchronized void removeAllHistoryAfterNode(final HistoryNode removeAfter
final HistoryNode node = (HistoryNode) enumeration.nextElement();
if (node instanceof IndexedHistoryNode) {
final int index = ((IndexedHistoryNode) node).getChangeStartIndex();
if (index >= lastChange) {
if (index >= nextChangeIndex) {
startRemoving = true;
}
if (startRemoving) {
nodesToRemove.add(node);
}
}
}
while (!nodesToRemove.isEmpty()) {
removeNodeFromParent(nodesToRemove.remove(0));
for (HistoryNode node : nodesToRemove) {
removeNodeFromParent(node);
}
}
}

public Optional<HistoryNode> getNearestLeafAtOrBefore(HistoryNode node) {
if (node.isLeaf()) {
return Optional.of(node);
}
return Optional.ofNullable((HistoryNode) node.getPreviousLeaf());
}

synchronized void changeAdded(final Change change) {
changes.add(change);
if (seekingEnabled && nextChangeIndex == changes.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,6 @@ public void actionPerformed(final ActionEvent ae) {
Injections.getInstance().getEngineVersion())
.orElse(null);
if (datacopy != null) {
datacopy.getHistory().gotoNode(historyPanel.getCurrentPopupNode());
datacopy
.getHistory()
.removeAllHistoryAfterNode(historyPanel.getCurrentPopupNode());
Expand All @@ -1901,14 +1900,11 @@ public void actionPerformed(final ActionEvent ae) {
datacopy.getSequence().getStep(0).getDisplayName();
GamePlayer currentPlayer =
datacopy.getSequence().getStep(0).getPlayerId();
int roundOffset = datacopy.getSequence().getRoundOffset();
while (enumeration.hasMoreElements()) {
final HistoryNode node = (HistoryNode) enumeration.nextElement();
if (node instanceof Round) {
round =
Math.max(
0,
((Round) node).getRoundNo()
- datacopy.getSequence().getRoundOffset());
round = Math.max(0, ((Round) node).getRoundNo() - roundOffset);
currentPlayer = null;
stepDisplayName = node.getTitle();
} else if (node instanceof Step) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,8 @@ private void gotoNode(final HistoryNode node) {
// If this is not a leaf node, set the game state to the last leaf node before it. This way,
// selecting something like "Round 3" shows the state at the start of the round, which ensures
// chronological order when moving up/down the nodes using arrow keys even if some are expanded.
if (!node.isLeaf()) {
final TreeNode leaf = node.getPreviousLeaf();
// If no previous leaf, select the root node.
data.getHistory().gotoNode((HistoryNode) Optional.ofNullable(leaf).orElse(node.getRoot()));
} else {
data.getHistory().gotoNode(node);
}
Optional<HistoryNode> target = data.getHistory().getNearestLeafAtOrBefore(node);
data.getHistory().gotoNode(target.orElse((HistoryNode) node.getRoot()));
}

public HistoryNode getCurrentNode() {
Expand Down

0 comments on commit 1366fde

Please sign in to comment.