added more logic for the server state diagram
This commit is contained in:
@@ -301,7 +301,6 @@ public Boolean getGameHasStarted() {
|
||||
*/
|
||||
public void setGameHasStarted(Boolean gameHasStarted) {
|
||||
this.gameHasStarted = gameHasStarted;
|
||||
if (gameHasStarted) notifyObservers();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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) {
|
||||
@@ -8,7 +9,8 @@ public Animation(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedAnimationEnd(AnimationEnd msg) {
|
||||
public void receivedAnimationEnd(AnimationEnd msg, int from) {
|
||||
logic.send(logic.getGame().getStartPlayer(), new DiceNow());
|
||||
parent.gotoState(new Turn(parent, logic));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,41 +2,43 @@
|
||||
|
||||
import pp.mdga.game.Player;
|
||||
import pp.mdga.message.client.RequestDice;
|
||||
import pp.mdga.message.server.DiceNow;
|
||||
import pp.mdga.message.server.RankingRollAgain;
|
||||
import pp.mdga.message.server.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DetermineStartPlayer extends ServerState {
|
||||
private final List<Player> player = new ArrayList<>();
|
||||
|
||||
public DetermineStartPlayer(ServerState parent, ServerGameLogic logic) {
|
||||
super(parent, logic);
|
||||
logic.getGame().addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg) {
|
||||
// todo: implement
|
||||
}
|
||||
public void receivedRequestDice(RequestDice msg, int from) {
|
||||
logic.send();
|
||||
|
||||
// todo: msg?, sent to everyone?
|
||||
@Override
|
||||
public void sentRankingResponse() {
|
||||
// todo: implemtent
|
||||
broadcastUpdate(new Dice());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
if (logic.getGame().allRanked()) {
|
||||
if (logic.getGame().getStartPlayer() == null) {
|
||||
// todo: send it to everyone? or player with the same dice value? save the players with the same value?
|
||||
logic.send(new Player(1), new RankingRollAgain());
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
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 FirstRoll extends ServerState {
|
||||
public FirstRoll(ServerState parent, ServerGameLogic logic) {
|
||||
@@ -11,21 +8,21 @@ public FirstRoll(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
// 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));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package pp.mdga.server;
|
||||
|
||||
import pp.mdga.game.Piece;
|
||||
import pp.mdga.message.client.*;
|
||||
import pp.mdga.message.server.PauseGame;
|
||||
import pp.mdga.message.server.PossibleCard;
|
||||
import pp.mdga.message.server.RankingResponce;
|
||||
|
||||
public class GameState extends ServerState {
|
||||
private final GameStateMachine gameStateMachine = new GameStateMachine(this, logic);
|
||||
@@ -18,58 +19,50 @@ public void entry() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedAnimationEnd(AnimationEnd msg) {
|
||||
gameStateMachine.receivedAnimationEnd(msg);
|
||||
}
|
||||
|
||||
// todo piece?
|
||||
@Override
|
||||
public void receivedConfirmPiece(Piece piece) {
|
||||
gameStateMachine.receivedConfirmPiece(piece);
|
||||
public void exit() {
|
||||
parent.gotoState(new Ceremony(parent, logic));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedNoPowerCard(NoPowerCard msg) {
|
||||
gameStateMachine.receivedNoPowerCard(msg);
|
||||
public void receivedAnimationEnd(AnimationEnd msg, int from) {
|
||||
gameStateMachine.receivedAnimationEnd(msg, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedSelectCard(SelectCard msg) {
|
||||
gameStateMachine.receivedSelectCard(msg);
|
||||
public void receivedNoPowerCard(NoPowerCard msg, int from) {
|
||||
gameStateMachine.receivedNoPowerCard(msg, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg) {
|
||||
gameStateMachine.receivedRequestDice(msg);
|
||||
}
|
||||
|
||||
// todo msg?
|
||||
@Override
|
||||
public void receivedRollRankingDice() {
|
||||
gameStateMachine.receivedRollRankingDice();
|
||||
public void receivedSelectCard(SelectCard msg, int from) {
|
||||
gameStateMachine.receivedSelectCard(msg, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedSelectedPieces(SelectedPieces msg) {
|
||||
gameStateMachine.receivedSelectedPieces(msg);
|
||||
public void receivedRequestDice(RequestDice msg, int from) {
|
||||
gameStateMachine.receivedRequestDice(msg, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sentPossibleCard(PossibleCard msg) {
|
||||
gameStateMachine.sentPossibleCard(msg);
|
||||
public void receivedSelectedPieces(SelectedPieces msg, int from) {
|
||||
gameStateMachine.receivedSelectedPieces(msg, from);
|
||||
}
|
||||
|
||||
// todo msg?, sent to everyone?
|
||||
@Override
|
||||
public void sentRankingResponse() {
|
||||
gameStateMachine.sentRankingResponse();
|
||||
public void sentPossibleCard(PossibleCard msg, int from) {
|
||||
gameStateMachine.sentPossibleCard(msg, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sentRankingResponse(RankingResponce msg, int from) {
|
||||
gameStateMachine.sentRankingResponse(msg, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
if (logic.getGame().playerHasDisconnected()) {
|
||||
parent.gotoState(new Interrupt(parent, logic));
|
||||
// todo: change to interrupt, save the last state of gamestatemachine, change from interrupt to gamestate has to restore the last state
|
||||
if (Boolean.TRUE.equals(logic.getGame().playerHasDisconnected())) {
|
||||
broadcastUpdate(new PauseGame());
|
||||
parent.gotoState(new Interrupt(parent, logic, this));
|
||||
logic.getGame().removeObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
package pp.mdga.server;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
super(parent, logic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the initial state of the state machine, which is DetermineStartPlayer.
|
||||
*
|
||||
* @return the initial state
|
||||
*/
|
||||
@Override
|
||||
public DetermineStartPlayer initialState() {
|
||||
return new DetermineStartPlayer(this, logic);
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
package pp.mdga.server;
|
||||
|
||||
import pp.mdga.message.server.ResumeGame;
|
||||
|
||||
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);
|
||||
this.lastState = lastState;
|
||||
logic.getGame().addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
if (!logic.getGame().gameIsInterrupted()) {
|
||||
parent.gotoState(new GameState(parent, logic));
|
||||
if (Boolean.FALSE.equals(logic.getGame().gameIsInterrupted())) {
|
||||
broadcastUpdate(new ResumeGame());
|
||||
parent.gotoState(lastState);
|
||||
logic.getGame().removeObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +1,74 @@
|
||||
package pp.mdga.server;
|
||||
|
||||
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.UpdateTSK;
|
||||
|
||||
/**
|
||||
* Represents the lobby state of the server.
|
||||
*/
|
||||
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) {
|
||||
super(parent, logic);
|
||||
logic.getGame().addObserver(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the DeselectTSK message.
|
||||
*
|
||||
* @param msg the DeselectTSK message
|
||||
*/
|
||||
@Override
|
||||
public void receivedDeselectTSK(DeselectTSK msg) {
|
||||
public void receivedDeselectTSK(DeselectTSK msg, int from) {
|
||||
broadcastUpdate(new UpdateTSK());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the LobbyNotReady message.
|
||||
*
|
||||
* @param msg the LobbyNotReady message
|
||||
*/
|
||||
@Override
|
||||
public void receivedNotReady(LobbyNotReady msg) {
|
||||
public void receivedNotReady(LobbyNotReady msg, int from) {
|
||||
broadcastUpdate(new UpdateReady());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the LobbyReady message.
|
||||
*
|
||||
* @param msg the LobbyReady message
|
||||
*/
|
||||
@Override
|
||||
public void receivedReady(LobbyReady msg) {
|
||||
public void receivedReady(LobbyReady msg, int from) {
|
||||
broadcastUpdate(new UpdateReady());
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the SelectTSK message.
|
||||
*
|
||||
* @param msg the SelectTSK message
|
||||
*/
|
||||
@Override
|
||||
public void receivedSelectTSK(SelectTSK msg) {
|
||||
public void receivedSelectTSK(SelectTSK msg, int from) {
|
||||
broadcastUpdate(new UpdateTSK());
|
||||
}
|
||||
|
||||
private void broadcastUpdate(ServerMessage updateMessage) {
|
||||
for (var entry : logic.getGame().getPlayers().entrySet()) {
|
||||
logic.send(entry.getValue(), updateMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the ClientStartGame message.
|
||||
*
|
||||
* @param msg the ClientStartGame message
|
||||
*/
|
||||
@Override
|
||||
public void receivedStartGame(ClientStartGame msg) {
|
||||
// todo: implement??
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
if (logic.getGame().getGameHasStarted()) {
|
||||
public void receivedStartGame(ClientStartGame msg, int from) {
|
||||
if (Boolean.TRUE.equals(logic.getGame().allRanked())) {
|
||||
broadcastUpdate(new ServerStartGame());
|
||||
parent.gotoState(new GameState(parent, logic));
|
||||
logic.getGame().removeObserver(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,45 +11,45 @@ public NoPiece(ServerState parent, ServerGameLogic logic) {
|
||||
|
||||
@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();
|
||||
}
|
||||
// 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();
|
||||
}
|
||||
// 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());
|
||||
}
|
||||
// if (player.canMove()) {
|
||||
// parent.gotoState(new NoTurn(parent, logic));
|
||||
// } else {
|
||||
// logic.send(new Player(1), new SelectPiece());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public NoTurn(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sentEndOfTurn(EndOfTurn msg) {
|
||||
public void sentEndOfTurn(EndOfTurn msg, int from) {
|
||||
// todo: goto end of turn
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ public PowerCard(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedNoPowerCard(NoPowerCard msg) {
|
||||
public void receivedNoPowerCard(NoPowerCard msg, int from) {
|
||||
// todo: send to everyone? or one player?
|
||||
// todo: right msg?
|
||||
logic.send(new Player(1), new DiceNow());
|
||||
@@ -22,29 +22,29 @@ public void receivedNoPowerCard(NoPowerCard msg) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedSelectCard(SelectCard msg) {
|
||||
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) {
|
||||
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));
|
||||
}
|
||||
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) {
|
||||
public void sentPossibleCard(PossibleCard msg, int from) {
|
||||
// todo: implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
if (!logic.getGame().getMovablePieces()) {
|
||||
if (!super.getMoveablePieces(logic.getGame().getActiveColor()).isEmpty()) {
|
||||
// todo: send to everyone? or one player?
|
||||
// todo: right msg?
|
||||
logic.send(new Player(1), new DiceNow());
|
||||
|
||||
@@ -11,15 +11,15 @@ public SecondRoll(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg) {
|
||||
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));
|
||||
}
|
||||
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));
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
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 {
|
||||
@@ -10,12 +11,12 @@ public SelectPiece(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedConfirmPiece(Piece p) {
|
||||
if (verifyPiece(p)) {
|
||||
logic.send(new Player(1), new Animation());
|
||||
// todo: goto state
|
||||
} else {
|
||||
logic.send(new Player(1), new StartPiece());
|
||||
}
|
||||
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());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,25 @@
|
||||
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 {
|
||||
/**
|
||||
* Constructs a new ServerAutomaton with the specified game logic.
|
||||
*
|
||||
* @param logic the server game logic
|
||||
*/
|
||||
public ServerAutomaton(ServerGameLogic logic) {
|
||||
super(null, logic);
|
||||
entry();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the initial state of the state machine, which is Lobby.
|
||||
*
|
||||
* @return the initial state
|
||||
*/
|
||||
@Override
|
||||
public Lobby initialState() {
|
||||
return new Lobby(this, logic);
|
||||
|
||||
@@ -125,7 +125,6 @@ public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
// todo: remove
|
||||
public ServerState getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,50 @@
|
||||
package pp.mdga.server;
|
||||
|
||||
import pp.mdga.game.Piece;
|
||||
import pp.mdga.message.client.*;
|
||||
import pp.mdga.message.server.EndOfTurn;
|
||||
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 {
|
||||
|
||||
/**
|
||||
* The current state of the state machine.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the initial state of a state machine.
|
||||
*/
|
||||
public abstract ServerState initialState();
|
||||
|
||||
/**
|
||||
* Transitions to a new state.
|
||||
*
|
||||
* @param newState the new state to transition to
|
||||
*/
|
||||
@Override
|
||||
public void gotoState(ServerState newState) {
|
||||
LOGGER.log(System.Logger.Level.DEBUG, "{0}: {1} --> {2}", this, state, newState);
|
||||
enter(newState);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when the state is entered.
|
||||
*/
|
||||
@Override
|
||||
public void entry() {
|
||||
final ServerState newState = initialState();
|
||||
@@ -28,6 +52,12 @@ public void entry() {
|
||||
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) {
|
||||
if (newState.parent != this)
|
||||
throw new IllegalArgumentException("Wrong state: " + newState + " belongs to " + newState.parent + " instead of " + this);
|
||||
@@ -35,86 +65,170 @@ private void enter(ServerState newState) {
|
||||
state.entry();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when the state is exited.
|
||||
*/
|
||||
@Override
|
||||
public void receivedAnimationEnd(AnimationEnd msg) {
|
||||
state.receivedAnimationEnd(msg);
|
||||
public void exit() {
|
||||
state.exit();
|
||||
}
|
||||
|
||||
// todo piece?
|
||||
/**
|
||||
* This method is called when an animation ends.
|
||||
*
|
||||
* @param msg the animation end message
|
||||
*/
|
||||
@Override
|
||||
public void receivedConfirmPiece(Piece piece) {
|
||||
state.receivedConfirmPiece(piece);
|
||||
public void receivedAnimationEnd(AnimationEnd msg, int from) {
|
||||
state.receivedAnimationEnd(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a TSK is deselected.
|
||||
*
|
||||
* @param msg the deselect TSK message
|
||||
*/
|
||||
@Override
|
||||
public void receivedDeselectTSK(DeselectTSK msg) {
|
||||
state.receivedDeselectTSK(msg);
|
||||
public void receivedDeselectTSK(DeselectTSK msg, int from) {
|
||||
state.receivedDeselectTSK(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a NoPowerCard message is received.
|
||||
*
|
||||
* @param msg the NoPowerCard message
|
||||
*/
|
||||
@Override
|
||||
public void receivedNoPowerCard(NoPowerCard msg) {
|
||||
state.receivedNoPowerCard(msg);
|
||||
public void receivedNoPowerCard(NoPowerCard msg, int from) {
|
||||
state.receivedNoPowerCard(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a LobbyNotReady message is received.
|
||||
*
|
||||
* @param msg the LobbyNotReady message
|
||||
*/
|
||||
@Override
|
||||
public void receivedNotReady(LobbyNotReady msg) {
|
||||
state.receivedNotReady(msg);
|
||||
public void receivedNotReady(LobbyNotReady msg, int from) {
|
||||
state.receivedNotReady(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a LobbyReady message is received.
|
||||
*
|
||||
* @param msg the LobbyReady message
|
||||
*/
|
||||
@Override
|
||||
public void receivedSelectCard(SelectCard msg) {
|
||||
state.receivedSelectCard(msg);
|
||||
public void receivedReady(LobbyReady msg, int from) {
|
||||
state.receivedReady(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a RequestDice message is received.
|
||||
*
|
||||
* @param msg the RequestDice message
|
||||
*/
|
||||
@Override
|
||||
public void receivedReady(LobbyReady msg) {
|
||||
state.receivedReady(msg);
|
||||
public void receivedRequestDice(RequestDice msg, int from) {
|
||||
state.receivedRequestDice(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a RequestMove message is received.
|
||||
*
|
||||
* @param msg the RequestMove message
|
||||
*/
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg) {
|
||||
state.receivedRequestDice(msg);
|
||||
public void receivedRequestMove(RequestMove msg, int from) {
|
||||
state.receivedRequestMove(msg, from);
|
||||
}
|
||||
|
||||
// todo msg?
|
||||
/**
|
||||
* This method is called when a SelectCard message is received.
|
||||
*
|
||||
* @param msg the SelectCard message
|
||||
*/
|
||||
@Override
|
||||
public void receivedRollRankingDice() {
|
||||
state.receivedRollRankingDice();
|
||||
public void receivedSelectCard(SelectCard msg, int from) {
|
||||
state.receivedSelectCard(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a SelectTSK message is received.
|
||||
*
|
||||
* @param msg the SelectTSK message
|
||||
*/
|
||||
@Override
|
||||
public void receivedSelectTSK(SelectTSK msg) {
|
||||
state.receivedSelectTSK(msg);
|
||||
public void receivedSelectTSK(SelectTSK msg, int from) {
|
||||
state.receivedSelectTSK(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a SelectedPieces message is received.
|
||||
*
|
||||
* @param msg the SelectedPieces message
|
||||
*/
|
||||
@Override
|
||||
public void receivedSelectedPieces(SelectedPieces msg) {
|
||||
state.receivedSelectedPieces(msg);
|
||||
public void receivedSelectedPieces(SelectedPieces msg, int from) {
|
||||
state.receivedSelectedPieces(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a StartGame message is received.
|
||||
*
|
||||
* @param msg the StartGame message
|
||||
*/
|
||||
@Override
|
||||
public void receivedStartGame(ClientStartGame msg) {
|
||||
state.receivedStartGame(msg);
|
||||
public void receivedStartGame(ClientStartGame msg, int from) {
|
||||
state.receivedStartGame(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when an EndOfTurn message is sent.
|
||||
*
|
||||
* @param msg the EndOfTurn message
|
||||
*/
|
||||
@Override
|
||||
public void sentEndOfTurn(EndOfTurn msg) {
|
||||
state.sentEndOfTurn(msg);
|
||||
public void sentEndOfTurn(EndOfTurn msg, int from) {
|
||||
state.sentEndOfTurn(msg, from);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called when a PossibleCard message is sent.
|
||||
*
|
||||
* @param msg the PossibleCard message
|
||||
*/
|
||||
@Override
|
||||
public void sentPossibleCard(PossibleCard msg) {
|
||||
state.sentPossibleCard(msg);
|
||||
public void sentPossibleCard(PossibleCard msg, int from) {
|
||||
state.sentPossibleCard(msg, from);
|
||||
}
|
||||
|
||||
// todo msg?, sent to everyone?
|
||||
/**
|
||||
* This method is called when a RankingResponce message is sent.
|
||||
*
|
||||
* @param msg the RankingResponce message
|
||||
*/
|
||||
@Override
|
||||
public void sentRankingResponse() {
|
||||
state.sentRankingResponse();
|
||||
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
|
||||
public String toString() {
|
||||
return super.toString() + "(in " + state + ")";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current state.
|
||||
*
|
||||
* @return the current state
|
||||
*/
|
||||
public ServerState getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import pp.mdga.game.Piece;
|
||||
import pp.mdga.game.Player;
|
||||
import pp.mdga.message.client.RequestMove;
|
||||
|
||||
public class StartPiece extends ServerState {
|
||||
public StartPiece(ServerState parent, ServerGameLogic logic) {
|
||||
@@ -9,12 +10,12 @@ public StartPiece(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedConfirmPiece(Piece p) {
|
||||
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());
|
||||
}
|
||||
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());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,15 +11,15 @@ public ThirdRoll(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedRequestDice(RequestDice msg) {
|
||||
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
|
||||
}
|
||||
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
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
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 {
|
||||
private final TurnStateMachine turnStateMachine = new TurnStateMachine(this, logic);
|
||||
|
||||
@@ -8,4 +14,23 @@ public Turn(ServerState parent, ServerGameLogic 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
import pp.mdga.game.Piece;
|
||||
import pp.mdga.game.Player;
|
||||
import pp.mdga.message.client.AnimationEnd;
|
||||
import pp.mdga.message.client.RequestDice;
|
||||
import pp.mdga.message.client.RequestMove;
|
||||
import pp.mdga.message.server.StartPiece;
|
||||
|
||||
public class WaitingPiece extends ServerState {
|
||||
@@ -11,12 +12,12 @@ public WaitingPiece(ServerState parent, ServerGameLogic logic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receivedConfirmPiece(Piece p) {
|
||||
if (verifyPiece(p)) {
|
||||
logic.send(new Player(1), new Animation());
|
||||
// todo: goto state
|
||||
} else {
|
||||
logic.send(new Player(1), new StartPiece());
|
||||
}
|
||||
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());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user