Make starting new rounds on network mode work
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@553 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
d09041304b
commit
5d0d593297
5 changed files with 99 additions and 43 deletions
|
@ -1,10 +1,12 @@
|
||||||
package jrummikub.control;
|
package jrummikub.control;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import jrummikub.model.GameSettings;
|
import jrummikub.model.GameSettings;
|
||||||
import jrummikub.model.GameState;
|
import jrummikub.model.GameState;
|
||||||
|
import jrummikub.model.IPlayer;
|
||||||
import jrummikub.model.IRoundState;
|
import jrummikub.model.IRoundState;
|
||||||
import jrummikub.model.RoundState;
|
import jrummikub.model.RoundState;
|
||||||
import jrummikub.model.Score;
|
import jrummikub.model.Score;
|
||||||
|
@ -33,11 +35,11 @@ public class GameControl {
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param gameSettings
|
* @param gameSettings
|
||||||
* the game settings
|
* the game settings
|
||||||
* @param saveControl
|
* @param saveControl
|
||||||
* the save control
|
* the save control
|
||||||
* @param view
|
* @param view
|
||||||
* the view
|
* the view
|
||||||
*/
|
*/
|
||||||
public GameControl(GameSettings gameSettings, SaveControl saveControl,
|
public GameControl(GameSettings gameSettings, SaveControl saveControl,
|
||||||
IView view) {
|
IView view) {
|
||||||
|
@ -49,9 +51,8 @@ public class GameControl {
|
||||||
gameState = new GameState();
|
gameState = new GameState();
|
||||||
saveControl.setGameState(gameState);
|
saveControl.setGameState(gameState);
|
||||||
|
|
||||||
gameState
|
gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
|
||||||
.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
|
.getPlayerList().size()));
|
||||||
.getPlayerList().size()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connections.add(view.getNewRoundEvent().add(new IListener() {
|
connections.add(view.getNewRoundEvent().add(new IListener() {
|
||||||
|
@ -113,9 +114,9 @@ public class GameControl {
|
||||||
* Continues game after loading
|
* Continues game after loading
|
||||||
*
|
*
|
||||||
* @param gameState
|
* @param gameState
|
||||||
* the saved GameState (Players, startplayer, points)
|
* the saved GameState (Players, startplayer, points)
|
||||||
* @param roundState
|
* @param roundState
|
||||||
* the saved RoundState (activePlayer, Table, heap etc)
|
* the saved RoundState (activePlayer, Table, heap etc)
|
||||||
*/
|
*/
|
||||||
public void continueGame(GameState gameState, IRoundState roundState) {
|
public void continueGame(GameState gameState, IRoundState roundState) {
|
||||||
this.gameState = gameState;
|
this.gameState = gameState;
|
||||||
|
@ -161,25 +162,24 @@ public class GameControl {
|
||||||
* Prepare a new round by setting start player, adding listeners
|
* Prepare a new round by setting start player, adding listeners
|
||||||
*
|
*
|
||||||
* @param roundState
|
* @param roundState
|
||||||
* of current round
|
* of current round
|
||||||
*/
|
*/
|
||||||
private void prepareRound(IRoundState roundState) {
|
private void prepareRound(IRoundState roundState) {
|
||||||
saveControl.setRoundState(roundState);
|
saveControl.setRoundState(roundState);
|
||||||
|
|
||||||
if (roundState != null) {
|
if (roundState != null) {
|
||||||
roundState.setActivePlayerNumber(gameState
|
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
|
||||||
.getFirstRoundFirstPlayer() + gameState.getScores().size());
|
+ gameState.getScores().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
roundControl = createRoundControl(roundState);
|
roundControl = createRoundControl(roundState);
|
||||||
roundControl.getRoundStateUpdateEvent().add(
|
roundControl.getRoundStateUpdateEvent().add(new IListener1<IRoundState>() {
|
||||||
new IListener1<IRoundState>() {
|
@Override
|
||||||
@Override
|
public void handle(IRoundState newState) {
|
||||||
public void handle(IRoundState newState) {
|
gameState = newState.getGameState();
|
||||||
gameState = newState.getGameState();
|
gameSettings = newState.getGameSettings();
|
||||||
gameSettings = newState.getGameSettings();
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
roundControl.getEndOfRoundEvent().add(new IListener1<Score>() {
|
roundControl.getEndOfRoundEvent().add(new IListener1<Score>() {
|
||||||
@Override
|
@Override
|
||||||
public void handle(Score roundScore) {
|
public void handle(Score roundScore) {
|
||||||
|
@ -209,7 +209,7 @@ public class GameControl {
|
||||||
* Creates a new round control with the specified round state
|
* Creates a new round control with the specified round state
|
||||||
*
|
*
|
||||||
* @param roundState
|
* @param roundState
|
||||||
* for new round control
|
* for new round control
|
||||||
* @return the round control
|
* @return the round control
|
||||||
*/
|
*/
|
||||||
protected RoundControl createRoundControl(IRoundState roundState) {
|
protected RoundControl createRoundControl(IRoundState roundState) {
|
||||||
|
@ -228,7 +228,7 @@ public class GameControl {
|
||||||
* Sets the score and default values for saving when round ends
|
* Sets the score and default values for saving when round ends
|
||||||
*
|
*
|
||||||
* @param roundScore
|
* @param roundScore
|
||||||
* score for ended round
|
* score for ended round
|
||||||
*/
|
*/
|
||||||
private void endOfRound(Score roundScore) {
|
private void endOfRound(Score roundScore) {
|
||||||
gameState.getScores().add(roundScore);
|
gameState.getScores().add(roundScore);
|
||||||
|
@ -242,17 +242,20 @@ public class GameControl {
|
||||||
* Sets score panel visible
|
* Sets score panel visible
|
||||||
*/
|
*/
|
||||||
private void showScorePanel() {
|
private void showScorePanel() {
|
||||||
view.showSidePanel(false);
|
view.getSidePanel().setPlayers(Collections.<IPlayer> emptyList());
|
||||||
view.setBottomPanel(BottomPanelType.WIN_PANEL);
|
showWinPanel();
|
||||||
|
|
||||||
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
|
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
|
||||||
view.getScorePanel().setScores(gameState.getScores());
|
view.getScorePanel().setScores(gameState.getScores());
|
||||||
view.getScorePanel().setAccumulatedScore(
|
view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
|
||||||
gameState.getAccumulatedScore());
|
|
||||||
view.getScorePanel().update();
|
view.getScorePanel().update();
|
||||||
view.showScorePanel(true);
|
view.showScorePanel(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void showWinPanel() {
|
||||||
|
view.setBottomPanel(BottomPanelType.WIN_PANEL);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exits System without warnings if no game control is active
|
* Exits System without warnings if no game control is active
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -7,6 +7,7 @@ import jrummikub.model.GameSettings;
|
||||||
import jrummikub.model.IRoundState;
|
import jrummikub.model.IRoundState;
|
||||||
import jrummikub.util.IListener;
|
import jrummikub.util.IListener;
|
||||||
import jrummikub.view.IView;
|
import jrummikub.view.IView;
|
||||||
|
import jrummikub.view.IView.BottomPanelType;
|
||||||
|
|
||||||
public class NetworkGameControl extends GameControl {
|
public class NetworkGameControl extends GameControl {
|
||||||
private IConnectionControl connectionControl;
|
private IConnectionControl connectionControl;
|
||||||
|
@ -21,11 +22,11 @@ public class NetworkGameControl extends GameControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startGame() {
|
protected void startRound() {
|
||||||
connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
|
connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handle() {
|
public void handle() {
|
||||||
startRound();
|
NetworkGameControl.super.startRound();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -43,4 +44,10 @@ public class NetworkGameControl extends GameControl {
|
||||||
protected RoundControl createRoundControl(IRoundState roundState) {
|
protected RoundControl createRoundControl(IRoundState roundState) {
|
||||||
return new NetworkRoundControl(roundState, view, connectionControl, host);
|
return new NetworkRoundControl(roundState, view, connectionControl, host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void showWinPanel() {
|
||||||
|
view.setBottomPanel(host ? BottomPanelType.WIN_PANEL
|
||||||
|
: BottomPanelType.NETWORK_WIN_PANEL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -366,6 +366,8 @@ public interface IView {
|
||||||
/** */
|
/** */
|
||||||
NONHUMAN_HAND_PANEL,
|
NONHUMAN_HAND_PANEL,
|
||||||
/** */
|
/** */
|
||||||
WIN_PANEL
|
WIN_PANEL,
|
||||||
|
/** */
|
||||||
|
NETWORK_WIN_PANEL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -643,13 +643,16 @@ public class View extends JFrame implements IView {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doSetBottomPanel(BottomPanelType type) {
|
private void doSetBottomPanel(BottomPanelType type) {
|
||||||
boolean showStartTurnPanel = type == BottomPanelType.START_TURN_PANEL
|
boolean showStartTurnPanel = (type == BottomPanelType.START_TURN_PANEL || type == BottomPanelType.INVALID_TURN_PANEL);
|
||||||
|| type == BottomPanelType.INVALID_TURN_PANEL;
|
|
||||||
startTurnPanel.setVisible(showStartTurnPanel);
|
|
||||||
startTurnPanel.setType(type);
|
startTurnPanel.setType(type);
|
||||||
winPanel.setVisible(type == BottomPanelType.WIN_PANEL);
|
startTurnPanel.setVisible(showStartTurnPanel);
|
||||||
|
|
||||||
|
boolean showWinPanel = (type == BottomPanelType.WIN_PANEL || type == BottomPanelType.NETWORK_WIN_PANEL);
|
||||||
|
winPanel.setType(type);
|
||||||
|
winPanel.setVisible(showWinPanel);
|
||||||
|
|
||||||
playerPanel.setVisible((!showStartTurnPanel)
|
playerPanel.setVisible((!showStartTurnPanel)
|
||||||
&& type != BottomPanelType.WIN_PANEL && type != null);
|
&& (!showWinPanel) && type != null);
|
||||||
|
|
||||||
if (type == BottomPanelType.START_GAME_PANEL) {
|
if (type == BottomPanelType.START_GAME_PANEL) {
|
||||||
table.setStoneSets(Collections.<Pair<StoneSet, Position>> emptyList());
|
table.setStoneSets(Collections.<Pair<StoneSet, Position>> emptyList());
|
||||||
|
|
|
@ -7,11 +7,13 @@ import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
import jrummikub.util.Event;
|
import jrummikub.util.Event;
|
||||||
import jrummikub.util.IEvent;
|
import jrummikub.util.IEvent;
|
||||||
|
import jrummikub.view.IView.BottomPanelType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A panel that is displayed when a player has won
|
* A panel that is displayed when a player has won
|
||||||
|
@ -25,6 +27,7 @@ class WinPanel extends JPanel {
|
||||||
private final static int PANEL_MAX_WIDTH = 180;
|
private final static int PANEL_MAX_WIDTH = 180;
|
||||||
private final static float MAX_BUTTON_FONT_SIZE = 12;
|
private final static float MAX_BUTTON_FONT_SIZE = 12;
|
||||||
|
|
||||||
|
private JLabel waitingLabel;
|
||||||
private JButton newRoundButton;
|
private JButton newRoundButton;
|
||||||
private JButton newGameButton;
|
private JButton newGameButton;
|
||||||
private JButton endProgramButton;
|
private JButton endProgramButton;
|
||||||
|
@ -32,6 +35,7 @@ class WinPanel extends JPanel {
|
||||||
private Event endProgramEvent = new Event();
|
private Event endProgramEvent = new Event();
|
||||||
private Event newGameEvent = new Event();
|
private Event newGameEvent = new Event();
|
||||||
private Event newRoundEvent = new Event();
|
private Event newRoundEvent = new Event();
|
||||||
|
private BottomPanelType type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new WinPanel
|
* Creates a new WinPanel
|
||||||
|
@ -41,6 +45,11 @@ class WinPanel extends JPanel {
|
||||||
setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET,
|
setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET,
|
||||||
PANEL_INSET));
|
PANEL_INSET));
|
||||||
|
|
||||||
|
waitingLabel = new JLabel("Warte auf Host...");
|
||||||
|
waitingLabel.setHorizontalAlignment(JLabel.CENTER);
|
||||||
|
waitingLabel.setVerticalAlignment(JLabel.CENTER);
|
||||||
|
add(waitingLabel);
|
||||||
|
|
||||||
newRoundButton = new JButton("Neue Runde");
|
newRoundButton = new JButton("Neue Runde");
|
||||||
newRoundButton.addActionListener(new ActionListener() {
|
newRoundButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -108,22 +117,54 @@ class WinPanel extends JPanel {
|
||||||
width = width / 2 + PANEL_MAX_WIDTH / 2;
|
width = width / 2 + PANEL_MAX_WIDTH / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int buttonWidth = (width - 2 * PANEL_SEPARATOR) / 3;
|
int buttonWidth;
|
||||||
int buttonHeight = height;
|
int buttonHeight;
|
||||||
|
int buttonY;
|
||||||
|
if (type == BottomPanelType.WIN_PANEL) {
|
||||||
|
buttonWidth = (width - 2 * PANEL_SEPARATOR) / 3;
|
||||||
|
buttonHeight = height;
|
||||||
|
buttonY = y;
|
||||||
|
} else {
|
||||||
|
buttonWidth = (width - PANEL_SEPARATOR) / 2;
|
||||||
|
buttonHeight = height * 2 / 3 - PANEL_SEPARATOR;
|
||||||
|
buttonY = y + height - buttonHeight;
|
||||||
|
}
|
||||||
|
int labelHeight = height - buttonHeight - PANEL_SEPARATOR;
|
||||||
|
|
||||||
float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5;
|
float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5;
|
||||||
if (fontSize > MAX_BUTTON_FONT_SIZE)
|
if (fontSize > MAX_BUTTON_FONT_SIZE)
|
||||||
fontSize = MAX_BUTTON_FONT_SIZE;
|
fontSize = MAX_BUTTON_FONT_SIZE;
|
||||||
|
|
||||||
newRoundButton.setBounds(x, y, buttonWidth, buttonHeight);
|
if (type == BottomPanelType.WIN_PANEL) {
|
||||||
newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
|
waitingLabel.setVisible(false);
|
||||||
|
newRoundButton.setBounds(x, buttonY, buttonWidth, buttonHeight);
|
||||||
|
newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
|
||||||
|
newRoundButton.setVisible(true);
|
||||||
|
|
||||||
newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y, buttonWidth,
|
newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, buttonY,
|
||||||
buttonHeight);
|
buttonWidth, buttonHeight);
|
||||||
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
|
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
|
||||||
|
|
||||||
endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR), y,
|
endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR),
|
||||||
buttonWidth, buttonHeight);
|
buttonY, buttonWidth, buttonHeight);
|
||||||
endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
|
endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
|
||||||
|
} else if (type == BottomPanelType.NETWORK_WIN_PANEL) {
|
||||||
|
waitingLabel.setBounds(x, y, width, labelHeight);
|
||||||
|
waitingLabel.setVisible(true);
|
||||||
|
|
||||||
|
newRoundButton.setVisible(false);
|
||||||
|
|
||||||
|
newGameButton.setBounds(x, buttonY, buttonWidth, buttonHeight);
|
||||||
|
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
|
||||||
|
|
||||||
|
endProgramButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, buttonY,
|
||||||
|
buttonWidth, buttonHeight);
|
||||||
|
endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setType(BottomPanelType type) {
|
||||||
|
this.type = type;
|
||||||
|
rescale();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue