Correctly handle disappearing players during games

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@577 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-22 05:44:23 +02:00
parent ba421e2c8b
commit 77e119e77d
4 changed files with 140 additions and 74 deletions

View file

@ -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() { connections.add(connectionControl.getGameStartEvent().add(new IListener() {
@Override @Override

View file

@ -17,6 +17,7 @@ import jrummikub.util.IListener1;
import jrummikub.util.LoginData; import jrummikub.util.LoginData;
import jrummikub.view.IQuitWarningPanel.QuitMode; import jrummikub.view.IQuitWarningPanel.QuitMode;
import jrummikub.view.IView; import jrummikub.view.IView;
import jrummikub.view.IView.BottomPanelType;
import jrummikub.view.LoginError; import jrummikub.view.LoginError;
/** /**
@ -45,13 +46,13 @@ public class NetworkControl {
* Creates a new network control * Creates a new network control
* *
* @param loginData * @param loginData
* user's login data * user's login data
* @param connectionControl * @param connectionControl
* current connection for events and messages * current connection for events and messages
* @param saveControl * @param saveControl
* save control if saving will ever be allowed * save control if saving will ever be allowed
* @param view * @param view
* for events and handlers * for events and handlers
*/ */
public NetworkControl(final LoginData loginData, public NetworkControl(final LoginData loginData,
IConnectionControl connectionControl, SaveControl saveControl, IConnectionControl connectionControl, SaveControl saveControl,
@ -132,9 +133,49 @@ public class NetworkControl {
* Adds the listeners for connection control events * Adds the listeners for connection control events
* *
* @param view * @param view
* view for events * view for events
*/ */
public void addConnectionControlListeners(final IView view) { public void addConnectionControlListeners(final IView view) {
addOfferUpdateListener();
connections.add(connectionControl.getGameWithdrawalEvent().add(
new IListener1<UUID>() {
@Override
public void handle(UUID uuid) {
games.remove(uuid);
gameMap.remove(uuid);
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
public void handle(Boolean ack) {
if (ack) {
createGameJoinControl();
} else {
view.showGameListPanel(true);
}
}
}));
}
private void addOfferUpdateListener() {
connections.add(connectionControl.getGameOfferEvent().add( connections.add(connectionControl.getGameOfferEvent().add(
new IListener1<GameData>() { new IListener1<GameData>() {
@Override @Override
@ -154,41 +195,18 @@ public class NetworkControl {
updateGameList(); updateGameList();
} }
})); }));
connections.add(connectionControl.getGameWithdrawalEvent().add(
new IListener1<UUID>() {
@Override
public void handle(UUID uuid) {
games.remove(uuid);
gameMap.remove(uuid);
updateGameList();
}
}));
connections.add(connectionControl.getGameJoinAckEvent().add(
new IListener1<Boolean>() {
@Override
public void handle(Boolean ack) {
if (ack) {
createGameJoinControl();
} else {
view.showGameListPanel(true);
}
}
}));
} }
private void addConnectionSetupListeners(final LoginData loginData, private void addConnectionSetupListeners(final LoginData loginData,
final IView view) { final IView view) {
connections.add(connectionControl.getConnectedEvent().add( connections.add(connectionControl.getConnectedEvent().add(new IListener() {
new IListener() { @Override
@Override public void handle() {
public void handle() { view.getGameListPanel().setChannelName(loginData.getChannelName());
view.getGameListPanel().setChannelName( view.showConnectPanel(false);
loginData.getChannelName()); view.showGameListPanel(true);
view.showConnectPanel(false); }
view.showGameListPanel(true); }));
}
}));
connections.add(connectionControl.getConnectionFailedEvent().add( connections.add(connectionControl.getConnectionFailedEvent().add(
new IListener1<LoginError>() { new IListener1<LoginError>() {
@ -219,31 +237,6 @@ public class NetworkControl {
view.getGameListPanel().setGameList(gameList); 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 * Starts a new network connection with the specified data
*/ */
@ -327,12 +320,34 @@ public class NetworkControl {
settingsControl.startSettings(); 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) { private void createGameOfferControl(GameSettings settings) {
if (gameOfferControl != null) { if (gameOfferControl != null) {
return; return;
} }
gameOfferControl = new GameOfferControl(connectionControl, settings, gameOfferControl = new GameOfferControl(connectionControl, settings, view);
view);
gameOfferControl.getBackEvent().add(new IListener() { gameOfferControl.getBackEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
@ -340,16 +355,27 @@ public class NetworkControl {
view.showGameListPanel(true); view.showGameListPanel(true);
} }
}); });
gameOfferControl.getStartGameEvent().add( gameOfferControl.getStartGameEvent().add(new IListener1<GameSettings>() {
new IListener1<GameSettings>() { @Override
@Override public void handle(GameSettings settings) {
public void handle(GameSettings settings) { createGameControl(settings, true);
gameControl = new NetworkGameControl(settings, }
saveControl, view, connectionControl, true); });
gameControl.startGame();
}
});
gameOfferControl.startGameOffer(); 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();
}
} }

View file

@ -7,6 +7,8 @@ import jrummikub.model.GameSettings;
import jrummikub.model.IRoundState; import jrummikub.model.IRoundState;
import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type; import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.util.IListener; import jrummikub.util.IListener;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
import jrummikub.view.IView; import jrummikub.view.IView;
@ -18,6 +20,7 @@ import jrummikub.view.IView.BottomPanelType;
public class NetworkGameControl extends GameControl { public class NetworkGameControl extends GameControl {
private IConnectionControl connectionControl; private IConnectionControl connectionControl;
private boolean host; private boolean host;
private Event backEvent = new Event();
/** /**
* Creates new network game control * Creates new network game control
@ -51,8 +54,7 @@ public class NetworkGameControl extends GameControl {
for (PlayerSettings s : NetworkGameControl.this.gameSettings for (PlayerSettings s : NetworkGameControl.this.gameSettings
.getPlayerList()) { .getPlayerList()) {
if (s.getName().equals(nickname) && s.getType() == Type.NETWORK) { if (s.getName().equals(nickname) && s.getType() == Type.NETWORK) {
abortGame(); connectionLost();
view.setBottomPanel(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL);
return; return;
} }
} }
@ -60,6 +62,10 @@ public class NetworkGameControl extends GameControl {
})); }));
} }
public IEvent getBackEvent() {
return backEvent;
}
@Override @Override
protected void startRound() { protected void startRound() {
connections.add(connectionControl.getRoundStartEvent().add(new IListener() { connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
@ -89,4 +95,24 @@ public class NetworkGameControl extends GameControl {
view.setBottomPanel(host ? BottomPanelType.WIN_PANEL view.setBottomPanel(host ? BottomPanelType.WIN_PANEL
: BottomPanelType.NETWORK_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);
}
} }

View file

@ -58,6 +58,10 @@ class WinPanel extends JPanel {
connectionLostLabel.setVerticalAlignment(JLabel.CENTER); connectionLostLabel.setVerticalAlignment(JLabel.CENTER);
add(connectionLostLabel); add(connectionLostLabel);
createButtons();
}
private void createButtons() {
newRoundButton = new JButton("Neue Runde"); newRoundButton = new JButton("Neue Runde");
newRoundButton.addActionListener(new ActionListener() { newRoundButton.addActionListener(new ActionListener() {
@Override @Override