From 0b1151e6af8ce156d4ad5d6153cb5a02e99329d7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 21 Jun 2011 01:24:10 +0200 Subject: Fix network mode git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@521 72836036-5685-4462-b002-a69064685172 --- .../control/network/ConnectionControl.java | 43 ++++++++++++++++++++++ .../control/network/IConnectionControl.java | 9 ++++- .../control/network/NetworkRoundControl.java | 31 ++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) (limited to 'src/jrummikub/control/network') diff --git a/src/jrummikub/control/network/ConnectionControl.java b/src/jrummikub/control/network/ConnectionControl.java index 4ed160a..c63259d 100644 --- a/src/jrummikub/control/network/ConnectionControl.java +++ b/src/jrummikub/control/network/ConnectionControl.java @@ -85,9 +85,11 @@ public class ConnectionControl implements IConnectionControl { private Event gameStartEvent = new Event(); private Event roundStartEvent = new Event(); + private Event1 roundStateUpdateEvent = new Event1(); private Event1 tableUpdateEvent = new Event1(); private Event2 turnEndEvent = new Event2(); + private Event nextPlayerEvent = new Event(); private Event turnStartEvent = new Event(); private GameData currentGame; @@ -181,6 +183,11 @@ public class ConnectionControl implements IConnectionControl { return roundStartEvent; } + @Override + public IEvent1 getRoundStateUpdateEvent() { + return roundStateUpdateEvent; + } + @Override public IEvent1 getTableUpdateEvent() { return tableUpdateEvent; @@ -191,6 +198,11 @@ public class ConnectionControl implements IConnectionControl { return turnEndEvent; } + @Override + public IEvent getNextPlayerEvent() { + return nextPlayerEvent; + } + @Override public IEvent getTurnStartEvent() { return turnStartEvent; @@ -312,6 +324,20 @@ public class ConnectionControl implements IConnectionControl { }); } + @Override + public void updateRoundState(final IRoundState roundState) { + final UUID uuid = currentGame.getGameID(); + run(new SendRunner() { + @Override + protected void addData(DefaultPacketExtension extension) { + extension.setValue("messageType", "round_state_update"); + extension.setValue("uuid", uuid.toString()); + extension.setValue("state", + Base64.encodeObject(roundState, Base64.GZIP)); + } + }); + } + @Override public void updateTable(final ITable table) { final UUID uuid = currentGame.getGameID(); @@ -340,6 +366,18 @@ public class ConnectionControl implements IConnectionControl { }); } + @Override + public void nextPlayer() { + final UUID uuid = currentGame.getGameID(); + run(new SendRunner() { + @Override + protected void addData(DefaultPacketExtension extension) { + extension.setValue("messageType", "next_player"); + extension.setValue("uuid", uuid.toString()); + } + }); + } + @Override public void startTurn() { final UUID uuid = currentGame.getGameID(); @@ -459,6 +497,9 @@ public class ConnectionControl implements IConnectionControl { gameStartEvent.emit(); } else if (messageType.equals("round_start")) { roundStartEvent.emit(); + } else if (messageType.equals("round_state_update")) { + roundStateUpdateEvent.emit((IRoundState) Base64.decodeToObject(extension + .getValue("state"))); } else if (messageType.equals("table_update")) { tableUpdateEvent.emit((ITable) Base64.decodeToObject(extension .getValue("table"))); @@ -466,6 +507,8 @@ public class ConnectionControl implements IConnectionControl { TurnEndData data = (TurnEndData) Base64.decodeToObject(extension .getValue("data")); turnEndEvent.emit(data.getRoundState(), data.getInvalidTurnInfo()); + } else if (messageType.equals("next_player")) { + nextPlayerEvent.emit(); } else if (messageType.equals("turn_start")) { turnStartEvent.emit(); } else { diff --git a/src/jrummikub/control/network/IConnectionControl.java b/src/jrummikub/control/network/IConnectionControl.java index b93748c..667a4f9 100644 --- a/src/jrummikub/control/network/IConnectionControl.java +++ b/src/jrummikub/control/network/IConnectionControl.java @@ -40,10 +40,14 @@ interface IConnectionControl { public IEvent getRoundStartEvent(); + public IEvent1 getRoundStateUpdateEvent(); + public IEvent1 getTableUpdateEvent(); public IEvent2 getTurnEndEvent(); + public IEvent getNextPlayerEvent(); + public IEvent getTurnStartEvent(); public void offerGame(GameData data); @@ -66,10 +70,13 @@ interface IConnectionControl { public void startRound(); + public void updateRoundState(IRoundState roundState); + public void updateTable(ITable table); public void endTurn(IRoundState state, InvalidTurnInfo invalidTurnInfo); - public void startTurn(); + public void nextPlayer(); + public void startTurn(); } \ No newline at end of file diff --git a/src/jrummikub/control/network/NetworkRoundControl.java b/src/jrummikub/control/network/NetworkRoundControl.java index 64faebb..d21e8e8 100644 --- a/src/jrummikub/control/network/NetworkRoundControl.java +++ b/src/jrummikub/control/network/NetworkRoundControl.java @@ -20,12 +20,27 @@ public class NetworkRoundControl extends RoundControl { this.connectionControl = connectionControl; currentlyActive = startActive; + connections.add(connectionControl.getRoundStateUpdateEvent().add( + new IListener1() { + @Override + public void handle(IRoundState state) { + NetworkControl.fixGameSettings(state.getGameSettings(), + connectionControl.getNickname()); + setRoundState(state); + } + })); connections.add(connectionControl.getTurnStartEvent().add(new IListener() { @Override public void handle() { startTurn(); } })); + connections.add(connectionControl.getNextPlayerEvent().add(new IListener() { + @Override + public void handle() { + NetworkRoundControl.super.nextPlayer(); + } + })); } @Override @@ -67,6 +82,22 @@ public class NetworkRoundControl extends RoundControl { } } + @Override + protected void deal() { + super.deal(); + + if (currentlyActive) { + connectionControl.updateRoundState(roundState); + } + } + + @Override + protected void nextPlayer() { + if (currentlyActive) { + connectionControl.nextPlayer(); + } + } + @Override protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) { if (currentlyActive) { -- cgit v1.2.3