diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerState.java b/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerState.java index a12b189f..949eb48f 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerState.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/server/ServerState.java @@ -1,195 +1,25 @@ package pp.mdga.server; -import pp.mdga.game.Color; -import pp.mdga.game.Node; -import pp.mdga.game.Piece; -import pp.mdga.game.PieceState; -import pp.mdga.game.PlayerData; -import pp.mdga.message.client.*; -import pp.mdga.message.server.EndOfTurn; -import pp.mdga.message.server.PossibleCard; -import java.lang.System.Logger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +/** + * Defines the behavior and state transitions for the server-side game logic. + * Different states of the game logic implement this interface to handle various game events and actions. + */ +public class ServerState { + /** + * The server logic object. + */ + private final ServerGameLogic logic; -public abstract class ServerState implements Observer{ - protected static final Logger LOGGER = System.getLogger(ServerState.class.getName()); - protected ServerState parent; - protected ServerGameLogic logic; - - public ServerState(ServerState parent, ServerGameLogic logic) { - this.parent = parent; + /** + * Constructs a server state of the specified game logic. + * + * @param logic the game logic + */ + public ServerState(ServerGameLogic logic) { this.logic = logic; } - public void entry() { /* do nothing */ } - public void receivedAnimationEnd(AnimationEnd msg) { /* do nothing */ } - // todo piece? - public void receivedConfirmPiece(Piece piece) { /* do nothing */ } - public void receivedDeselectTSK(DeselectTSK msg) { /* do nothing */ } - - public void receivedNoPowerCard(NoPowerCard msg) { /* do nothing */ } - - public void receivedNotReady(LobbyNotReady msg) { /* do nothing */ } - - public void receivedReady(LobbyReady msg) { /* do nothing */ } - - public void receivedRequestDice(RequestDice msg) { /* do nothing */ } - - // todo msg? - public void receivedRollRankingDice() { /* do nothing */ } - - public void receivedSelectCard(SelectCard msg) { /* do nothing */ } - - public void receivedSelectTSK(SelectTSK msg) { /* do nothing */ } - - public void receivedSelectedPieces(SelectedPieces msg) { /* do nothing */ } - - public void receivedStartGame(ClientStartGame msg) { /* do nothing */ } - - public void sentEndOfTurn(EndOfTurn msg) { /* do nothing */ } - - public void sentPossibleCard(PossibleCard msg) { /* do nothing */ } - - // todo msg?, sent to everyone? - public void sentRankingResponse() { /* do nothing */ } - - public void gotoState(ServerState state) { - throw new IllegalStateException("not in a statemachine"); - } - - public ServerState getParent() { - return parent; - } - - 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) { - if (piece.getState() == PieceState.HOME){ - return tryHomeMove(piece, calculateSteps()); - } else { - int homeMoves = getHomeMoves(piece, calculateSteps()); - if (homeMoves > 0){ - return tryHomeMove(piece, homeMoves); - } else { - return tryInfieldMove(piece, calculateSteps()); - } - } - } - - /** - * 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 getMoveablePieces(Color color) { - ArrayList moveablePieces = new ArrayList<>(); - ArrayList 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; - } - - @Override - public String toString() { - return getClass().getSimpleName(); - } }