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);
}
}
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);
}
}
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);
}
}
}
}
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();
}
}
}
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));
}
}
Aggregations