From 1b9c7c47783a0872ca3bedfad6fb120f611d354b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 15 Jun 2011 19:02:13 +0200 Subject: Start implementing color changing over network git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@442 72836036-5685-4462-b002-a69064685172 --- .../control/network/ConnectionControl.java | 38 ++++++++++++++++++++++ src/jrummikub/control/network/GameJoinControl.java | 21 +++++++++++- .../control/network/GameOfferControl.java | 35 ++++++++++++++++++-- 3 files changed, 91 insertions(+), 3 deletions(-) (limited to 'src/jrummikub/control/network') diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index d6bb5fe..6cd3fc0 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -1,5 +1,6 @@ package jrummikub.control.network; +import java.awt.Color; import java.util.UUID; import javax.swing.SwingUtilities; @@ -8,10 +9,12 @@ import jrummikub.model.GameSettings; import jrummikub.util.Event; import jrummikub.util.Event1; import jrummikub.util.Event2; +import jrummikub.util.Event3; import jrummikub.util.GameData; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; +import jrummikub.util.IEvent3; import jrummikub.util.LoginData; import org.jivesoftware.smack.Connection; @@ -50,6 +53,8 @@ class ConnectionControl { private Event2 gameJoinAckEvent = new Event2(); + private Event3 changeColorEvent = new Event3(); + private volatile GameData offeredGame; ConnectionControl(LoginData loginData) { @@ -99,6 +104,10 @@ class ConnectionControl { return gameJoinAckEvent; } + IEvent3 getChangeColorEvent() { + return changeColorEvent; + } + void offerGame(GameData data) { offeredGame = data; @@ -181,6 +190,9 @@ class ConnectionControl { } else if (messageType.equals("game_join_ack")) { gameJoinAckEvent.emit(UUID.fromString(extension.getValue("uuid")), Boolean.valueOf(extension.getValue("ack"))); + } else if (messageType.equals("changeColor")) { + changeColorEvent.emit(UUID.fromString(extension.getValue("uuid")), + sender, (Color) Base64.decodeToObject(extension.getValue("color"))); } else { System.err.println("Received unrecognized message of type '" + messageType + "'"); @@ -225,6 +237,15 @@ class ConnectionControl { return createMessage(extension); } + private Message createChangeColorMessage(Color color) { + DefaultPacketExtension extension = createJRummikubExtension(); + + extension.setValue("messageType", "change_color"); + extension.setValue("color", Base64.encodeObject(color, Base64.GZIP)); + + return createMessage(extension); + } + private Message createGameAckMessage(UUID uuid, String recipient, boolean ack) { DefaultPacketExtension extension = createJRummikubExtension(); @@ -376,6 +397,23 @@ class ConnectionControl { } } + private class SendChangeColorRunner implements Runnable { + private Color color; + + public SendChangeColorRunner(Color color) { + this.color = color; + } + + @Override + public void run() { + synchronized (ConnectionControl.this) { + if (connection != null) { + connection.sendPacket(createChangeColorMessage(color)); + } + } + } + } + private class SendGameLeaveRunner implements Runnable { private UUID uuid; diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 048491a..55f584d 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -1,7 +1,11 @@ package jrummikub.control.network; +import java.awt.Color; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; @@ -9,6 +13,7 @@ import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.GameData; import jrummikub.util.IListener1; +import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; @@ -38,12 +43,26 @@ public class GameJoinControl { fixGameSettings(settings); gameData.setGameSettings(settings); - view.getSettingsPanel().setGameSettings(settings); + + updateSettingsPanel(settings); } } })); } + private void updateSettingsPanel(GameSettings settings) { + view.getSettingsPanel().setGameSettings(settings); + + Set colors = new HashSet( + Arrays.asList(ISettingsPanel.PLAYER_COLORS)); + + for (PlayerSettings player : settings.getPlayerList()) { + colors.remove(player.getColor()); + } + + view.getSettingsPanel().setPlayerColors(colors); + } + 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 7cd289f..247cd18 100644 --- a/src/jrummikub/control/network/GameOfferControl.java +++ b/src/jrummikub/control/network/GameOfferControl.java @@ -1,7 +1,11 @@ package jrummikub.control.network; +import java.awt.Color; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.UUID; import jrummikub.model.GameSettings; @@ -10,6 +14,7 @@ import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.GameData; import jrummikub.util.IListener2; +import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.IView; @@ -28,7 +33,7 @@ public class GameOfferControl { view.getSettingsPanel().setSettingsMode(SettingsMode.NETWORK_OFFER); view.getSettingsPanel().enableAddPlayerButton(false); - view.getSettingsPanel().setGameSettings(settings); + updateSettingsPanel(settings); connections.add(connectionControl.getGameJoinEvent().add( new IListener2() { @@ -42,7 +47,7 @@ public class GameOfferControl { if (player.getType() == Type.VACANT) { player.setName(sender); player.setType(Type.NETWORK); - view.getSettingsPanel().setGameSettings(settings); + updateSettingsPanel(settings); connectionControl.ackJoinGame(uuid, sender, true); connectionControl.offerGame(gameData); return; @@ -52,6 +57,32 @@ public class GameOfferControl { connectionControl.ackJoinGame(uuid, sender, false); } })); + connections.add(view.getSettingsPanel().getChangePlayerColorEvent() + .add(new IListener2() { + @Override + public void handle(Integer i, Color color) { + for (PlayerSettings player : settings.getPlayerList()) { + if (player.getColor() == color) { + return; + } + } + settings.getPlayerList().get(i).setColor(color); + updateSettingsPanel(settings); + } + })); + } + + private void updateSettingsPanel(GameSettings settings) { + view.getSettingsPanel().setGameSettings(settings); + + Set colors = new HashSet( + Arrays.asList(ISettingsPanel.PLAYER_COLORS)); + + for (PlayerSettings player : settings.getPlayerList()) { + colors.remove(player.getColor()); + } + + view.getSettingsPanel().setPlayerColors(colors); } public void startGameOffer() { -- cgit v1.2.3