summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
authorIda Massow <massow@informatik.uni-luebeck.de>2011-06-14 00:55:58 +0200
committerIda Massow <massow@informatik.uni-luebeck.de>2011-06-14 00:55:58 +0200
commiteee80d605c95bac3a2489f0dbadadd81ec1ad753 (patch)
tree4ffce7c17267fbc209ffc147fcd1799e8836f9b8 /src/jrummikub/control
parent8e0669a737af19cbcb1d6a1cf64ffcbc6acf2394 (diff)
downloadJRummikub-eee80d605c95bac3a2489f0dbadadd81ec1ad753.tar
JRummikub-eee80d605c95bac3a2489f0dbadadd81ec1ad753.zip
neue GameOfferControl
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@430 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/AbstractSettingsControl.java345
-rw-r--r--src/jrummikub/control/SettingsControl.java3
-rw-r--r--src/jrummikub/control/network/GameOfferControl.java27
-rw-r--r--src/jrummikub/control/network/NetworkControl.java50
-rw-r--r--src/jrummikub/control/network/NetworkSettingsControl.java3
5 files changed, 406 insertions, 22 deletions
diff --git a/src/jrummikub/control/AbstractSettingsControl.java b/src/jrummikub/control/AbstractSettingsControl.java
new file mode 100644
index 0000000..693e0f0
--- /dev/null
+++ b/src/jrummikub/control/AbstractSettingsControl.java
@@ -0,0 +1,345 @@
+package jrummikub.control;
+
+import java.awt.Color;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import jrummikub.model.GameSettings;
+import jrummikub.model.PlayerSettings;
+import jrummikub.model.PlayerSettings.Type;
+import jrummikub.model.StoneColor;
+import jrummikub.util.Connection;
+import jrummikub.util.IListener;
+import jrummikub.util.IListener1;
+import jrummikub.util.IListener2;
+import jrummikub.view.ISettingsPanel;
+import jrummikub.view.IView;
+
+public abstract class AbstractSettingsControl {
+ protected IView view;
+ protected List<Connection> connections = new ArrayList<Connection>();
+
+ protected GameSettings settings;
+
+ /**
+ * Create a new settings control
+ *
+ * @param view
+ * the view to use
+ * @param settings
+ * initial game settings
+ */
+ protected AbstractSettingsControl(IView view, GameSettings settings) {
+ this.view = view;
+ this.settings = settings;
+ }
+
+ protected void addListeners() {
+ addPlayerSettingsListeners();
+
+ addOptionListeners1();
+ addOptionListeners2();
+ addVariantListeners();
+ }
+
+ private void addPlayerSettingsListeners() {
+ connections.add(view.getSettingsPanel().getAddPlayerEvent()
+ .add(new IListener() {
+ @Override
+ public void handle() {
+ addPlayer();
+ }
+ }));
+ connections.add(view.getSettingsPanel().getRemovePlayerEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer i) {
+ removePlayer(i);
+ }
+ }));
+ connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
+ .add(new IListener2<Integer, Color>() {
+ @Override
+ public void handle(Integer i, Color color) {
+ setPlayerColor(i, color);
+ }
+ }));
+ connections.add(view.getSettingsPanel().getChangePlayerNameEvent()
+ .add(new IListener2<Integer, String>() {
+ @Override
+ public void handle(Integer i, String name) {
+ setPlayerName(i, name);
+ }
+ }));
+ connections.add(view.getSettingsPanel().getChangePlayerTypeEvent()
+ .add(new IListener2<Integer, Type>() {
+ @Override
+ public void handle(Integer i, Type type) {
+ setPlayerType(i, type);
+ }
+ }));
+ }
+
+ private void addOptionListeners1() {
+ connections.add(view.getSettingsPanel()
+ .getChangeInitialMeldThresholdEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer value) {
+ settings.setInitialMeldThreshold(value);
+ update();
+ }
+ }));
+
+ connections.add(view.getSettingsPanel().getChangeJokerNumberEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer value) {
+ settings.setJokerNumber(value);
+ update();
+ }
+ }));
+
+ connections.add(view.getSettingsPanel().getChangeStoneSetNumberEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer value) {
+ settings.setStoneSetNumber(value);
+ update();
+ }
+ }));
+ connections.add(view.getSettingsPanel()
+ .getChangeNumberOfStonesDealtEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer value) {
+ settings.setNumberOfStonesDealt(value);
+ update();
+ }
+ }));
+ }
+
+ private void addOptionListeners2() {
+ connections.add(view.getSettingsPanel().getChangeHighestValueEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer value) {
+ settings.setHighestValue(value);
+ update();
+ }
+ }));
+ connections.add(view.getSettingsPanel().getChangeTimeEvent()
+ .add(new IListener1<Integer>() {
+ @Override
+ public void handle(Integer value) {
+ settings.setTotalTime(value);
+ update();
+ }
+ }));
+ connections.add(view.getSettingsPanel().getChangeStoneColorsEvent()
+ .add(new IListener1<Set<StoneColor>>() {
+ @Override
+ public void handle(Set<StoneColor> value) {
+ settings.setStoneColors(new HashSet<StoneColor>(value));
+ update();
+ }
+ }));
+
+ connections.add(view.getSettingsPanel().getChangeNoLimitsEvent()
+ .add(new IListener1<Boolean>() {
+ @Override
+ public void handle(Boolean value) {
+ settings.setNoLimits(value);
+ update();
+ }
+ }));
+ }
+
+ /**
+ * Add event listeners for variants, e.g. standard or children
+ */
+ private void addVariantListeners() {
+ connections.add(view.getSettingsPanel().getSetVariantDefaultEvent()
+ .add(new IListener() {
+ @Override
+ public void handle() {
+ settings.reset();
+ update();
+ }
+ }));
+ connections.add(view.getSettingsPanel().getSetVariantChildrenEvent()
+ .add(new IListener() {
+ @Override
+ public void handle() {
+ settings.reset();
+ settings.setHighestValue(10);
+ settings.setJokerNumber(6);
+ settings.setTotalTime(120);
+ settings.setInitialMeldThreshold(20);
+ update();
+ }
+ }));
+ }
+
+ /**
+ * Start the operation of the settings control
+ */
+ public void startSettings() {
+ view.showSettingsPanel(true);
+ }
+
+ protected Color findUnusedColor() {
+ Color color = null;
+ colorLoop: for (Color c : ISettingsPanel.PLAYER_COLORS) {
+ color = c;
+ for (PlayerSettings player : settings.getPlayerList()) {
+ if (c == player.getColor()) {
+ continue colorLoop;
+ }
+ }
+ break;
+ }
+ return color;
+ }
+
+ private void setPlayerColor(int i, Color color) {
+ PlayerSettings player = settings.getPlayerList().get(i);
+
+ if (player.getColor() == color) {
+ return;
+ }
+
+ for (PlayerSettings other : settings.getPlayerList()) {
+ if (other.getColor() == color) {
+ other.setColor(player.getColor());
+ break;
+ }
+ }
+
+ player.setColor(color);
+ update();
+ }
+
+ private void setPlayerName(int i, String name) {
+ settings.getPlayerList().get(i).setName(name);
+ update();
+ }
+
+ private void removePlayer(int i) {
+ settings.getPlayerList().remove(i);
+ update();
+ }
+
+ protected boolean checkSettings() {
+ if (!checkName()) {
+ return false;
+ }
+ if (!checkErrors()) {
+ return false;
+ }
+
+ view.getSettingsPanel().setError(ISettingsPanel.SettingsError.NO_ERROR);
+ view.getSettingsPanel().enableStartGameButton(true);
+
+ checkWarnings();
+
+ return true;
+ }
+
+ private boolean checkName() {
+ for (PlayerSettings player : settings.getPlayerList()) {
+ if (player.getName().isEmpty()) {
+ view.getSettingsPanel().setError(
+ ISettingsPanel.SettingsError.NO_PLAYER_NAME_ERROR);
+ view.getSettingsPanel().enableStartGameButton(false);
+ return false;
+ }
+ }
+ for (int i = 0; i < settings.getPlayerList().size(); ++i) {
+ 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) {
+ 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;
+ }
+ }
+ }
+ return true;
+ }
+
+ private boolean checkErrors() {
+ int totalStonesDealt = settings.getNumberOfStonesDealt()
+ * settings.getPlayerList().size();
+ int totalStones = settings.getHighestValue()
+ * settings.getStoneSetNumber()
+ * settings.getStoneColors().size() + settings.getJokerNumber();
+
+ if (totalStones <= totalStonesDealt) {
+ view.getSettingsPanel().setError(
+ ISettingsPanel.SettingsError.NOT_ENOUGH_STONES_ERROR);
+ view.getSettingsPanel().enableStartGameButton(false);
+ return false;
+ }
+
+ if (settings.getStoneColors().size() < 3) {
+ view.getSettingsPanel().setError(
+ ISettingsPanel.SettingsError.NOT_ENOUGH_COLORS_ERROR);
+ view.getSettingsPanel().enableStartGameButton(false);
+ return false;
+ }
+
+ return true;
+ }
+
+ private void checkWarnings() {
+ if (settings.getInitialMeldThreshold() >= 100) {
+ view.getSettingsPanel().setError(
+ ISettingsPanel.SettingsError.TOO_HIGH_THRESHOLD_WARNING);
+ return;
+ }
+
+ boolean humanPlayerFound = false;
+ for (PlayerSettings player : settings.getPlayerList()) {
+ if (player.getType() == Type.HUMAN) {
+ humanPlayerFound = true;
+ break;
+ }
+ }
+
+ if (!humanPlayerFound) {
+ view.getSettingsPanel().setError(
+ ISettingsPanel.SettingsError.COMPUTER_PLAYERS_ONLY_WARNING);
+ return;
+ }
+ }
+
+ /**
+ * Abort settings control once settings are set
+ */
+ public void abort() {
+ view.showSettingsPanel(false);
+ for (Connection c : connections) {
+ c.remove();
+ }
+ }
+
+ abstract protected void addPlayer();
+
+ abstract protected void setPlayerType(int i, Type type);
+
+ abstract protected void update();
+
+}
diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java
index a3fad13..4659828 100644
--- a/src/jrummikub/control/SettingsControl.java
+++ b/src/jrummikub/control/SettingsControl.java
@@ -14,6 +14,7 @@ import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.IView;
+import jrummikub.view.ISettingsPanel.SettingsMode;
/**
* The settings control controls the settings panel
@@ -31,7 +32,7 @@ public class SettingsControl extends AbstractSettingsControl {
*/
public SettingsControl(IView view, GameSettings settings) {
super(view, settings);
- view.getSettingsPanel().enableNetworkMode(false);
+ view.getSettingsPanel().setSettingsMode(SettingsMode.DEFAULT);
view.getSettingsPanel().setPlayerNamesEditable(
Collections.<Boolean> emptyList());
diff --git a/src/jrummikub/control/network/GameOfferControl.java b/src/jrummikub/control/network/GameOfferControl.java
new file mode 100644
index 0000000..0e90078
--- /dev/null
+++ b/src/jrummikub/control/network/GameOfferControl.java
@@ -0,0 +1,27 @@
+package jrummikub.control.network;
+
+import jrummikub.model.GameSettings;
+import jrummikub.view.ISettingsPanel.SettingsMode;
+import jrummikub.view.IView;
+
+public class GameOfferControl {
+ private ConnectionControl connectionControl;
+ private GameSettings settings;
+ private IView view;
+
+ public GameOfferControl(ConnectionControl connectionControl,
+ GameSettings settings, IView view) {
+ this.connectionControl = connectionControl;
+ this.settings = settings;
+ this.view = view;
+
+ view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_OFFER);
+ view.getSettingsPanel().setGameSettings(settings);
+ }
+
+ public void startGameOffer() {
+ view.showSettingsPanel(true);
+
+ }
+
+}
diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java
index bb60556..e09cdc0 100644
--- a/src/jrummikub/control/network/NetworkControl.java
+++ b/src/jrummikub/control/network/NetworkControl.java
@@ -26,6 +26,7 @@ public class NetworkControl {
private Event stopNetworkEvent = new Event();
private NetworkSettingsControl settingsControl;
+ private GameOfferControl gameOfferControl;
private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();
@@ -157,27 +158,36 @@ public class NetworkControl {
}
private void createSettingsControl() {
- if (settingsControl == null) {
- view.showGameListPanel(false);
-
- settingsControl = new NetworkSettingsControl(
- connectionControl.getNickname(), view, new GameSettings());
- settingsControl.getOfferGameEvent().add(new IListener1<GameSettings>() {
- @Override
- public void handle(GameSettings value) {
- // TODO Auto-generated method stub
-
- }
- });
- settingsControl.getBackEvent().add(new IListener() {
- @Override
- public void handle() {
- settingsControl=null;
- view.showGameListPanel(true);
- }
- });
- settingsControl.startSettings();
+ if (settingsControl != null) {
+ return;
}
+ view.showGameListPanel(false);
+
+ settingsControl = new NetworkSettingsControl(
+ connectionControl.getNickname(), view, new GameSettings());
+ settingsControl.getOfferGameEvent().add(new IListener1<GameSettings>() {
+ @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.startGameOffer();
}
}
diff --git a/src/jrummikub/control/network/NetworkSettingsControl.java b/src/jrummikub/control/network/NetworkSettingsControl.java
index 9c989c6..ca8c74b 100644
--- a/src/jrummikub/control/network/NetworkSettingsControl.java
+++ b/src/jrummikub/control/network/NetworkSettingsControl.java
@@ -13,6 +13,7 @@ import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IListener;
import jrummikub.view.ISettingsPanel;
+import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;
/**
@@ -38,7 +39,7 @@ public class NetworkSettingsControl extends AbstractSettingsControl {
super(view, settings);
this.nickname = nickname;
- view.getSettingsPanel().enableNetworkMode(true);
+ view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_SETUP);
view.getSettingsPanel().setPlayerNamesEditable(
Collections.<Boolean> emptyList());