Search in sources :

Example 1 with IDelegate

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

the class GameParser method parseDelegates.

private void parseDelegates(final List<Element> delegateList) throws GameParseException {
    final DelegateList delegates = data.getDelegateList();
    for (final Element current : delegateList) {
        // load the class
        final String className = current.getAttribute("javaClass");
        final XmlGameElementMapper elementMapper = new XmlGameElementMapper();
        final IDelegate delegate = elementMapper.getDelegate(className).orElseThrow(() -> newGameParseException("Class <" + className + "> is not a delegate."));
        final String name = current.getAttribute("name");
        String displayName = current.getAttribute("display");
        if (displayName == null) {
            displayName = name;
        }
        delegate.initialize(name, displayName);
        delegates.addDelegate(delegate);
    }
}
Also used : Element(org.w3c.dom.Element) XmlGameElementMapper(games.strategy.engine.data.gameparser.XmlGameElementMapper) IDelegate(games.strategy.engine.delegate.IDelegate)

Example 2 with IDelegate

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

the class GameParser method parseSteps.

private void parseSteps(final List<Element> stepList) throws GameParseException {
    for (final Element current : stepList) {
        final IDelegate delegate = getDelegate(current, "delegate", true);
        final PlayerID player = getPlayerId(current, "player", false);
        final String name = current.getAttribute("name");
        String displayName = null;
        final List<Element> propertyElements = getChildren("stepProperty", current);
        final Properties stepProperties = pareStepProperties(propertyElements);
        if (current.hasAttribute("display")) {
            displayName = current.getAttribute("display");
        }
        final GameStep step = new GameStep(name, displayName, player, delegate, data, stepProperties);
        if (current.hasAttribute("maxRunCount")) {
            final int runCount = Integer.parseInt(current.getAttribute("maxRunCount"));
            if (runCount <= 0) {
                throw newGameParseException("maxRunCount must be positive");
            }
            step.setMaxRunCount(runCount);
        }
        data.getSequence().addStep(step);
    }
}
Also used : Element(org.w3c.dom.Element) GameProperties(games.strategy.engine.data.properties.GameProperties) Properties(java.util.Properties) IDelegate(games.strategy.engine.delegate.IDelegate)

Example 3 with IDelegate

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

the class TriggerAttachment method triggerVictory.

public static void triggerVictory(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
    final GameData data = bridge.getData();
    Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, victoryMatch());
    if (testWhen) {
        trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
    }
    if (testUses) {
        trigs = CollectionUtils.getMatches(trigs, availableUses);
    }
    for (final TriggerAttachment t : trigs) {
        if (testChance && !t.testChance(bridge)) {
            continue;
        }
        if (useUses) {
            t.use(bridge);
        }
        if (t.getVictory() == null || t.getPlayers() == null) {
            continue;
        }
        final String victoryMessage = NotificationMessages.getInstance().getMessage(t.getVictory().trim());
        final String sounds = NotificationMessages.getInstance().getSoundsKey(t.getVictory().trim());
        if (victoryMessage != null) {
            if (sounds != null) {
                // only play the sound if we are also notifying everyone
                bridge.getSoundChannelBroadcaster().playSoundToPlayers(SoundPath.CLIP_TRIGGERED_VICTORY_SOUND + sounds.trim(), t.getPlayers(), null, true);
                bridge.getSoundChannelBroadcaster().playSoundToPlayers(SoundPath.CLIP_TRIGGERED_DEFEAT_SOUND + sounds.trim(), data.getPlayerList().getPlayers(), t.getPlayers(), false);
            }
            String messageForRecord = victoryMessage.trim();
            if (messageForRecord.length() > 150) {
                messageForRecord = messageForRecord.replaceAll("\\<br.*?>", " ");
                messageForRecord = messageForRecord.replaceAll("\\<.*?>", "");
                if (messageForRecord.length() > 155) {
                    messageForRecord = messageForRecord.substring(0, 150) + "....";
                }
            }
            try {
                bridge.getHistoryWriter().startEvent("Players: " + MyFormatter.defaultNamedToTextList(t.getPlayers()) + " have just won the game, with this victory: " + messageForRecord);
                final IDelegate delegateEndRound = data.getDelegateList().getDelegate("endRound");
                ((EndRoundDelegate) delegateEndRound).signalGameOver(victoryMessage.trim(), t.getPlayers(), bridge);
            } catch (final Exception e) {
                ClientLogger.logQuietly("Failed to signal game over", e);
            }
        }
    }
}
Also used : EndRoundDelegate(games.strategy.triplea.delegate.EndRoundDelegate) GameData(games.strategy.engine.data.GameData) IDelegate(games.strategy.engine.delegate.IDelegate) GameParseException(games.strategy.engine.data.GameParseException)

Example 4 with IDelegate

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

the class ServerGame method startPersistentDelegates.

private void startPersistentDelegates() {
    for (final IDelegate delegate : gameData.getDelegateList()) {
        if (!(delegate instanceof IPersistentDelegate)) {
            continue;
        }
        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);
        delegateExecutionManager.enterDelegateExecution();
        try {
            delegate.setDelegateBridgeAndPlayer(bridge);
            delegate.start();
        } finally {
            delegateExecutionManager.leaveDelegateExecution();
        }
    }
}
Also used : IPersistentDelegate(games.strategy.engine.delegate.IPersistentDelegate) DelegateHistoryWriter(games.strategy.engine.history.DelegateHistoryWriter) DefaultDelegateBridge(games.strategy.engine.delegate.DefaultDelegateBridge) IDelegate(games.strategy.engine.delegate.IDelegate)

Example 5 with IDelegate

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

the class ServerGame method runStep.

private void runStep(final boolean stepIsRestoredFromSavedGame) {
    if (getCurrentStep().hasReachedMaxRunCount()) {
        gameData.getSequence().next();
        return;
    }
    if (isGameOver) {
        return;
    }
    final GameStep currentStep = gameData.getSequence().getStep();
    final IDelegate currentDelegate = currentStep.getDelegate();
    if (!stepIsRestoredFromSavedGame && currentDelegate.getClass().isAnnotationPresent(AutoSave.class) && currentDelegate.getClass().getAnnotation(AutoSave.class).beforeStepStart()) {
        autoSaveBefore(currentDelegate);
    }
    startStep(stepIsRestoredFromSavedGame);
    if (!stepIsRestoredFromSavedGame && currentDelegate.getClass().isAnnotationPresent(AutoSave.class) && currentDelegate.getClass().getAnnotation(AutoSave.class).afterStepStart()) {
        autoSaveBefore(currentDelegate);
    }
    if (isGameOver) {
        return;
    }
    waitForPlayerToFinishStep();
    if (isGameOver) {
        return;
    }
    // save after the step has advanced
    // otherwise, the delegate will execute again.
    final boolean autoSaveThisDelegate = currentDelegate.getClass().isAnnotationPresent(AutoSave.class) && currentDelegate.getClass().getAnnotation(AutoSave.class).afterStepEnd();
    if (autoSaveThisDelegate && currentStep.getName().endsWith("Move")) {
        autoSave(getAutoSaveAfterFileNameForGameStep(currentStep));
    }
    endStep();
    if (isGameOver) {
        return;
    }
    if (gameData.getSequence().next()) {
        gameData.getHistory().getHistoryWriter().startNextRound(gameData.getSequence().getRound());
        autoSave(gameData.getSequence().getRound() % 2 == 0 ? SaveGameFileChooser.getAutoSaveEvenFileName() : SaveGameFileChooser.getAutoSaveOddFileName());
    }
    if (autoSaveThisDelegate && !currentStep.getName().endsWith("Move")) {
        autoSave(getAutoSaveAfterFileNameForDelegate(currentDelegate));
    }
}
Also used : AutoSave(games.strategy.engine.delegate.AutoSave) GameStep(games.strategy.engine.data.GameStep) IDelegate(games.strategy.engine.delegate.IDelegate)

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