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() {
@Override

View file

@ -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;
/**
@ -135,6 +136,46 @@ public class NetworkControl {
* view for events
*/
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(
new IListener1<GameData>() {
@Override
@ -154,37 +195,14 @@ public class NetworkControl {
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,
final IView view) {
connections.add(connectionControl.getConnectedEvent().add(
new IListener() {
connections.add(connectionControl.getConnectedEvent().add(new IListener() {
@Override
public void handle() {
view.getGameListPanel().setChannelName(
loginData.getChannelName());
view.getGameListPanel().setChannelName(loginData.getChannelName());
view.showConnectPanel(false);
view.showGameListPanel(true);
}
@ -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>() {
gameOfferControl.getStartGameEvent().add(new IListener1<GameSettings>() {
@Override
public void handle(GameSettings settings) {
gameControl = new NetworkGameControl(settings,
saveControl, view, connectionControl, true);
gameControl.startGame();
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();
}
}

View file

@ -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);
}
}

View file

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