summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/SettingsControl.java6
-rw-r--r--src/jrummikub/control/network/ConnectionControl.java38
-rw-r--r--src/jrummikub/control/network/GameJoinControl.java21
-rw-r--r--src/jrummikub/control/network/GameOfferControl.java35
4 files changed, 95 insertions, 5 deletions
diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java
index 4659828..e54fa6d 100644
--- a/src/jrummikub/control/SettingsControl.java
+++ b/src/jrummikub/control/SettingsControl.java
@@ -1,20 +1,21 @@
package jrummikub.control;
+import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
-import jrummikub.util.Connection;
import jrummikub.util.Event1;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.view.ISettingsPanel;
-import jrummikub.view.IView;
import jrummikub.view.ISettingsPanel.SettingsMode;
+import jrummikub.view.IView;
/**
* The settings control controls the settings panel
@@ -35,6 +36,7 @@ public class SettingsControl extends AbstractSettingsControl {
view.getSettingsPanel().setSettingsMode(SettingsMode.DEFAULT);
view.getSettingsPanel().setPlayerNamesEditable(
Collections.<Boolean> emptyList());
+ view.getSettingsPanel().setPlayerColors(new HashSet<Color>(Arrays.asList(ISettingsPanel.PLAYER_COLORS)));
addPlayer();
addPlayer();
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<UUID, Boolean> gameJoinAckEvent = new Event2<UUID, Boolean>();
+ private Event3<UUID, String, Color> changeColorEvent = new Event3<UUID, String, Color>();
+
private volatile GameData offeredGame;
ConnectionControl(LoginData loginData) {
@@ -99,6 +104,10 @@ class ConnectionControl {
return gameJoinAckEvent;
}
+ IEvent3<UUID, String, Color> 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<Color> colors = new HashSet<Color>(
+ 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<UUID, String>() {
@@ -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<Integer, Color>() {
+ @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<Color> colors = new HashSet<Color>(
+ Arrays.asList(ISettingsPanel.PLAYER_COLORS));
+
+ for (PlayerSettings player : settings.getPlayerList()) {
+ colors.remove(player.getColor());
+ }
+
+ view.getSettingsPanel().setPlayerColors(colors);
}
public void startGameOffer() {