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/GameJoinControl.java10
-rw-r--r--src/jrummikub/control/network/NetworkControl.java162
-rw-r--r--src/jrummikub/control/network/NetworkGameControl.java30
3 files changed, 132 insertions, 70 deletions
diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java
index cbdc4de..79753ca 100644
--- a/src/jrummikub/control/network/GameJoinControl.java
+++ b/src/jrummikub/control/network/GameJoinControl.java
@@ -54,6 +54,16 @@ public class GameJoinControl extends AbstractGameBeginControl {
}
}
}));
+ connections.add(connectionControl.getParticipantLeftEvent().add(
+ new IListener1<String>() {
+ @Override
+ public void handle(String nickname) {
+ if (nickname.equals(gameData.getHost())) {
+ abort();
+ backEvent.emit();
+ }
+ }
+ }));
connections.add(connectionControl.getGameStartEvent().add(new IListener() {
@Override
diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java
index ce46a0f..6e42f83 100644
--- a/src/jrummikub/control/network/NetworkControl.java
+++ b/src/jrummikub/control/network/NetworkControl.java
@@ -17,6 +17,7 @@ import jrummikub.util.IListener1;
import jrummikub.util.LoginData;
import jrummikub.view.IQuitWarningPanel.QuitMode;
import jrummikub.view.IView;
+import jrummikub.view.IView.BottomPanelType;
import jrummikub.view.LoginError;
/**
@@ -45,13 +46,13 @@ public class NetworkControl {
* Creates a new network control
*
* @param loginData
- * user's login data
+ * user's login data
* @param connectionControl
- * current connection for events and messages
+ * current connection for events and messages
* @param saveControl
- * save control if saving will ever be allowed
+ * save control if saving will ever be allowed
* @param view
- * for events and handlers
+ * for events and handlers
*/
public NetworkControl(final LoginData loginData,
IConnectionControl connectionControl, SaveControl saveControl,
@@ -132,28 +133,11 @@ public class NetworkControl {
* Adds the listeners for connection control events
*
* @param view
- * view for events
+ * view for events
*/
public void addConnectionControlListeners(final IView view) {
- connections.add(connectionControl.getGameOfferEvent().add(
- new IListener1<GameData>() {
- @Override
- public void handle(GameData gameData) {
- UUID uuid = gameData.getGameID();
-
- GameData game = gameMap.get(uuid);
-
- if (game == null) {
- game = gameData;
- gameMap.put(uuid, gameData);
- games.add(uuid);
- } else {
- game.setGameSettings(gameData.getGameSettings());
- }
+ addOfferUpdateListener();
- updateGameList();
- }
- }));
connections.add(connectionControl.getGameWithdrawalEvent().add(
new IListener1<UUID>() {
@Override
@@ -164,6 +148,20 @@ public class NetworkControl {
updateGameList();
}
}));
+ connections.add(connectionControl.getParticipantLeftEvent().add(
+ new IListener1<String>() {
+ @Override
+ public void handle(String nickname) {
+ for (Map.Entry<UUID, GameData> entry : gameMap.entrySet()) {
+ if (entry.getValue().getHost().equals(nickname)) {
+ games.remove(entry.getKey());
+ gameMap.remove(entry.getKey());
+ }
+ }
+
+ updateGameList();
+ }
+ }));
connections.add(connectionControl.getGameJoinAckEvent().add(
new IListener1<Boolean>() {
@Override
@@ -177,18 +175,38 @@ public class NetworkControl {
}));
}
- private void addConnectionSetupListeners(final LoginData loginData,
- final IView view) {
- connections.add(connectionControl.getConnectedEvent().add(
- new IListener() {
+ private void addOfferUpdateListener() {
+ connections.add(connectionControl.getGameOfferEvent().add(
+ new IListener1<GameData>() {
@Override
- public void handle() {
- view.getGameListPanel().setChannelName(
- loginData.getChannelName());
- view.showConnectPanel(false);
- view.showGameListPanel(true);
+ public void handle(GameData gameData) {
+ UUID uuid = gameData.getGameID();
+
+ GameData game = gameMap.get(uuid);
+
+ if (game == null) {
+ game = gameData;
+ gameMap.put(uuid, gameData);
+ games.add(uuid);
+ } else {
+ game.setGameSettings(gameData.getGameSettings());
+ }
+
+ updateGameList();
}
}));
+ }
+
+ private void addConnectionSetupListeners(final LoginData loginData,
+ final IView view) {
+ connections.add(connectionControl.getConnectedEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ view.getGameListPanel().setChannelName(loginData.getChannelName());
+ view.showConnectPanel(false);
+ view.showGameListPanel(true);
+ }
+ }));
connections.add(connectionControl.getConnectionFailedEvent().add(
new IListener1<LoginError>() {
@@ -219,31 +237,6 @@ public class NetworkControl {
view.getGameListPanel().setGameList(gameList);
}
- private void createGameJoinControl() {
- if (gameJoinControl != null) {
- return;
- }
-
- GameData gameData = connectionControl.getCurrentGame();
- gameJoinControl = new GameJoinControl(connectionControl, gameData, view);
- gameJoinControl.getBackEvent().add(new IListener() {
- @Override
- public void handle() {
- gameJoinControl = null;
- view.showGameListPanel(true);
- }
- });
- gameJoinControl.getStartGameEvent().add(new IListener() {
- @Override
- public void handle() {
- gameControl = new NetworkGameControl(null, saveControl, view,
- connectionControl, false);
- gameControl.startGame();
- }
- });
- gameJoinControl.startGameJoin();
- }
-
/**
* Starts a new network connection with the specified data
*/
@@ -327,12 +320,34 @@ public class NetworkControl {
settingsControl.startSettings();
}
+ private void createGameJoinControl() {
+ if (gameJoinControl != null) {
+ return;
+ }
+
+ GameData gameData = connectionControl.getCurrentGame();
+ gameJoinControl = new GameJoinControl(connectionControl, gameData, view);
+ gameJoinControl.getBackEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ gameJoinControl = null;
+ view.showGameListPanel(true);
+ }
+ });
+ gameJoinControl.getStartGameEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ createGameControl(null, false);
+ }
+ });
+ gameJoinControl.startGameJoin();
+ }
+
private void createGameOfferControl(GameSettings settings) {
if (gameOfferControl != null) {
return;
}
- gameOfferControl = new GameOfferControl(connectionControl, settings,
- view);
+ gameOfferControl = new GameOfferControl(connectionControl, settings, view);
gameOfferControl.getBackEvent().add(new IListener() {
@Override
public void handle() {
@@ -340,16 +355,27 @@ 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.getStartGameEvent().add(new IListener1<GameSettings>() {
+ @Override
+ public void handle(GameSettings settings) {
+ createGameControl(settings, true);
+ }
+ });
gameOfferControl.startGameOffer();
}
+ private void createGameControl(GameSettings settings, boolean host) {
+ gameControl = new NetworkGameControl(settings, saveControl, view,
+ connectionControl, host);
+ gameControl.getBackEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ view.setBottomPanel(BottomPanelType.START_GAME_PANEL);
+ createSettingsControl();
+ }
+ });
+
+ gameControl.startGame();
+ }
+
}
diff --git a/src/jrummikub/control/network/NetworkGameControl.java b/src/jrummikub/control/network/NetworkGameControl.java
index 768f0c3..262b380 100644
--- a/src/jrummikub/control/network/NetworkGameControl.java
+++ b/src/jrummikub/control/network/NetworkGameControl.java
@@ -7,6 +7,8 @@ import jrummikub.model.GameSettings;
import jrummikub.model.IRoundState;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
+import jrummikub.util.Event;
+import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.IView;
@@ -18,6 +20,7 @@ import jrummikub.view.IView.BottomPanelType;
public class NetworkGameControl extends GameControl {
private IConnectionControl connectionControl;
private boolean host;
+ private Event backEvent = new Event();
/**
* Creates new network game control
@@ -51,8 +54,7 @@ public class NetworkGameControl extends GameControl {
for (PlayerSettings s : NetworkGameControl.this.gameSettings
.getPlayerList()) {
if (s.getName().equals(nickname) && s.getType() == Type.NETWORK) {
- abortGame();
- view.setBottomPanel(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL);
+ connectionLost();
return;
}
}
@@ -60,6 +62,10 @@ public class NetworkGameControl extends GameControl {
}));
}
+ public IEvent getBackEvent() {
+ return backEvent;
+ }
+
@Override
protected void startRound() {
connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
@@ -89,4 +95,24 @@ public class NetworkGameControl extends GameControl {
view.setBottomPanel(host ? BottomPanelType.WIN_PANEL
: BottomPanelType.NETWORK_WIN_PANEL);
}
+
+ private void connectionLost() {
+ abortGame();
+
+ connections.add(view.getNewGameEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ abortGame();
+ backEvent.emit();
+ }
+ }));
+ connections.add(view.getQuitEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ System.exit(0);
+ }
+ }));
+
+ view.setBottomPanel(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL);
+ }
}