package jrummikub.control.network; import java.util.UUID; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.util.GameData; import jrummikub.util.IListener1; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; /** * Control for joining a network game */ public class GameJoinControl extends AbstractGameBeginControl { /** * Creates new game join control * * @param connectionControl * the current connection control for events and messages * @param gameData * the game data for settings, game id * @param view * the view */ public GameJoinControl(final IConnectionControl connectionControl, final GameData gameData, final IView view) { super(connectionControl, view, gameData, SettingsMode.NETWORK_JOIN); connections.add(connectionControl.getGameOfferEvent().add( new IListener1() { @Override public void handle(GameData data) { if (data.getGameID().equals(gameData.getGameID())) { GameSettings settings = data.getGameSettings(); fixGameSettings(settings); gameData.setGameSettings(settings); updateSettingsPanel(); } } })); connections.add(connectionControl.getGameWithdrawalEvent().add( new IListener1() { @Override public void handle(UUID uuid) { if (uuid.equals(gameData.getGameID())) { abort(); backEvent.emit(); } } })); } private void fixGameSettings(GameSettings settings) { for (PlayerSettings player : settings.getPlayerList()) { if (player.getType() == Type.HUMAN) { player.setType(Type.NETWORK); } else if (player.getType() == Type.NETWORK && player.getName().equals(connectionControl.getNickname())) { player.setType(Type.HUMAN); } } } /** * Starts the join control and sets the settings panel in game join mode */ public void startGameJoin() { view.showSettingsPanel(true); } /** * Aborts joining and goes back to game list */ @Override protected void goBack() { abort(); connectionControl.leaveGame(); backEvent.emit(); } }