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 --- src/jrummikub/control/RoundControl.java | 14 ++++--- .../control/network/ConnectionControl.java | 43 ++++++++++++++++++++++ .../control/network/IConnectionControl.java | 9 ++++- .../control/network/NetworkRoundControl.java | 31 ++++++++++++++++ 4 files changed, 91 insertions(+), 6 deletions(-) (limited to 'src/jrummikub') diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index aeff095..71980e1 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -264,7 +264,7 @@ public class RoundControl { return TurnControlFactory.getFactory(type).create(); } - void deal() { + protected void deal() { for (int i = 0; i < roundState.getPlayerCount(); i++) { IHand hand = roundState.getNthNextPlayer(i).getHand(); for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) { @@ -276,8 +276,9 @@ public class RoundControl { } protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) { - boolean wasHuman = (turnControl instanceof HumanTurnControl); - boolean wasAI = (turnControl instanceof AIControl); + boolean wasAI = turnControl instanceof AIControl; + boolean wasHuman = turnControl instanceof HumanTurnControl; + turnControl = null; view.getTablePanel().setStoneSets(invalidTurnInfo.getTable()); @@ -314,10 +315,13 @@ public class RoundControl { } view.setBottomPanel(BottomPanelType.NONHUMAN_HAND_PANEL); - nextPlayer(); + + if (wasHuman || wasAI) { + nextPlayer(); + } } - private void nextPlayer() { + protected void nextPlayer() { view.setSelectedStones(Collections. emptyList()); view.setInvalidStoneSets(Collections. emptyList()); view.setStoneCollectionHidden(false); 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