summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control/network')
-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
7 files changed, 137 insertions, 23 deletions
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);
+ }
+}