package jrummikub.control.network; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import jrummikub.control.SaveControl; import jrummikub.model.GameSettings; import jrummikub.util.Connection; import jrummikub.util.Event; import jrummikub.util.GameData; import jrummikub.util.IEvent; import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.util.LoginData; import jrummikub.view.IQuitWarningPanel.QuitMode; import jrummikub.view.IView; import jrummikub.view.LoginError; /** * Class dealing with network connection, offering and choice of network games */ public class NetworkControl { private IConnectionControl connectionControl; private IView view; private List connections = new ArrayList(); Connection tempConnection; private Event stopNetworkEvent = new Event(); private Event backToLoginEvent = new Event(); private NetworkSettingsControl settingsControl; private GameOfferControl gameOfferControl; private GameJoinControl gameJoinControl; private SaveControl saveControl; private NetworkGameControl gameControl; private List games = new ArrayList(); private Map gameMap = new HashMap(); /** * Creates a new network control * * @param loginData * user's login data * @param connectionControl * current connection for events and messages * @param saveControl * save control if saving will ever be allowed * @param view * for events and handlers */ public NetworkControl(final LoginData loginData, IConnectionControl connectionControl, SaveControl saveControl, final IView view) { this.view = view; this.connectionControl = connectionControl; this.saveControl = saveControl; addConnectionSetupListeners(loginData, view); addConnectionControlListeners(view); connections.add(view.getQuitEvent().add(new IListener() { @Override public void handle() { if (gameControl == null) { System.exit(0); } else { view.getQuitWarningPanel().setMode(QuitMode.QUIT_PROCESS); view.showQuitWarningPanel(true); tempConnection = view.getQuitWarningPanel().getQuitEvent() .add(new IListener() { @Override public void handle() { System.exit(0); } }); } } })); addViewEventListeners(); } private void addViewEventListeners() { connections.add(view.getQuitWarningPanel().getCancelEvent() .add(new IListener() { @Override public void handle() { view.showQuitWarningPanel(false); if (tempConnection != null) { tempConnection.remove(); tempConnection = null; } } })); connections.add(view.getGameListPanel().getJoinEvent() .add(new IListener1() { @Override public void handle(GameData gameData) { join(gameData); } })); connections.add(view.getGameListPanel().getOpenNewGameEvent() .add(new IListener() { @Override public void handle() { createSettingsControl(); } })); connections.add(view.getGameListPanel().getCancelEvent() .add(new IListener() { @Override public void handle() { abort(); stopNetworkEvent.emit(); } })); } private void join(GameData gameData) { view.showGameListPanel(false); connectionControl.joinGame(gameData); } /** * Adds the listeners for connection control events * * @param view * view for events */ public void addConnectionControlListeners(final IView view) { connections.add(connectionControl.getGameOfferEvent().add( new IListener1() { @Override public void handle(GameData gameData) { UUID uuid = gameData.getGameID(); GameData game = gameMap.get(uuid); if (game == null) { game = gameData; gameMap.put(uuid, gameData); games.add(uuid); } else { game.setGameSettings(gameData.getGameSettings()); } updateGameList(); } })); connections.add(connectionControl.getGameWithdrawalEvent().add( new IListener1() { @Override public void handle(UUID uuid) { games.remove(uuid); gameMap.remove(uuid); updateGameList(); } })); connections.add(connectionControl.getGameJoinAckEvent().add( new IListener1() { @Override public void handle(Boolean ack) { if (ack) { createGameJoinControl(); } else { view.showGameListPanel(true); } } })); } private void addConnectionSetupListeners(final LoginData loginData, final IView view) { connections.add(connectionControl.getConnectedEvent().add( new IListener() { @Override public void handle() { view.getGameListPanel().setChannelName( loginData.getChannelName()); view.showConnectPanel(false); view.showGameListPanel(true); } })); connections.add(connectionControl.getConnectionFailedEvent().add( new IListener1() { @Override public void handle(LoginError error) { view.getConnectPanel().setMode(error); view.showConnectPanel(true); } })); connections.add(view.getConnectPanel().getCancelEvent() .add(new IListener() { @Override public void handle() { abort(); backToLoginEvent.emit(); } })); } private void updateGameList() { List gameList = new ArrayList(); for (UUID uuid : games) { gameList.add(gameMap.get(uuid)); } view.getGameListPanel().setGameList(gameList); } private void createGameJoinControl() { if (gameJoinControl != null) { return; } GameData gameData = connectionControl.getCurrentGame(); gameJoinControl = new GameJoinControl(connectionControl, gameData, view); gameJoinControl.getBackEvent().add(new IListener() { @Override public void handle() { gameJoinControl = null; view.showGameListPanel(true); } }); gameJoinControl.getStartGameEvent().add(new IListener() { @Override public void handle() { gameControl = new NetworkGameControl(null, saveControl, view, connectionControl, false); gameControl.startGame(); } }); gameJoinControl.startGameJoin(); } /** * Starts a new network connection with the specified data */ public void startNetwork() { view.showConnectPanel(true); view.getConnectPanel().setMode(null); connectionControl.connect(); } /** * Ends the network connection if canceled */ public void abort() { for (Connection c : connections) { c.remove(); } view.showGameListPanel(false); view.showConnectPanel(false); if (settingsControl != null) { settingsControl.abort(); settingsControl = null; } if (gameOfferControl != null) { gameOfferControl.abort(); gameOfferControl = null; } if (gameJoinControl != null) { gameJoinControl.abort(); gameJoinControl = null; } if (gameControl != null) { gameControl.abortGame(); gameControl = null; } connectionControl.disconnect(); } /** * Getter for stopNetworkEvent * * @return stopNetworkEvent */ public IEvent getStopNetworkEvent() { return stopNetworkEvent; } /** * The back to login event is emitted when the player aborted the connecting * process or when a connection error has occured and been acknowledged * * @return the event */ public IEvent getBackToLoginEvent() { return backToLoginEvent; } private void createSettingsControl() { if (settingsControl != null) { return; } view.showGameListPanel(false); settingsControl = new NetworkSettingsControl( connectionControl.getNickname(), view, new GameSettings()); settingsControl.getOfferGameEvent().add(new IListener1() { @Override public void handle(GameSettings settings) { settingsControl = null; createGameOfferControl(settings); } }); settingsControl.getBackEvent().add(new IListener() { @Override public void handle() { settingsControl = null; view.showGameListPanel(true); } }); settingsControl.startSettings(); } private void createGameOfferControl(GameSettings settings) { if (gameOfferControl != null) { return; } gameOfferControl = new GameOfferControl(connectionControl, settings, view); gameOfferControl.getBackEvent().add(new IListener() { @Override public void handle() { gameOfferControl = null; view.showGameListPanel(true); } }); gameOfferControl.getStartGameEvent().add( new IListener1() { @Override public void handle(GameSettings settings) { gameControl = new NetworkGameControl(settings, saveControl, view, connectionControl, true); gameControl.startGame(); } }); gameOfferControl.startGameOffer(); } }