Search in sources :

Example 6 with IDelegate

use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.

the class DefaultPlayerBridge method getRemoteDelegate.

@Override
public IRemote getRemoteDelegate() {
    if (game.isGameOver()) {
        throw new GameOverException("Game Over");
    }
    try {
        game.getData().acquireReadLock();
        try {
            final IDelegate delegate = game.getData().getDelegateList().getDelegate(currentDelegate);
            if (delegate == null) {
                final String errorMessage = "IDelegate in DefaultPlayerBridge.getRemote() cannot be null. CurrentStep: " + stepName + ", and CurrentDelegate: " + currentDelegate;
                // for some reason, client isn't getting or seeing the errors, so make sure we print it to err
                // too
                System.err.println(errorMessage);
                // Veqryn: hope that this suffices...?
                throw new IllegalStateException(errorMessage);
            }
            final RemoteName remoteName;
            try {
                remoteName = ServerGame.getRemoteName(delegate);
            } catch (final Exception e) {
                final String errorMessage = "IDelegate IRemote interface class returned null or was not correct interface. CurrentStep: " + stepName + ", and CurrentDelegate: " + currentDelegate;
                // for some reason, client isn't getting or seeing the errors, so make sure we print it to err
                // too
                System.err.println(errorMessage);
                ClientLogger.logQuietly(errorMessage, e);
                throw new IllegalStateException(errorMessage, e);
            }
            return getRemoteThatChecksForGameOver(game.getRemoteMessenger().getRemote(remoteName));
        } finally {
            game.getData().releaseReadLock();
        }
    } catch (final MessengerException me) {
        throw new GameOverException("Game Over!");
    }
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) MessengerException(games.strategy.engine.message.MessengerException) GameOverException(games.strategy.engine.GameOverException) IDelegate(games.strategy.engine.delegate.IDelegate) RemoteNotFoundException(games.strategy.engine.message.RemoteNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GameOverException(games.strategy.engine.GameOverException) MessengerException(games.strategy.engine.message.MessengerException)

Example 7 with IDelegate

use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.

the class ServerGame method startStep.

private void startStep(final boolean stepIsRestoredFromSavedGame) {
    // dont save if we just loaded
    final DefaultDelegateBridge bridge = new DefaultDelegateBridge(gameData, this, new DelegateHistoryWriter(channelMessenger), randomStats, delegateExecutionManager);
    if (delegateRandomSource == null) {
        delegateRandomSource = (IRandomSource) delegateExecutionManager.createOutboundImplementation(randomSource, new Class<?>[] { IRandomSource.class });
    }
    bridge.setRandomSource(delegateRandomSource);
    // node is created.
    if (needToInitialize) {
        addPlayerTypesToGameData(gamePlayers.values(), playerManager, bridge);
    }
    notifyGameStepChanged(stepIsRestoredFromSavedGame);
    delegateExecutionManager.enterDelegateExecution();
    try {
        final IDelegate delegate = getCurrentStep().getDelegate();
        delegate.setDelegateBridgeAndPlayer(bridge);
        delegate.start();
    } finally {
        delegateExecutionManager.leaveDelegateExecution();
    }
}
Also used : DelegateHistoryWriter(games.strategy.engine.history.DelegateHistoryWriter) DefaultDelegateBridge(games.strategy.engine.delegate.DefaultDelegateBridge) IDelegate(games.strategy.engine.delegate.IDelegate)

Example 8 with IDelegate

use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.

the class ServerGame method stopGame.

public void stopGame() {
    // we have already shut down
    if (isGameOver) {
        System.out.println("Game previously stopped, cannot stop again.");
        return;
    } else if (HeadlessGameServer.headless()) {
        System.out.println("Attempting to stop game.");
    }
    isGameOver = true;
    delegateExecutionStoppedLatch.countDown();
    // tell the players (especially the AI's) that the game is stopping, so stop doing stuff.
    for (final IGamePlayer player : gamePlayers.values()) {
        // not sure whether to put this before or after we delegate execution block, but definitely before the game loader
        // shutdown
        player.stopGame();
    }
    // block delegate execution to prevent outbound messages to the players while we shut down.
    try {
        if (!delegateExecutionManager.blockDelegateExecution(16000)) {
            System.err.println("Could not stop delegate execution.");
            if (HeadlessGameServer.getInstance() != null) {
                HeadlessGameServer.getInstance().printThreadDumpsAndStatus();
            } else {
                ErrorConsole.getConsole().dumpStacks();
            }
            // Try one more time
            if (!delegateExecutionManager.blockDelegateExecution(16000)) {
                System.err.println("Exiting...");
                System.exit(-1);
            }
        }
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    // shutdown
    try {
        delegateExecutionManager.setGameOver();
        getGameModifiedBroadcaster().shutDown();
        randomStats.shutDown();
        channelMessenger.unregisterChannelSubscriber(gameModifiedChannel, IGame.GAME_MODIFICATION_CHANNEL);
        remoteMessenger.unregisterRemote(SERVER_REMOTE);
        vault.shutDown();
        for (final IGamePlayer gp : gamePlayers.values()) {
            remoteMessenger.unregisterRemote(getRemoteName(gp.getPlayerId(), gameData));
        }
        for (final IDelegate delegate : gameData.getDelegateList()) {
            final Class<? extends IRemote> remoteType = delegate.getRemoteType();
            // if its null then it shouldnt be added as an IRemote
            if (remoteType == null) {
                continue;
            }
            remoteMessenger.unregisterRemote(getRemoteName(delegate));
        }
    } catch (final RuntimeException e) {
        ClientLogger.logQuietly("Failed to shut down server game", e);
    } finally {
        delegateExecutionManager.resumeDelegateExecution();
    }
    gameData.getGameLoader().shutDown();
    if (HeadlessGameServer.headless()) {
        System.out.println("StopGame successful.");
    }
}
Also used : IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) IDelegate(games.strategy.engine.delegate.IDelegate)

Example 9 with IDelegate

use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.

the class GameDataExporter method gamePlay.

private void gamePlay(final GameData data) {
    xmlfile.append("\n");
    xmlfile.append("    <gamePlay>\n");
    for (final IDelegate delegate : data.getDelegateList()) {
        if (!delegate.getName().equals("edit")) {
            xmlfile.append("        <delegate name=\"").append(delegate.getName()).append("\" javaClass=\"").append(delegate.getClass().getCanonicalName()).append("\" display=\"").append(delegate.getDisplayName()).append("\"/>\n");
        }
    }
    sequence(data);
    xmlfile.append("        <offset round=\"").append(data.getSequence().getRound() - 1).append("\"/>\n");
    xmlfile.append("    </gamePlay>\n");
}
Also used : IDelegate(games.strategy.engine.delegate.IDelegate)

Example 10 with IDelegate

use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.

the class GameDataManager method loadDelegates.

private static void loadDelegates(final ObjectInputStream input, final GameData data) throws ClassNotFoundException, IOException {
    for (Object endMarker = input.readObject(); !endMarker.equals(DELEGATE_LIST_END); endMarker = input.readObject()) {
        final String name = (String) input.readObject();
        final String displayName = (String) input.readObject();
        final String className = (String) input.readObject();
        final IDelegate instance;
        try {
            instance = (IDelegate) Class.forName(className).getDeclaredConstructor().newInstance();
            instance.initialize(name, displayName);
            data.getDelegateList().addDelegate(instance);
        } catch (final Exception e) {
            throw new IOException(e);
        }
        final String next = (String) input.readObject();
        if (next.equals(DELEGATE_DATA_NEXT)) {
            instance.loadState((Serializable) input.readObject());
        }
    }
}
Also used : IOException(java.io.IOException) IDelegate(games.strategy.engine.delegate.IDelegate) IOException(java.io.IOException)

Aggregations

IDelegate (games.strategy.engine.delegate.IDelegate)13 GameOverException (games.strategy.engine.GameOverException)2 GameStep (games.strategy.engine.data.GameStep)2 DefaultDelegateBridge (games.strategy.engine.delegate.DefaultDelegateBridge)2 IPersistentDelegate (games.strategy.engine.delegate.IPersistentDelegate)2 DelegateHistoryWriter (games.strategy.engine.history.DelegateHistoryWriter)2 MessengerException (games.strategy.engine.message.MessengerException)2 Element (org.w3c.dom.Element)2 GameData (games.strategy.engine.data.GameData)1 GameParseException (games.strategy.engine.data.GameParseException)1 GameSequence (games.strategy.engine.data.GameSequence)1 XmlGameElementMapper (games.strategy.engine.data.gameparser.XmlGameElementMapper)1 GameProperties (games.strategy.engine.data.properties.GameProperties)1 AutoSave (games.strategy.engine.delegate.AutoSave)1 IGamePlayer (games.strategy.engine.gamePlayer.IGamePlayer)1 RemoteName (games.strategy.engine.message.RemoteName)1 RemoteNotFoundException (games.strategy.engine.message.RemoteNotFoundException)1 EndRoundDelegate (games.strategy.triplea.delegate.EndRoundDelegate)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1