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

@ -37,6 +37,8 @@ public class MockConnectionControl implements IConnectionControl {
/** */
public MockEvent gameStartEvent = new MockEvent();
/** */
public MockEvent roundStartEvent = new MockEvent();
/** */
public MockEvent1<ITable> tableUpdateEvent = new MockEvent1<ITable>();
/** */
public MockEvent1<ITable> turnEndEvent = new MockEvent1<ITable>();
@ -124,6 +126,11 @@ public class MockConnectionControl implements IConnectionControl {
return gameStartEvent;
}
@Override
public IEvent getRoundStartEvent() {
return roundStartEvent;
}
@Override
public IEvent1<ITable> getTableUpdateEvent() {
return tableUpdateEvent;
@ -203,4 +210,10 @@ public class MockConnectionControl implements IConnectionControl {
public void startTurn(IRoundState state) {
turnStarted = true;
}
@Override
public void startRound() {
// TODO Auto-generated method stub
}
}

View file

@ -26,14 +26,16 @@ public class MockRoundState implements IRoundState {
/** */
public int turnNumber;
/** */
public GameState gameState;
/** */
public MockRoundState() {
table = new MockTable();
players = new ArrayList<MockPlayer>();
players.add(new MockPlayer(new PlayerSettings("Player 1", Color.RED),
gameSettings));
players.add(new MockPlayer(
new PlayerSettings("Player 2", Color.YELLOW), gameSettings));
players.add(new MockPlayer(new PlayerSettings("Player 2", Color.YELLOW),
gameSettings));
players.add(new MockPlayer(new PlayerSettings("Player 3", Color.GREEN),
gameSettings));
players.add(new MockPlayer(new PlayerSettings("Player 4", Color.BLACK),
@ -126,4 +128,9 @@ public class MockRoundState implements IRoundState {
public void nextTurn() {
turnNumber++;
}
@Override
public GameState getGameState() {
return gameState;
}
}

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);
}
}

View file

@ -96,13 +96,15 @@ public interface IRoundState extends Serializable {
*/
public void setActivePlayerNumber(int i);
public GameState getGameState();
/**
* Gets the number of the current turn. Numbers smaller than one indicate hand
* inspection turns
*
* @return current turn number
*/
public abstract int getTurnNumber();
public int getTurnNumber();
/**
* Increments the turn number

View file

@ -7,6 +7,7 @@ public class RoundState implements IRoundState {
private static final long serialVersionUID = 8678490099871939059L;
private GameSettings gameSettings;
private GameState gameState;
private ITable table;
private ArrayList<Player> players;
@ -21,8 +22,9 @@ public class RoundState implements IRoundState {
* @param gameSettings
* the game settings
*/
public RoundState(GameSettings gameSettings) {
public RoundState(GameSettings gameSettings, GameState gameState) {
this.gameSettings = gameSettings;
this.gameState = gameState;
table = new Table(gameSettings);
players = new ArrayList<Player>();
@ -118,4 +120,9 @@ public class RoundState implements IRoundState {
public void nextTurn() {
turnNumber++;
}
@Override
public GameState getGameState() {
return gameState;
}
}

View file

@ -51,7 +51,7 @@ public class RoundControlTest {
private boolean roundEnded;
private Score roundScore;
protected boolean roundRestarted;
Stone blueFour = new Stone(4, BLUE);
Stone blackFour = new Stone(4, BLACK);
Stone redFour = new Stone(4, RED);
@ -67,15 +67,15 @@ public class RoundControlTest {
Stone blueOne = new Stone(1, BLUE);
Stone blueTwo = new Stone(2, BLUE);
Stone blueThree = new Stone(3, BLUE);
Stone blueTen = new Stone(10, BLUE);
Stone redTen = new Stone(10, RED);
Stone blueEleven = new Stone(11, BLUE);
Stone redEight = new Stone(8, RED);
Stone redNine = new Stone(9, RED);
Stone redEleven = new Stone(11, RED);
Stone blueEight = new Stone(8, BLUE);
Stone blackEight = new Stone(8, BLACK);
Stone orangeEight = new Stone(8, ORANGE);
@ -105,7 +105,7 @@ public class RoundControlTest {
new PlayerSettings("Matthias", Color.YELLOW));
gameSettings.getPlayerList().add(new PlayerSettings("Jannis", Color.GREEN));
gameSettings.getPlayerList().add(new PlayerSettings("Bennet", Color.BLACK));
roundState = new RoundState(gameSettings);
roundState = new RoundState(gameSettings, null);
roundControl = new RoundControl(roundState, view);
}

View file

@ -24,7 +24,7 @@ import org.junit.Test;
public class SaveControlTest {
/**
* @throws IOException
* if input cannot be saved/loaded
* if input cannot be saved/loaded
*/
@Test
public void testSaveLoad() throws IOException {
@ -33,7 +33,7 @@ public class SaveControlTest {
settings.getPlayerList().add(new PlayerSettings("Ida", Color.PINK));
GameState gameState = new GameState();
RoundState roundState = new RoundState(settings);
RoundState roundState = new RoundState(settings, null);
Stone stone1 = new Stone(1, RED);
Stone stone2 = new Stone(5, RED);
@ -57,12 +57,11 @@ public class SaveControlTest {
new IListener3<GameSettings, GameState, IRoundState>() {
@Override
public void handle(GameSettings settings,
GameState gameState, IRoundState roundState) {
public void handle(GameSettings settings, GameState gameState,
IRoundState roundState) {
assertEquals(2, settings.getPlayerList().size());
assertEquals(1, roundState.getTable().getSize());
assertEquals(2, roundState.getActivePlayer().getHand()
.getSize());
assertEquals(2, roundState.getActivePlayer().getHand().getSize());
}
});

View file

@ -1,7 +1,6 @@
package jrummikub.control.network;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.awt.Color;
import java.util.UUID;
@ -37,7 +36,7 @@ public class GameJoinControlTest {
mockConnection.nickname = "Karl";
view = new MockView();
loginData = new LoginData("Karl", "server", "password", "channel");
networkControl = new NetworkControl(loginData, mockConnection, view);
networkControl = new NetworkControl(loginData, mockConnection, null, view);
networkControl.startNetwork();
mockConnection.connectedEvent.emit();

View file

@ -1,9 +1,6 @@
package jrummikub.control.network;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.awt.Color;
import java.util.UUID;
@ -36,7 +33,7 @@ public class GameOfferControlTest {
mockConnection.nickname = "Karl";
view = new MockView();
loginData = new LoginData("Karl", "server", "password", "channel");
networkControl = new NetworkControl(loginData, mockConnection, view);
networkControl = new NetworkControl(loginData, mockConnection, null, view);
networkControl.startNetwork();
mockConnection.connectedEvent.emit();

View file

@ -1,9 +1,6 @@
package jrummikub.control.network;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.awt.Color;
import java.util.UUID;
@ -38,7 +35,7 @@ public class NetworkControlTest {
mockConnection.nickname = "Karl";
view = new MockView();
loginData = new LoginData("Karl", "server", "password", "channel");
networkControl = new NetworkControl(loginData, mockConnection, view);
networkControl = new NetworkControl(loginData, mockConnection, null, view);
networkControl.startNetwork();
}

View file

@ -47,7 +47,7 @@ public class NetworkRoundControlTest {
gameSettings.getPlayerList().get(1).setType(Type.COMPUTER);
gameSettings.getPlayerList().get(2).setType(Type.NETWORK);
gameSettings.getPlayerList().get(3).setType(Type.COMPUTER);
testRoundState = new RoundState(gameSettings);
testRoundState = new RoundState(gameSettings, null);
testRound = new NetworkRoundControl(testRoundState, view,
connectionControl, true);
@ -122,7 +122,7 @@ public class NetworkRoundControlTest {
gameSettings.getPlayerList().get(0).setType(Type.NETWORK);
gameSettings.getPlayerList().get(1).setType(Type.COMPUTER);
gameSettings.getPlayerList().get(3).setType(Type.COMPUTER);
testRoundState = new RoundState(gameSettings);
testRoundState = new RoundState(gameSettings, null);
for (int i = 0; i < 4; ++i) {
IPlayer player = testRoundState.getNthPlayer(i);

View file

@ -1,9 +1,6 @@
package jrummikub.control.network;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.util.UUID;
@ -35,7 +32,7 @@ public class NetworkSettingsControlTest {
mockConnection.nickname = "Karl";
view = new MockView();
loginData = new LoginData("Karl", "server", "password", "channel");
networkControl = new NetworkControl(loginData, mockConnection, view);
networkControl = new NetworkControl(loginData, mockConnection, null, view);
networkControl.startNetwork();
mockConnection.connectedEvent.emit();

View file

@ -21,7 +21,7 @@ public class RoundStateTest {
settings.getPlayerList().add(new PlayerSettings("Player 2", Color.YELLOW));
settings.getPlayerList().add(new PlayerSettings("Player 3", Color.BLUE));
testRound = new RoundState(settings);
testRound = new RoundState(settings, null);
}
/** */