From bf24a9279ae23336cc8e14d6e1c40f1a2a0c33a7 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Sun, 19 Jun 2011 02:47:38 +0200 Subject: Start game button in a network game sends an event git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@479 72836036-5685-4462-b002-a69064685172 --- .../control/network/AbstractGameBeginControl.java | 62 ++++++++++++++-------- .../control/network/ConnectionControl.java | 22 ++++++++ src/jrummikub/control/network/GameJoinControl.java | 10 +++- .../control/network/GameOfferControl.java | 38 +++++++++---- .../control/network/IConnectionControl.java | 4 ++ 5 files changed, 103 insertions(+), 33 deletions(-) (limited to 'src/jrummikub/control') diff --git a/src/jrummikub/control/network/AbstractGameBeginControl.java b/src/jrummikub/control/network/AbstractGameBeginControl.java index fd3b546..341f94e 100644 --- a/src/jrummikub/control/network/AbstractGameBeginControl.java +++ b/src/jrummikub/control/network/AbstractGameBeginControl.java @@ -11,7 +11,10 @@ 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; @@ -19,7 +22,7 @@ import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; /** - * Abstract class for network game controls inbetween choosing and starting a + * Abstract class for network game controls in between choosing and starting a * game */ public abstract class AbstractGameBeginControl { @@ -28,18 +31,19 @@ public abstract class AbstractGameBeginControl { protected IConnectionControl connectionControl; protected IView view; protected Event backEvent = new Event(); + private Event1 gameStartEvent = new Event1(); /** * Create a new game begin control * * @param connection - * connection control for mesages and events + * connection control for messages and events * @param view - * the view + * the view * @param gameData - * game data of chosen game + * game data of chosen game * @param settingsMode - * mode of settings panel + * mode of settings panel */ public AbstractGameBeginControl(IConnectionControl connection, IView view, final GameData gameData, SettingsMode settingsMode) { @@ -57,11 +61,10 @@ public abstract class AbstractGameBeginControl { new IListener2() { @Override public void handle(String sender, Color color) { - List players = gameData - .getGameSettings().getPlayerList(); + List players = gameData.getGameSettings() + .getPlayerList(); for (PlayerSettings s : players) { - if (s.getName().equals(sender) - && s.getType() == Type.NETWORK) { + if (s.getName().equals(sender) && s.getType() == Type.NETWORK) { s.setColor(color); break; } @@ -72,13 +75,12 @@ public abstract class AbstractGameBeginControl { } private void addViewListeners(IView view, final GameData gameData) { - connections.add(view.getSettingsPanel().getBackEvent().add( - new IListener() { - @Override - public void handle() { - goBack(); - } - })); + connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() { + @Override + public void handle() { + goBack(); + } + })); connections.add(view.getSettingsPanel().getChangePlayerColorEvent() .add(new IListener2() { @Override @@ -89,8 +91,8 @@ public abstract class AbstractGameBeginControl { return; } } - PlayerSettings player = gameData.getGameSettings() - .getPlayerList().get(i); + PlayerSettings player = gameData.getGameSettings().getPlayerList() + .get(i); if (player.getType() != Type.HUMAN) { return; } @@ -104,15 +106,29 @@ public abstract class AbstractGameBeginControl { protected abstract void goBack(); /** - * The back event is emitted when the player wants to go back to the - * previous control and panel + * The back event is emitted when the player wants to go back to the previous + * control and panel * * @return the event */ - public Event getBackEvent() { + public IEvent getBackEvent() { return backEvent; } + /** + * The event that is emitted when the game is started + * + * @return the event + */ + public IEvent1 getStartTurnEvent() { + return gameStartEvent; + } + + protected void startGame() { + abort(); + gameStartEvent.emit(gameData); + } + protected void abort() { view.showSettingsPanel(false); for (Connection c : connections) { @@ -123,8 +139,8 @@ public abstract class AbstractGameBeginControl { protected void updateSettingsPanel() { view.getSettingsPanel().setGameSettings(gameData.getGameSettings()); - Set colors = new HashSet(Arrays - .asList(ISettingsPanel.PLAYER_COLORS)); + Set colors = new HashSet( + Arrays.asList(ISettingsPanel.PLAYER_COLORS)); for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) { colors.remove(player.getColor()); diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 86322b2..6ded0f3 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -53,6 +53,8 @@ public class ConnectionControl implements IConnectionControl { private Event2 changeColorEvent = new Event2(); + private Event gameStartEvent = new Event(); + private GameData currentGame; private volatile GameData offeredGame; @@ -128,6 +130,11 @@ public class ConnectionControl implements IConnectionControl { return changeColorEvent; } + @Override + public IEvent getGameStartEvent() { + return gameStartEvent; + } + @Override public void offerGame(GameData data) { offeredGame = data; @@ -221,6 +228,19 @@ public class ConnectionControl implements IConnectionControl { } + @Override + public void startGame() { + final UUID uuid = currentGame.getGameID(); + run(new SendRunner() { + @Override + protected void addData(DefaultPacketExtension extension) { + extension.setValue("messageType", "game_start"); + extension.setValue("uuid", uuid.toString()); + } + }); + + } + private void sendGameOffer() { final GameData data = offeredGame; run(new SendRunner() { @@ -315,6 +335,8 @@ public class ConnectionControl implements IConnectionControl { } else if (messageType.equals("change_color")) { changeColorEvent.emit(sender, (Color) Base64.decodeToObject(extension.getValue("color"))); + } else if (messageType.equals("game_start")) { + gameStartEvent.emit(); } else { System.err.println("Received unrecognized message of type '" + messageType + "'"); diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 184ce75..b9690b6 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -6,6 +6,7 @@ import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.util.GameData; +import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; @@ -47,6 +48,7 @@ public class GameJoinControl extends AbstractGameBeginControl { } } })); + connections.add(connectionControl.getGameWithdrawalEvent().add( new IListener1() { @Override @@ -57,6 +59,13 @@ public class GameJoinControl extends AbstractGameBeginControl { } } })); + + connections.add(connectionControl.getGameStartEvent().add(new IListener() { + @Override + public void handle() { + startGame(); + } + })); } private void fixGameSettings(GameSettings settings) { @@ -86,5 +95,4 @@ public class GameJoinControl extends AbstractGameBeginControl { connectionControl.leaveGame(); backEvent.emit(); } - } diff --git a/src/jrummikub/control/network/GameOfferControl.java b/src/jrummikub/control/network/GameOfferControl.java index 0c94c1f..8c5820d 100644 --- a/src/jrummikub/control/network/GameOfferControl.java +++ b/src/jrummikub/control/network/GameOfferControl.java @@ -7,6 +7,7 @@ import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.util.GameData; +import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; @@ -20,16 +21,15 @@ public class GameOfferControl extends AbstractGameBeginControl { * Creates new game offer control * * @param connectionControl - * for events (listening and handling) + * for events (listening and handling) * @param settings - * the game settings for player list, colors, names + * the game settings for player list, colors, names * @param view - * the view + * the view */ public GameOfferControl(final IConnectionControl connectionControl, final GameSettings settings, final IView view) { - super(connectionControl, view, - new GameData(UUID.randomUUID(), settings), + super(connectionControl, view, new GameData(UUID.randomUUID(), settings), SettingsMode.NETWORK_OFFER); connections.add(connectionControl.getGameJoinEvent().add( @@ -54,11 +54,10 @@ public class GameOfferControl extends AbstractGameBeginControl { new IListener1() { @Override public void handle(String sender) { - List players = gameData - .getGameSettings().getPlayerList(); + List players = gameData.getGameSettings() + .getPlayerList(); for (PlayerSettings s : players) { - if (s.getName().equals(sender) - && s.getType() == Type.NETWORK) { + if (s.getName().equals(sender) && s.getType() == Type.NETWORK) { s.setType(Type.VACANT); s.setName("Offen"); break; @@ -68,6 +67,27 @@ public class GameOfferControl extends AbstractGameBeginControl { connectionControl.offerGame(gameData); } })); + + connections.add(view.getSettingsPanel().getStartGameEvent() + .add(new IListener() { + @Override + public void handle() { + List players = gameData.getGameSettings() + .getPlayerList(); + for (PlayerSettings s : players) { + if (s.getType() == Type.NETWORK) { + startGame(); + return; + } + } + } + })); + } + + @Override + protected void startGame() { + super.startGame(); + connectionControl.startGame(); } /** diff --git a/src/jrummikub/control/network/IConnectionControl.java b/src/jrummikub/control/network/IConnectionControl.java index 6d0bab4..feacf19 100644 --- a/src/jrummikub/control/network/IConnectionControl.java +++ b/src/jrummikub/control/network/IConnectionControl.java @@ -32,6 +32,8 @@ interface IConnectionControl { public IEvent2 getChangeColorEvent(); + public IEvent getGameStartEvent(); + public void offerGame(GameData data); public void withdrawGame(); @@ -48,4 +50,6 @@ interface IConnectionControl { public void changeColor(final Color color); + public void startGame(); + } \ No newline at end of file -- cgit v1.2.3