Search in sources :

Example 1 with IGamePlayer

use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.

the class ServerGame method waitForPlayerToFinishStep.

private void waitForPlayerToFinishStep() {
    final PlayerID playerId = getCurrentStep().getPlayerId();
    // no player specified for the given step
    if (playerId == null) {
        return;
    }
    if (!getCurrentStep().getDelegate().delegateCurrentlyRequiresUserInput()) {
        return;
    }
    final IGamePlayer player = gamePlayers.get(playerId);
    if (player != null) {
        // a local player
        player.start(getCurrentStep().getName());
    } else {
        // a remote player
        final INode destination = playerManager.getNode(playerId.getName());
        final IGameStepAdvancer advancer = (IGameStepAdvancer) remoteMessenger.getRemote(ClientGame.getRemoteStepAdvancerName(destination));
        advancer.startPlayerStep(getCurrentStep().getName(), playerId);
    }
}
Also used : IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) PlayerID(games.strategy.engine.data.PlayerID) INode(games.strategy.net.INode)

Example 2 with IGamePlayer

use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.

the class LocalLauncher method launchInNewThread.

@Override
protected void launchInNewThread(final Component parent) {
    Exception exceptionLoadingGame = null;
    ServerGame game = null;
    try {
        gameData.doPreGameStartDataModifications(playerListing);
        final Messengers messengers = new Messengers(new HeadlessServerMessenger());
        final Set<IGamePlayer> gamePlayers = gameData.getGameLoader().createPlayers(playerListing.getLocalPlayerTypes());
        game = new ServerGame(gameData, gamePlayers, new HashMap<>(), messengers);
        game.setRandomSource(randomSource);
        // for debugging, we can use a scripted random source
        if (ScriptedRandomSource.useScriptedRandom()) {
            game.setRandomSource(new ScriptedRandomSource());
        }
        gameData.getGameLoader().startGame(game, gamePlayers, headless);
    } catch (final MapNotFoundException e) {
        exceptionLoadingGame = e;
    } catch (final Exception ex) {
        ClientLogger.logQuietly("Failed to start game", ex);
        exceptionLoadingGame = ex;
    } finally {
        gameLoadingWindow.doneWait();
    }
    try {
        if (exceptionLoadingGame == null) {
            game.startGame();
        }
    } finally {
        // todo(kg), this does not occur on the swing thread, and this notifies setupPanel observers
        // having an oddball issue with the zip stream being closed while parsing to load default game. might be caused
        // by closing of stream while unloading map resources.
        Interruptibles.sleep(100);
        gameSelectorModel.loadDefaultGameNewThread();
        SwingUtilities.invokeLater(() -> JOptionPane.getFrameForComponent(parent).setVisible(true));
    }
}
Also used : IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) Messengers(games.strategy.net.Messengers) ServerGame(games.strategy.engine.framework.ServerGame) HashMap(java.util.HashMap) HeadlessServerMessenger(games.strategy.net.HeadlessServerMessenger) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource)

Example 3 with IGamePlayer

use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.

the class TripleA method createPlayers.

@Override
public Set<IGamePlayer> createPlayers(final Map<String, String> playerNames) {
    final Set<IGamePlayer> players = new HashSet<>();
    for (final String name : playerNames.keySet()) {
        final String type = playerNames.get(name);
        if (type.equals(WEAK_COMPUTER_PLAYER_TYPE)) {
            players.add(new WeakAi(name, type));
        } else if (type.equals(FAST_COMPUTER_PLAYER_TYPE)) {
            players.add(new FastAi(name, type));
        } else if (type.equals(PRO_COMPUTER_PLAYER_TYPE)) {
            players.add(new ProAi(name, type));
        } else if (type.equals(DOESNOTHINGAI_COMPUTER_PLAYER_TYPE)) {
            players.add(new DoesNothingAi(name, type));
        } else if (type.equals(HUMAN_PLAYER_TYPE) || type.equals(CLIENT_PLAYER_TYPE)) {
            final TripleAPlayer player = new TripleAPlayer(name, type);
            players.add(player);
        } else {
            throw new IllegalStateException("Player type not recognized:" + type);
        }
    }
    return players;
}
Also used : IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) WeakAi(games.strategy.triplea.ai.weak.WeakAi) ITripleAPlayer(games.strategy.triplea.player.ITripleAPlayer) DoesNothingAi(games.strategy.triplea.ai.weak.DoesNothingAi) FastAi(games.strategy.triplea.ai.fast.FastAi) ProAi(games.strategy.triplea.ai.pro.ProAi) HashSet(java.util.HashSet)

Example 4 with IGamePlayer

use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.

the class ClientModel method startGameInNewThread.

private void startGameInNewThread(final byte[] gameData, final Map<String, INode> players, final boolean gameRunning) {
    final GameData data;
    try {
        // this normally takes a couple seconds, but can take
        // up to 60 seconds for a freaking huge game
        data = IoUtils.readFromMemory(gameData, GameDataManager::loadGame);
    } catch (final IOException ex) {
        ClientLogger.logQuietly("Failed to load game", ex);
        return;
    }
    objectStreamFactory.setData(data);
    final Map<String, String> playerMapping = playersToNodes.entrySet().stream().filter(e -> e.getValue().equals(messenger.getLocalNode().getName())).collect(Collectors.toMap(Map.Entry::getKey, e -> IGameLoader.CLIENT_PLAYER_TYPE));
    final Set<IGamePlayer> playerSet = data.getGameLoader().createPlayers(playerMapping);
    final Messengers messengers = new Messengers(messenger, remoteMessenger, channelMessenger);
    game = new ClientGame(data, playerSet, players, messengers);
    new Thread(() -> {
        SwingUtilities.invokeLater(() -> JOptionPane.getFrameForComponent(ui).setVisible(false));
        try {
            // game will be null if we loose the connection
            if (game != null) {
                try {
                    data.getGameLoader().startGame(game, playerSet, false);
                    data.testLocksOnRead();
                } catch (final Exception e) {
                    ClientLogger.logError("Failed to start Game", e);
                    game.shutDown();
                    messenger.shutDown();
                    gameLoadingWindow.doneWait();
                    // an ugly hack, we need a better
                    // way to get the main frame
                    GameRunner.clientLeftGame();
                }
            }
            if (!gameRunning) {
                ((IServerReady) remoteMessenger.getRemote(CLIENT_READY_CHANNEL)).clientReady();
            }
        } finally {
            gameLoadingWindow.doneWait();
        }
    }, "Client Game Launcher").start();
}
Also used : ClientGame(games.strategy.engine.framework.ClientGame) ClientMessenger(games.strategy.net.ClientMessenger) IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) TRIPLEA_STARTED(games.strategy.engine.framework.CliProperties.TRIPLEA_STARTED) TRIPLEA_HOST(games.strategy.engine.framework.CliProperties.TRIPLEA_HOST) Map(java.util.Map) RemoteMessenger(games.strategy.engine.message.RemoteMessenger) SetMapClientAction(games.strategy.engine.framework.network.ui.SetMapClientAction) INode(games.strategy.net.INode) PlayerListing(games.strategy.engine.framework.message.PlayerListing) Collection(java.util.Collection) Set(java.util.Set) Logger(java.util.logging.Logger) GameRunner(games.strategy.engine.framework.GameRunner) Component(java.awt.Component) Collectors(java.util.stream.Collectors) TRIPLEA_PORT(games.strategy.engine.framework.CliProperties.TRIPLEA_PORT) IChatPanel(games.strategy.engine.chat.IChatPanel) UnifiedMessenger(games.strategy.engine.message.unifiedmessenger.UnifiedMessenger) IChannelMessenger(games.strategy.engine.message.IChannelMessenger) CountDownLatch(java.util.concurrent.CountDownLatch) GameData(games.strategy.engine.data.GameData) List(java.util.List) ChangeToAutosaveClientAction(games.strategy.engine.framework.network.ui.ChangeToAutosaveClientAction) EventThreadJOptionPane(games.strategy.util.EventThreadJOptionPane) TRIPLEA_NAME(games.strategy.engine.framework.CliProperties.TRIPLEA_NAME) WaitWindow(games.strategy.engine.framework.ui.background.WaitWindow) SwingAction(games.strategy.ui.SwingAction) Setter(lombok.Setter) Getter(lombok.Getter) ChangeGameOptionsClientAction(games.strategy.engine.framework.network.ui.ChangeGameOptionsClientAction) Messengers(games.strategy.net.Messengers) HashMap(java.util.HashMap) Action(javax.swing.Action) MacFinder(games.strategy.net.MacFinder) RemoteName(games.strategy.engine.message.RemoteName) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) IMessengerErrorListener(games.strategy.net.IMessengerErrorListener) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SwingUtilities(javax.swing.SwingUtilities) ClientLogin(games.strategy.engine.framework.startup.login.ClientLogin) ClientOptions(games.strategy.engine.framework.startup.ui.ClientOptions) IMessenger(games.strategy.net.IMessenger) GameDataManager(games.strategy.engine.framework.GameDataManager) IoUtils(games.strategy.io.IoUtils) ClientSetting(games.strategy.triplea.settings.ClientSetting) Nonnull(javax.annotation.Nonnull) GetGameSaveClientAction(games.strategy.engine.framework.network.ui.GetGameSaveClientAction) ChangeGameToSaveGameClientAction(games.strategy.engine.framework.network.ui.ChangeGameToSaveGameClientAction) TRIPLEA_CLIENT(games.strategy.engine.framework.CliProperties.TRIPLEA_CLIENT) Interruptibles(games.strategy.util.Interruptibles) SaveGameFileChooser(games.strategy.engine.framework.ui.SaveGameFileChooser) IOException(java.io.IOException) Chat(games.strategy.engine.chat.Chat) JOptionPane(javax.swing.JOptionPane) IServerReady(games.strategy.engine.framework.startup.launcher.IServerReady) GameObjectStreamFactory(games.strategy.engine.framework.GameObjectStreamFactory) TimeUnit(java.util.concurrent.TimeUnit) ClientLogger(games.strategy.debug.ClientLogger) IRemoteMessenger(games.strategy.engine.message.IRemoteMessenger) ChannelMessenger(games.strategy.engine.message.ChannelMessenger) IGameLoader(games.strategy.engine.framework.IGameLoader) CouldNotLogInException(games.strategy.net.CouldNotLogInException) Preconditions(com.google.common.base.Preconditions) ChatPanel(games.strategy.engine.chat.ChatPanel) IClientMessenger(games.strategy.net.IClientMessenger) IGamePlayer(games.strategy.engine.gamePlayer.IGamePlayer) Messengers(games.strategy.net.Messengers) GameData(games.strategy.engine.data.GameData) IOException(java.io.IOException) ClientGame(games.strategy.engine.framework.ClientGame) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IOException(java.io.IOException) CouldNotLogInException(games.strategy.net.CouldNotLogInException)

Example 5 with IGamePlayer

use of games.strategy.engine.gamePlayer.IGamePlayer 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)

Aggregations

IGamePlayer (games.strategy.engine.gamePlayer.IGamePlayer)10 PlayerID (games.strategy.engine.data.PlayerID)5 Messengers (games.strategy.net.Messengers)3 GameData (games.strategy.engine.data.GameData)2 ServerGame (games.strategy.engine.framework.ServerGame)2 RemoteName (games.strategy.engine.message.RemoteName)2 INode (games.strategy.net.INode)2 TripleAPlayer (games.strategy.triplea.TripleAPlayer)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Preconditions (com.google.common.base.Preconditions)1 ClientLogger (games.strategy.debug.ClientLogger)1 Chat (games.strategy.engine.chat.Chat)1 ChatPanel (games.strategy.engine.chat.ChatPanel)1 IChatPanel (games.strategy.engine.chat.IChatPanel)1 CompositeChange (games.strategy.engine.data.CompositeChange)1 GameStep (games.strategy.engine.data.GameStep)1 PlayerList (games.strategy.engine.data.PlayerList)1 IDelegate (games.strategy.engine.delegate.IDelegate)1 TRIPLEA_CLIENT (games.strategy.engine.framework.CliProperties.TRIPLEA_CLIENT)1