Search in sources :

Example 1 with Event

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

the class ServerGame method addPlayerTypesToGameData.

private void addPlayerTypesToGameData(final Collection<IGamePlayer> localPlayers, final PlayerManager allPlayers, final IDelegateBridge bridge) {
    final GameData data = bridge.getData();
    // start before making changes.
    if (getCurrentStep() == null || getCurrentStep().getPlayerId() == null || (firstRun)) {
        firstRun = false;
        return;
    }
    // we can't add a new event or add new changes if we are not in a step.
    final HistoryNode curNode = data.getHistory().getLastNode();
    if (!(curNode instanceof Step) && !(curNode instanceof Event) && !(curNode instanceof EventChild)) {
        return;
    }
    final CompositeChange change = new CompositeChange();
    final Set<String> allPlayersString = allPlayers.getPlayers();
    bridge.getHistoryWriter().startEvent("Game Loaded");
    for (final IGamePlayer player : localPlayers) {
        allPlayersString.remove(player.getName());
        final boolean isHuman = player instanceof TripleAPlayer;
        bridge.getHistoryWriter().addChildToEvent(player.getName() + ((player.getName().endsWith("s") || player.getName().endsWith("ese") || player.getName().endsWith("ish")) ? " are" : " is") + " now being played by: " + player.getType());
        final PlayerID p = data.getPlayerList().getPlayerId(player.getName());
        final String newWhoAmI = ((isHuman ? "Human" : "AI") + ":" + player.getType());
        if (!p.getWhoAmI().equals(newWhoAmI)) {
            change.add(ChangeFactory.changePlayerWhoAmIChange(p, newWhoAmI));
        }
    }
    final Iterator<String> playerIter = allPlayersString.iterator();
    while (playerIter.hasNext()) {
        final String player = playerIter.next();
        playerIter.remove();
        bridge.getHistoryWriter().addChildToEvent(player + ((player.endsWith("s") || player.endsWith("ese") || player.endsWith("ish")) ? " are" : " is") + " now being played by: Human:Client");
        final PlayerID p = data.getPlayerList().getPlayerId(player);
        final String newWhoAmI = "Human:Client";
        if (!p.getWhoAmI().equals(newWhoAmI)) {
            change.add(ChangeFactory.changePlayerWhoAmIChange(p, newWhoAmI));
        }
    }
    if (!change.isEmpty()) {
        bridge.addChange(change);
    }
    needToInitialize = false;
    if (!allPlayersString.isEmpty()) {
        throw new IllegalStateException("Not all Player Types (ai/human/client) could be added to game data.");
    }
}
Also used : IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) PlayerID(games.strategy.engine.data.PlayerID) GameData(games.strategy.engine.data.GameData) GameStep(games.strategy.engine.data.GameStep) Step(games.strategy.engine.history.Step) EventChild(games.strategy.engine.history.EventChild) TripleAPlayer(games.strategy.triplea.TripleAPlayer) HistoryNode(games.strategy.engine.history.HistoryNode) Event(games.strategy.engine.history.Event) CompositeChange(games.strategy.engine.data.CompositeChange)

Example 2 with Event

use of games.strategy.engine.history.Event 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 3 with Event

use of games.strategy.engine.history.Event 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)

Aggregations

Event (games.strategy.engine.history.Event)3 HistoryNode (games.strategy.engine.history.HistoryNode)3 GameData (games.strategy.engine.data.GameData)2 PlayerID (games.strategy.engine.data.PlayerID)2 EventChild (games.strategy.engine.history.EventChild)2 Step (games.strategy.engine.history.Step)2 CompositeChange (games.strategy.engine.data.CompositeChange)1 GameStep (games.strategy.engine.data.GameStep)1 IGamePlayer (games.strategy.engine.gamePlayer.IGamePlayer)1 TripleAPlayer (games.strategy.triplea.TripleAPlayer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Icon (javax.swing.Icon)1 ImageIcon (javax.swing.ImageIcon)1 TreeModelEvent (javax.swing.event.TreeModelEvent)1 BadLocationException (javax.swing.text.BadLocationException)1 Document (javax.swing.text.Document)1 TreeNode (javax.swing.tree.TreeNode)1