summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-06-20 03:59:04 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-06-20 03:59:04 +0200
commitf3f8ffe4621e91d33732bba9e103ff790a16c7b3 (patch)
treea4663053ea570c7a077a244de9f4f884294208d1 /src/jrummikub/control
parente79295f271062f2186c2ce0f9b69f1ddfc964abf (diff)
downloadJRummikub-f3f8ffe4621e91d33732bba9e103ff790a16c7b3.tar
JRummikub-f3f8ffe4621e91d33732bba9e103ff790a16c7b3.zip
Added NetworkGameControl
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@503 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/ApplicationControl.java2
-rw-r--r--src/jrummikub/control/GameControl.java55
-rw-r--r--src/jrummikub/control/RoundControl.java1
-rw-r--r--src/jrummikub/control/network/AbstractGameBeginControl.java17
-rw-r--r--src/jrummikub/control/network/ConnectionControl.java20
-rw-r--r--src/jrummikub/control/network/GameJoinControl.java15
-rw-r--r--src/jrummikub/control/network/GameOfferControl.java33
-rw-r--r--src/jrummikub/control/network/IConnectionControl.java4
-rw-r--r--src/jrummikub/control/network/NetworkControl.java25
-rw-r--r--src/jrummikub/control/network/NetworkGameControl.java46
10 files changed, 172 insertions, 46 deletions
diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java
index bc16cb4..f4e5a23 100644
--- a/src/jrummikub/control/ApplicationControl.java
+++ b/src/jrummikub/control/ApplicationControl.java
@@ -206,7 +206,7 @@ public class ApplicationControl {
private void createNetworkControl(LoginData loginData) {
ConnectionControl connectionControl = new ConnectionControl(loginData);
- networkControl = new NetworkControl(loginData, connectionControl, view);
+ networkControl = new NetworkControl(loginData, connectionControl, saveControl, view);
networkControl.getStopNetworkEvent().add(new IListener() {
@Override
diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java
index 66a96b6..ead4c51 100644
--- a/src/jrummikub/control/GameControl.java
+++ b/src/jrummikub/control/GameControl.java
@@ -20,24 +20,24 @@ import jrummikub.view.IView.BottomPanelType;
* Controls a Game, at some point including all Rounds, starts new Rounds
*/
public class GameControl {
- private SaveControl saveControl;
+ protected SaveControl saveControl;
+ protected GameSettings gameSettings;
+ protected IView view;
+ protected RoundControl roundControl;
+ protected GameState gameState;
+ protected List<Connection> connections = new ArrayList<Connection>();
- private GameSettings gameSettings;
- private IView view;
- RoundControl roundControl;
- private GameState gameState;
- private List<Connection> connections = new ArrayList<Connection>();
private Event endOfGameEvent = new Event();
/**
* Constructor
*
* @param gameSettings
- * the game settings
+ * the game settings
* @param saveControl
- * the save control
+ * the save control
* @param view
- * the view
+ * the view
*/
public GameControl(GameSettings gameSettings, SaveControl saveControl,
IView view) {
@@ -45,11 +45,13 @@ public class GameControl {
this.saveControl = saveControl;
this.view = view;
- gameState = new GameState();
- saveControl.setGameState(gameState);
+ if (gameSettings != null) {
+ gameState = new GameState();
+ saveControl.setGameState(gameState);
- gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
- .getPlayerList().size()));
+ gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
+ .getPlayerList().size()));
+ }
connections.add(view.getNewRoundEvent().add(new IListener() {
@Override
@@ -104,9 +106,9 @@ public class GameControl {
* Continues game after loading
*
* @param gameState
- * the saved GameState (Players, startplayer, points)
+ * the saved GameState (Players, startplayer, points)
* @param roundState
- * the saved RoundState (activePlayer, Table, heap etc)
+ * the saved RoundState (activePlayer, Table, heap etc)
*/
public void continueGame(GameState gameState, IRoundState roundState) {
this.gameState = gameState;
@@ -133,14 +135,14 @@ public class GameControl {
view.clearView();
}
- private void startRound() {
+ protected void startRound() {
if (roundControl != null) {
return;
}
view.showScorePanel(false);
- IRoundState roundState = new RoundState(gameSettings);
+ IRoundState roundState = createRoundState();
prepareRound(roundState);
roundControl.startRound();
}
@@ -148,10 +150,12 @@ public class GameControl {
private void prepareRound(IRoundState roundState) {
saveControl.setRoundState(roundState);
- roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
- + gameState.getScores().size());
+ if (roundState != null) {
+ roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
+ + gameState.getScores().size());
+ }
- roundControl = new RoundControl(roundState, view);
+ roundControl = createRoundControl(roundState);
roundControl.getEndOfRoundEvent().add(new IListener1<Score>() {
@Override
@@ -169,6 +173,14 @@ public class GameControl {
});
}
+ protected IRoundState createRoundState() {
+ return new RoundState(gameSettings, gameState);
+ }
+
+ protected RoundControl createRoundControl(IRoundState roundState) {
+ return new RoundControl(roundState, view);
+ }
+
private void restartRound() {
roundControl = null;
startRound();
@@ -188,8 +200,7 @@ public class GameControl {
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
view.getScorePanel().setScores(gameState.getScores());
- view.getScorePanel().setAccumulatedScore(
- gameState.getAccumulatedScore());
+ view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
view.getScorePanel().update();
view.showScorePanel(true);
}
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index ac3acf3..408c275 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -244,6 +244,7 @@ public class RoundControl {
}
}
+ view.getSidePanel().setHeapSize(roundState.getGameHeap().getSize());
}
private boolean laidOutValidPoints() {
diff --git a/src/jrummikub/control/network/AbstractGameBeginControl.java b/src/jrummikub/control/network/AbstractGameBeginControl.java
index 341f94e..4fc0cc6 100644
--- a/src/jrummikub/control/network/AbstractGameBeginControl.java
+++ b/src/jrummikub/control/network/AbstractGameBeginControl.java
@@ -11,10 +11,8 @@ import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Connection;
import jrummikub.util.Event;
-import jrummikub.util.Event1;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
-import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
@@ -31,7 +29,6 @@ public abstract class AbstractGameBeginControl {
protected IConnectionControl connectionControl;
protected IView view;
protected Event backEvent = new Event();
- private Event1<GameData> gameStartEvent = new Event1<GameData>();
/**
* Create a new game begin control
@@ -115,20 +112,6 @@ public abstract class AbstractGameBeginControl {
return backEvent;
}
- /**
- * The event that is emitted when the game is started
- *
- * @return the event
- */
- public IEvent1<GameData> getStartTurnEvent() {
- return gameStartEvent;
- }
-
- protected void startGame() {
- abort();
- gameStartEvent.emit(gameData);
- }
-
protected void abort() {
view.showSettingsPanel(false);
for (Connection c : connections) {
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java
index 54c9512..d791344 100644
--- a/src/jrummikub/control/network/ConnectionControl.java
+++ b/src/jrummikub/control/network/ConnectionControl.java
@@ -60,6 +60,7 @@ public class ConnectionControl implements IConnectionControl {
private Event2<String, Color> changeColorEvent = new Event2<String, Color>();
private Event gameStartEvent = new Event();
+ private Event roundStartEvent = new Event();
private Event1<ITable> tableUpdateEvent = new Event1<ITable>();
private Event1<ITable> turnEndEvent = new Event1<ITable>();
@@ -152,6 +153,11 @@ public class ConnectionControl implements IConnectionControl {
}
@Override
+ public IEvent getRoundStartEvent() {
+ return roundStartEvent;
+ }
+
+ @Override
public IEvent1<ITable> getTableUpdateEvent() {
return tableUpdateEvent;
}
@@ -271,6 +277,18 @@ public class ConnectionControl implements IConnectionControl {
}
@Override
+ public void startRound() {
+ final UUID uuid = currentGame.getGameID();
+ run(new SendRunner() {
+ @Override
+ protected void addData(DefaultPacketExtension extension) {
+ extension.setValue("messageType", "round_start");
+ extension.setValue("uuid", uuid.toString());
+ }
+ });
+ }
+
+ @Override
public void updateTable(final ITable table) {
final UUID uuid = currentGame.getGameID();
run(new SendRunner() {
@@ -414,6 +432,8 @@ public class ConnectionControl implements IConnectionControl {
(Color) Base64.decodeToObject(extension.getValue("color")));
} else if (messageType.equals("game_start")) {
gameStartEvent.emit();
+ } else if (messageType.equals("round_start")) {
+ roundStartEvent.emit();
} else if (messageType.equals("table_update")) {
tableUpdateEvent.emit((ITable) Base64.decodeToObject(extension
.getValue("table")));
diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java
index b9690b6..5d31e74 100644
--- a/src/jrummikub/control/network/GameJoinControl.java
+++ b/src/jrummikub/control/network/GameJoinControl.java
@@ -5,7 +5,9 @@ import java.util.UUID;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
+import jrummikub.util.Event;
import jrummikub.util.GameData;
+import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
@@ -15,6 +17,7 @@ import jrummikub.view.IView;
* Control for joining a network game
*/
public class GameJoinControl extends AbstractGameBeginControl {
+ private Event gameStartEvent = new Event();
/**
* Creates new game join control
@@ -63,11 +66,21 @@ public class GameJoinControl extends AbstractGameBeginControl {
connections.add(connectionControl.getGameStartEvent().add(new IListener() {
@Override
public void handle() {
- startGame();
+ abort();
+ gameStartEvent.emit();
}
}));
}
+ /**
+ * The event that is emitted when the game is started
+ *
+ * @return the event
+ */
+ public IEvent getStartGameEvent() {
+ return gameStartEvent;
+ }
+
private void fixGameSettings(GameSettings settings) {
for (PlayerSettings player : settings.getPlayerList()) {
if (player.getType() == Type.HUMAN) {
diff --git a/src/jrummikub/control/network/GameOfferControl.java b/src/jrummikub/control/network/GameOfferControl.java
index 8c5820d..42e3b8b 100644
--- a/src/jrummikub/control/network/GameOfferControl.java
+++ b/src/jrummikub/control/network/GameOfferControl.java
@@ -1,12 +1,15 @@
package jrummikub.control.network;
+import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
+import jrummikub.util.Event1;
import jrummikub.util.GameData;
+import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
@@ -16,6 +19,7 @@ import jrummikub.view.IView;
* Control for network game host
*/
public class GameOfferControl extends AbstractGameBeginControl {
+ private Event1<GameSettings> gameStartEvent = new Event1<GameSettings>();
/**
* Creates new game offer control
@@ -84,10 +88,32 @@ public class GameOfferControl extends AbstractGameBeginControl {
}));
}
- @Override
- protected void startGame() {
- super.startGame();
+ /**
+ * The event that is emitted when the game is started
+ *
+ * @return the event
+ */
+ public IEvent1<GameSettings> getStartGameEvent() {
+ return gameStartEvent;
+ }
+
+ private void startGame() {
+ abort();
connectionControl.startGame();
+
+ GameSettings settings = gameData.getGameSettings();
+ removeVacant(settings);
+
+ gameStartEvent.emit(settings);
+ }
+
+ private void removeVacant(GameSettings settings) {
+ Iterator<PlayerSettings> it = settings.getPlayerList().iterator();
+ while (it.hasNext()) {
+ if (it.next().getType() == Type.VACANT) {
+ it.remove();
+ }
+ }
}
/**
@@ -98,7 +124,6 @@ public class GameOfferControl extends AbstractGameBeginControl {
connectionControl.offerGame(gameData);
view.showSettingsPanel(true);
-
}
@Override
diff --git a/src/jrummikub/control/network/IConnectionControl.java b/src/jrummikub/control/network/IConnectionControl.java
index ff23c82..52c7fd1 100644
--- a/src/jrummikub/control/network/IConnectionControl.java
+++ b/src/jrummikub/control/network/IConnectionControl.java
@@ -37,6 +37,8 @@ interface IConnectionControl {
public IEvent getGameStartEvent();
+ public IEvent getRoundStartEvent();
+
public IEvent1<ITable> getTableUpdateEvent();
public IEvent1<ITable> getTurnEndEvent();
@@ -61,6 +63,8 @@ interface IConnectionControl {
public void startGame();
+ public void startRound();
+
public void updateTable(ITable table);
public void endTurn(ITable table);
diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java
index 60640db..6579b0f 100644
--- a/src/jrummikub/control/network/NetworkControl.java
+++ b/src/jrummikub/control/network/NetworkControl.java
@@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
+import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.util.Connection;
import jrummikub.util.Event;
@@ -31,6 +32,10 @@ public class NetworkControl {
private GameOfferControl gameOfferControl;
private GameJoinControl gameJoinControl;
+ private SaveControl saveControl;
+
+ private NetworkGameControl gameControl;
+
private List<UUID> games = new ArrayList<UUID>();
private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();
@@ -45,9 +50,11 @@ public class NetworkControl {
* for events and handlers
*/
public NetworkControl(final LoginData loginData,
- IConnectionControl connectionControl, final IView view) {
+ IConnectionControl connectionControl, SaveControl saveControl,
+ final IView view) {
this.view = view;
this.connectionControl = connectionControl;
+ this.saveControl = saveControl;
addConnectionSetupListeners(loginData, view);
addConnectionControlListeners(view);
@@ -186,6 +193,14 @@ public class NetworkControl {
view.showGameListPanel(true);
}
});
+ gameJoinControl.getStartGameEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ gameControl = new NetworkGameControl(null, saveControl, view,
+ connectionControl, false);
+ gameControl.startGame();
+ }
+ });
gameJoinControl.startGameJoin();
}
@@ -271,6 +286,14 @@ public class NetworkControl {
view.showGameListPanel(true);
}
});
+ gameOfferControl.getStartGameEvent().add(new IListener1<GameSettings>() {
+ @Override
+ public void handle(GameSettings settings) {
+ gameControl = new NetworkGameControl(settings, saveControl, view,
+ connectionControl, true);
+ gameControl.startGame();
+ }
+ });
gameOfferControl.startGameOffer();
}
diff --git a/src/jrummikub/control/network/NetworkGameControl.java b/src/jrummikub/control/network/NetworkGameControl.java
new file mode 100644
index 0000000..1040909
--- /dev/null
+++ b/src/jrummikub/control/network/NetworkGameControl.java
@@ -0,0 +1,46 @@
+package jrummikub.control.network;
+
+import jrummikub.control.GameControl;
+import jrummikub.control.RoundControl;
+import jrummikub.control.SaveControl;
+import jrummikub.model.GameSettings;
+import jrummikub.model.IRoundState;
+import jrummikub.util.IListener;
+import jrummikub.view.IView;
+
+public class NetworkGameControl extends GameControl {
+ private IConnectionControl connectionControl;
+ private boolean host;
+
+ public NetworkGameControl(GameSettings gameSettings, SaveControl saveControl,
+ IView view, IConnectionControl connectionControl, boolean host) {
+ super(gameSettings, saveControl, view);
+
+ this.connectionControl = connectionControl;
+ this.host = host;
+ }
+
+ @Override
+ public void startGame() {
+ connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ startRound();
+ }
+ }));
+
+ if (host) {
+ connectionControl.startRound();
+ }
+ }
+
+ @Override
+ protected IRoundState createRoundState() {
+ return host ? super.createRoundState() : null;
+ }
+
+ @Override
+ protected RoundControl createRoundControl(IRoundState roundState) {
+ return new NetworkRoundControl(roundState, view, connectionControl, host);
+ }
+}