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