diff options
Diffstat (limited to 'src/jrummikub/control/network')
-rw-r--r-- | src/jrummikub/control/network/ConnectionControl.java | 4 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkControl.java | 2 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkSettingsControl.java | 114 |
3 files changed, 98 insertions, 22 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 56dac8c..fd8087a 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -49,6 +49,10 @@ class ConnectionControl { this.loginData = loginData; } + String getNickname() { + return muc.getNickname(); + } + void connect() { new Thread(new ConnectRunner()).start(); } diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java index 8a1c8e4..929ce03 100644 --- a/src/jrummikub/control/network/NetworkControl.java +++ b/src/jrummikub/control/network/NetworkControl.java @@ -61,7 +61,7 @@ public class NetworkControl { view.showGameListPanel(false); settingsControl = new NetworkSettingsControl( - connectionControl, view, new GameSettings()); + connectionControl.getNickname(), view, new GameSettings()); settingsControl.startSettings(); } } diff --git a/src/jrummikub/control/network/NetworkSettingsControl.java b/src/jrummikub/control/network/NetworkSettingsControl.java index b9af560..61fcfa3 100644 --- a/src/jrummikub/control/network/NetworkSettingsControl.java +++ b/src/jrummikub/control/network/NetworkSettingsControl.java @@ -1,54 +1,126 @@ package jrummikub.control.network; +import java.util.ArrayList; import java.util.Arrays; -import java.util.UUID; +import java.util.Collections; +import java.util.List; import jrummikub.control.SettingsControl; import jrummikub.model.GameSettings; +import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; -import jrummikub.view.IGameListPanel.GameData; +import jrummikub.view.ISettingsPanel; import jrummikub.view.IView; /** * Settings for network games, entered by host */ public class NetworkSettingsControl extends SettingsControl { - private GameData gameData = new GameData(UUID.randomUUID(), settings); - private ConnectionControl connectionControl; + private String nickname; /** * Creates a new network settings control * * @param connectionControl - * for gameOffers + * for gameOffers * @param view - * for events and handlers + * for events and handlers * @param settings - * for playerNumber, color... + * for playerNumber, color... */ - public NetworkSettingsControl(ConnectionControl connectionControl, - IView view, GameSettings settings) { - super(view, settings); - this.connectionControl = connectionControl; - /*view.getSettingsPanel().setPlayerTypeChoices( - Arrays.asList(Type.NETWORK, Type.COMPUTER));*/ + public NetworkSettingsControl(String nickname, IView view, + GameSettings settings) { + this.nickname = nickname; + this.view = view; + this.settings = settings; view.getSettingsPanel().enableNetworkMode(true); + view.getSettingsPanel().setPlayerNamesEditable( + Collections.<Boolean> emptyList()); + + addFirstPlayer(); + addPlayer(); + + addListeners(); } @Override - public void startSettings() { - super.startSettings(); + protected void update() { + view.getSettingsPanel().enableAddPlayerButton( + settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); + + checkSettings(); + + List<List<Type>> choices = new ArrayList<List<Type>>(); + List<Boolean> enableRemoveButtons = new ArrayList<Boolean>(); + List<Boolean> playerNamesEditable = new ArrayList<Boolean>(); + + for (int i = 0; i < settings.getPlayerList().size(); i++) { + if (i == 0) { + choices.add(Collections.singletonList(Type.HUMAN)); + enableRemoveButtons.add(false); + } else { + choices.add(Arrays.asList(Type.NETWORK, Type.COMPUTER)); + enableRemoveButtons.add(settings.getPlayerList().size() > 2); + } + + playerNamesEditable + .add(settings.getPlayerList().get(i).getType() == Type.COMPUTER); + } + view.getSettingsPanel().enableRemovePlayerButtons(enableRemoveButtons); + view.getSettingsPanel().setPlayerNamesEditable(playerNamesEditable); + view.getSettingsPanel().setPlayerTypeChoices(choices); + + view.getSettingsPanel().setGameSettings(settings); } @Override - protected void update() { - super.update(); - connectionControl.offerGame(gameData); + protected void setPlayerType(int i, Type type) { + Type oldType = settings.getPlayerList().get(i).getType(); + + settings.getPlayerList().get(i).setType(type); + + if (type == Type.NETWORK) { + settings.getPlayerList().get(i).setName("Offen"); + } else if (oldType != Type.COMPUTER && type == Type.COMPUTER) { + // Find unused player name + int num = 0; + + nameLoop: while (true) { + num++; + String playerName = "Computer " + num; + + for (PlayerSettings player : settings.getPlayerList()) { + if (player.getType() == Type.COMPUTER + && playerName.equals(player.getName())) { + continue nameLoop; + } + } + break; + } + + settings.getPlayerList().get(i).setName("Computer " + num); + } + + update(); + } + + private void addFirstPlayer() { + PlayerSettings playerSettings = new PlayerSettings(nickname, + findUnusedColor()); + settings.getPlayerList().add(playerSettings); } @Override - public void abort() { - super.abort(); - connectionControl.withdrawGame(gameData.getGameID()); + protected void addPlayer() { + if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) { + return; + } + + PlayerSettings playerSettings = new PlayerSettings("Offen", + findUnusedColor()); + playerSettings.setType(Type.NETWORK); + settings.getPlayerList().add(playerSettings); + + update(); } } |