Search in sources :

Example 6 with HistoryNode

use of games.strategy.engine.history.HistoryNode in project triplea by triplea-game.

the class BaseEditDelegate method logEvent.

// We don't know the current context, so we need to figure
// out whether it makes more sense to log a new event or a child.
// If any child events came before us, then we'll log a child event.
// Otherwise, we'll log a new event.
void logEvent(final String message, final Object renderingObject) {
    // find last event node
    final GameData gameData = getData();
    gameData.acquireReadLock();
    boolean foundChild = false;
    try {
        HistoryNode curNode = gameData.getHistory().getLastNode();
        while (!(curNode instanceof Step) && !(curNode instanceof Event)) {
            if (curNode instanceof EventChild) {
                foundChild = true;
                break;
            }
            curNode = (HistoryNode) curNode.getPreviousNode();
        }
    } finally {
        gameData.releaseReadLock();
    }
    if (foundChild) {
        bridge.getHistoryWriter().addChildToEvent(message, renderingObject);
    } else {
        bridge.getHistoryWriter().startEvent(message, renderingObject);
    }
}
Also used : GameData(games.strategy.engine.data.GameData) HistoryNode(games.strategy.engine.history.HistoryNode) Event(games.strategy.engine.history.Event) Step(games.strategy.engine.history.Step) EventChild(games.strategy.engine.history.EventChild)

Example 7 with HistoryNode

use of games.strategy.engine.history.HistoryNode in project triplea by triplea-game.

the class CommentPanel method loadHistory.

private void loadHistory() {
    final Document doc = text.getDocument();
    final HistoryNode rootNode = (HistoryNode) data.getHistory().getRoot();
    @SuppressWarnings("unchecked") final Enumeration<TreeNode> nodeEnum = rootNode.preorderEnumeration();
    final Pattern p = Pattern.compile("^COMMENT: (.*)");
    String player = "";
    int round = 0;
    Icon icon = null;
    while (nodeEnum.hasMoreElements()) {
        final HistoryNode node = (HistoryNode) nodeEnum.nextElement();
        if (node instanceof Round) {
            round++;
        } else if (node instanceof Step) {
            final PlayerID playerId = ((Step) node).getPlayerId();
            if (playerId != null) {
                player = playerId.getName();
                icon = iconMap.get(playerId);
            }
        } else {
            final String title = node.getTitle();
            final Matcher m = p.matcher(title);
            if (m.matches()) {
                try {
                    // insert into ui document
                    final String prefix = " " + player + "(" + round + ") : ";
                    text.insertIcon(icon);
                    doc.insertString(doc.getLength(), prefix, bold);
                    doc.insertString(doc.getLength(), m.group(1) + "\n", normal);
                } catch (final BadLocationException e) {
                    ClientLogger.logQuietly("Failed to add history", e);
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) PlayerID(games.strategy.engine.data.PlayerID) Matcher(java.util.regex.Matcher) Step(games.strategy.engine.history.Step) Document(javax.swing.text.Document) HistoryNode(games.strategy.engine.history.HistoryNode) TreeNode(javax.swing.tree.TreeNode) Round(games.strategy.engine.history.Round) ImageIcon(javax.swing.ImageIcon) Icon(javax.swing.Icon) BadLocationException(javax.swing.text.BadLocationException)

Example 8 with HistoryNode

use of games.strategy.engine.history.HistoryNode in project triplea by triplea-game.

the class CommentPanel method readHistoryTreeEvent.

private void readHistoryTreeEvent(final TreeModelEvent e) {
    SwingAction.invokeNowOrLater(() -> {
        data.acquireReadLock();
        try {
            final Document doc = text.getDocument();
            final HistoryNode node = (HistoryNode) (e.getTreePath().getLastPathComponent());
            final TreeNode child = node == null ? null : (node.getChildCount() > 0 ? node.getLastChild() : null);
            final String title = child != null ? (child instanceof Event ? ((Event) child).getDescription() : child.toString()) : (node != null ? node.getTitle() : "");
            final Pattern p = Pattern.compile("^COMMENT: (.*)");
            final Matcher m = p.matcher(title);
            if (m.matches()) {
                final PlayerID playerId = data.getSequence().getStep().getPlayerId();
                final int round = data.getSequence().getRound();
                final String player = playerId.getName();
                final Icon icon = iconMap.get(playerId);
                try {
                    // insert into ui document
                    final String prefix = " " + player + "(" + round + ") : ";
                    text.insertIcon(icon);
                    doc.insertString(doc.getLength(), prefix, bold);
                    doc.insertString(doc.getLength(), m.group(1) + "\n", normal);
                } catch (final BadLocationException e1) {
                    ClientLogger.logQuietly("Failed to add history node", e1);
                }
            }
        } finally {
            data.releaseReadLock();
        }
    });
}
Also used : Pattern(java.util.regex.Pattern) PlayerID(games.strategy.engine.data.PlayerID) HistoryNode(games.strategy.engine.history.HistoryNode) Matcher(java.util.regex.Matcher) TreeNode(javax.swing.tree.TreeNode) TreeModelEvent(javax.swing.event.TreeModelEvent) Event(games.strategy.engine.history.Event) ImageIcon(javax.swing.ImageIcon) Icon(javax.swing.Icon) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 9 with HistoryNode

use of games.strategy.engine.history.HistoryNode in project triplea by triplea-game.

the class InitialSetup method run.

protected void run(final PrintGenerationData printData, final boolean useOriginalState) {
    final GameData gameData = printData.getData();
    if (useOriginalState) {
        final HistoryNode root = (HistoryNode) gameData.getHistory().getRoot();
        gameData.getHistory().gotoNode(root);
    }
    for (final UnitType currentType : gameData.getUnitTypeList()) {
        final UnitAttachment currentTypeUnitAttachment = UnitAttachment.get(currentType);
        unitInfoMap.put(currentType, currentTypeUnitAttachment);
    }
    new UnitInformation().saveToFile(printData, unitInfoMap);
    for (final PlayerID currentPlayer : gameData.getPlayerList()) {
        new CountryChart().saveToFile(currentPlayer, printData);
    }
    new PuInfo().saveToFile(printData);
    try {
        new PlayerOrder().saveToFile(printData);
        new PuChart(printData).saveToFile();
    } catch (final IOException e) {
        ClientLogger.logQuietly("Failed to save print generation data", e);
    }
}
Also used : UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) HistoryNode(games.strategy.engine.history.HistoryNode) UnitType(games.strategy.engine.data.UnitType) IOException(java.io.IOException)

Example 10 with HistoryNode

use of games.strategy.engine.history.HistoryNode in project triplea by triplea-game.

the class TripleAFrame method showHistory.

private void showHistory() {
    inHistory = true;
    inGame = false;
    setWidgetActivation();
    final GameData clonedGameData;
    data.acquireReadLock();
    try {
        // we want to use a clone of the data, so we can make changes to it
        // as we walk up and down the history
        clonedGameData = GameDataUtils.cloneGameData(data);
        if (clonedGameData == null) {
            return;
        }
        data.removeDataChangeListener(dataChangeListener);
        clonedGameData.testLocksOnRead();
        if (historySyncher != null) {
            throw new IllegalStateException("Two history synchers?");
        }
        historySyncher = new HistorySynchronizer(clonedGameData, game);
        clonedGameData.addDataChangeListener(dataChangeListener);
    } finally {
        data.releaseReadLock();
    }
    statsPanel.setGameData(clonedGameData);
    economyPanel.setGameData(clonedGameData);
    if (objectivePanel != null && !objectivePanel.isEmpty()) {
        objectivePanel.setGameData(clonedGameData);
    }
    details.setGameData(clonedGameData);
    mapPanel.setGameData(clonedGameData);
    final HistoryDetailsPanel historyDetailPanel = new HistoryDetailsPanel(clonedGameData, mapPanel);
    tabsPanel.removeAll();
    tabsPanel.add("History", historyDetailPanel);
    addTab("Players", statsPanel, 'P');
    addTab("Resources", economyPanel, 'R');
    if (objectivePanel != null && !objectivePanel.isEmpty()) {
        addTab(objectivePanel.getName(), objectivePanel, 'O');
    }
    addTab("Notes", notesPanel, 'N');
    addTab("Territory", details, 'T');
    if (getEditMode()) {
        tabsPanel.add("Edit", editPanel);
    }
    if (actionButtons.getCurrent() != null) {
        actionButtons.getCurrent().setActive(false);
    }
    historyComponent.removeAll();
    historyComponent.setLayout(new BorderLayout());
    // create history tree context menu
    // actions need to clear the history panel popup state when done
    final JPopupMenu popup = new JPopupMenu();
    popup.add(new AbstractAction("Show Summary Log") {

        private static final long serialVersionUID = -6730966512179268157L;

        @Override
        public void actionPerformed(final ActionEvent ae) {
            final HistoryLog historyLog = new HistoryLog();
            historyLog.printRemainingTurn(historyPanel.getCurrentPopupNode(), false, data.getDiceSides(), null);
            historyLog.printTerritorySummary(historyPanel.getCurrentPopupNode(), clonedGameData);
            historyLog.printProductionSummary(clonedGameData);
            historyPanel.clearCurrentPopupNode();
            historyLog.setVisible(true);
        }
    });
    popup.add(new AbstractAction("Show Detailed Log") {

        private static final long serialVersionUID = -8709762764495294671L;

        @Override
        public void actionPerformed(final ActionEvent ae) {
            final HistoryLog historyLog = new HistoryLog();
            historyLog.printRemainingTurn(historyPanel.getCurrentPopupNode(), true, data.getDiceSides(), null);
            historyLog.printTerritorySummary(historyPanel.getCurrentPopupNode(), clonedGameData);
            historyLog.printProductionSummary(clonedGameData);
            historyPanel.clearCurrentPopupNode();
            historyLog.setVisible(true);
        }
    });
    popup.add(new AbstractAction("Export Map Snapshot") {

        private static final long serialVersionUID = 1222760138263428443L;

        @Override
        public void actionPerformed(final ActionEvent ae) {
            ScreenshotExporter.exportScreenshot(TripleAFrame.this, data, historyPanel.getCurrentPopupNode());
            historyPanel.clearCurrentPopupNode();
        }
    });
    popup.add(new AbstractAction("Save Game at this point (BETA)") {

        private static final long serialVersionUID = 1430512376199927896L;

        @Override
        public void actionPerformed(final ActionEvent ae) {
            JOptionPane.showMessageDialog(TripleAFrame.this, "Please first left click on the spot you want to save from, Then right click and select 'Save Game From " + "History'" + "\n\nIt is recommended that when saving the game from the History panel:" + "\n * Your CURRENT GAME is at the start of some player's turn, and that no moves have been made and " + "no actions taken yet." + "\n * The point in HISTORY that you are trying to save at, is at the beginning of a player's turn, " + "or the beginning of a round." + "\nSaving at any other point, could potentially create errors." + "\nFor example, saving while your current game is in the middle of a move or battle phase will " + "always create errors in the savegame." + "\nAnd you will also get errors in the savegame if you try to create a save at a point in history " + "such as a move or battle phase.", "Save Game from History", JOptionPane.INFORMATION_MESSAGE);
            data.acquireReadLock();
            try {
                final File f = TripleAMenuBar.getSaveGameLocation(TripleAFrame.this);
                if (f != null) {
                    try (FileOutputStream fout = new FileOutputStream(f)) {
                        final GameData datacopy = GameDataUtils.cloneGameData(data, true);
                        datacopy.getHistory().gotoNode(historyPanel.getCurrentPopupNode());
                        datacopy.getHistory().removeAllHistoryAfterNode(historyPanel.getCurrentPopupNode());
                        // TODO: the saved current delegate is still the current delegate,
                        // rather than the delegate at that history popup node
                        // TODO: it still shows the current round number, rather than the round at the history popup node
                        // TODO: this could be solved easily if rounds/steps were changes,
                        // but that could greatly increase the file size :(
                        // TODO: this also does not undo the runcount of each delegate step
                        @SuppressWarnings("unchecked") final Enumeration<TreeNode> enumeration = ((DefaultMutableTreeNode) datacopy.getHistory().getRoot()).preorderEnumeration();
                        enumeration.nextElement();
                        int round = 0;
                        String stepDisplayName = datacopy.getSequence().getStep(0).getDisplayName();
                        PlayerID currentPlayer = datacopy.getSequence().getStep(0).getPlayerId();
                        while (enumeration.hasMoreElements()) {
                            final HistoryNode node = (HistoryNode) enumeration.nextElement();
                            if (node instanceof Round) {
                                round = Math.max(0, ((Round) node).getRoundNo() - datacopy.getSequence().getRoundOffset());
                                currentPlayer = null;
                                stepDisplayName = node.getTitle();
                            } else if (node instanceof Step) {
                                currentPlayer = ((Step) node).getPlayerId();
                                stepDisplayName = node.getTitle();
                            }
                        }
                        datacopy.getSequence().setRoundAndStep(round, stepDisplayName, currentPlayer);
                        GameDataManager.saveGame(fout, datacopy);
                        JOptionPane.showMessageDialog(TripleAFrame.this, "Game Saved", "Game Saved", JOptionPane.INFORMATION_MESSAGE);
                    } catch (final IOException e) {
                        ClientLogger.logQuietly("Failed to save game: " + f.getAbsolutePath(), e);
                    }
                }
            } finally {
                data.releaseReadLock();
            }
            historyPanel.clearCurrentPopupNode();
        }
    });
    final JSplitPane split = new JSplitPane();
    split.setOneTouchExpandable(true);
    split.setDividerSize(8);
    historyPanel = new HistoryPanel(clonedGameData, historyDetailPanel, popup, uiContext);
    split.setLeftComponent(historyPanel);
    split.setRightComponent(gameCenterPanel);
    split.setDividerLocation(150);
    historyComponent.add(split, BorderLayout.CENTER);
    historyComponent.add(gameSouthPanel, BorderLayout.SOUTH);
    getContentPane().removeAll();
    getContentPane().add(historyComponent, BorderLayout.CENTER);
    validate();
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) Enumeration(java.util.Enumeration) ActionEvent(java.awt.event.ActionEvent) HistorySynchronizer(games.strategy.engine.framework.HistorySynchronizer) HistoryLog(games.strategy.triplea.ui.history.HistoryLog) Step(games.strategy.engine.history.Step) IOException(java.io.IOException) HistoryPanel(games.strategy.triplea.ui.history.HistoryPanel) JPopupMenu(javax.swing.JPopupMenu) BorderLayout(java.awt.BorderLayout) HistoryNode(games.strategy.engine.history.HistoryNode) FileOutputStream(java.io.FileOutputStream) Round(games.strategy.engine.history.Round) HistoryDetailsPanel(games.strategy.triplea.ui.history.HistoryDetailsPanel) JSplitPane(javax.swing.JSplitPane) AbstractAction(javax.swing.AbstractAction) File(java.io.File)

Aggregations

HistoryNode (games.strategy.engine.history.HistoryNode)14 PlayerID (games.strategy.engine.data.PlayerID)9 Step (games.strategy.engine.history.Step)8 GameData (games.strategy.engine.data.GameData)5 Round (games.strategy.engine.history.Round)5 Event (games.strategy.engine.history.Event)3 IOException (java.io.IOException)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 TreeNode (javax.swing.tree.TreeNode)3 TreePath (javax.swing.tree.TreePath)3 UnitType (games.strategy.engine.data.UnitType)2 EventChild (games.strategy.engine.history.EventChild)2 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)2 HistoryPanel (games.strategy.triplea.ui.history.HistoryPanel)2 File (java.io.File)2 HashSet (java.util.HashSet)2 Icon (javax.swing.Icon)2 ImageIcon (javax.swing.ImageIcon)2 BadLocationException (javax.swing.text.BadLocationException)2