From 570187c95000afd1ac9f6d6e8bb5cab30b984e87 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 13 Jun 2011 22:29:28 +0200 Subject: Implemented most of network settings control git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@425 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/RoundControl.java | 6 +- src/jrummikub/control/SettingsControl.java | 106 +++++++++++-------- .../control/network/ConnectionControl.java | 4 + src/jrummikub/control/network/NetworkControl.java | 2 +- .../control/network/NetworkSettingsControl.java | 114 +++++++++++++++++---- 5 files changed, 162 insertions(+), 70 deletions(-) (limited to 'src/jrummikub/control') diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index a420b6d..89baeb2 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -100,7 +100,7 @@ public class RoundControl { private void prepareTurn() { boolean isHuman = roundState.getActivePlayer().getPlayerSettings() - .getTurnControlType() == HUMAN; + .getType() == HUMAN; clonedTable = (ITable) roundState.getTable().clone(); clonedHand = (IHand) roundState.getActivePlayer().getHand().clone(); @@ -123,7 +123,7 @@ public class RoundControl { if (turnControl != null) return; boolean isHuman = roundState.getActivePlayer().getPlayerSettings() - .getTurnControlType() == HUMAN; + .getType() == HUMAN; TurnMode turnMode = TurnMode.NORMAL_TURN; @@ -139,7 +139,7 @@ public class RoundControl { } turnControl = TurnControlFactory.getFactory( roundState.getActivePlayer().getPlayerSettings() - .getTurnControlType()).create(); + .getType()).create(); turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand, roundState.getActivePlayer().getLaidOut(), turnMode), roundState.getGameSettings(), view); diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index abcbd5d..398ce5d 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -3,6 +3,7 @@ package jrummikub.control; import java.awt.Color; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -34,20 +35,44 @@ public class SettingsControl { * Create a new settings control * * @param view - * the view to use + * the view to use * @param settings - * initial game settings + * initial game settings */ public SettingsControl(IView view, GameSettings settings) { this.view = view; this.settings = settings; view.getSettingsPanel().enableNetworkMode(false); + view.getSettingsPanel().setPlayerNamesEditable( + Collections. emptyList()); + addPlayer(); + addPlayer(); + + addListeners(); + + connections.add(view.getSettingsPanel().getStartGameEvent() + .add(new IListener() { + @Override + public void handle() { + startGame(); + } + })); + } + + protected SettingsControl() { + } + + protected void addListeners() { + addPlayerSettingsListeners(); + + addOptionListeners1(); + addOptionListeners2(); } /** - * the start game event is emitted when the user wants to start a game and - * the settings made are valid + * the start game event is emitted when the user wants to start a game and the + * settings made are valid * * @return the event */ @@ -59,28 +84,12 @@ public class SettingsControl { * Start the operation of the settings control */ public void startSettings() { - addPlayer(); - addPlayer(); - - addPlayerSettingsListeners(); - - addOptionListeners1(); - addOptionListeners2(); - - connections.add(view.getSettingsPanel().getStartGameEvent() - .add(new IListener() { - @Override - public void handle() { - startGame(); - } - })); view.showSettingsPanel(true); } private void addOptionListeners1() { connections.add(view.getSettingsPanel() - .getChangeInitialMeldThresholdEvent() - .add(new IListener1() { + .getChangeInitialMeldThresholdEvent().add(new IListener1() { @Override public void handle(Integer value) { settings.setInitialMeldThreshold(value); @@ -105,8 +114,7 @@ public class SettingsControl { update(); } })); - connections.add(view.getSettingsPanel() - .getChangeNumberOfStonesDealtEvent() + connections.add(view.getSettingsPanel().getChangeNumberOfStonesDealtEvent() .add(new IListener1() { @Override public void handle(Integer value) { @@ -217,7 +225,7 @@ public class SettingsControl { })); } - private void addPlayer() { + protected void addPlayer() { if (settings.getPlayerList().size() >= ISettingsPanel.PLAYER_COLORS.length) { return; } @@ -237,7 +245,13 @@ public class SettingsControl { break; } - // Next, find unused color + settings.getPlayerList().add( + new PlayerSettings("Spieler " + num, findUnusedColor())); + + update(); + } + + protected Color findUnusedColor() { Color color = null; colorLoop: for (Color c : ISettingsPanel.PLAYER_COLORS) { color = c; @@ -248,11 +262,7 @@ public class SettingsControl { } break; } - - settings.getPlayerList().add( - new PlayerSettings("Spieler " + num, color)); - - update(); + return color; } private void removePlayer(int i) { @@ -283,17 +293,18 @@ public class SettingsControl { update(); } - private void setPlayerType(int i, Type type) { - settings.getPlayerList().get(i).setTurnControlType(type); + protected void setPlayerType(int i, Type type) { + settings.getPlayerList().get(i).setType(type); update(); } protected void update() { + boolean enableRemoveButtons = settings.getPlayerList().size() > 2; view.getSettingsPanel().enableRemovePlayerButtons( - settings.getPlayerList().size() > 2); - view.getSettingsPanel() - .enableAddPlayerButton( - settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); + Collections.nCopies(settings.getPlayerList().size(), + enableRemoveButtons)); + view.getSettingsPanel().enableAddPlayerButton( + settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); checkSettings(); @@ -305,7 +316,7 @@ public class SettingsControl { view.getSettingsPanel().setGameSettings(settings); } - private boolean checkSettings() { + protected boolean checkSettings() { if (!checkErrors()) { return false; } @@ -328,13 +339,19 @@ public class SettingsControl { } } for (int i = 0; i < settings.getPlayerList().size(); ++i) { - String name = settings.getPlayerList().get(i).getName(); + PlayerSettings player1 = settings.getPlayerList().get(i); + String name = player1.getName(); + Type type = player1.getType(); + + if (type == Type.NETWORK) { + continue; + } for (int j = i + 1; j < settings.getPlayerList().size(); ++j) { - if (settings.getPlayerList().get(j).getName().equals(name)) { - view.getSettingsPanel() - .setError( - ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); + PlayerSettings player2 = settings.getPlayerList().get(j); + if (player2.getName().equals(name) && player2.getType() == type) { + view.getSettingsPanel().setError( + ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); view.getSettingsPanel().enableStartGameButton(false); return false; } @@ -343,8 +360,7 @@ public class SettingsControl { int totalStonesDealt = settings.getNumberOfStonesDealt() * settings.getPlayerList().size(); - int totalStones = settings.getHighestValue() - * settings.getStoneSetNumber() + int totalStones = settings.getHighestValue() * settings.getStoneSetNumber() * settings.getStoneColors().size() + settings.getJokerNumber(); if (totalStones <= totalStonesDealt) { @@ -373,7 +389,7 @@ public class SettingsControl { boolean humanPlayerFound = false; for (PlayerSettings player : settings.getPlayerList()) { - if (player.getTurnControlType() == Type.HUMAN) { + if (player.getType() == Type.HUMAN) { humanPlayerFound = true; break; } 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. 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> choices = new ArrayList>(); + List enableRemoveButtons = new ArrayList(); + List playerNamesEditable = new ArrayList(); + + 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(); } } -- cgit v1.2.3