merge development into dev/client

This commit is contained in:
Cedric Beck
2024-11-24 10:39:28 +01:00
46 changed files with 1380 additions and 122 deletions

View File

@@ -57,4 +57,13 @@ public Node[] getInfield() {
public void setPieceOnBoard(int index, Piece piece) { public void setPieceOnBoard(int index, Piece piece) {
infield[index].setOccupant(piece); infield[index].setOccupant(piece);
} }
public int getInfieldIndexOfPiece(Piece piece) {
for (int i = 0; i < infield.length; i++) {
if (infield[i].getOccupant() == piece) {
return i;
}
}
return -1;
}
} }

View File

@@ -22,8 +22,12 @@ public class Game {
private Map<Color, Integer> playerConnectionID; private Map<Color, Integer> playerConnectionID;
private ArrayList<Observer> observers = new ArrayList<>(); private ArrayList<Observer> observers = new ArrayList<>();
private Player startPlayer;
private Boolean gameHasStarted = false; private Boolean gameHasStarted = false;
private Boolean playerHasDisconnected = false; private Boolean playerHasDisconnected = false;
private Boolean gameIsInterrupted = false;
private Boolean allRanked = false;
private Boolean movablePieces = false;
private static final int AMOUNT_OF_TURBO_CARDS = 16; private static final int AMOUNT_OF_TURBO_CARDS = 16;
private static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12; private static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
@@ -297,7 +301,6 @@ public Boolean getGameHasStarted() {
*/ */
public void setGameHasStarted(Boolean gameHasStarted) { public void setGameHasStarted(Boolean gameHasStarted) {
this.gameHasStarted = gameHasStarted; this.gameHasStarted = gameHasStarted;
if (gameHasStarted) notifyObservers();
} }
/** /**
@@ -309,6 +312,44 @@ public Boolean playerHasDisconnected() {
return playerHasDisconnected; return playerHasDisconnected;
} }
/**
* This method sets the game interruption state.
*
* @param gameIsInterrupted the new game interruption state
*/
public void setGameIsInterrupted(Boolean gameIsInterrupted) {
this.gameIsInterrupted = gameIsInterrupted;
if (!gameIsInterrupted) notifyObservers();
}
/**
* This method returns whether the game is interrupted.
*
* @return true if the game is interrupted, false otherwise
*/
public Boolean gameIsInterrupted() {
return gameIsInterrupted;
}
/**
* This method returns whether the game is interrupted.
*
* @return true if the game is interrupted, false otherwise
*/
public Boolean getMovablePieces() {
return movablePieces;
}
/**
* This method sets the game interruption state.
*
* @param movablePieces the new game interruption state
*/
public void setMovablePieces(Boolean movablePieces) {
this.movablePieces = movablePieces;
if (!movablePieces) notifyObservers();
}
/** /**
* This method sets the player has disconnected. * This method sets the player has disconnected.
* *
@@ -316,9 +357,44 @@ public Boolean playerHasDisconnected() {
*/ */
public void setPlayerHasDisconnected(Boolean playerHasDisconnected) { public void setPlayerHasDisconnected(Boolean playerHasDisconnected) {
this.playerHasDisconnected = playerHasDisconnected; this.playerHasDisconnected = playerHasDisconnected;
if (playerHasDisconnected) { if (playerHasDisconnected) notifyObservers();
notifyObservers(); }
}
/**
* This method returns whether all players have ranked.
*
* @return true if all players have ranked, false otherwise
*/
public Boolean allRanked() {
return allRanked;
}
/**
* This method sets whether all players have ranked.
*
* @param allRanked the new all ranked state
*/
public void setAllRanked(Boolean allRanked) {
this.allRanked = allRanked;
if (allRanked) notifyObservers();
}
/**
* This method returns the start player.
*
* @return the start player
*/
public Player getStartPlayer() {
return startPlayer;
}
/**
* This method sets the start player.
*
* @param startPlayer the new start player
*/
public void setStartPlayer(Player startPlayer) {
this.startPlayer = startPlayer;
} }
/** /**
@@ -329,4 +405,17 @@ public void notifyObservers() {
observer.update(); observer.update();
} }
} }
/**
* This method returns the piece through its identifier.
*
* @param identifier the identifier of the piece
* @return the piece
*/
public Piece getPieceThroughIdentifier(String identifier) {
String[] parts = identifier.split("-");
Color color = Color.valueOf(parts[0]);
int index = Integer.parseInt(parts[1]);
return board.getPlayerData().get(color).getPieces()[index];
}
} }

View File

@@ -6,7 +6,8 @@
public class Piece { public class Piece {
private ShieldState shield; private ShieldState shield;
private PieceState state; private PieceState state;
private Color color; private final Color color;
private final int id;
/** /**
* This constructor is used to create a new Piece * This constructor is used to create a new Piece
@@ -14,10 +15,11 @@ public class Piece {
* @param color the color of the piece * @param color the color of the piece
* @param state the state of the piece * @param state the state of the piece
*/ */
public Piece(Color color, PieceState state) { public Piece(Color color, PieceState state, int id) {
this.color = color; this.color = color;
this.state = state; this.state = state;
shield = ShieldState.NONE; shield = ShieldState.NONE;
this.id = id;
} }
/** /**
@@ -73,4 +75,22 @@ public boolean isShielded() {
public boolean isSuppressed() { public boolean isSuppressed() {
return shield == ShieldState.SUPPRESSED; return shield == ShieldState.SUPPRESSED;
} }
/**
* This method is used to get the color of the piece
*
* @return the color of the piece
*/
public Color getColor() {
return color;
}
/**
* This method is used to get the color of the piece
*
* @return the color of the piece
*/
public String getIdentifier() {
return color.toString() + "-" + id;
}
} }

View File

@@ -19,7 +19,7 @@ public PlayerData(Color color) {
waitingArea = new Piece[4]; waitingArea = new Piece[4];
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
homeNodes[i] = new HomeNode(); homeNodes[i] = new HomeNode();
pieces[i] = new Piece(color, PieceState.WAITING); pieces[i] = new Piece(color, PieceState.WAITING, i);
waitingArea[i] = pieces[i]; waitingArea[i] = pieces[i];
} }
} }
@@ -108,4 +108,46 @@ public Piece removePieceFromWaitingArea() {
public void setPieceInHome(int index, Piece piece) { public void setPieceInHome(int index, Piece piece) {
homeNodes[index].setOccupant(piece); homeNodes[index].setOccupant(piece);
} }
/**
* This method returns the homeNodes
*
* @return the homeNodes
*/
public boolean homeIncludes(Piece piece) {
for (int i = 0; i < 4; i++) {
if (homeNodes[i].getOccupant() == piece) {
return true;
}
}
return false;
}
/**
* This method returns the homeNodes
*
* @return the homeNodes
*/
public int getIndexInHome(Piece piece) {
for (int i = 0; i < 4; i++) {
if (homeNodes[i].getOccupant() == piece) {
return i;
}
}
return -1;
}
/**
* This method returns the homeNodes
*
* @return the homeNodes
*/
public boolean hasPieceInWaitingArea() {
for (Piece piece : waitingArea) {
if (piece != null) {
return true;
}
}
return false;
}
} }

View File

@@ -1,9 +1,28 @@
package pp.mdga.message.client; package pp.mdga.message.client;
import pp.mdga.game.Color;
public class RequestMove extends ClientMessage { public class RequestMove extends ClientMessage {
private final String pieceIdentifier;
/** Constructor for RequestMove
* @param pieceIdentifier the piece identifier
*/
public RequestMove(String pieceIdentifier) {
this.pieceIdentifier = pieceIdentifier;
}
/** Getter for the piece identifier
* @return the piece identifier
*/
public String getPieceIdentifier() {
return pieceIdentifier;
}
@Override @Override
public String toString() { public String toString() {
return "null"; return pieceIdentifier;
} }
@Override @Override

View File

@@ -1,9 +1,28 @@
package pp.mdga.message.client; package pp.mdga.message.client;
import pp.mdga.game.BonusCard;
public class RequestPlayCard extends ClientMessage { public class RequestPlayCard extends ClientMessage {
private final BonusCard card;
private final String pieceIdentifier;
public RequestPlayCard(BonusCard card, String pieceIdentifier) {
this.pieceIdentifier = pieceIdentifier;
this.card = card;
}
public BonusCard getCard() {
return card;
}
public String getPieceIdentifier() {
return pieceIdentifier;
}
@Override @Override
public String toString() { public String toString() {
return "null"; return card.toString();
} }
@Override @Override

View File

@@ -1,6 +1,19 @@
package pp.mdga.message.client; package pp.mdga.message.client;
import pp.mdga.game.BonusCard;
public class SelectCard extends ClientMessage { public class SelectCard extends ClientMessage {
private final BonusCard card;
public SelectCard(BonusCard card) {
this.card = card;
}
public BonusCard getCard() {
return card;
}
@Override @Override
public String toString() { public String toString() {
return "null"; return "null";

View File

@@ -1,6 +1,19 @@
package pp.mdga.message.client; package pp.mdga.message.client;
import pp.mdga.game.Color;
public class SelectTSK extends ClientMessage { public class SelectTSK extends ClientMessage {
private final Color color;
public SelectTSK(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
@Override @Override
public String toString() { public String toString() {
return "null"; return "null";

View File

@@ -1,6 +1,25 @@
package pp.mdga.message.client; package pp.mdga.message.client;
import pp.mdga.game.Color;
public class SelectedPieces extends ClientMessage { public class SelectedPieces extends ClientMessage {
private String pieceIdentifier;
/** Constructor for SelectedPieces
* @param pieceIdentifier the piece identifier
*/
public SelectedPieces(String pieceIdentifier) {
this.pieceIdentifier = pieceIdentifier;
}
/** Getter for the piece identifier
* @return the piece identifier
*/
public String getPieceIdentifier() {
return pieceIdentifier;
}
@Override @Override
public String toString() { public String toString() {
return "null"; return "null";

View File

@@ -1,6 +1,25 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.Color;
public class ActivePlayer extends ServerMessage { public class ActivePlayer extends ServerMessage {
private Color color;
/** Constructor for ActivePlayer
* @param color the color of the active player
*/
public ActivePlayer(Color color) {
this.color = color;
}
/** Getter for the color of the active player
* @return the color of the active player
*/
public Color getColor() {
return color;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,31 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import java.util.ArrayList;
public class AnyPiece extends ServerMessage { public class AnyPiece extends ServerMessage {
private ArrayList<String> piece;
/** Constructor for AnyPiece
*/
public AnyPiece() {
piece = new ArrayList<>();
}
/** Add a piece to the list of pieces
* @param piece the piece to add
*/
public void addPiece(String piece) {
this.piece.add(piece);
}
/** Getter for the list of pieces
* @return the list of pieces
*/
public ArrayList<String> getPiece() {
return piece;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,52 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import java.util.ArrayList;
public class Dice extends ServerMessage { public class Dice extends ServerMessage {
private final int diceEye;
private final ArrayList<String> moveablePieces;
/** Constructor for Dice
* @param diceEye the eye of the dice
* @param moveablePieces the pieces that can be moved
*/
public Dice(int diceEye, ArrayList<String> moveablePieces) {
this.diceEye = diceEye;
this.moveablePieces = moveablePieces;
}
/** Constructor for inactivePlayer
* @param diceEye the eye of the dice
* @return a new Dice object
*/
public static Dice inactivePlayer(int diceEye) {
return new Dice(diceEye, null);
}
/** Constructor for activePlayer
* @param diceEye the eye of the dice
* @param moveablePieces the pieces that can be moved
* @return a new Dice object
*/
public static Dice activePlayer(int diceEye, ArrayList<String> moveablePieces) {
return new Dice(diceEye, moveablePieces);
}
/** Getter for the eye of the dice
* @return the eye of the dice
*/
public int getDiceEye() {
return diceEye;
}
/** Getter for the pieces that can be moved
* @return the pieces that can be moved
*/
public ArrayList<String> getMoveablePieces() {
return moveablePieces;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,17 @@
package pp.mdga.message.server; package pp.mdga.message.server;
public class LobbyPlayerJoin extends ServerMessage { public class LobbyPlayerJoin extends ServerMessage {
private final String name;
public LobbyPlayerJoin(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,24 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.Color;
public class LobbyPlayerLeave extends ServerMessage { public class LobbyPlayerLeave extends ServerMessage {
private final String name;
private final Color color;
public LobbyPlayerLeave(String name, Color color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public Color getColor() {
return color;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,26 @@
package pp.mdga.message.server; package pp.mdga.message.server;
public class MoveMessage extends ServerMessage { public class MoveMessage extends ServerMessage {
private final String pieceIdentifier;
/**
* @param identifier the identifier of the piece that should be moved
*/
public MoveMessage(String identifier) {
this.pieceIdentifier = identifier;
}
/**
* @return the identifier of the piece that should be moved
*/
public String getIdentifier() {
return pieceIdentifier;
}
/**
* @return the identifier of the piece that should be moved
*/
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,34 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.BonusCard;
public class PlayCard extends ServerMessage { public class PlayCard extends ServerMessage {
private final BonusCard card;
private final String pieceIdentifier;
/**
* @param card the card that should be played
* @param pieceIdentifier the identifier of the piece that should be moved
*/
public PlayCard(BonusCard card, String pieceIdentifier) {
this.card = card;
this.pieceIdentifier = pieceIdentifier;
}
/**
* @return the card that should be played
*/
public BonusCard getCard() {
return card;
}
/**
* @return the identifier of the piece that should be moved
*/
public String getPieceIdentifier() {
return pieceIdentifier;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,33 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.BonusCard;
import java.util.ArrayList;
public class PossibleCard extends ServerMessage { public class PossibleCard extends ServerMessage {
private ArrayList<BonusCard> possibleCards;
/** Constructor for PossibleCard
*/
public PossibleCard() {
possibleCards = new ArrayList<>();
}
/** Add a possible card to the list of possible cards
* @param card the possible card to add
*/
public void addPossibleCard(BonusCard card) {
this.possibleCards.add(card);
}
/** Getter for the list of possible cards
* @return the list of possible cards
*/
public ArrayList<BonusCard> getPossibleCards() {
return possibleCards;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,40 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import java.util.ArrayList;
public class PossiblePiece extends ServerMessage { public class PossiblePiece extends ServerMessage {
private final ArrayList<String> possibleOwnPieces;
private final ArrayList<String> possibleEnemyPieces;
/** Constructor for PossiblePiece
*/
public PossiblePiece() {
possibleOwnPieces = new ArrayList<>();
possibleEnemyPieces = new ArrayList<>();
}
/** Add a piece to the list of possible pieces
* @param piece the piece to add
*/
public void addOwnPossiblePiece(String piece) {
this.possibleOwnPieces.add(piece);
}
/** Add a piece to the list of possible enemy pieces
* @param piece the piece to add
*/
public void addEnemyPossiblePiece(String piece) {
this.possibleEnemyPieces.add(piece);
}
/** Getter for the list of possible pieces
* @return the list of possible pieces
*/
public ArrayList<String> getPossiblePieces() {
return possibleOwnPieces;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,18 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.Game;
public class ReconnectBriefing extends ServerMessage { public class ReconnectBriefing extends ServerMessage {
private final Game game;
public ReconnectBriefing(Game game) {
this.game = game;
}
public Game getGame() {
return game;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,17 @@
package pp.mdga.message.server; package pp.mdga.message.server;
public class StartPiece extends ServerMessage { public class StartPiece extends ServerMessage {
private final String pieceIdentifier;
public StartPiece(String pieceIdentifier) {
this.pieceIdentifier = pieceIdentifier;
}
public String getPieceIdentifier() {
return pieceIdentifier;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,24 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.Color;
public class UpdateReady extends ServerMessage { public class UpdateReady extends ServerMessage {
private final Color color;
private final boolean ready;
public UpdateReady(Color color, boolean ready) {
this.color = color;
this.ready = ready;
}
public Color getColor() {
return color;
}
public boolean isReady() {
return ready;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -1,6 +1,24 @@
package pp.mdga.message.server; package pp.mdga.message.server;
import pp.mdga.game.Color;
public class UpdateTSK extends ServerMessage { public class UpdateTSK extends ServerMessage {
private final String name;
private final Color color;
public UpdateTSK(String name, Color color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public Color getColor() {
return color;
}
@Override @Override
public void accept(ServerInterpreter interpreter) { public void accept(ServerInterpreter interpreter) {

View File

@@ -0,0 +1,16 @@
package pp.mdga.server;
import pp.mdga.message.client.AnimationEnd;
import pp.mdga.message.server.DiceNow;
public class Animation extends ServerState {
public Animation(ServerState parent, ServerGameLogic logic) {
super(parent, logic);
}
@Override
public void receivedAnimationEnd(AnimationEnd msg, int from) {
logic.send(logic.getGame().getStartPlayer(), new DiceNow());
parent.gotoState(new Turn(parent, logic));
}
}

View File

@@ -6,7 +6,7 @@ public ChoosePieceStateMachine(ServerState parent, ServerGameLogic logic) {
} }
@Override @Override
public ServerState initialState() { public NoPiece initialState() {
return null; return new NoPiece(this, logic);
} }
} }

View File

@@ -1,7 +1,44 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Player;
import pp.mdga.message.client.RequestDice;
import pp.mdga.message.server.*;
import java.util.ArrayList;
import java.util.List;
public class DetermineStartPlayer extends ServerState { public class DetermineStartPlayer extends ServerState {
private final List<Player> player = new ArrayList<>();
public DetermineStartPlayer(ServerState parent, ServerGameLogic logic) { public DetermineStartPlayer(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
logic.getGame().addObserver(this);
}
@Override
public void receivedRequestDice(RequestDice msg, int from) {
logic.send();
broadcastUpdate(new Dice());
}
@Override
public void update() {
if (Boolean.TRUE.equals(logic.getGame().allRanked())) {
broadcastUpdate(new RankingResponce());
if (logic.getGame().getOrder().isEmpty()) {
// todo: save the players with the same value?
broadcastUpdate(new RankingRollAgain());
broadcastUpdate(new EndOfTurn());
} else {
// todo: set start player
Player startPlayer = new Player(1);
logic.getGame().setStartPlayer(startPlayer);
logic.send(startPlayer, new DiceNow());
broadcastUpdate(new EndOfTurn());
parent.gotoState(new Animation(parent, logic));
logic.getGame().removeObserver(this);
}
}
} }
} }

View File

@@ -1,7 +1,28 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.message.client.RequestDice;
public class FirstRoll extends ServerState { public class FirstRoll extends ServerState {
public FirstRoll(ServerState parent, ServerGameLogic logic) { public FirstRoll(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void receivedRequestDice(RequestDice msg, int from) {
// todo: implement player.hasMovablePieces()
// if (player.hasMovablePieces()) {
// // todo: goto ChoosePiece
// } else {
// // todo: implement roll
// if (roll == 6) {
// // todo: send to everyone? or one player?
// logic.send(new Player(1), new Dice());
// // todo: goto ChoosePiece
// } else {
// // todo: send to everyone? or one player?
// logic.send(new Player(1), new DiceAgain());
// parent.gotoState(new SecondRoll(parent, logic));
// }
// }
}
} }

View File

@@ -1,8 +1,9 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Piece;
import pp.mdga.message.client.*; import pp.mdga.message.client.*;
import pp.mdga.message.server.PauseGame;
import pp.mdga.message.server.PossibleCard; import pp.mdga.message.server.PossibleCard;
import pp.mdga.message.server.RankingResponce;
public class GameState extends ServerState { public class GameState extends ServerState {
private final GameStateMachine gameStateMachine = new GameStateMachine(this, logic); private final GameStateMachine gameStateMachine = new GameStateMachine(this, logic);
@@ -18,57 +19,50 @@ public void entry() {
} }
@Override @Override
public void receivedAnimationEnd(AnimationEnd msg) { public void exit() {
gameStateMachine.receivedAnimationEnd(msg); parent.gotoState(new Ceremony(parent, logic));
}
// todo piece?
@Override
public void receivedConfirmPiece(Piece piece) {
gameStateMachine.receivedConfirmPiece(piece);
} }
@Override @Override
public void receivedNoPowerCard(NoPowerCard msg) { public void receivedAnimationEnd(AnimationEnd msg, int from) {
gameStateMachine.receivedNoPowerCard(msg); gameStateMachine.receivedAnimationEnd(msg, from);
} }
@Override @Override
public void receivedPowerCardChoice(SelectCard msg) { public void receivedNoPowerCard(NoPowerCard msg, int from) {
gameStateMachine.receivedPowerCardChoice(msg); gameStateMachine.receivedNoPowerCard(msg, from);
} }
@Override @Override
public void receivedRequestDice(RequestDice msg) { public void receivedSelectCard(SelectCard msg, int from) {
gameStateMachine.receivedRequestDice(msg); gameStateMachine.receivedSelectCard(msg, from);
}
// todo msg?
@Override
public void receivedRollRankingDice() {
gameStateMachine.receivedRollRankingDice();
} }
@Override @Override
public void receivedSelectedPieces(SelectedPieces msg) { public void receivedRequestDice(RequestDice msg, int from) {
gameStateMachine.receivedSelectedPieces(msg); gameStateMachine.receivedRequestDice(msg, from);
} }
@Override @Override
public void sentPossibleCard(PossibleCard msg) { public void receivedSelectedPieces(SelectedPieces msg, int from) {
gameStateMachine.sentPossibleCard(msg); gameStateMachine.receivedSelectedPieces(msg, from);
} }
// todo msg?, sent to everyone?
@Override @Override
public void sentRankingResponse() { public void sentPossibleCard(PossibleCard msg, int from) {
gameStateMachine.sentRankingResponse(); gameStateMachine.sentPossibleCard(msg, from);
}
@Override
public void sentRankingResponse(RankingResponce msg, int from) {
gameStateMachine.sentRankingResponse(msg, from);
} }
@Override @Override
public void update() { public void update() {
if (logic.getGame().playerHasDisconnected()) { if (Boolean.TRUE.equals(logic.getGame().playerHasDisconnected())) {
parent.gotoState(new Ceremony(parent, logic)); broadcastUpdate(new PauseGame());
parent.gotoState(new Interrupt(parent, logic, this));
logic.getGame().removeObserver(this); logic.getGame().removeObserver(this);
} }
} }

View File

@@ -1,10 +1,24 @@
package pp.mdga.server; package pp.mdga.server;
public class GameStateMachine extends ServerStateMachine{ /**
* The GameStateMachine class represents the state machine for the game state.
*/
public class GameStateMachine extends ServerStateMachine {
/**
* Constructs a new GameStateMachine with the specified parent state and game logic.
*
* @param parent the parent state
* @param logic the server game logic
*/
public GameStateMachine(ServerState parent, ServerGameLogic logic) { public GameStateMachine(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
/**
* Returns the initial state of the state machine, which is DetermineStartPlayer.
*
* @return the initial state
*/
@Override @Override
public DetermineStartPlayer initialState() { public DetermineStartPlayer initialState() {
return new DetermineStartPlayer(this, logic); return new DetermineStartPlayer(this, logic);

View File

@@ -1,7 +1,22 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.message.server.ResumeGame;
public class Interrupt extends ServerState { public class Interrupt extends ServerState {
public Interrupt(ServerState parent, ServerGameLogic logic) { private final GameState lastState;
public Interrupt(ServerState parent, ServerGameLogic logic, GameState lastState) {
super(parent, logic); super(parent, logic);
this.lastState = lastState;
logic.getGame().addObserver(this);
}
@Override
public void update() {
if (Boolean.FALSE.equals(logic.getGame().gameIsInterrupted())) {
broadcastUpdate(new ResumeGame());
parent.gotoState(lastState);
logic.getGame().removeObserver(this);
}
} }
} }

View File

@@ -1,52 +1,74 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.message.client.*; import pp.mdga.message.client.*;
import pp.mdga.message.server.ServerMessage; import pp.mdga.message.server.ServerStartGame;
import pp.mdga.message.server.UpdateReady; import pp.mdga.message.server.UpdateReady;
import pp.mdga.message.server.UpdateTSK; import pp.mdga.message.server.UpdateTSK;
/**
* Represents the lobby state of the server.
*/
public class Lobby extends ServerState { public class Lobby extends ServerState {
/**
* Constructs a new Lobby state.
*
* @param parent the parent state
* @param logic the server game logic
*/
public Lobby(ServerState parent, ServerGameLogic logic) { public Lobby(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
logic.getGame().addObserver(this);
} }
/**
* Handles the DeselectTSK message.
*
* @param msg the DeselectTSK message
*/
@Override @Override
public void receivedDeselectTSK(DeselectTSK msg) { public void receivedDeselectTSK(DeselectTSK msg, int from) {
broadcastUpdate(new UpdateTSK()); broadcastUpdate(new UpdateTSK());
} }
/**
* Handles the LobbyNotReady message.
*
* @param msg the LobbyNotReady message
*/
@Override @Override
public void receivedNotReady(LobbyNotReady msg) { public void receivedNotReady(LobbyNotReady msg, int from) {
broadcastUpdate(new UpdateReady()); broadcastUpdate(new UpdateReady());
} }
/**
* Handles the LobbyReady message.
*
* @param msg the LobbyReady message
*/
@Override @Override
public void receivedReady(LobbyReady msg) { public void receivedReady(LobbyReady msg, int from) {
broadcastUpdate(new UpdateReady()); broadcastUpdate(new UpdateReady());
} }
/**
* Handles the SelectTSK message.
*
* @param msg the SelectTSK message
*/
@Override @Override
public void receivedSelectTSK(SelectTSK msg) { public void receivedSelectTSK(SelectTSK msg, int from) {
broadcastUpdate(new UpdateTSK()); broadcastUpdate(new UpdateTSK());
} }
private void broadcastUpdate(ServerMessage updateMessage) { /**
for (var entry : logic.getGame().getPlayers().entrySet()) { * Handles the ClientStartGame message.
logic.send(entry.getValue(), updateMessage); *
} * @param msg the ClientStartGame message
} */
@Override @Override
public void receivedStartGame(ClientStartGame msg) { public void receivedStartGame(ClientStartGame msg, int from) {
// todo: implement?? if (Boolean.TRUE.equals(logic.getGame().allRanked())) {
} broadcastUpdate(new ServerStartGame());
@Override
public void update() {
if (logic.getGame().getGameHasStarted()) {
parent.gotoState(new GameState(parent, logic)); parent.gotoState(new GameState(parent, logic));
logic.getGame().removeObserver(this);
} }
} }
} }

View File

@@ -1,7 +1,55 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Player;
import pp.mdga.message.server.WaitPiece;
public class NoPiece extends ServerState { public class NoPiece extends ServerState {
public NoPiece(ServerState parent, ServerGameLogic logic) { public NoPiece(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
entry();
}
@Override
public void entry() {
// if (hasTurbo() || turbo == 0) {
// if (roll == 6 &&
// logic.getGame().getBoard().getPlayerData().getWaitingArea().hasPieces() &&
// logic.getGame().getBoard().getNodes().getStartNode(Color).isOccupied()) {
// parent.gotoState(new WaitingPiece(parent, logic));
// } else {
// parent.gotoState(new NoTurn(parent, logic));
// }
// } else {
// validateHasPieces();
// }
}
private void validateHasPieces() {
// if (logic.getGame().getBoard().getPlayerData().getWaitingArea().hasPieces()) {
// if (logic.getGame().getBoard().getNodes().getStartNode(Color).isOccupied()) {
// if (roll == 6) {
// logic.send(new Player(1), new WaitPiece());
// } else {
// validateMove();
// }
// } else {
// if (logic.getGame().getBoard().getNodes().getStartNode(Color).getPiece().canMove()) {
// logic.send(new Player(1), new WaitPiece());
// parent.gotoState(new StartPiece(parent, logic));
// } else {
// validateMove();
// }
// }
// } else {
// validateMove();
// }
}
private void validateMove() {
// if (player.canMove()) {
// parent.gotoState(new NoTurn(parent, logic));
// } else {
// logic.send(new Player(1), new SelectPiece());
// }
} }
} }

View File

@@ -1,7 +1,14 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.message.server.EndOfTurn;
public class NoTurn extends ServerState { public class NoTurn extends ServerState {
public NoTurn(ServerState parent, ServerGameLogic logic) { public NoTurn(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void sentEndOfTurn(EndOfTurn msg, int from) {
// todo: goto end of turn
}
} }

View File

@@ -0,0 +1,7 @@
package pp.mdga.server;
public class PlayPowerCard extends ServerState {
public PlayPowerCard(ServerState parent, ServerGameLogic logic) {
super(parent, logic);
}
}

View File

@@ -1,7 +1,54 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Player;
import pp.mdga.message.client.NoPowerCard;
import pp.mdga.message.client.SelectCard;
import pp.mdga.message.client.SelectedPieces;
import pp.mdga.message.server.DiceNow;
import pp.mdga.message.server.PossibleCard;
public class PowerCard extends ServerState { public class PowerCard extends ServerState {
public PowerCard(ServerState parent, ServerGameLogic logic) { public PowerCard(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
logic.getGame().addObserver(this);
}
@Override
public void receivedNoPowerCard(NoPowerCard msg, int from) {
// todo: send to everyone? or one player?
// todo: right msg?
logic.send(new Player(1), new DiceNow());
parent.gotoState(new RollDice(parent, logic));
}
@Override
public void receivedSelectCard(SelectCard msg, int from) {
// todo: send to everyone? or one player?
logic.send(new Player(1), new PossibleCard());
}
@Override
public void receivedSelectedPieces(SelectedPieces msg, int from) {
// if (verifySelectedPieces()) {
// // todo: send to everyone? or one player?
// // todo: msg PowerCardAnimation?
// logic.send(new Player(1), new PowerCardAnimation());
// parent.gotoState(new PlayPowerCard(parent, logic));
// }
}
@Override
public void sentPossibleCard(PossibleCard msg, int from) {
// todo: implement
}
@Override
public void update() {
if (!super.getMoveablePieces(logic.getGame().getActiveColor()).isEmpty()) {
// todo: send to everyone? or one player?
// todo: right msg?
logic.send(new Player(1), new DiceNow());
parent.gotoState(new RollDice(parent, logic));
}
} }
} }

View File

@@ -6,7 +6,7 @@ public RollDiceMachine(ServerState parent, ServerGameLogic logic) {
} }
@Override @Override
public ServerState initialState() { public FirstRoll initialState() {
return null; return new FirstRoll(this, logic);
} }
} }

View File

@@ -1,7 +1,25 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Player;
import pp.mdga.message.client.RequestDice;
import pp.mdga.message.server.Dice;
import pp.mdga.message.server.DiceAgain;
public class SecondRoll extends ServerState { public class SecondRoll extends ServerState {
public SecondRoll(ServerState parent, ServerGameLogic logic) { public SecondRoll(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void receivedRequestDice(RequestDice msg, int from) {
// if (roll == 6) {
// // todo: send to everyone? or one player?
// logic.send(new Player(1), new Dice());
// // todo: goto ChoosePiece
// } else {
// // todo: send to everyone? or one player?
// logic.send(new Player(1), new DiceAgain());
// parent.gotoState(new ThirdRoll(parent, logic));
// }
}
} }

View File

@@ -1,7 +1,22 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Piece;
import pp.mdga.game.Player;
import pp.mdga.message.client.RequestMove;
import pp.mdga.message.server.StartPiece;
public class SelectPiece extends ServerState { public class SelectPiece extends ServerState {
public SelectPiece(ServerState parent, ServerGameLogic logic) { public SelectPiece(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void receivedRequestMove(RequestMove msg, int from) {
// if (verifyPiece(p)) {
// logic.send(new Player(1), new Animation());
// // todo: goto state
// } else {
// logic.send(new Player(1), new StartPiece());
// }
}
} }

View File

@@ -1,11 +1,25 @@
package pp.mdga.server; package pp.mdga.server;
/**
* The ServerAutomaton class represents the top-level state machine for the server.
* It initializes the state machine and sets the initial state to Lobby.
*/
public class ServerAutomaton extends ServerStateMachine { public class ServerAutomaton extends ServerStateMachine {
/**
* Constructs a new ServerAutomaton with the specified game logic.
*
* @param logic the server game logic
*/
public ServerAutomaton(ServerGameLogic logic) { public ServerAutomaton(ServerGameLogic logic) {
super(null, logic); super(null, logic);
entry(); entry();
} }
/**
* Returns the initial state of the state machine, which is Lobby.
*
* @return the initial state
*/
@Override @Override
public Lobby initialState() { public Lobby initialState() {
return new Lobby(this, logic); return new Lobby(this, logic);

View File

@@ -125,7 +125,6 @@ public Game getGame() {
return game; return game;
} }
// todo: remove
public ServerState getState() { public ServerState getState() {
return state; return state;
} }

View File

@@ -1,63 +1,322 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Color;
import pp.mdga.game.Node;
import pp.mdga.game.Piece; import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.game.PlayerData;
import pp.mdga.message.client.*; import pp.mdga.message.client.*;
import pp.mdga.message.server.EndOfTurn;
import pp.mdga.message.server.PossibleCard; import pp.mdga.message.server.PossibleCard;
import java.lang.System.Logger; import pp.mdga.message.server.RankingResponce;
import pp.mdga.message.server.ServerMessage;
public abstract class ServerState implements Observer{ import java.lang.System.Logger;
import java.util.ArrayList;
import java.util.List;
/**
* Abstract class representing a state in the server's state machine.
* Implements the Observer pattern to observe changes in the game state.
*/
public abstract class ServerState implements Observer {
/**
* Logger for logging messages within the application.
*/
protected static final Logger LOGGER = System.getLogger(ServerState.class.getName()); protected static final Logger LOGGER = System.getLogger(ServerState.class.getName());
/**
* The parent state of the current state.
*/
protected ServerState parent; protected ServerState parent;
/**
* The game logic associated with the server state.
*/
protected ServerGameLogic logic; protected ServerGameLogic logic;
public ServerState(ServerState parent, ServerGameLogic logic) { /**
* Constructs a new ServerState with the specified parent state and game logic.
*
* @param parent the parent state of the current state
* @param logic the game logic associated with the server state
*/
protected ServerState(ServerState parent, ServerGameLogic logic) {
this.parent = parent; this.parent = parent;
this.logic = logic; this.logic = logic;
} }
/**
* This method is called when the state is entered.
*/
public void entry() { /* do nothing */ } public void entry() { /* do nothing */ }
public void receivedAnimationEnd(AnimationEnd msg) { /* do nothing */ } /**
* This method is called when the state is exited.
*/
public void exit() { /* do nothing */ }
// todo piece? /**
public void receivedConfirmPiece(Piece piece) { /* do nothing */ } * This method is called when an animation ends.
*
* @param msg the animation end message
*/
public void receivedAnimationEnd(AnimationEnd msg, int from) { /* do nothing */ }
public void receivedDeselectTSK(DeselectTSK msg) { /* do nothing */ } /**
* This method is called when a TSK is deselected.
*
* @param msg the deselect TSK message
*/
public void receivedDeselectTSK(DeselectTSK msg, int from) { /* do nothing */ }
public void receivedNoPowerCard(NoPowerCard msg) { /* do nothing */ } /**
* This method is called when a NoPowerCard message is received.
*
* @param msg the NoPowerCard message
*/
public void receivedNoPowerCard(NoPowerCard msg, int from) { /* do nothing */ }
public void receivedNotReady(LobbyNotReady msg) { /* do nothing */ } /**
* This method is called when a LobbyNotReady message is received.
*
* @param msg the LobbyNotReady message
*/
public void receivedNotReady(LobbyNotReady msg, int from) { /* do nothing */ }
public void receivedPowerCardChoice(SelectCard msg) { /* do nothing */ } /**
* This method is called when a LobbyReady message is received.
*
* @param msg the LobbyReady message
*/
public void receivedReady(LobbyReady msg, int from) { /* do nothing */ }
public void receivedReady(LobbyReady msg) { /* do nothing */ } /**
* This method is called when a RequestDice message is received.
*
* @param msg the RequestDice message
*/
public void receivedRequestDice(RequestDice msg, int from) { /* do nothing */ }
public void receivedRequestDice(RequestDice msg) { /* do nothing */ } /**
* This method is called when a RequestMove message is received.
*
* @param msg the RequestMove message
*/
public void receivedRequestMove(RequestMove msg, int from) { /* do nothing */ }
// todo msg? /**
public void receivedRollRankingDice() { /* do nothing */ } * This method is called when a SelectCard message is received.
*
* @param msg the SelectCard message
*/
public void receivedSelectCard(SelectCard msg, int from) { /* do nothing */ }
public void receivedSelectTSK(SelectTSK msg) { /* do nothing */ } /**
* This method is called when a SelectTSK message is received.
*
* @param msg the SelectTSK message
*/
public void receivedSelectTSK(SelectTSK msg, int from) { /* do nothing */ }
public void receivedSelectedPieces(SelectedPieces msg) { /* do nothing */ } /**
* This method is called when a SelectedPieces message is received.
*
* @param msg the SelectedPieces message
*/
public void receivedSelectedPieces(SelectedPieces msg, int from) { /* do nothing */ }
public void receivedStartGame(ClientStartGame msg) { /* do nothing */ } /**
* This method is called when a StartGame message is received.
*
* @param msg the StartGame message
*/
public void receivedStartGame(ClientStartGame msg, int from) { /* do nothing */ }
public void sentPossibleCard(PossibleCard msg) { /* do nothing */ } /**
* This method is called when an EndOfTurn message is sent.
*
* @param msg the EndOfTurn message
*/
public void sentEndOfTurn(EndOfTurn msg, int from) { /* do nothing */ }
// todo msg?, sent to everyone? /**
public void sentRankingResponse() { /* do nothing */ } * This method is called when a PossibleCard message is sent.
*
* @param msg the PossibleCard message
*/
public void sentPossibleCard(PossibleCard msg, int from) { /* do nothing */ }
/**
* This method is called when a RankingResponce message is sent.
*
* @param msg the RankingResponce message
*/
public void sentRankingResponse(RankingResponce msg, int from) { /* do nothing */ }
/**
* This method transitions to a new state.
*
* @param state the new state to transition to
* @throws IllegalStateException if called outside a state machine
*/
public void gotoState(ServerState state) { public void gotoState(ServerState state) {
throw new IllegalStateException("not in a statemachine"); throw new IllegalStateException("not in a statemachine");
} }
/**
* Returns the parent state of the current state.
*
* @return the parent state
*/
public ServerState getParent() { public ServerState getParent() {
return parent; return parent;
} }
/**
* This method is called when the observed object is changed.
* It is part of the Observer pattern implementation.
*/
public void update() { /* do nothing */ } public void update() { /* do nothing */ }
/**
* This method is used to calculate the steps a piece can move
*
* @return the steps a piece can move
*/
private int calculateSteps() {
return logic.getGame().getDiceEyes() * logic.getGame().getDiceModifier();
}
/**
* This method is used to test if u can move a piece
*
* @param piece the piece to be moved
* @return true if the piece can be moved, false otherwise
*/
protected boolean tryMove(Piece piece) {
int steps = calculateSteps();
if (piece.getState() == PieceState.HOME) {
return tryHomeMove(piece, steps);
} else {
int homeMoves = getHomeMoves(piece, steps);
if (homeMoves > 0) {
return tryHomeMove(piece, homeMoves);
} else {
return tryInfieldMove(piece, steps);
}
}
}
/**
* This method is used to determine if a piece would move into the home area.
*
* @param piece the piece to be moved
* @param steps the steps the piece would move
* @return the number of steps the piece would move into the home area
*/
protected int getHomeMoves(Piece piece, int steps) {
int figureIndex = logic.getGame().getBoard().getInfieldIndexOfPiece(piece);
Color col = piece.getColor();
int startIndex = logic.getGame().getBoard().getPlayerData().get(col).getStartNodeIndex();
int moveIndex = startIndex + steps;
if (moveIndex > logic.getGame().getBoard().getInfield().length) {
moveIndex %= logic.getGame().getBoard().getInfield().length;
if (moveIndex >= startIndex) {
return moveIndex - startIndex + 1;
}
} else if (figureIndex < startIndex && moveIndex >= startIndex) {
return moveIndex - startIndex + 1;
}
return 0;
}
/**
* This method is used to determine if a piece can move in the infield
*
* @param piece the piece to be moved
* @param steps the steps the piece would move
* @return true if the piece can move in the infield, false otherwise
*/
protected boolean tryInfieldMove(Piece piece, int steps) {
int figureIndex = logic.getGame().getBoard().getInfieldIndexOfPiece(piece);
int moveIndex = (figureIndex + steps) % logic.getGame().getBoard().getInfield().length;
Piece occupant = logic.getGame().getBoard().getInfield()[moveIndex].getOccupant();
if (occupant != null) {
return occupant.getColor() != piece.getColor();
}
return true;
}
/**
* This method is used to determine if a piece can move inside the home area
*
* @param piece the piece to be moved
* @param steps the steps the piece would move
* @return true if the piece can move into the home area, false otherwise
*/
protected boolean tryHomeMove(Piece piece, int steps) {
Color col = piece.getColor();
PlayerData playerData = logic.getGame().getBoard().getPlayerData().get(col);
Node[] homeNodes = playerData.getHomeNodes();
int index;
if (playerData.homeIncludes(piece)) {
index = playerData.getIndexInHome(piece);
} else {
index = 0;
}
if (index + steps >= homeNodes.length) {
return false;
} else {
for (int i = index; i <= index + steps; i++) {
if (homeNodes[i].getOccupant() != null) {
return false;
}
}
return true;
}
}
/**
* This method is used to get the pieces that can be moved
*
* @param color the color of the pieces
* @return the pieces that can be moved
*/
protected List<Piece> getMoveablePieces(Color color) {
ArrayList<Piece> moveablePieces = new ArrayList<>();
ArrayList<Piece> pieces = new ArrayList<>();
for (Piece piece : logic.getGame().getBoard().getPlayerData().get(color).getPieces()) {
if (piece.getState() == PieceState.ACTIVE || piece.getState() == PieceState.HOME) {
pieces.add(piece);
}
}
for (Piece piece : pieces) {
if (tryMove(piece)) {
moveablePieces.add(piece);
}
}
return moveablePieces;
}
/**
* Broadcasts an update message to all players.
*
* @param updateMessage the update message to be sent
*/
protected void broadcastUpdate(ServerMessage updateMessage) {
for (var entry : logic.getGame().getPlayers().entrySet()) {
logic.send(entry.getValue(), updateMessage);
}
}
/**
* Returns a string representation of the object.
*
* @return the simple name of the class
*/
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName(); return getClass().getSimpleName();

View File

@@ -1,25 +1,50 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Piece;
import pp.mdga.message.client.*; import pp.mdga.message.client.*;
import pp.mdga.message.server.EndOfTurn;
import pp.mdga.message.server.PossibleCard; import pp.mdga.message.server.PossibleCard;
import pp.mdga.message.server.RankingResponce;
/**
* Abstract class representing a state machine for the server.
* It manages the transitions between different states and delegates
* the handling of messages to the current state.
*/
public abstract class ServerStateMachine extends ServerState { public abstract class ServerStateMachine extends ServerState {
/**
* The current state of the state machine.
*/
private ServerState state; private ServerState state;
public ServerStateMachine(ServerState parent, ServerGameLogic logic) { /**
* Constructs a new instance of ServerStateMachine.
*
* @param parent the parent state
* @param logic the server game logic
*/
protected ServerStateMachine(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
/**
* Creates the initial state of a state machine.
*/
public abstract ServerState initialState(); public abstract ServerState initialState();
/**
* Transitions to a new state.
*
* @param newState the new state to transition to
*/
@Override @Override
public void gotoState(ServerState newState) { public void gotoState(ServerState newState) {
LOGGER.log(System.Logger.Level.DEBUG, "{0}: {1} --> {2}", this, state, newState); LOGGER.log(System.Logger.Level.DEBUG, "{0}: {1} --> {2}", this, state, newState);
enter(newState); enter(newState);
} }
/**
* This method is called when the state is entered.
*/
@Override @Override
public void entry() { public void entry() {
final ServerState newState = initialState(); final ServerState newState = initialState();
@@ -27,6 +52,12 @@ public void entry() {
enter(newState); enter(newState);
} }
/**
* Enters a new state.
*
* @param newState the new state to enter
* @throws IllegalArgumentException if the new state does not belong to this state machine
*/
private void enter(ServerState newState) { private void enter(ServerState newState) {
if (newState.parent != this) if (newState.parent != this)
throw new IllegalArgumentException("Wrong state: " + newState + " belongs to " + newState.parent + " instead of " + this); throw new IllegalArgumentException("Wrong state: " + newState + " belongs to " + newState.parent + " instead of " + this);
@@ -34,81 +65,170 @@ private void enter(ServerState newState) {
state.entry(); state.entry();
} }
/**
* This method is called when the state is exited.
*/
@Override @Override
public void receivedAnimationEnd(AnimationEnd msg) { public void exit() {
state.receivedAnimationEnd(msg); state.exit();
} }
// todo piece? /**
* This method is called when an animation ends.
*
* @param msg the animation end message
*/
@Override @Override
public void receivedConfirmPiece(Piece piece) { public void receivedAnimationEnd(AnimationEnd msg, int from) {
state.receivedConfirmPiece(piece); state.receivedAnimationEnd(msg, from);
} }
/**
* This method is called when a TSK is deselected.
*
* @param msg the deselect TSK message
*/
@Override @Override
public void receivedDeselectTSK(DeselectTSK msg) { public void receivedDeselectTSK(DeselectTSK msg, int from) {
state.receivedDeselectTSK(msg); state.receivedDeselectTSK(msg, from);
} }
/**
* This method is called when a NoPowerCard message is received.
*
* @param msg the NoPowerCard message
*/
@Override @Override
public void receivedNoPowerCard(NoPowerCard msg) { public void receivedNoPowerCard(NoPowerCard msg, int from) {
state.receivedNoPowerCard(msg); state.receivedNoPowerCard(msg, from);
} }
/**
* This method is called when a LobbyNotReady message is received.
*
* @param msg the LobbyNotReady message
*/
@Override @Override
public void receivedNotReady(LobbyNotReady msg) { public void receivedNotReady(LobbyNotReady msg, int from) {
state.receivedNotReady(msg); state.receivedNotReady(msg, from);
} }
/**
* This method is called when a LobbyReady message is received.
*
* @param msg the LobbyReady message
*/
@Override @Override
public void receivedPowerCardChoice(SelectCard msg) { public void receivedReady(LobbyReady msg, int from) {
state.receivedPowerCardChoice(msg); state.receivedReady(msg, from);
} }
/**
* This method is called when a RequestDice message is received.
*
* @param msg the RequestDice message
*/
@Override @Override
public void receivedReady(LobbyReady msg) { public void receivedRequestDice(RequestDice msg, int from) {
state.receivedReady(msg); state.receivedRequestDice(msg, from);
} }
/**
* This method is called when a RequestMove message is received.
*
* @param msg the RequestMove message
*/
@Override @Override
public void receivedRequestDice(RequestDice msg) { public void receivedRequestMove(RequestMove msg, int from) {
state.receivedRequestDice(msg); state.receivedRequestMove(msg, from);
} }
// todo msg? /**
* This method is called when a SelectCard message is received.
*
* @param msg the SelectCard message
*/
@Override @Override
public void receivedRollRankingDice() { public void receivedSelectCard(SelectCard msg, int from) {
state.receivedRollRankingDice(); state.receivedSelectCard(msg, from);
} }
/**
* This method is called when a SelectTSK message is received.
*
* @param msg the SelectTSK message
*/
@Override @Override
public void receivedSelectTSK(SelectTSK msg) { public void receivedSelectTSK(SelectTSK msg, int from) {
state.receivedSelectTSK(msg); state.receivedSelectTSK(msg, from);
} }
/**
* This method is called when a SelectedPieces message is received.
*
* @param msg the SelectedPieces message
*/
@Override @Override
public void receivedSelectedPieces(SelectedPieces msg) { public void receivedSelectedPieces(SelectedPieces msg, int from) {
state.receivedSelectedPieces(msg); state.receivedSelectedPieces(msg, from);
} }
/**
* This method is called when a StartGame message is received.
*
* @param msg the StartGame message
*/
@Override @Override
public void receivedStartGame(ClientStartGame msg) { public void receivedStartGame(ClientStartGame msg, int from) {
state.receivedStartGame(msg); state.receivedStartGame(msg, from);
} }
/**
* This method is called when an EndOfTurn message is sent.
*
* @param msg the EndOfTurn message
*/
@Override @Override
public void sentPossibleCard(PossibleCard msg) { public void sentEndOfTurn(EndOfTurn msg, int from) {
state.sentPossibleCard(msg); state.sentEndOfTurn(msg, from);
} }
// todo msg?, sent to everyone? /**
* This method is called when a PossibleCard message is sent.
*
* @param msg the PossibleCard message
*/
@Override @Override
public void sentRankingResponse() { public void sentPossibleCard(PossibleCard msg, int from) {
state.sentRankingResponse(); state.sentPossibleCard(msg, from);
} }
/**
* This method is called when a RankingResponce message is sent.
*
* @param msg the RankingResponce message
*/
@Override
public void sentRankingResponse(RankingResponce msg, int from) {
state.sentRankingResponse(msg, from);
}
/**
* Returns a string representation of the object, including the current state.
*
* @return the string representation of the object
*/
@Override @Override
public String toString() { public String toString() {
return super.toString() + "(in " + state + ")"; return super.toString() + "(in " + state + ")";
} }
/**
* Returns the current state.
*
* @return the current state
*/
public ServerState getState() {
return state;
}
} }

View File

@@ -1,7 +1,21 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Piece;
import pp.mdga.game.Player;
import pp.mdga.message.client.RequestMove;
public class StartPiece extends ServerState { public class StartPiece extends ServerState {
public StartPiece(ServerState parent, ServerGameLogic logic) { public StartPiece(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void receivedRequestMove(RequestMove msg, int from) {
// if (verifyPiece(p)) {
// logic.send(new Player(1), new Animation());
// // todo: goto state
// } else {
// logic.send(new Player(1), new pp.mdga.message.server.StartPiece());
// }
}
} }

View File

@@ -1,7 +1,25 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Player;
import pp.mdga.message.client.RequestDice;
import pp.mdga.message.server.Dice;
import pp.mdga.message.server.DiceAgain;
public class ThirdRoll extends ServerState { public class ThirdRoll extends ServerState {
public ThirdRoll(ServerState parent, ServerGameLogic logic) { public ThirdRoll(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void receivedRequestDice(RequestDice msg, int from) {
// if (roll == 6) {
// // todo: send to everyone? or one player?
// logic.send(new Player(1), new Dice());
// // todo: goto ChoosePiece
// } else {
// // todo: send to everyone? or one player?
// logic.send(new Player(1), new DiceAgain());
// // todo: goto End from Turn
// }
}
} }

View File

@@ -1,9 +1,36 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.client.Spectator;
import pp.mdga.game.Player;
import pp.mdga.message.server.ActivePlayer;
import pp.mdga.message.server.Ceremony;
import pp.mdga.message.server.EndOfTurn;
public class Turn extends ServerState { public class Turn extends ServerState {
private final TurnStateMachine turnStateMachine = new TurnStateMachine(this, logic); private final TurnStateMachine turnStateMachine = new TurnStateMachine(this, logic);
public Turn(ServerState parent, ServerGameLogic logic) { public Turn(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
// todo: when TurnStateMachine is in the end state, and then?
@Override
public void exit() {
Player player = logic.getGame().getStartPlayer();
// if (player.isFinished()) {
// logic.send(player, new Spectator());
// } else {
// logic.send(player, new EndOfTurn());
// }
if (logic.getGame().getPlayers().size() == 1) {
broadcastUpdate(new Ceremony());
this.getParent().getParent().exit();
} else {
// todo: next player
broadcastUpdate(new ActivePlayer());
parent.gotoState(new Animation(parent, logic));
}
}
} }

View File

@@ -6,7 +6,7 @@ public TurnStateMachine(ServerState parent, ServerGameLogic logic) {
} }
@Override @Override
public ServerState initialState() { public PowerCard initialState() {
return null; return new PowerCard(this, logic);
} }
} }

View File

@@ -1,7 +1,23 @@
package pp.mdga.server; package pp.mdga.server;
import pp.mdga.game.Piece;
import pp.mdga.game.Player;
import pp.mdga.message.client.RequestDice;
import pp.mdga.message.client.RequestMove;
import pp.mdga.message.server.StartPiece;
public class WaitingPiece extends ServerState { public class WaitingPiece extends ServerState {
public WaitingPiece(ServerState parent, ServerGameLogic logic) { public WaitingPiece(ServerState parent, ServerGameLogic logic) {
super(parent, logic); super(parent, logic);
} }
@Override
public void receivedRequestMove(RequestMove msg, int from) {
// if (verifyPiece(p)) {
// logic.send(new Player(1), new Animation());
// // todo: goto state
// } else {
// logic.send(new Player(1), new StartPiece());
// }
}
} }