Search in sources :

Example 1 with EventChild

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

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

Aggregations

GameData (games.strategy.engine.data.GameData)2 Event (games.strategy.engine.history.Event)2 EventChild (games.strategy.engine.history.EventChild)2 HistoryNode (games.strategy.engine.history.HistoryNode)2 Step (games.strategy.engine.history.Step)2 CompositeChange (games.strategy.engine.data.CompositeChange)1 GameStep (games.strategy.engine.data.GameStep)1 PlayerID (games.strategy.engine.data.PlayerID)1 IGamePlayer (games.strategy.engine.gamePlayer.IGamePlayer)1 TripleAPlayer (games.strategy.triplea.TripleAPlayer)1