Start game button in a network game sends an event

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@479 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-06-19 02:47:38 +02:00
parent 4ddf87fc96
commit bf24a9279a
6 changed files with 116 additions and 33 deletions

View file

@ -32,6 +32,8 @@ public class MockConnectionControl implements IConnectionControl {
/** */ /** */
public MockEvent2<String, Color> changeColorEvent = new MockEvent2<String, Color>(); public MockEvent2<String, Color> changeColorEvent = new MockEvent2<String, Color>();
/** */ /** */
public MockEvent gameStartEvent = new MockEvent();
/** */
public GameData currentGame; public GameData currentGame;
/** */ /** */
public GameData offeredGame; public GameData offeredGame;
@ -104,6 +106,11 @@ public class MockConnectionControl implements IConnectionControl {
return changeColorEvent; return changeColorEvent;
} }
@Override
public IEvent getGameStartEvent() {
return gameStartEvent;
}
@Override @Override
public void offerGame(GameData data) { public void offerGame(GameData data) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -146,4 +153,10 @@ public class MockConnectionControl implements IConnectionControl {
public void changeColor(Color color) { public void changeColor(Color color) {
playerColor = color; playerColor = color;
} }
@Override
public void startGame() {
// TODO Auto-generated method stub
}
} }

View file

@ -11,7 +11,10 @@ import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type; import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Connection; import jrummikub.util.Connection;
import jrummikub.util.Event; import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.GameData; import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener; import jrummikub.util.IListener;
import jrummikub.util.IListener2; import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel; import jrummikub.view.ISettingsPanel;
@ -19,7 +22,7 @@ import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView; import jrummikub.view.IView;
/** /**
* Abstract class for network game controls inbetween choosing and starting a * Abstract class for network game controls in between choosing and starting a
* game * game
*/ */
public abstract class AbstractGameBeginControl { public abstract class AbstractGameBeginControl {
@ -28,18 +31,19 @@ public abstract class AbstractGameBeginControl {
protected IConnectionControl connectionControl; protected IConnectionControl connectionControl;
protected IView view; protected IView view;
protected Event backEvent = new Event(); protected Event backEvent = new Event();
private Event1<GameData> gameStartEvent = new Event1<GameData>();
/** /**
* Create a new game begin control * Create a new game begin control
* *
* @param connection * @param connection
* connection control for mesages and events * connection control for messages and events
* @param view * @param view
* the view * the view
* @param gameData * @param gameData
* game data of chosen game * game data of chosen game
* @param settingsMode * @param settingsMode
* mode of settings panel * mode of settings panel
*/ */
public AbstractGameBeginControl(IConnectionControl connection, IView view, public AbstractGameBeginControl(IConnectionControl connection, IView view,
final GameData gameData, SettingsMode settingsMode) { final GameData gameData, SettingsMode settingsMode) {
@ -57,11 +61,10 @@ public abstract class AbstractGameBeginControl {
new IListener2<String, Color>() { new IListener2<String, Color>() {
@Override @Override
public void handle(String sender, Color color) { public void handle(String sender, Color color) {
List<PlayerSettings> players = gameData List<PlayerSettings> players = gameData.getGameSettings()
.getGameSettings().getPlayerList(); .getPlayerList();
for (PlayerSettings s : players) { for (PlayerSettings s : players) {
if (s.getName().equals(sender) if (s.getName().equals(sender) && s.getType() == Type.NETWORK) {
&& s.getType() == Type.NETWORK) {
s.setColor(color); s.setColor(color);
break; break;
} }
@ -72,13 +75,12 @@ public abstract class AbstractGameBeginControl {
} }
private void addViewListeners(IView view, final GameData gameData) { private void addViewListeners(IView view, final GameData gameData) {
connections.add(view.getSettingsPanel().getBackEvent().add( connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() {
new IListener() { @Override
@Override public void handle() {
public void handle() { goBack();
goBack(); }
} }));
}));
connections.add(view.getSettingsPanel().getChangePlayerColorEvent() connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
.add(new IListener2<Integer, Color>() { .add(new IListener2<Integer, Color>() {
@Override @Override
@ -89,8 +91,8 @@ public abstract class AbstractGameBeginControl {
return; return;
} }
} }
PlayerSettings player = gameData.getGameSettings() PlayerSettings player = gameData.getGameSettings().getPlayerList()
.getPlayerList().get(i); .get(i);
if (player.getType() != Type.HUMAN) { if (player.getType() != Type.HUMAN) {
return; return;
} }
@ -104,15 +106,29 @@ public abstract class AbstractGameBeginControl {
protected abstract void goBack(); protected abstract void goBack();
/** /**
* The back event is emitted when the player wants to go back to the * The back event is emitted when the player wants to go back to the previous
* previous control and panel * control and panel
* *
* @return the event * @return the event
*/ */
public Event getBackEvent() { public IEvent getBackEvent() {
return backEvent; return backEvent;
} }
/**
* The event that is emitted when the game is started
*
* @return the event
*/
public IEvent1<GameData> getStartTurnEvent() {
return gameStartEvent;
}
protected void startGame() {
abort();
gameStartEvent.emit(gameData);
}
protected void abort() { protected void abort() {
view.showSettingsPanel(false); view.showSettingsPanel(false);
for (Connection c : connections) { for (Connection c : connections) {
@ -123,8 +139,8 @@ public abstract class AbstractGameBeginControl {
protected void updateSettingsPanel() { protected void updateSettingsPanel() {
view.getSettingsPanel().setGameSettings(gameData.getGameSettings()); view.getSettingsPanel().setGameSettings(gameData.getGameSettings());
Set<Color> colors = new HashSet<Color>(Arrays Set<Color> colors = new HashSet<Color>(
.asList(ISettingsPanel.PLAYER_COLORS)); Arrays.asList(ISettingsPanel.PLAYER_COLORS));
for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) { for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
colors.remove(player.getColor()); colors.remove(player.getColor());

View file

@ -53,6 +53,8 @@ public class ConnectionControl implements IConnectionControl {
private Event2<String, Color> changeColorEvent = new Event2<String, Color>(); private Event2<String, Color> changeColorEvent = new Event2<String, Color>();
private Event gameStartEvent = new Event();
private GameData currentGame; private GameData currentGame;
private volatile GameData offeredGame; private volatile GameData offeredGame;
@ -128,6 +130,11 @@ public class ConnectionControl implements IConnectionControl {
return changeColorEvent; return changeColorEvent;
} }
@Override
public IEvent getGameStartEvent() {
return gameStartEvent;
}
@Override @Override
public void offerGame(GameData data) { public void offerGame(GameData data) {
offeredGame = data; offeredGame = data;
@ -221,6 +228,19 @@ public class ConnectionControl implements IConnectionControl {
} }
@Override
public void startGame() {
final UUID uuid = currentGame.getGameID();
run(new SendRunner() {
@Override
protected void addData(DefaultPacketExtension extension) {
extension.setValue("messageType", "game_start");
extension.setValue("uuid", uuid.toString());
}
});
}
private void sendGameOffer() { private void sendGameOffer() {
final GameData data = offeredGame; final GameData data = offeredGame;
run(new SendRunner() { run(new SendRunner() {
@ -315,6 +335,8 @@ public class ConnectionControl implements IConnectionControl {
} else if (messageType.equals("change_color")) { } else if (messageType.equals("change_color")) {
changeColorEvent.emit(sender, changeColorEvent.emit(sender,
(Color) Base64.decodeToObject(extension.getValue("color"))); (Color) Base64.decodeToObject(extension.getValue("color")));
} else if (messageType.equals("game_start")) {
gameStartEvent.emit();
} else { } else {
System.err.println("Received unrecognized message of type '" System.err.println("Received unrecognized message of type '"
+ messageType + "'"); + messageType + "'");

View file

@ -6,6 +6,7 @@ import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type; import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.GameData; import jrummikub.util.GameData;
import jrummikub.util.IListener;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView; import jrummikub.view.IView;
@ -47,6 +48,7 @@ public class GameJoinControl extends AbstractGameBeginControl {
} }
} }
})); }));
connections.add(connectionControl.getGameWithdrawalEvent().add( connections.add(connectionControl.getGameWithdrawalEvent().add(
new IListener1<UUID>() { new IListener1<UUID>() {
@Override @Override
@ -57,6 +59,13 @@ public class GameJoinControl extends AbstractGameBeginControl {
} }
} }
})); }));
connections.add(connectionControl.getGameStartEvent().add(new IListener() {
@Override
public void handle() {
startGame();
}
}));
} }
private void fixGameSettings(GameSettings settings) { private void fixGameSettings(GameSettings settings) {
@ -86,5 +95,4 @@ public class GameJoinControl extends AbstractGameBeginControl {
connectionControl.leaveGame(); connectionControl.leaveGame();
backEvent.emit(); backEvent.emit();
} }
} }

View file

@ -7,6 +7,7 @@ import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type; import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.GameData; import jrummikub.util.GameData;
import jrummikub.util.IListener;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode; import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView; import jrummikub.view.IView;
@ -20,16 +21,15 @@ public class GameOfferControl extends AbstractGameBeginControl {
* Creates new game offer control * Creates new game offer control
* *
* @param connectionControl * @param connectionControl
* for events (listening and handling) * for events (listening and handling)
* @param settings * @param settings
* the game settings for player list, colors, names * the game settings for player list, colors, names
* @param view * @param view
* the view * the view
*/ */
public GameOfferControl(final IConnectionControl connectionControl, public GameOfferControl(final IConnectionControl connectionControl,
final GameSettings settings, final IView view) { final GameSettings settings, final IView view) {
super(connectionControl, view, super(connectionControl, view, new GameData(UUID.randomUUID(), settings),
new GameData(UUID.randomUUID(), settings),
SettingsMode.NETWORK_OFFER); SettingsMode.NETWORK_OFFER);
connections.add(connectionControl.getGameJoinEvent().add( connections.add(connectionControl.getGameJoinEvent().add(
@ -54,11 +54,10 @@ public class GameOfferControl extends AbstractGameBeginControl {
new IListener1<String>() { new IListener1<String>() {
@Override @Override
public void handle(String sender) { public void handle(String sender) {
List<PlayerSettings> players = gameData List<PlayerSettings> players = gameData.getGameSettings()
.getGameSettings().getPlayerList(); .getPlayerList();
for (PlayerSettings s : players) { for (PlayerSettings s : players) {
if (s.getName().equals(sender) if (s.getName().equals(sender) && s.getType() == Type.NETWORK) {
&& s.getType() == Type.NETWORK) {
s.setType(Type.VACANT); s.setType(Type.VACANT);
s.setName("Offen"); s.setName("Offen");
break; break;
@ -68,6 +67,27 @@ public class GameOfferControl extends AbstractGameBeginControl {
connectionControl.offerGame(gameData); connectionControl.offerGame(gameData);
} }
})); }));
connections.add(view.getSettingsPanel().getStartGameEvent()
.add(new IListener() {
@Override
public void handle() {
List<PlayerSettings> players = gameData.getGameSettings()
.getPlayerList();
for (PlayerSettings s : players) {
if (s.getType() == Type.NETWORK) {
startGame();
return;
}
}
}
}));
}
@Override
protected void startGame() {
super.startGame();
connectionControl.startGame();
} }
/** /**

View file

@ -32,6 +32,8 @@ interface IConnectionControl {
public IEvent2<String, Color> getChangeColorEvent(); public IEvent2<String, Color> getChangeColorEvent();
public IEvent getGameStartEvent();
public void offerGame(GameData data); public void offerGame(GameData data);
public void withdrawGame(); public void withdrawGame();
@ -48,4 +50,6 @@ interface IConnectionControl {
public void changeColor(final Color color); public void changeColor(final Color color);
public void startGame();
} }