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 MockEvent gameStartEvent = new MockEvent();
/** */
public GameData currentGame;
/** */
public GameData offeredGame;
@ -104,6 +106,11 @@ public class MockConnectionControl implements IConnectionControl {
return changeColorEvent;
}
@Override
public IEvent getGameStartEvent() {
return gameStartEvent;
}
@Override
public void offerGame(GameData data) {
// TODO Auto-generated method stub
@ -146,4 +153,10 @@ public class MockConnectionControl implements IConnectionControl {
public void changeColor(Color 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.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
@ -19,7 +22,7 @@ import jrummikub.view.ISettingsPanel.SettingsMode;
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
*/
public abstract class AbstractGameBeginControl {
@ -28,18 +31,19 @@ public abstract class AbstractGameBeginControl {
protected IConnectionControl connectionControl;
protected IView view;
protected Event backEvent = new Event();
private Event1<GameData> gameStartEvent = new Event1<GameData>();
/**
* Create a new game begin control
*
* @param connection
* connection control for mesages and events
* connection control for messages and events
* @param view
* the view
* the view
* @param gameData
* game data of chosen game
* game data of chosen game
* @param settingsMode
* mode of settings panel
* mode of settings panel
*/
public AbstractGameBeginControl(IConnectionControl connection, IView view,
final GameData gameData, SettingsMode settingsMode) {
@ -57,11 +61,10 @@ public abstract class AbstractGameBeginControl {
new IListener2<String, Color>() {
@Override
public void handle(String sender, Color color) {
List<PlayerSettings> players = gameData
.getGameSettings().getPlayerList();
List<PlayerSettings> players = gameData.getGameSettings()
.getPlayerList();
for (PlayerSettings s : players) {
if (s.getName().equals(sender)
&& s.getType() == Type.NETWORK) {
if (s.getName().equals(sender) && s.getType() == Type.NETWORK) {
s.setColor(color);
break;
}
@ -72,13 +75,12 @@ public abstract class AbstractGameBeginControl {
}
private void addViewListeners(IView view, final GameData gameData) {
connections.add(view.getSettingsPanel().getBackEvent().add(
new IListener() {
@Override
public void handle() {
goBack();
}
}));
connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() {
@Override
public void handle() {
goBack();
}
}));
connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
.add(new IListener2<Integer, Color>() {
@Override
@ -89,8 +91,8 @@ public abstract class AbstractGameBeginControl {
return;
}
}
PlayerSettings player = gameData.getGameSettings()
.getPlayerList().get(i);
PlayerSettings player = gameData.getGameSettings().getPlayerList()
.get(i);
if (player.getType() != Type.HUMAN) {
return;
}
@ -104,15 +106,29 @@ public abstract class AbstractGameBeginControl {
protected abstract void goBack();
/**
* The back event is emitted when the player wants to go back to the
* previous control and panel
* The back event is emitted when the player wants to go back to the previous
* control and panel
*
* @return the event
*/
public Event getBackEvent() {
public IEvent getBackEvent() {
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() {
view.showSettingsPanel(false);
for (Connection c : connections) {
@ -123,8 +139,8 @@ public abstract class AbstractGameBeginControl {
protected void updateSettingsPanel() {
view.getSettingsPanel().setGameSettings(gameData.getGameSettings());
Set<Color> colors = new HashSet<Color>(Arrays
.asList(ISettingsPanel.PLAYER_COLORS));
Set<Color> colors = new HashSet<Color>(
Arrays.asList(ISettingsPanel.PLAYER_COLORS));
for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
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 Event gameStartEvent = new Event();
private GameData currentGame;
private volatile GameData offeredGame;
@ -128,6 +130,11 @@ public class ConnectionControl implements IConnectionControl {
return changeColorEvent;
}
@Override
public IEvent getGameStartEvent() {
return gameStartEvent;
}
@Override
public void offerGame(GameData 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() {
final GameData data = offeredGame;
run(new SendRunner() {
@ -315,6 +335,8 @@ public class ConnectionControl implements IConnectionControl {
} else if (messageType.equals("change_color")) {
changeColorEvent.emit(sender,
(Color) Base64.decodeToObject(extension.getValue("color")));
} else if (messageType.equals("game_start")) {
gameStartEvent.emit();
} else {
System.err.println("Received unrecognized message of type '"
+ messageType + "'");

View file

@ -6,6 +6,7 @@ import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.GameData;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;
@ -47,6 +48,7 @@ public class GameJoinControl extends AbstractGameBeginControl {
}
}
}));
connections.add(connectionControl.getGameWithdrawalEvent().add(
new IListener1<UUID>() {
@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) {
@ -86,5 +95,4 @@ public class GameJoinControl extends AbstractGameBeginControl {
connectionControl.leaveGame();
backEvent.emit();
}
}

View file

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