use of games.strategy.engine.gamePlayer.IGamePlayer 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.");
}
}
use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.
the class AbstractGame method setupLocalPlayers.
private void setupLocalPlayers(final Set<IGamePlayer> localPlayers) {
final PlayerList playerList = gameData.getPlayerList();
for (final IGamePlayer gp : localPlayers) {
final PlayerID player = playerList.getPlayerId(gp.getName());
gamePlayers.put(player, gp);
final IPlayerBridge bridge = new DefaultPlayerBridge(this);
gp.initialize(bridge, player);
final RemoteName descriptor = ServerGame.getRemoteName(gp.getPlayerId(), gameData);
remoteMessenger.registerRemote(gp, descriptor);
}
}
use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.
the class ClientGame method shutDown.
public void shutDown() {
if (isGameOver) {
return;
}
isGameOver = true;
try {
channelMessenger.unregisterChannelSubscriber(gameModifiedChannel, IGame.GAME_MODIFICATION_CHANNEL);
remoteMessenger.unregisterRemote(getRemoteStepAdvancerName(channelMessenger.getLocalNode()));
vault.shutDown();
for (final IGamePlayer gp : gamePlayers.values()) {
PlayerID player;
gameData.acquireReadLock();
try {
player = gameData.getPlayerList().getPlayerId(gp.getName());
} finally {
gameData.releaseReadLock();
}
gamePlayers.put(player, gp);
remoteMessenger.unregisterRemote(ServerGame.getRemoteName(gp.getPlayerId(), gameData));
remoteMessenger.unregisterRemote(ServerGame.getRemoteRandomName(player));
}
} catch (final RuntimeException e) {
ClientLogger.logQuietly("Failed to shut down client game", e);
}
gameData.getGameLoader().shutDown();
}
use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.
the class ServerLauncher method launchInNewThread.
@Override
protected void launchInNewThread(final Component parent) {
try {
// the order of this stuff does matter
serverModel.setServerLauncher(this);
serverReady = new ServerReady(clientCount);
if (inGameLobbyWatcher != null) {
inGameLobbyWatcher.setGameStatus(GameDescription.GameStatus.LAUNCHING, null);
}
serverModel.allowRemoveConnections();
ui = parent;
if (headless) {
HeadlessGameServer.log("Game Status: Launching");
}
remoteMessenger.registerRemote(serverReady, ClientModel.CLIENT_READY_CHANNEL);
gameData.doPreGameStartDataModifications(playerListing);
abortLaunch = testShouldWeAbort();
final byte[] gameDataAsBytes = gameData.toBytes();
final Set<IGamePlayer> localPlayerSet = gameData.getGameLoader().createPlayers(playerListing.getLocalPlayerTypes());
final Messengers messengers = new Messengers(messenger, remoteMessenger, channelMessenger);
serverGame = new ServerGame(gameData, localPlayerSet, remotePlayers, messengers);
serverGame.setInGameLobbyWatcher(inGameLobbyWatcher);
if (headless) {
HeadlessGameServer.setServerGame(serverGame);
}
// tell the clients to start,
// later we will wait for them to all
// signal that they are ready.
((IClientChannel) channelMessenger.getChannelBroadcastor(IClientChannel.CHANNEL_NAME)).doneSelectingPlayers(gameDataAsBytes, serverGame.getPlayerManager().getPlayerMapping());
final boolean useSecureRandomSource = !remotePlayers.isEmpty();
if (useSecureRandomSource) {
// server game.
// try to find an opponent to be the other side of the crypto random source.
final PlayerID remotePlayer = serverGame.getPlayerManager().getRemoteOpponent(messenger.getLocalNode(), gameData);
final CryptoRandomSource randomSource = new CryptoRandomSource(remotePlayer, serverGame);
serverGame.setRandomSource(randomSource);
}
try {
gameData.getGameLoader().startGame(serverGame, localPlayerSet, headless);
} catch (final Exception e) {
ClientLogger.logError("Failed to launch", e);
abortLaunch = true;
if (gameLoadingWindow != null) {
gameLoadingWindow.doneWait();
}
}
if (headless) {
HeadlessGameServer.log("Game Successfully Loaded. " + (abortLaunch ? "Aborting Launch." : "Starting Game."));
}
if (abortLaunch) {
serverReady.countDownAll();
}
if (!serverReady.await(ClientSetting.SERVER_START_GAME_SYNC_WAIT_TIME.intValue(), TimeUnit.SECONDS)) {
System.out.println("Waiting for clients to be ready timed out!");
abortLaunch = true;
}
remoteMessenger.unregisterRemote(ClientModel.CLIENT_READY_CHANNEL);
new Thread(() -> {
try {
isLaunching = false;
abortLaunch = testShouldWeAbort();
if (!abortLaunch) {
if (useSecureRandomSource) {
warmUpCryptoRandomSource();
}
if (gameLoadingWindow != null) {
gameLoadingWindow.doneWait();
}
if (headless) {
HeadlessGameServer.log("Starting Game Delegates.");
}
serverGame.startGame();
} else {
stopGame();
if (!headless) {
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(ui, "Problem during startup, game aborted."));
} else {
System.out.println("Problem during startup, game aborted.");
}
}
} catch (final MessengerException me) {
// if just connection lost, no need to scare the user with some giant stack trace
if (me instanceof ConnectionLostException) {
System.out.println("Game Player disconnection: " + me.getMessage());
} else {
me.printStackTrace(System.out);
}
// wait for the connection handler to notice, and shut us down
try {
// we are already aborting the launch
if (!abortLaunch) {
if (!errorLatch.await(ClientSetting.SERVER_OBSERVER_JOIN_WAIT_TIME.intValue(), TimeUnit.SECONDS)) {
System.err.println("Waiting on error latch timed out!");
}
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
stopGame();
} catch (final Exception e) {
e.printStackTrace(System.err);
if (headless) {
System.out.println(DebugUtils.getThreadDumps());
HeadlessGameServer.sendChat("If this is a repeatable issue or error, please make a copy of this savegame " + "and contact a Mod and/or file a bug report.");
}
stopGame();
}
// 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(200);
// either game ended, or aborted, or a player left or disconnected
if (headless) {
try {
System.out.println("Game ended, going back to waiting.");
// if we do not do this, we can get into an infinite loop of launching a game,
// then crashing out, then launching, etc.
serverModel.setAllPlayersToNullNodes();
final File f1 = new File(ClientSetting.SAVE_GAMES_FOLDER_PATH.value(), SaveGameFileChooser.getAutoSaveFileName());
if (f1.exists()) {
gameSelectorModel.load(f1, null);
} else {
gameSelectorModel.resetGameDataToNull();
}
} catch (final Exception e1) {
ClientLogger.logQuietly("Failed to load game", e1);
gameSelectorModel.resetGameDataToNull();
}
} else {
gameSelectorModel.loadDefaultGameNewThread();
}
if (parent != null) {
SwingUtilities.invokeLater(() -> JOptionPane.getFrameForComponent(parent).setVisible(true));
}
serverModel.setServerLauncher(null);
serverModel.newGame();
if (inGameLobbyWatcher != null) {
inGameLobbyWatcher.setGameStatus(GameDescription.GameStatus.WAITING_FOR_PLAYERS, null);
}
if (headless) {
// tell headless server to wait for new connections:
HeadlessGameServer.waitForUsersHeadlessInstance();
HeadlessGameServer.log("Game Status: Waiting For Players");
}
}, "Triplea, start server game").start();
} finally {
if (gameLoadingWindow != null) {
gameLoadingWindow.doneWait();
}
if (inGameLobbyWatcher != null) {
inGameLobbyWatcher.setGameStatus(GameDescription.GameStatus.IN_PROGRESS, serverGame);
}
if (headless) {
HeadlessGameServer.log("Game Status: In Progress");
}
}
}
use of games.strategy.engine.gamePlayer.IGamePlayer in project triplea by triplea-game.
the class TripleADisplay method reportMessageToAll.
@Override
public void reportMessageToAll(final String message, final String title, final boolean doNotIncludeHost, final boolean doNotIncludeClients, final boolean doNotIncludeObservers) {
if (doNotIncludeHost && doNotIncludeClients && doNotIncludeObservers) {
return;
}
if (doNotIncludeHost || doNotIncludeClients || doNotIncludeObservers) {
boolean isHost = false;
boolean isClient = false;
boolean isObserver = true;
for (final IGamePlayer player : ui.getLocalPlayers().getLocalPlayers()) {
// if we have any local players, we are not an observer
isObserver = false;
if (player instanceof TripleAPlayer) {
if (IGameLoader.CLIENT_PLAYER_TYPE.equals(player.getType())) {
isClient = true;
} else {
isHost = true;
}
} else {
// AIs are run by the host machine
isHost = true;
}
}
if ((doNotIncludeHost && isHost) || (doNotIncludeClients && isClient) || (doNotIncludeObservers && isObserver)) {
return;
}
}
ui.notifyMessage(message, title);
}
Aggregations