diff options
4 files changed, 68 insertions, 56 deletions
diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 1844922..2d97558 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -48,12 +48,14 @@ class ConnectionControl { private Event1<GameData> gameOfferEvent = new Event1<GameData>(); private Event1<UUID> gameWithdrawalEvent = new Event1<UUID>(); - private Event2<UUID, String> gameJoinEvent = new Event2<UUID, String>(); - private Event2<UUID, String> gameLeaveEvent = new Event2<UUID, String>(); + private Event1<String> gameJoinEvent = new Event1<String>(); + private Event1<String> gameLeaveEvent = new Event1<String>(); - private Event2<UUID, Boolean> gameJoinAckEvent = new Event2<UUID, Boolean>(); + private Event1<Boolean> gameJoinAckEvent = new Event1<Boolean>(); - private Event3<UUID, String, Color> changeColorEvent = new Event3<UUID, String, Color>(); + private Event2<String, Color> changeColorEvent = new Event2<String, Color>(); + + private GameData currentGame; private volatile GameData offeredGame; @@ -92,44 +94,54 @@ class ConnectionControl { return gameWithdrawalEvent; } - IEvent2<UUID, String> getGameJoinEvent() { + IEvent1<String> getGameJoinEvent() { return gameJoinEvent; } - IEvent2<UUID, String> getGameLeaveEvent() { + IEvent1<String> getGameLeaveEvent() { return gameLeaveEvent; } - IEvent2<UUID, Boolean> getGameJoinAckEvent() { + IEvent1<Boolean> getGameJoinAckEvent() { return gameJoinAckEvent; } - IEvent3<UUID, String, Color> getChangeColorEvent() { + IEvent2<String, Color> getChangeColorEvent() { return changeColorEvent; } void offerGame(GameData data) { offeredGame = data; - + currentGame = data; sendGameOffer(); } - void withdrawGame(UUID uuid) { + void withdrawGame() { offeredGame = null; + currentGame = null; + new Thread(new SendGameWithdrawRunner(currentGame.getGameID())).start(); + } + + GameData getCurrentGame() { + return currentGame; + } - new Thread(new SendGameWithdrawRunner(uuid)).start(); + void setCurrentGame(GameData game) { + this.currentGame = game; } - void joinGame(UUID uuid) { - new Thread(new SendGameJoinRunner(uuid)).start(); + void joinGame(GameData game) { + setCurrentGame(game); + new Thread(new SendGameJoinRunner(currentGame.getGameID())).start(); } - void leaveGame(UUID uuid) { - new Thread(new SendGameLeaveRunner(uuid)).start(); + void leaveGame() { + currentGame = null; + new Thread(new SendGameLeaveRunner(currentGame.getGameID())).start(); } - void ackJoinGame(UUID uuid, String recipient, boolean ack) { - new Thread(new SendGameJoinAckRunner(uuid, recipient, ack)).start(); + void ackJoinGame(String recipient, boolean ack) { + new Thread(new SendGameJoinAckRunner(currentGame.getGameID(), recipient, ack)).start(); } private void sendGameOffer() { @@ -182,28 +194,31 @@ class ConnectionControl { GameData gameData = new GameData(uuid, settings, sender); gameOfferEvent.emit(gameData); } else if (messageType.equals("game_withdrawal")) { - gameWithdrawalEvent.emit(UUID - .fromString(extension.getValue("uuid"))); + gameWithdrawalEvent + .emit(UUID.fromString(extension.getValue("uuid"))); } else if (messageType.equals("game_request")) { if (offeredGame != null) { sendGameOffer(); } - } else if (messageType.equals("game_join")) { - gameJoinEvent.emit(UUID.fromString(extension.getValue("uuid")), - sender); - } else if (messageType.equals("game_leave")) { - gameLeaveEvent.emit(UUID.fromString(extension.getValue("uuid")), - sender); - } 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 + "'"); + } else if (currentGame != null) { + UUID uuid = UUID.fromString(extension.getValue("uuid")); + if (!currentGame.getGameID().equals(uuid)) { + return; + } + if (messageType.equals("game_join")) { + gameJoinEvent.emit(sender); + } else if (messageType.equals("game_leave")) { + gameLeaveEvent.emit(sender); + } else if (messageType.equals("game_join_ack")) { + gameJoinAckEvent + .emit(Boolean.valueOf(extension.getValue("ack"))); + } else if (messageType.equals("changeColor")) { + changeColorEvent.emit(sender, (Color) Base64 + .decodeToObject(extension.getValue("color"))); + } else { + System.err.println("Received unrecognized message of type '" + + messageType + "'"); + } } } @@ -212,8 +227,8 @@ class ConnectionControl { extension.setValue("messageType", "game_offer"); extension.setValue("uuid", data.getGameID().toString()); - extension.setValue("gameSettings", Base64.encodeObject(data - .getGameSettings(), Base64.GZIP)); + extension.setValue("gameSettings", + Base64.encodeObject(data.getGameSettings(), Base64.GZIP)); return createMessage(extension); } diff --git a/src/jrummikub/control/network/GameJoinControl.java b/src/jrummikub/control/network/GameJoinControl.java index 83df0ed..afb78d4 100644 --- a/src/jrummikub/control/network/GameJoinControl.java +++ b/src/jrummikub/control/network/GameJoinControl.java @@ -108,7 +108,7 @@ public class GameJoinControl { */ private void goBack() { abort(); - connectionControl.leaveGame(gameData.getGameID()); + connectionControl.leaveGame(); view.showSettingsPanel(false); backEvent.emit(); } diff --git a/src/jrummikub/control/network/GameOfferControl.java b/src/jrummikub/control/network/GameOfferControl.java index fd55fc1..f0dc526 100644 --- a/src/jrummikub/control/network/GameOfferControl.java +++ b/src/jrummikub/control/network/GameOfferControl.java @@ -14,6 +14,7 @@ import jrummikub.model.PlayerSettings.Type; import jrummikub.util.Connection; import jrummikub.util.GameData; import jrummikub.util.IListener; +import jrummikub.util.IListener1; import jrummikub.util.IListener2; import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel.SettingsMode; @@ -37,25 +38,21 @@ public class GameOfferControl { updateSettingsPanel(settings); connections.add(connectionControl.getGameJoinEvent().add( - new IListener2<UUID, String>() { + new IListener1<String>() { @Override - public void handle(UUID uuid, String sender) { - if (!uuid.equals(gameData.getGameID())) { - return; - } - + public void handle(String sender) { for (PlayerSettings player : settings.getPlayerList()) { if (player.getType() == Type.VACANT) { player.setName(sender); player.setType(Type.NETWORK); updateSettingsPanel(settings); - connectionControl.ackJoinGame(uuid, sender, true); + connectionControl.ackJoinGame(sender, true); connectionControl.offerGame(gameData); return; } } - connectionControl.ackJoinGame(uuid, sender, false); + connectionControl.ackJoinGame(sender, false); } })); connections.add(view.getSettingsPanel().getChangePlayerColorEvent() @@ -71,13 +68,13 @@ public class GameOfferControl { updateSettingsPanel(settings); } })); - connections.add(connectionControl.getGameLeaveEvent().add(new IListener2<UUID, String>() { + connections.add(connectionControl.getGameLeaveEvent().add(new IListener1<String>() { @Override - public void handle(UUID value1, String value2) { + public void handle(String sender) { List<PlayerSettings> players = gameData.getGameSettings().getPlayerList(); int index=0; for(PlayerSettings s:players){ - if (s.getName().equals(value2)){ + if (s.getName().equals(sender)){ break; } index++; @@ -122,6 +119,6 @@ public class GameOfferControl { } public void abort() { - connectionControl.withdrawGame(gameData.getGameID()); + connectionControl.withdrawGame(); } } diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java index 0ae3808..0e8381b 100644 --- a/src/jrummikub/control/network/NetworkControl.java +++ b/src/jrummikub/control/network/NetworkControl.java @@ -75,7 +75,7 @@ public class NetworkControl { private void join(GameData gameData) { view.showGameListPanel(false); - connectionControl.joinGame(gameData.getGameID()); + connectionControl.joinGame(gameData); } /** @@ -135,11 +135,11 @@ public class NetworkControl { } })); connections.add(connectionControl.getGameJoinAckEvent().add( - new IListener2<UUID, Boolean>() { + new IListener1<Boolean>() { @Override - public void handle(UUID uuid, Boolean ack) { + public void handle(Boolean ack) { if (ack) { - createGameJoinControl(uuid); + createGameJoinControl(); } else { // TODO Error message view.showGameListPanel(true); @@ -158,12 +158,12 @@ public class NetworkControl { view.getGameListPanel().setGameList(gameList); } - private void createGameJoinControl(UUID uuid) { + private void createGameJoinControl() { if (gameJoinControl != null) { return; } - GameData gameData = gameMap.get(uuid); + GameData gameData = connectionControl.getCurrentGame(); gameJoinControl = new GameJoinControl(connectionControl, gameData, view); gameJoinControl.getBackEvent().add(new IListener() { @Override |