Added NetworkGameControl

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@503 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-20 03:59:04 +02:00
parent e79295f271
commit f3f8ffe462
22 changed files with 227 additions and 83 deletions

View file

@ -206,7 +206,7 @@ public class ApplicationControl {
private void createNetworkControl(LoginData loginData) {
ConnectionControl connectionControl = new ConnectionControl(loginData);
networkControl = new NetworkControl(loginData, connectionControl, view);
networkControl = new NetworkControl(loginData, connectionControl, saveControl, view);
networkControl.getStopNetworkEvent().add(new IListener() {
@Override

View file

@ -20,24 +20,24 @@ import jrummikub.view.IView.BottomPanelType;
* Controls a Game, at some point including all Rounds, starts new Rounds
*/
public class GameControl {
private SaveControl saveControl;
protected SaveControl saveControl;
protected GameSettings gameSettings;
protected IView view;
protected RoundControl roundControl;
protected GameState gameState;
protected List<Connection> connections = new ArrayList<Connection>();
private GameSettings gameSettings;
private IView view;
RoundControl roundControl;
private GameState gameState;
private List<Connection> connections = new ArrayList<Connection>();
private Event endOfGameEvent = new Event();
/**
* Constructor
*
* @param gameSettings
* the game settings
* the game settings
* @param saveControl
* the save control
* the save control
* @param view
* the view
* the view
*/
public GameControl(GameSettings gameSettings, SaveControl saveControl,
IView view) {
@ -45,11 +45,13 @@ public class GameControl {
this.saveControl = saveControl;
this.view = view;
gameState = new GameState();
saveControl.setGameState(gameState);
if (gameSettings != null) {
gameState = new GameState();
saveControl.setGameState(gameState);
gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
.getPlayerList().size()));
gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
.getPlayerList().size()));
}
connections.add(view.getNewRoundEvent().add(new IListener() {
@Override
@ -104,9 +106,9 @@ public class GameControl {
* Continues game after loading
*
* @param gameState
* the saved GameState (Players, startplayer, points)
* the saved GameState (Players, startplayer, points)
* @param roundState
* the saved RoundState (activePlayer, Table, heap etc)
* the saved RoundState (activePlayer, Table, heap etc)
*/
public void continueGame(GameState gameState, IRoundState roundState) {
this.gameState = gameState;
@ -133,14 +135,14 @@ public class GameControl {
view.clearView();
}
private void startRound() {
protected void startRound() {
if (roundControl != null) {
return;
}
view.showScorePanel(false);
IRoundState roundState = new RoundState(gameSettings);
IRoundState roundState = createRoundState();
prepareRound(roundState);
roundControl.startRound();
}
@ -148,10 +150,12 @@ public class GameControl {
private void prepareRound(IRoundState roundState) {
saveControl.setRoundState(roundState);
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
+ gameState.getScores().size());
if (roundState != null) {
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
+ gameState.getScores().size());
}
roundControl = new RoundControl(roundState, view);
roundControl = createRoundControl(roundState);
roundControl.getEndOfRoundEvent().add(new IListener1<Score>() {
@Override
@ -169,6 +173,14 @@ public class GameControl {
});
}
protected IRoundState createRoundState() {
return new RoundState(gameSettings, gameState);
}
protected RoundControl createRoundControl(IRoundState roundState) {
return new RoundControl(roundState, view);
}
private void restartRound() {
roundControl = null;
startRound();
@ -188,8 +200,7 @@ public class GameControl {
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
view.getScorePanel().setScores(gameState.getScores());
view.getScorePanel().setAccumulatedScore(
gameState.getAccumulatedScore());
view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
view.getScorePanel().update();
view.showScorePanel(true);
}

View file

@ -244,6 +244,7 @@ public class RoundControl {
}
}
view.getSidePanel().setHeapSize(roundState.getGameHeap().getSize());
}
private boolean laidOutValidPoints() {

View file

@ -11,10 +11,8 @@ 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;
@ -31,7 +29,6 @@ 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
@ -115,20 +112,6 @@ public abstract class AbstractGameBeginControl {
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) {

View file

@ -60,6 +60,7 @@ public class ConnectionControl implements IConnectionControl {
private Event2<String, Color> changeColorEvent = new Event2<String, Color>();
private Event gameStartEvent = new Event();
private Event roundStartEvent = new Event();
private Event1<ITable> tableUpdateEvent = new Event1<ITable>();
private Event1<ITable> turnEndEvent = new Event1<ITable>();
@ -151,6 +152,11 @@ public class ConnectionControl implements IConnectionControl {
return gameStartEvent;
}
@Override
public IEvent getRoundStartEvent() {
return roundStartEvent;
}
@Override
public IEvent1<ITable> getTableUpdateEvent() {
return tableUpdateEvent;
@ -270,6 +276,18 @@ public class ConnectionControl implements IConnectionControl {
});
}
@Override
public void startRound() {
final UUID uuid = currentGame.getGameID();
run(new SendRunner() {
@Override
protected void addData(DefaultPacketExtension extension) {
extension.setValue("messageType", "round_start");
extension.setValue("uuid", uuid.toString());
}
});
}
@Override
public void updateTable(final ITable table) {
final UUID uuid = currentGame.getGameID();
@ -414,6 +432,8 @@ public class ConnectionControl implements IConnectionControl {
(Color) Base64.decodeToObject(extension.getValue("color")));
} else if (messageType.equals("game_start")) {
gameStartEvent.emit();
} else if (messageType.equals("round_start")) {
roundStartEvent.emit();
} else if (messageType.equals("table_update")) {
tableUpdateEvent.emit((ITable) Base64.decodeToObject(extension
.getValue("table")));

View file

@ -5,7 +5,9 @@ import java.util.UUID;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
@ -15,6 +17,7 @@ import jrummikub.view.IView;
* Control for joining a network game
*/
public class GameJoinControl extends AbstractGameBeginControl {
private Event gameStartEvent = new Event();
/**
* Creates new game join control
@ -63,11 +66,21 @@ public class GameJoinControl extends AbstractGameBeginControl {
connections.add(connectionControl.getGameStartEvent().add(new IListener() {
@Override
public void handle() {
startGame();
abort();
gameStartEvent.emit();
}
}));
}
/**
* The event that is emitted when the game is started
*
* @return the event
*/
public IEvent getStartGameEvent() {
return gameStartEvent;
}
private void fixGameSettings(GameSettings settings) {
for (PlayerSettings player : settings.getPlayerList()) {
if (player.getType() == Type.HUMAN) {

View file

@ -1,12 +1,15 @@
package jrummikub.control.network;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event1;
import jrummikub.util.GameData;
import jrummikub.util.IEvent1;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.ISettingsPanel.SettingsMode;
@ -16,6 +19,7 @@ import jrummikub.view.IView;
* Control for network game host
*/
public class GameOfferControl extends AbstractGameBeginControl {
private Event1<GameSettings> gameStartEvent = new Event1<GameSettings>();
/**
* Creates new game offer control
@ -84,10 +88,32 @@ public class GameOfferControl extends AbstractGameBeginControl {
}));
}
@Override
protected void startGame() {
super.startGame();
/**
* The event that is emitted when the game is started
*
* @return the event
*/
public IEvent1<GameSettings> getStartGameEvent() {
return gameStartEvent;
}
private void startGame() {
abort();
connectionControl.startGame();
GameSettings settings = gameData.getGameSettings();
removeVacant(settings);
gameStartEvent.emit(settings);
}
private void removeVacant(GameSettings settings) {
Iterator<PlayerSettings> it = settings.getPlayerList().iterator();
while (it.hasNext()) {
if (it.next().getType() == Type.VACANT) {
it.remove();
}
}
}
/**
@ -98,7 +124,6 @@ public class GameOfferControl extends AbstractGameBeginControl {
connectionControl.offerGame(gameData);
view.showSettingsPanel(true);
}
@Override

View file

@ -37,6 +37,8 @@ interface IConnectionControl {
public IEvent getGameStartEvent();
public IEvent getRoundStartEvent();
public IEvent1<ITable> getTableUpdateEvent();
public IEvent1<ITable> getTurnEndEvent();
@ -61,6 +63,8 @@ interface IConnectionControl {
public void startGame();
public void startRound();
public void updateTable(ITable table);
public void endTurn(ITable table);

View file

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.util.Connection;
import jrummikub.util.Event;
@ -31,6 +32,10 @@ public class NetworkControl {
private GameOfferControl gameOfferControl;
private GameJoinControl gameJoinControl;
private SaveControl saveControl;
private NetworkGameControl gameControl;
private List<UUID> games = new ArrayList<UUID>();
private Map<UUID, GameData> gameMap = new HashMap<UUID, GameData>();
@ -45,9 +50,11 @@ public class NetworkControl {
* for events and handlers
*/
public NetworkControl(final LoginData loginData,
IConnectionControl connectionControl, final IView view) {
IConnectionControl connectionControl, SaveControl saveControl,
final IView view) {
this.view = view;
this.connectionControl = connectionControl;
this.saveControl = saveControl;
addConnectionSetupListeners(loginData, view);
addConnectionControlListeners(view);
@ -186,6 +193,14 @@ public class NetworkControl {
view.showGameListPanel(true);
}
});
gameJoinControl.getStartGameEvent().add(new IListener() {
@Override
public void handle() {
gameControl = new NetworkGameControl(null, saveControl, view,
connectionControl, false);
gameControl.startGame();
}
});
gameJoinControl.startGameJoin();
}
@ -271,6 +286,14 @@ public class NetworkControl {
view.showGameListPanel(true);
}
});
gameOfferControl.getStartGameEvent().add(new IListener1<GameSettings>() {
@Override
public void handle(GameSettings settings) {
gameControl = new NetworkGameControl(settings, saveControl, view,
connectionControl, true);
gameControl.startGame();
}
});
gameOfferControl.startGameOffer();
}

View file

@ -0,0 +1,46 @@
package jrummikub.control.network;
import jrummikub.control.GameControl;
import jrummikub.control.RoundControl;
import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.model.IRoundState;
import jrummikub.util.IListener;
import jrummikub.view.IView;
public class NetworkGameControl extends GameControl {
private IConnectionControl connectionControl;
private boolean host;
public NetworkGameControl(GameSettings gameSettings, SaveControl saveControl,
IView view, IConnectionControl connectionControl, boolean host) {
super(gameSettings, saveControl, view);
this.connectionControl = connectionControl;
this.host = host;
}
@Override
public void startGame() {
connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
@Override
public void handle() {
startRound();
}
}));
if (host) {
connectionControl.startRound();
}
}
@Override
protected IRoundState createRoundState() {
return host ? super.createRoundState() : null;
}
@Override
protected RoundControl createRoundControl(IRoundState roundState) {
return new NetworkRoundControl(roundState, view, connectionControl, host);
}
}