package jrummikub.control.network; import java.awt.Color; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.UUID; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.Event; import jrummikub.util.GameData; import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.util.IListener2; import jrummikub.util.IListener3; import jrummikub.view.ISettingsPanel; import jrummikub.view.IView; import jrummikub.view.ISettingsPanel.SettingsMode; public class GameJoinControl { private List connections = new ArrayList(); private GameData gameData; private ConnectionControl connectionControl; private IView view; private Event backEvent = new Event(); public GameJoinControl(final ConnectionControl connectionControl, final GameData gameData, final IView view) { this.connectionControl = connectionControl; this.gameData = gameData; this.view = view; view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_JOIN); view.getSettingsPanel().enableAddPlayerButton(false); view.getSettingsPanel().setGameSettings(gameData.getGameSettings()); 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(settings); } } })); connections.add(view.getSettingsPanel().getBackEvent().add( new IListener() { @Override public void handle() { goBack(); } })); connections.add(view.getSettingsPanel().getChangePlayerColorEvent() .add(new IListener2() { @Override public void handle(Integer playerNumber, Color color) { // TODO Auto-generated method stub } })); } private void updateSettingsPanel(GameSettings settings) { view.getSettingsPanel().setGameSettings(settings); Set colors = new HashSet(Arrays .asList(ISettingsPanel.PLAYER_COLORS)); for (PlayerSettings player : settings.getPlayerList()) { colors.remove(player.getColor()); } view.getSettingsPanel().setPlayerColors(colors); } 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); } } } public Event getBackEvent(){ return backEvent; } public void startGameJoin() { view.showSettingsPanel(true); } /** * Aborts joining and goes back to game list */ private void goBack() { abort(); connectionControl.leaveGame(); view.showSettingsPanel(false); backEvent.emit(); } /** * Aborts joining */ public void abort() { for (Connection c : connections) { c.remove(); } } }