Merge branch 'dev/model' into dev/client_koppe2

This commit is contained in:
Felix Koppe
2024-12-06 08:11:37 +01:00
51 changed files with 1463 additions and 262 deletions

View File

@@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="MdgaApp" type="Application" factoryName="Application" singleton="false" nameIsGenerated="true">
<option name="ALTERNATIVE_JRE_PATH" value="temurin-20" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="pp.mdga.client.MdgaApp" />
<module name="Projekte.mdga.client.main" />

View File

@@ -5,6 +5,7 @@
import com.jme3.network.serializing.serializers.EnumSerializer;
import pp.mdga.Resources;
import pp.mdga.game.*;
import pp.mdga.game.card.*;
import pp.mdga.message.client.*;
import pp.mdga.message.server.*;
import pp.mdga.server.ServerGameLogic;
@@ -124,7 +125,7 @@ private void initializeSerializables() {
Serializer.registerClass(NoTurnMessage.class);
Serializer.registerClass(PauseGameMessage.class);
Serializer.registerClass(PlayCardMessage.class);
Serializer.registerClass(PossibleCardMessage.class);
Serializer.registerClass(PossibleCardsMessage.class);
Serializer.registerClass(PossiblePieceMessage.class);
Serializer.registerClass(RankingResponseMessage.class);
Serializer.registerClass(RankingRollAgainMessage.class);
@@ -145,10 +146,16 @@ private void initializeSerializables() {
Serializer.registerClass(BonusNode.class);
Serializer.registerClass(StartNode.class);
Serializer.registerClass(HomeNode.class);
Serializer.registerClass(PowerCard.class);
Serializer.registerClass(TurboCard.class);
Serializer.registerClass(SwapCard.class);
Serializer.registerClass(ShieldCard.class);
Serializer.registerClass(HiddenCard.class);
Serializer.registerClass(Color.class, new EnumSerializer());
Serializer.registerClass(PieceState.class, new EnumSerializer());
Serializer.registerClass(ShieldState.class, new EnumSerializer());
Serializer.registerClass(BonusCard.class, new EnumSerializer());
}
private void registerListeners() {

View File

@@ -330,7 +330,7 @@ public void received(PlayCardMessage msg) {
* @param msg the PossibleCard message received
*/
@Override
public void received(PossibleCardMessage msg) {
public void received(PossibleCardsMessage msg) {
state.received(msg);
}
@@ -413,7 +413,7 @@ public void received(ShutdownMessage msg) {
*/
@Override
public void received(IncorrectRequestMessage msg) {
state.received(msg);
addNotification(new InfoNotification(Resources.stringLookup("incorrect.request." + msg.getId())));
}
/**

View File

@@ -116,7 +116,7 @@ public void received(PlayCardMessage msg) {
}
@Override
public void received(PossibleCardMessage msg) {
public void received(PossibleCardsMessage msg) {
LOGGER.log(Level.DEBUG, "Received {0} not allowed.", msg.toString());
}

View File

@@ -241,7 +241,7 @@ public void received(DiceAgainMessage msg){
* @param msg the message to be received
*/
@Override
public void received(PossibleCardMessage msg){
public void received(PossibleCardsMessage msg){
state.received(msg);
}

View File

@@ -65,6 +65,7 @@ public void selectStart() {
if (logic.isHost() && logic.getGame().areAllReady()) {
logic.send(new StartGameMessage());
} else {
logic.send(new StartGameMessage());
LOGGER.log(System.Logger.Level.ERROR, "You are not the host");
}
}

View File

@@ -12,25 +12,21 @@
import pp.mdga.notification.SwapPieceNotification;
import pp.mdga.notification.ThrowPieceNotification;
import java.util.UUID;
public abstract class GameStates extends ClientState {
public GameStates(ClientState parent, ClientGameLogic logic) {
super(parent, logic);
}
protected void handlePowerCard(PlayCardMessage msg) {
if (msg.getCard().equals(BonusCard.TURBO)) {
//logic.getGame().setDiceModifier(msg.getDiceModifier());
} else if (msg.getCard().equals(BonusCard.SHIELD)) {
if (logic.getGame().getBoard().getInfieldIndexOfPiece(logic.getGame().getPieceThroughUUID(msg.getPieceIdentifier())) % 10 != 0) {
logic.getGame().getPieceThroughUUID(msg.getPieceIdentifier()).setShield(ShieldState.SUPPRESSED);
logic.addNotification(new ShieldSuppressedNotification(logic.getGame().getPieceThroughUUID(msg.getPieceIdentifier()).getUuid()));
} else {
logic.getGame().getPieceThroughUUID(msg.getPieceIdentifier()).setShield(ShieldState.ACTIVE);
logic.addNotification(new ShieldActiveNotification(logic.getGame().getPieceThroughUUID(msg.getPieceIdentifier()).getUuid()));
}
if (msg.getCard().getCard().equals(BonusCard.TURBO)) {
logic.getGame().setDiceModifier(msg.getDiceModifier());
} else if (msg.getCard().getCard().equals(BonusCard.SHIELD)) {
handleShield(msg.getPieces().get(0).getUuid());
} else {
Piece ownPiece = logic.getGame().getPieceThroughUUID(msg.getPieceIdentifier());
Piece enemyPiece = logic.getGame().getPieceThroughUUID(msg.getPieceIdentifierEnemy());
Piece ownPiece = logic.getGame().getPieceThroughUUID(msg.getPieces().get(0).getUuid());
Piece enemyPiece = logic.getGame().getPieceThroughUUID(msg.getPieces().get(1).getUuid());
int ownIndex = logic.getGame().getBoard().getInfieldIndexOfPiece(ownPiece);
logic.addNotification(new SwapPieceNotification(ownPiece.getUuid(), enemyPiece.getUuid()));
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(enemyPiece)].setOccupant(ownPiece);
@@ -47,4 +43,14 @@ protected void throwPiece(Piece piece) {
logic.getGame().getGameStatistics().increasePiecesBeingThrown();
piece.setState(PieceState.WAITING);
}
private void handleShield(UUID uuid) {
if (logic.getGame().getBoard().getInfieldIndexOfPiece(logic.getGame().getPieceThroughUUID(uuid)) % 10 != 0) {
logic.getGame().getPieceThroughUUID(uuid).setShield(ShieldState.SUPPRESSED);
logic.addNotification(new ShieldSuppressedNotification(uuid));
} else {
logic.getGame().getPieceThroughUUID(uuid).setShield(ShieldState.ACTIVE);
logic.addNotification(new ShieldActiveNotification(uuid));
}
}
}

View File

@@ -47,7 +47,7 @@ public void received(DieMessage msg) {
@Override
public void received(PlayCardMessage msg) {
logic.addNotification(new PlayCardNotification(logic.getGame().getActiveColor(), msg.getCard()));
logic.addNotification(new PlayCardNotification(logic.getGame().getActiveColor(), msg.getCard().getCard()));
handlePowerCard(msg);
logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getPlayerStatistic().increaseCardsPlayed();
logic.getGame().getGameStatistics().increaseCardsPlayed();

View File

@@ -108,7 +108,7 @@ public void received(DiceAgainMessage msg){
}
@Override
public void received(PossibleCardMessage msg){
public void received(PossibleCardsMessage msg){
state.received(msg);
}

View File

@@ -40,8 +40,8 @@ public void received(DiceNowMessage msg) {
@Override
public void received(DieMessage msg) {
// logic.getGame().setDiceEyes(msg.getDiceEye());
// logic.addNotification(new RollDiceNotification(logic.getGame().getActiveColor(), logic.getGame().getDiceEyes(), logic.getGame().getDiceEyes() * logic.getGame().getDiceModifier()));
logic.getGame().setDiceEyes(msg.getDiceEye());
logic.addNotification(new RollDiceNotification(logic.getGame().getActiveColor(), logic.getGame().getDiceEyes()));
if (msg.getDiceEye() == 6) {
logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getPlayerStatistic().increaseDiced6();
logic.getGame().getGameStatistics().increaseDiced6();
@@ -51,7 +51,7 @@ public void received(DieMessage msg) {
@Override
public void received(PlayCardMessage msg) {
logic.addNotification(new PlayCardNotification(logic.getGame().getActiveColor(), msg.getCard()));
logic.addNotification(new PlayCardNotification(logic.getGame().getActiveColor(), msg.getCard().getCard()));
handlePowerCard(msg);
logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getPlayerStatistic().increaseCardsPlayed();
logic.getGame().getGameStatistics().increaseCardsPlayed();

View File

@@ -5,6 +5,9 @@
import pp.mdga.client.gamestate.DetermineStartPlayerState;
import pp.mdga.game.Player;
import pp.mdga.message.client.AnimationEndMessage;
import pp.mdga.notification.AcquireCardNotification;
import pp.mdga.notification.ActivePlayerNotification;
import pp.mdga.notification.DrawCardNotification;
import pp.mdga.notification.MovePieceNotification;
import java.util.Map;
@@ -46,9 +49,9 @@ public void enter() {
logic.getGame().getBoard().getInfield()[entry.getValue().getStartNodeIndex()].setOccupant(entry.getValue().getPieces()[0]);
animationCounter++;
if(entry.getKey() == logic.getOwnPlayerId()){
//logic.addNotification(new AcquireCardNotification(entry.getValue().getHandCards().get(0)));
logic.addNotification(new AcquireCardNotification(entry.getValue().getHandCards().get(0).getCard()));
} else {
//logic.addNotification(new DrawCardNotification(entry.getValue().getColor(), entry.getValue().getHandCards().get(0)));
logic.addNotification(new DrawCardNotification(entry.getValue().getColor(), entry.getValue().getHandCards().get(0).getCard()));
}
}
}
@@ -73,8 +76,10 @@ public void selectAnimationEnd(){
logic.send(new AnimationEndMessage());
if (logic.getGame().getActivePlayerId() == logic.getOwnPlayerId()){
parent.getParent().setState(parent.getParent().getTurn());
logic.addNotification(new ActivePlayerNotification(logic.getGame().getActiveColor()));
} else {
parent.getParent().setState(parent.getParent().getWaiting());
logic.addNotification(new ActivePlayerNotification(logic.getGame().getActiveColor()));
}
}
}

View File

@@ -4,21 +4,27 @@
import pp.mdga.client.ClientState;
import pp.mdga.client.gamestate.DetermineStartPlayerState;
import pp.mdga.message.client.AnimationEndMessage;
import pp.mdga.message.server.ActivePlayerMessage;
import pp.mdga.message.server.RankingResponseMessage;
import pp.mdga.message.server.RankingRollAgainMessage;
import pp.mdga.message.server.*;
import pp.mdga.notification.ActivePlayerNotification;
public class WaitRankingState extends DetermineStartPlayerStates {
private final DetermineStartPlayerState parent;
private boolean canTransition = false;
private boolean canChange = false;
public WaitRankingState(ClientState parent, ClientGameLogic logic) {
super(parent, logic);
this.parent = (DetermineStartPlayerState) parent;
}
private void changeToIntro(){
if(!canChange){
canChange = true;
return;
}
parent.setState(parent.getIntro());
}
@Override
public void enter() {
}
@@ -29,33 +35,24 @@ public void exit() {
}
@Override
public void received(RankingRollAgainMessage msg){
public void received(DiceNowMessage msg){
parent.setState(parent.getRollRankingDice());
}
@Override
public void received(RankingResponseMessage msg){
}
}
@Override
public void selectAnimationEnd(){
logic.send(new AnimationEndMessage());
changeToIntro();
logic.send(new AnimationEndMessage());
}
@Override
public void received(ActivePlayerMessage msg){
logic.addNotification(new ActivePlayerNotification(msg.getColor()));
logic.getGame().setActiveColor(msg.getColor());
changeToIntro();
}
private void changeToIntro(){
if (!canTransition){
canTransition = true;
return;
}
parent.setState(parent.getIntro());
}
}

View File

@@ -20,7 +20,7 @@ public PlayPowerCardState(ClientState parent, ClientGameLogic logic) {
@Override
public void enter() {
logic.addNotification(new PlayCardNotification(logic.getGame().getActiveColor() , playCardMessage.getCard()));
logic.addNotification(new PlayCardNotification(logic.getGame().getActiveColor() , playCardMessage.getCard().getCard()));
handlePowerCard(playCardMessage);
}

View File

@@ -11,7 +11,7 @@
import pp.mdga.game.Piece;
import pp.mdga.message.server.DiceNowMessage;
import pp.mdga.message.server.PlayCardMessage;
import pp.mdga.message.server.PossibleCardMessage;
import pp.mdga.message.server.PossibleCardsMessage;
import pp.mdga.message.server.PossiblePieceMessage;
public class PowerCardState extends TurnStates {
@@ -48,7 +48,7 @@ public void setState(PowerCardStates state) {
}
@Override
public void received(PossibleCardMessage msg){
public void received(PossibleCardsMessage msg){
state.received(msg);
}

View File

@@ -3,8 +3,11 @@
import pp.mdga.client.ClientGameLogic;
import pp.mdga.client.ClientState;
import pp.mdga.client.gamestate.TurnState;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.message.server.DieMessage;
import pp.mdga.message.server.NoTurnMessage;
import pp.mdga.notification.DiceNowNotification;
import pp.mdga.notification.RollDiceNotification;
public class RollDiceState extends TurnStates {
@@ -17,20 +20,25 @@ public RollDiceState(ClientState parent, ClientGameLogic logic) {
@Override
public void enter() {
logic.addNotification(new DiceNowNotification());
}
@Override
public void exit() {
//logic.getGame().setDiceModifier(1);
logic.getGame().setDiceModifier(1);
}
public TurnState getParent() {
return parent;
}
@Override
public void selectDice(){
logic.send(new RequestDieMessage());
}
public void received(DieMessage msg){
// logic.getGame().setDiceEyes(msg.getDiceEye());
logic.getGame().setDiceEyes(msg.getDiceEye());
parent.setState(parent.getChoosePiece());
}

View File

@@ -37,8 +37,10 @@ public void setPossiblePieces(ArrayList<Piece> possiblePieces) {
@Override
public void selectPiece(Piece piece) {
if (possiblePieces.contains(piece)) {
logic.send(new SelectedPiecesMessage(piece.getUuid()));
ArrayList<Piece> pieces = new ArrayList<>();
if(possiblePieces.contains(piece)){
pieces.add(piece);
logic.send(new SelectedPiecesMessage(pieces));
}
}

View File

@@ -7,6 +7,8 @@
import pp.mdga.message.client.SelectedPiecesMessage;
import pp.mdga.message.server.MoveMessage;
import java.util.ArrayList;
public class StartPieceState extends ChoosePieceStates {
private final ChoosePieceState parent;
@@ -30,8 +32,10 @@ public void exit() {
@Override
public void selectPiece(Piece piece){
ArrayList<Piece> pieces = new ArrayList<>();
if(moveablePiece.equals(piece)){
logic.send(new SelectedPiecesMessage(piece.getUuid()));
pieces.add(piece);
logic.send(new SelectedPiecesMessage(pieces));
}
}

View File

@@ -8,6 +8,9 @@
import pp.mdga.message.client.SelectedPiecesMessage;
import pp.mdga.message.server.MoveMessage;
import java.util.ArrayList;
import java.util.List;
public class WaitingPieceState extends ChoosePieceStates {
private final ChoosePieceState parent;
@@ -29,8 +32,10 @@ public void exit() {
@Override
public void selectPiece(Piece piece){
ArrayList<Piece> pieces = new ArrayList<>();
if(moveablePiece.equals(piece)){
logic.send(new SelectedPiecesMessage(piece.getUuid()));
pieces.add(piece);
logic.send(new SelectedPiecesMessage(pieces));
}
}

View File

@@ -4,11 +4,12 @@
import pp.mdga.client.ClientState;
import pp.mdga.client.gamestate.turnstate.PowerCardState;
import pp.mdga.game.BonusCard;
import pp.mdga.game.card.*;
import pp.mdga.message.client.NoPowerCardMessage;
import pp.mdga.message.client.SelectCardMessage;
import pp.mdga.message.server.DiceNowMessage;
import pp.mdga.message.server.PlayCardMessage;
import pp.mdga.message.server.PossibleCardMessage;
import pp.mdga.message.server.PossibleCardsMessage;
import pp.mdga.message.server.PossiblePieceMessage;
import pp.mdga.notification.SelectableCardsNotification;
@@ -21,7 +22,7 @@
public class ChoosePowerCardState extends PowerCardStates {
private final PowerCardState parent;
private ArrayList<BonusCard> possibleCards;
private ArrayList<PowerCard> possibleCards = new ArrayList<>();
/**
* Constructor
@@ -38,8 +39,6 @@ public ChoosePowerCardState(ClientState parent, ClientGameLogic logic) {
*/
@Override
public void enter() {
possibleCards = new ArrayList<>();
System.out.println("ChoosePowerCardState");
}
/**
@@ -47,7 +46,7 @@ public void enter() {
*/
@Override
public void exit() {
possibleCards = null;
possibleCards = new ArrayList<>();
}
/**
@@ -55,9 +54,15 @@ public void exit() {
* @param msg possible cards message
*/
@Override
public void received(PossibleCardMessage msg){
possibleCards = (ArrayList<BonusCard>) msg.getPossibleCards();
logic.addNotification(new SelectableCardsNotification(possibleCards));
public void received(PossibleCardsMessage msg){
possibleCards = (ArrayList<PowerCard>)msg.getPossibleCards();
ArrayList<BonusCard> possibleBonusCards = new ArrayList<>();
for (PowerCard card : possibleCards) {
if (!possibleBonusCards.contains(card.getCard())) {
possibleBonusCards.add(card.getCard());
}
}
logic.addNotification(new SelectableCardsNotification(possibleBonusCards));
}
/**
@@ -67,7 +72,7 @@ public void received(PossibleCardMessage msg){
@Override
public void selectCard(BonusCard card){
if(card != null){
logic.send(new SelectCardMessage(card));
logic.send(new SelectCardMessage(logic.getGame().getPlayers().get(logic.getOwnPlayerId()).getPowerCardByType(card)));
} else {
logic.send(new NoPowerCardMessage());
}
@@ -79,10 +84,9 @@ public void selectCard(BonusCard card){
*/
@Override
public void received(PlayCardMessage msg){
if(msg.getCard().equals(BonusCard.TURBO)){
//logic.getGame().setDiceModifier(msg.getDiceModifier());
} else {
LOGGER.log(System.Logger.Level.ERROR, "Received card that is not turbo");
if(msg.getCard().getCard().equals(BonusCard.TURBO)){
parent.getParent().getPlayPowerCard().setPlayCard(msg);
parent.getParent().setState(parent.getParent().getPlayPowerCard());
}
}

View File

@@ -1,8 +1,11 @@
package pp.mdga.game;
import com.jme3.network.serializing.Serializable;
/**
* Enum representing the different types of bonus cards.
*/
@Serializable
public enum BonusCard {
/**
* The hidden bonus card.

View File

@@ -1,5 +1,10 @@
package pp.mdga.game;
import pp.mdga.game.card.PowerCard;
import pp.mdga.game.card.ShieldCard;
import pp.mdga.game.card.SwapCard;
import pp.mdga.game.card.TurboCard;
import java.util.*;
/**
@@ -36,12 +41,12 @@ public class Game {
/**
* The pile of bonus cards available for drawing.
*/
private List<BonusCard> drawPile = new ArrayList<>();
private List<PowerCard> drawPile = new ArrayList<>();
/**
* The pile of bonus cards that have been discarded.
*/
private List<BonusCard> discardPile = new ArrayList<>();
private List<PowerCard> discardPile = new ArrayList<>();
/**
* The game board.
@@ -63,6 +68,16 @@ public class Game {
*/
private Color activeColor;
/**
* The dice modifier.
*/
private int diceModifier;
/**
* The number of eyes on the dice.
*/
private int diceEyes;
/**
* This constructor creates a new Game object.
*/
@@ -77,9 +92,23 @@ public Game() {
* This method initializes the draw pile with the predefined number of bonus cards.
*/
private void initializeDrawPile() {
addBonusCards(BonusCard.TURBO, AMOUNT_OF_TURBO_CARDS);
addBonusCards(BonusCard.SWAP, AMOUNT_OF_SWAP_CARDS);
addBonusCards(BonusCard.SHIELD, AMOUNT_OF_SHIELD_CARDS);
this.addBonusCards(new TurboCard(), AMOUNT_OF_TURBO_CARDS);
this.addBonusCards(new SwapCard(), AMOUNT_OF_SWAP_CARDS);
this.addBonusCards(new ShieldCard(), AMOUNT_OF_SHIELD_CARDS);
Collections.shuffle(this.drawPile);
}
/**
* This method will be used to remove the first card of the drawPile attribute of Game class.
*
* @return first card as a PowerCard enumeration.
*/
public PowerCard draw() {
if (!this.drawPile.isEmpty()) {
return this.drawPile.remove(0);
}
return null;
}
/**
@@ -88,7 +117,7 @@ private void initializeDrawPile() {
* @param card the card to add
* @param count the number of cards to add
*/
private void addBonusCards(BonusCard card, int count) {
private void addBonusCards(PowerCard card, int count) {
drawPile.addAll(Collections.nCopies(count, card));
}
@@ -290,21 +319,21 @@ public Statistic getGameStatistics() {
}
/**
* This method returns the draw pile.
* This method will be used to return drawPile attribute of Game class.
*
* @return the draw pile
* @return drawPile as a List of PowerCard objects.
*/
public List<BonusCard> getDrawPile() {
return drawPile;
public List<PowerCard> getDrawPile() {
return this.drawPile;
}
/**
* This method returns the discard pile.
* This method will be used to return discardPile attribute of Game class.
*
* @return the discard pile
* @return discardPile as a List of PowerCard objects.
*/
public List<BonusCard> getDiscardPile() {
return discardPile;
public List<PowerCard> getDiscardPile() {
return this.discardPile;
}
/**
@@ -362,20 +391,22 @@ public void setGameStatistics(Statistic gameStatistics) {
}
/**
* This method sets the draw pile.
* This method will be used to set drawPile attribute of Game class to the given discardPile parameter.
* It will be used to test cases.
*
* @param drawPile the new draw pile
* @param drawPile the new value of drawPile attribute as a List of PowerCards.
*/
public void setDrawPile(List<BonusCard> drawPile) {
public void setDrawPile(List<PowerCard> drawPile) {
this.drawPile = drawPile;
}
/**
* This method sets the discard pile.
* This method will be used to set discardPile attribute of Game class to the given discardPile parameter.
* It will be used to test cases.
*
* @param discardPile the new discard pile
* @param discardPile the new value of discardPile attribute as a List of PowerCards.
*/
public void setDiscardPile(List<BonusCard> discardPile) {
public void setDiscardPile(List<PowerCard> discardPile) {
this.discardPile = discardPile;
}
@@ -414,4 +445,40 @@ public void setDie(Die die) {
public void setHost(int host) {
this.host = host;
}
/**
* This method will be used to get the dice eyes.
*
* @return the dice eyes
*/
public int getDiceEyes() {
return diceEyes;
}
/**
* This method is used to get the dice modifier.
*
* @return the dice modifier
*/
public int getDiceModifier() {
return diceModifier;
}
/**
* This method will be used to set the dice eyes.
*
* @param diceEyes the new dice eyes
*/
public void setDiceEyes(int diceEyes) {
this.diceEyes = diceEyes;
}
/**
* This method is used to set the dice modifier.
*
* @param diceModifier the new dice modifier
*/
public void setDiceModifier(int diceModifier) {
this.diceModifier = diceModifier;
}
}

View File

@@ -121,4 +121,47 @@ public Color getColor() {
public UUID getUuid() {
return uuid;
}
/**
* This method will return all necessary information of Piece class in a more readable way.
*
* @return information as a String.
*/
@Override
public String toString() {
return "Piece with UUID: %s and color: %s with state: %s".formatted(this.uuid, color, state);
}
/**
* This method will be used to create the hash code of this Piece class.
*
* @return hashCode as an Integer.
*/
@Override
public int hashCode() {
return this.uuid.hashCode();
}
/**
* This method will be used to check if the given obj parameter is equal to this object.
* It will return false if the obj parameter is null or if the hash codes of both objects are not equal. Otherwise
* it will return true.
*
* @param obj as the object which will be compared as an Object.
* @return true or false.
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof Piece) {
Piece piece = (Piece) obj;
return this.hashCode() == piece.hashCode();
}
return false;
}
}

View File

@@ -2,6 +2,7 @@
import com.jme3.network.serializing.Serializable;
import pp.mdga.Resources;
import pp.mdga.game.card.PowerCard;
import java.util.ArrayList;
@@ -23,7 +24,7 @@ public class Player {
/**
* The hand cards of the player.
*/
private ArrayList<BonusCard> handCards = new ArrayList<>();
private ArrayList<PowerCard> handCards = new ArrayList<>();
/**
* The color of the player.
@@ -93,7 +94,7 @@ public void initialize() {
*
* @param card the card to be added to the players hand
*/
public void addHandCard(BonusCard card) {
public void addHandCard(PowerCard card) {
this.handCards.add(card);
}
@@ -102,7 +103,7 @@ public void addHandCard(BonusCard card) {
*
* @param card as the card which should be removed from the handCards attribute as a PowerCard object.
*/
public void removeHandCard(BonusCard card) {
public void removeHandCard(PowerCard card) {
this.handCards.remove(card);
}
@@ -136,6 +137,21 @@ public boolean isFinished() {
return true;
}
/**
* This method returns a PowerCard based on its Type.
*
* @param bonusCard the card Type to be matched
* @return the first PowerCard of this type
*/
public PowerCard getPowerCardByType(BonusCard bonusCard) {
for (PowerCard card : this.handCards) {
if(card.getCard().equals(bonusCard)) {
return card;
}
}
return null;
}
/**
* This method returns the give name of the Player
*
@@ -155,12 +171,12 @@ public Statistic getPlayerStatistic() {
}
/**
* This method returns the current handCards of the player
* This method will be used to return handCards attribute of Player class.
*
* @return the handCards of the player
* @return handCards as a List of PowerCard objects.
*/
public ArrayList<BonusCard> getHandCards() {
return handCards;
public ArrayList<PowerCard> getHandCards() {
return this.handCards;
}
/**

View File

@@ -0,0 +1,29 @@
package pp.mdga.game.card;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.visitor.Visitor;
/**
* This class represents the hidden power card of this application.
*/
@Serializable
public class HiddenCard extends PowerCard {
/**
* Constructor.
*/
public HiddenCard() {
this.card = BonusCard.HIDDEN;
}
/**
* This method will be used to call the visit method of the given visitor parameter and pass a PowerCard object.
*
* @param visitor as the visitor which will differentiates between all types of power card types as a Visitor
* interface.
*/
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -0,0 +1,86 @@
package pp.mdga.game.card;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.visitor.Visitor;
import java.util.UUID;
/**
* This abstract class represents the abstract power class which will be used to realize a visitor pattern for all
* types of different cards inside the game.
*/
@Serializable
public abstract class PowerCard {
/**
* Create PowerCard attributes.
*/
private final UUID uuid = UUID.randomUUID();
protected BonusCard card;
/**
* Constructor.
*/
public PowerCard() {
this.card = BonusCard.HIDDEN;
}
/**
* This method will be used to call the visit method of the given visitor parameter and pass a PowerCard object.
*
* @param visitor as the visitor which will differentiates between all types of power card types as a Visitor
* interface.
*/
public abstract void accept(Visitor visitor);
/**
* This method will be used to return uuid attribute of abstract PowerCard class.
*
* @return uuid as a UUID object.
*/
public UUID getUuid() {
return this.uuid;
}
/**
* This method will be used to return card attribute of abstract PowerCard class.
*
* @return card as a BonusCard enumeration.
*/
public BonusCard getCard() {
return this.card;
}
/**
* This method will be used to create the hash code of this abstract PowerCard class.
*
* @return hashCode as an Integer.
*/
@Override
public int hashCode() {
return this.uuid.hashCode();
}
/**
* This method will be used to check if the given obj parameter is equal to this object.
* It will return false if the obj parameter is null or if the hash codes of both objects are not equal. Otherwise
* it will return true.
*
* @param obj as the object which will be compared as an Object.
* @return true or false.
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof PowerCard) {
PowerCard card = (PowerCard) obj;
return this.hashCode() == card.hashCode();
}
return false;
}
}

View File

@@ -0,0 +1,28 @@
package pp.mdga.game.card;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.visitor.Visitor;
/**
* This class represents the shield power card of this application.
*/
@Serializable
public class ShieldCard extends PowerCard {
/**
* Constructor.
*/
public ShieldCard() {
this.card = BonusCard.SHIELD;
}
/**
* This method will be used to call the visit method of the given visitor parameter and pass a PowerCard object.
*
* @param visitor as the visitor which will differentiates between all types of power card types as a Visitor
* interface.
*/
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -0,0 +1,29 @@
package pp.mdga.game.card;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.visitor.Visitor;
/**
* This class represents the swap power card of this application.
*/
@Serializable
public class SwapCard extends PowerCard {
/**
* Constructor.
*/
public SwapCard() {
this.card = BonusCard.SWAP;
}
/**
* This method will be used to call the visit method of the given visitor parameter and pass a PowerCard object.
*
* @param visitor as the visitor which will differentiates between all types of power card types as a Visitor
* interface.
*/
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -0,0 +1,29 @@
package pp.mdga.game.card;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.visitor.Visitor;
/**
* This class represents the turbo power card of this application.
*/
@Serializable
public class TurboCard extends PowerCard {
/**
* Constructor.
*/
public TurboCard() {
this.card = BonusCard.TURBO;
}
/**
* This method will be used to call the visit method of the given visitor parameter and pass a PowerCard object.
*
* @param visitor as the visitor which will differentiates between all types of power card types as a Visitor
* interface.
*/
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@@ -2,6 +2,7 @@
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.game.card.PowerCard;
/**
* A message sent from the client to the server to select a bonus card.
@@ -11,14 +12,14 @@ public class SelectCardMessage extends ClientMessage {
/**
* The bonus card to be selected.
*/
private final BonusCard card;
private final PowerCard card;
/**
* Constructs a new SelectCard instance.
*
* @param card the bonus card to be selected
* @param card the power card to be selected
*/
public SelectCardMessage(BonusCard card) {
public SelectCardMessage(PowerCard card) {
this.card = card;
}
@@ -30,11 +31,11 @@ private SelectCardMessage() {
}
/**
* Gets the bonus card associated with this selection.
* Gets the power card associated with this selection.
*
* @return the bonus card
* @return the power card
*/
public BonusCard getCard() {
public PowerCard getCard() {
return card;
}

View File

@@ -1,8 +1,10 @@
package pp.mdga.message.client;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.Piece;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
/**
* A message sent by a client to indicate that a piece has been selected for a bonus cards.
@@ -10,43 +12,24 @@
@Serializable
public class SelectedPiecesMessage extends ClientMessage {
/**
* The piece identifier.
* Create SelectedPiecesMessage attributes.
*/
private final UUID pieceIdentifier;
private final List<Piece> pieces;
/**
* Constructor.
*/
private SelectedPiecesMessage() {
this.pieces = new ArrayList<>();
}
/**
* Constructor for SelectedPieces
*
* @param pieceIdentifier the piece identifier
* @param pieces the piece identifier
*/
public SelectedPiecesMessage(UUID pieceIdentifier) {
this.pieceIdentifier = pieceIdentifier;
}
/**
* Default constructor for serialization purposes.
*/
private SelectedPiecesMessage() {
pieceIdentifier = null;
}
/**
* Getter for the piece identifier
*
* @return the piece identifier
*/
public UUID getPieceIdentifier() {
return pieceIdentifier;
}
/**
* Returns a string representation of this message.
*
* @return a string representation of this message
*/
@Override
public String toString() {
return "SelectedPieces{pieceIdentifier=" + pieceIdentifier + '}';
public SelectedPiecesMessage(List<Piece> pieces) {
this.pieces = pieces;
}
/**
@@ -59,4 +42,30 @@ public String toString() {
public void accept(ClientInterpreter interpreter, int from) {
interpreter.received(this, from);
}
/**
* This method will be used to return pieces of SelectedPiecesMessage class.
*
* @return pieces as a List of Piece objects.
*/
public List<Piece> getPieces() {
return pieces;
}
/**
* Returns a string representation of this message.
*
* @return a string representation of this message
*/
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("SelectedPieces{Number of pieces: ").append(this.pieces.size()).append(" | ");
for (Piece piece : this.pieces) {
stringBuilder.append(piece).append(", ");
}
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
return stringBuilder.toString();
}
}

View File

@@ -27,6 +27,15 @@ public IncorrectRequestMessage() {
this.id = 0;
}
/**
* Returns the id of the incorrect request message
*
* @return the id of the error message
*/
public int getId(){
return id;
}
/**
* Accepts a visitor to process this message.
*

View File

@@ -1,9 +1,11 @@
package pp.mdga.message.server;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.game.Piece;
import pp.mdga.game.card.PowerCard;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
/**
* A message sent by the server to the active player to play a card.
@@ -13,17 +15,12 @@ public class PlayCardMessage extends ServerMessage {
/**
* The card that should be played.
*/
private final BonusCard card;
private final PowerCard card;
/**
* The identifier of the piece that should be moved.
* The list of pieces which will be used.
*/
private final UUID ownPieceID;
/**
* The identifier of the enemy piece that should be moved.
*/
private final UUID enemyPieceID;
private final List<Piece> pieces;
/**
* The modifier of the dice.
@@ -34,15 +31,13 @@ public class PlayCardMessage extends ServerMessage {
* Constructs a new PlayCard message.
*
* @param card the card that should be played
* @param ownPieceID the identifier of the piece that should be moved
* @param enemyPieceID the identifier of the enemy piece that should be moved
* @param pieces as the pieces which should be played as a List of Piece objects.
* @param diceModifier the new modifier of the dice
*/
public PlayCardMessage(BonusCard card, UUID ownPieceID, UUID enemyPieceID, int diceModifier) {
public PlayCardMessage(PowerCard card, List<Piece> pieces, int diceModifier) {
super();
this.card = card;
this.ownPieceID = ownPieceID;
this.enemyPieceID = enemyPieceID;
this.pieces = pieces;
this.diceModifier = diceModifier;
}
@@ -50,71 +45,30 @@ public PlayCardMessage(BonusCard card, UUID ownPieceID, UUID enemyPieceID, int d
* Default constructor for serialization purposes.
*/
private PlayCardMessage() {
ownPieceID = null;
card = null;
enemyPieceID = null;
diceModifier = 1;
super();
this.card = null;
this.pieces = new ArrayList<Piece>();
this.diceModifier = 1;
}
/**
* Creates a new PlayCard message for the given card and piece identifier.
* This method will be used to return card attribute of PlayCardMessage class.
*
* @param ownPieceID the identifier of the piece of the player that should be affected
* @param enemyPieceID the identifier of the enemy piece that should be affected
* @return a new PlayCard message
* @return card as a PowerCard object.
*/
public static PlayCardMessage swap(UUID ownPieceID, UUID enemyPieceID) {
return new PlayCardMessage(BonusCard.SWAP, ownPieceID, enemyPieceID, 1);
public PowerCard getCard() {
return this.card;
}
/**
* Creates a new PlayCard message for the given card and piece identifier.
* This mehtod will be usd to return pieces attribute of PlayCardMessage class.
*
* @param ownPieceID the identifier of the piece of the player that should be affected
* @return a new PlayCard message
* @return pieces as a List of Piece objects.
*/
public static PlayCardMessage shield(UUID ownPieceID) {
return new PlayCardMessage(BonusCard.SHIELD, ownPieceID, null, 1);
public List<Piece> getPieces() {
return this.pieces;
}
/**
* creates a new PlayCardMessage for a turbo card
*
* @param diceModifier the new modifier of the dice
* @return newly constructed message
*/
public static PlayCardMessage turbo(int diceModifier) {
return new PlayCardMessage(BonusCard.TURBO, null, null, diceModifier);
}
/**
* Returns the card that should be played.
*
* @return the card that should be played
*/
public BonusCard getCard() {
return card;
}
/**
* Returns the identifier of the piece that should be moved.
*
* @return the identifier of the piece that should be moved
*/
public UUID getPieceIdentifier() {
return ownPieceID;
}
/**
* Returns the identifier of the enemy piece that should be moved.
*
* @return the identifier of the enemy piece that should be moved
*/
public UUID getPieceIdentifierEnemy() {
return enemyPieceID;
}
/**
* this method returns the dice modifier
*
@@ -141,6 +95,15 @@ public void accept(ServerInterpreter interpreter) {
*/
@Override
public String toString() {
return "PlayCardMessage{" + "card=" + card + ", ownPieceID=" + ownPieceID + ", enemyPieceID=" + enemyPieceID + ", diceModifier=" + diceModifier + '}';
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("PlayCardMessage{card: ").append(this.card).append(" with selected pieces: {");
for (Piece piece : this.pieces) {
stringBuilder.append(piece).append(", ");
}
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
stringBuilder.append("} and dice modifier: ").append(this.diceModifier);
return stringBuilder.toString();
}
}

View File

@@ -1,7 +1,7 @@
package pp.mdga.message.server;
import com.jme3.network.serializing.Serializable;
import pp.mdga.game.BonusCard;
import pp.mdga.game.card.PowerCard;
import java.util.ArrayList;
import java.util.List;
@@ -10,36 +10,36 @@
* A message sent by the server to the client to indicate the possible cards that can be chosen.
*/
@Serializable
public class PossibleCardMessage extends ServerMessage {
public class PossibleCardsMessage extends ServerMessage {
/**
* The list of possible cards.
*/
private final List<BonusCard> possibleCards;
private final List<PowerCard> possibleCards;
/**
* Constructor for a PossibleCard instance.
*/
public PossibleCardMessage() {
public PossibleCardsMessage() {
super();
possibleCards = new ArrayList<>();
}
/**
* Add a possible card to the list of possible cards
* Constructor.
*
* @param card the possible card to add
* @param possibleCards as the list of possible power cards as a List of PowerCard objects.
*/
public void addPossibleCard(BonusCard card) {
this.possibleCards.add(card);
public PossibleCardsMessage(List<PowerCard> possibleCards) {
this.possibleCards = possibleCards;
}
/**
* Getter for the list of possible cards
* This method will be used to return possibleCards attribute of PossibleCardMessage class.
*
* @return the list of possible cards
* @return possibleCards as a List of PowerCard objects.
*/
public List<BonusCard> getPossibleCards() {
return possibleCards;
public List<PowerCard> getPossibleCards() {
return this.possibleCards;
}
/**

View File

@@ -2,37 +2,45 @@
import com.jme3.network.serializing.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* A message sent by the server to all client to inform them about the current ranking. (only in DetermineStartPlayer)
*/
@Serializable
public class RankingResponseMessage extends ServerMessage {
private final int startingPlayerId;
/**
* Constructs a new RankingResponse instance.
* Create RankingResponseMessage attributes.
*/
public RankingResponseMessage(int startingPlayerId) {
super();
this.startingPlayerId = startingPlayerId;
}
private final Map<Integer, Integer> rankingResults;
/**
* Constructs a new RankingResponse instance.
* Constructor.
*/
public RankingResponseMessage() {
super();
this.startingPlayerId = 0;
this.rankingResults = new HashMap<Integer, Integer>();
}
/**
* The id of the starting Player.
* Constructor.
*
* @return the id of the starting player as an int
* @param rankingResults as the results of all players after the start player was determined as a Map combining
* Integers and Integers.
*/
public int getStartingPlayerId() {
return startingPlayerId;
public RankingResponseMessage(Map<Integer, Integer> rankingResults) {
super();
this.rankingResults = rankingResults;
}
/**
* This method will be used to return rankingResults attribute of RankingResponseMessage class.
*
* @return rankingResults as a Map combining Integers and Integers.
*/
public Map<Integer, Integer> getRankingResults() {
return this.rankingResults;
}
/**
@@ -52,6 +60,14 @@ public void accept(ServerInterpreter interpreter) {
*/
@Override
public String toString() {
return "RankingResponseMessage{" + "startingPlayerId=" + startingPlayerId + '}';
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("RankingResponseMessage{");
for (Map.Entry<Integer, Integer> entry : this.rankingResults.entrySet()) {
stringBuilder.append("Player with ID: ").append(entry.getKey()).append(" rolled: ").append(entry.getValue()).append(", ");
}
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
stringBuilder.append("}");
return stringBuilder.toString();
}
}

View File

@@ -122,7 +122,7 @@ public interface ServerInterpreter {
*
* @param msg the PossibleCard message received
*/
void received(PossibleCardMessage msg);
void received(PossibleCardsMessage msg);
/**
* Handles a PossiblePiece message received from the server.

View File

@@ -1,6 +1,8 @@
package pp.mdga.server.automaton;
import pp.mdga.game.Color;
import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.game.Player;
import pp.mdga.message.client.*;
import pp.mdga.message.server.*;
@@ -49,6 +51,11 @@ public void exit() {
public void initializeGame() {
for (var player : this.logic.getGame().getPlayers().values()) {
player.initialize();
player.addHandCard(this.logic.getGame().draw());
Piece piece = player.getWaitingArea()[0];
player.getWaitingArea()[0] = null;
piece.setState(PieceState.ACTIVE);
this.logic.getGame().getBoard().getInfield()[player.getStartNodeIndex()].setOccupant(piece);
}
}
@@ -67,8 +74,8 @@ public void received(JoinedLobbyMessage msg, int from) {
for (Map.Entry<Integer, Player> entry : this.logic.getGame().getPlayers().entrySet()) {
this.logic.getServerSender().broadcast(new LobbyPlayerJoinedMessage(entry.getKey(), entry.getValue(), entry.getKey() == this.logic.getGame().getHost()));
if(entry.getKey() != from) {
this.logic.getServerSender().broadcast(new UpdateReadyMessage(entry.getKey(), entry.getValue().isReady()));
if (entry.getKey() != from && entry.getValue().isReady()) {
this.logic.getServerSender().send(from, new UpdateReadyMessage(entry.getKey(), entry.getValue().isReady()));
}
}
}
@@ -86,7 +93,8 @@ public void received(SelectTSKMessage msg, int from) {
this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, Color.NONE, false));
this.logic.getGame().getPlayerById(from).setColor(msg.getColor());
this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor(), true));
} else {
}
else {
this.logic.getServerSender().send(from, new IncorrectRequestMessage(0));
}
}
@@ -116,10 +124,11 @@ public void received(LobbyReadyMessage msg, int from) {
if (this.logic.getGame().getPlayerById(from).getColor() == Color.NONE) {
Color color = this.logic.getGame().getFirstUnusedColor();
if (color != null) {
if (color != Color.NONE) {
this.logic.getGame().getPlayerById(from).setColor(color);
this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, color, true));
} else {
}
else {
this.logic.getServerSender().send(from, new IncorrectRequestMessage(1));
}
}
@@ -154,5 +163,8 @@ public void received(StartGameMessage msg, int from) {
this.logic.getServerSender().broadcast(new ServerStartGameMessage(this.logic.getGame().getPlayersAsList(), this.logic.getGame().getBoard()));
this.logic.setCurrentState(this.logic.getGameState());
}
else {
this.logic.getServerSender().send(from, new IncorrectRequestMessage(5));
}
}
}

View File

@@ -1,13 +1,14 @@
package pp.mdga.server.automaton.game;
import pp.mdga.message.client.AnimationEndMessage;
import pp.mdga.message.server.DiceNowMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.GameState;
import java.util.HashSet;
import java.util.Set;
/**
* This class represents the animation state of the game state.
*
*/
public class AnimationState extends GameAutomatonState {
/**
@@ -18,7 +19,7 @@ public class AnimationState extends GameAutomatonState {
/**
* Create AnimationState attributes.
*/
private int messageReceived = 0;
private final Set<Integer> messageReceived = new HashSet<>();
/**
* Constructs a server state of the specified game logic.
@@ -38,7 +39,7 @@ public void enter() {
@Override
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited AnimationState state.");
this.messageReceived = 0;
this.messageReceived.clear();
}
/**
@@ -50,11 +51,10 @@ public void exit() {
*/
@Override
public void received(AnimationEndMessage msg, int from) {
if (this.messageReceived == this.logic.getGame().getPlayers().size()) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DiceNowMessage());
this.messageReceived.add(from);
if (this.messageReceived.size() == this.logic.getGame().getPlayers().size()) {
this.gameAutomaton.setCurrentState(this.gameAutomaton.getTurnState());
} else {
this.messageReceived++;
this.gameAutomaton.getTurnState().setCurrentState(this.gameAutomaton.getTurnState().getPowerCardState());
}
}
}

View File

@@ -1,20 +1,24 @@
package pp.mdga.server.automaton.game;
import pp.mdga.game.Player;
import pp.mdga.game.Color;
import pp.mdga.message.client.AnimationEndMessage;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.message.server.ActivePlayerMessage;
import pp.mdga.message.server.DiceNowMessage;
import pp.mdga.message.server.DieMessage;
import pp.mdga.message.server.EndOfTurnMessage;
import pp.mdga.message.server.RankingResponseMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.GameState;
import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.Set;
import java.util.logging.Logger;
public class DetermineStartPlayerState extends GameAutomatonState {
/**
@@ -25,8 +29,9 @@ public class DetermineStartPlayerState extends GameAutomatonState {
/**
* Create DetermineStartPlayerState attributes.
*/
private Map<Integer, Integer> diceResults = new HashMap<>();
private List<Integer> playersHaveToRoll = new ArrayList<>();
private final Map<Integer, Integer> diceResults = new HashMap<>();
private final List<Integer> playersHaveToRoll = new ArrayList<>();
private final Set<Integer> messageReceived = new HashSet<>();;
/**
* Constructs a server state of the specified game logic.
@@ -41,16 +46,13 @@ public DetermineStartPlayerState(GameState gameAutomaton, ServerGameLogic logic)
@Override
public void enter() {
LOGGER.log(System.Logger.Level.DEBUG, "Entered DetermineStartPlayerState state.");
for (Map.Entry<Integer, Player> entry: this.logic.getGame().getPlayers().entrySet()) {
this.playersHaveToRoll.add(entry.getKey());
}
}
@Override
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited DetermineStartPlayerState state.");
this.diceResults.clear();
this.playersHaveToRoll.clear();
this.messageReceived.clear();
}
/**
@@ -67,23 +69,51 @@ public void received(RequestDieMessage msg, int from) {
this.diceResults.put(from, roll);
if (this.diceResults.size() == this.logic.getGame().getPlayers().size()) {
int maximumRoll = 0;
for (Map.Entry<Integer, Integer> entry: this.diceResults.entrySet()) {
if (maximumRoll <= entry.getValue()) {
for (Map.Entry<Integer, Integer> entry : this.diceResults.entrySet()) {
if (maximumRoll == entry.getValue()) {
this.playersHaveToRoll.add(entry.getKey());
}
else if (maximumRoll < entry.getValue()) {
maximumRoll = entry.getValue();
} else {
this.playersHaveToRoll.remove(entry.getKey());
this.playersHaveToRoll.clear();
this.playersHaveToRoll.add(entry.getKey());
}
else {
this.logic.getServerSender().send(entry.getKey(), new EndOfTurnMessage());
}
}
if (this.playersHaveToRoll.size() == 1) {
this.logic.getServerSender().broadcast(new ActivePlayerMessage(this.logic.getGame().getPlayerById(this.playersHaveToRoll.get(0)).getColor()));
this.gameAutomaton.setCurrentState(this.gameAutomaton.getAnimationState());
} else {
for (Integer id: this.playersHaveToRoll) {
for (int id: this.playersHaveToRoll) {
this.diceResults.remove(id);
}
}
}
/**
* This method will be called whenever the server received an AnimationEndMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a AnimationEndMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(AnimationEndMessage msg, int from) {
this.messageReceived.add(from);
if (this.messageReceived.size() == this.logic.getGame().getPlayers().size()) {
this.messageReceived.clear();
if (this.playersHaveToRoll.size() > 1) {
for (Integer id : this.playersHaveToRoll) {
this.logic.getServerSender().send(id, new DiceNowMessage());
}
diceResults.clear();
}
else {
LOGGER.log(Level.INFO, "Players have to roll: %s".formatted(this.logic.getGame().getPlayerById(this.playersHaveToRoll.get(0))));
Color color = this.logic.getGame().getPlayerById(this.playersHaveToRoll.get(0)).getColor();
this.logic.getGame().setActiveColor(color);
this.logic.getServerSender().broadcast(new RankingResponseMessage(this.diceResults));
this.logic.getServerSender().broadcast(new ActivePlayerMessage(color));
this.gameAutomaton.setCurrentState(this.gameAutomaton.getAnimationState());
}
}
}

View File

@@ -1,8 +1,16 @@
package pp.mdga.server.automaton.game;
import pp.mdga.game.Player;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.GameState;
import pp.mdga.server.automaton.game.turn.*;
import pp.mdga.server.automaton.game.turn.ChoosePieceState;
import pp.mdga.server.automaton.game.turn.MovePieceState;
import pp.mdga.server.automaton.game.turn.PlayPowerCardState;
import pp.mdga.server.automaton.game.turn.PowerCardState;
import pp.mdga.server.automaton.game.turn.RollDiceState;
import pp.mdga.server.automaton.game.turn.TurnAutomatonState;
import java.lang.System.Logger.Level;
/**
* This class represents the turn state of the server state automaton.
@@ -22,6 +30,7 @@ public class TurnState extends GameAutomatonState {
private final RollDiceState rollDiceState;
private final ChoosePieceState choosePieceState;
private final MovePieceState movePieceState;
private Player player;
/**
* Constructs a server state of the specified game logic.
@@ -36,17 +45,17 @@ public TurnState(GameState gameAutomaton, ServerGameLogic logic) {
this.rollDiceState = new RollDiceState(this, logic);
this.choosePieceState = new ChoosePieceState(this, logic);
this.movePieceState = new MovePieceState(this, logic);
this.setCurrentState(this.powerCardState);
}
@Override
public void enter() {
LOGGER.log(System.Logger.Level.DEBUG, "Entered TurnState state.");
LOGGER.log(Level.DEBUG, "Entered TurnState state.");
this.player = this.logic.getGame().getPlayerById(this.logic.getGame().getActivePlayerId());
}
@Override
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited TurnState state.");
LOGGER.log(Level.DEBUG, "Exited TurnState state.");
}
/**
@@ -103,6 +112,15 @@ public MovePieceState getMovePieceState() {
return this.movePieceState;
}
/**
* This method will be used to return player attribute of TurnState class.
*
* @return player as a Player object.
*/
public Player getPlayer() {
return this.player;
}
/**
* This method will be used to set currentState attribute of TurnState class to the given state parameter.
* In Addition, the currentState will be exited, changed and entered.

View File

@@ -1,8 +1,20 @@
package pp.mdga.server.automaton.game.turn;
import pp.mdga.game.Piece;
import pp.mdga.game.card.PowerCard;
import pp.mdga.message.server.DiceNowMessage;
import pp.mdga.message.server.PossibleCardsMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.AnimationState;
import pp.mdga.server.automaton.game.TurnState;
import pp.mdga.server.automaton.game.turn.powercard.ChoosePowerCardState;
import pp.mdga.server.automaton.game.turn.powercard.PowerCardAutomatonState;
import pp.mdga.server.automaton.game.turn.powercard.ShieldCardState;
import pp.mdga.server.automaton.game.turn.powercard.SwapCardState;
import pp.mdga.server.automaton.game.turn.powercard.TurboCardState;
import pp.mdga.visitor.ServerCardVisitor;
import java.util.HashSet;
import java.util.Set;
public class PowerCardState extends TurnAutomatonState {
/**
@@ -10,6 +22,22 @@ public class PowerCardState extends TurnAutomatonState {
*/
private static final System.Logger LOGGER = System.getLogger(PowerCardState.class.getName());
/**
* Create PowerCardState states.
*/
private PowerCardAutomatonState currentState;
private final ChoosePowerCardState choosePowerCardState;
private final ShieldCardState shieldCardState;
private final SwapCardState swapCardState;
private final TurboCardState turboCardState;
/**
* Create PowerCardState attributes.
*/
private ServerCardVisitor visitor;
private PowerCard selectedCard;
private final Set<Piece> selectedPieces = new HashSet<>();
/**
* Constructs a server state of the specified game logic.
*
@@ -18,15 +46,138 @@ public class PowerCardState extends TurnAutomatonState {
*/
public PowerCardState(TurnState turnAutomaton, ServerGameLogic logic) {
super(turnAutomaton, logic);
this.choosePowerCardState = new ChoosePowerCardState(this, logic);
this.shieldCardState = new ShieldCardState(this, logic);
this.swapCardState = new SwapCardState(this, logic);
this.turboCardState = new TurboCardState(this, logic);
this.setCurrentState(this.choosePowerCardState);
}
@Override
public void enter() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited PowerCardState state.");
this.visitor = new ServerCardVisitor(this.logic);
for (PowerCard card : this.turnAutomaton.getPlayer().getHandCards()) {
card.accept(this.visitor);
}
if (this.visitor.getCards().isEmpty()) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DiceNowMessage());
this.turnAutomaton.setCurrentState(this.turnAutomaton.getRollDiceState());
}
else {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new PossibleCardsMessage(this.visitor.getCards()));
}
}
@Override
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Entered PowerCardState state.");
}
/**
* This method will be used to add the given piece parameter to the selectedPieces attribute of PowerCardState
* class.
*
* @param piece as the new selected piece as a Piece object.
*/
public void addSelectedPiece(Piece piece) {
this.selectedPieces.add(piece);
}
/**
* This method will be used to return currentState attribute of PowerCardState class.
*
* @return currentState as a PowerCardAutomatonState object.
*/
public PowerCardAutomatonState getCurrentState() {
return this.currentState;
}
/**
* This method will be used to return choosePowerCardState attribute of PowerCardState class.
*
* @return choosePowerCardState as a ChoosePowerCardState object.
*/
public ChoosePowerCardState getChoosePowerCardState() {
return this.choosePowerCardState;
}
/**
* This method will be used to return shieldCardState attribute of PowerCardState class.
*
* @return shieldCardState as a ShieldCardState object.
*/
public ShieldCardState getShieldCardState() {
return this.shieldCardState;
}
/**
* This method will be used to return swapCardState attribute of PowerCardState class.
*
* @return swapCardState as a SwapCardState object.
*/
public SwapCardState getSwapCardState() {
return this.swapCardState;
}
/**
* This method will be used to return turboCardState attribute of PowerCardState class.
*
* @return turboCardState as a TurboCardState object.
*/
public TurboCardState getTurboCardState() {
return this.turboCardState;
}
/**
* This method will be used to return visitor attribute of PowerCardState class.
*
* @return visitor as a ServerCardVisitor object.
*/
public ServerCardVisitor getVisitor() {
return this.visitor;
}
/**
* This method will be used to return selectedPieces attribute of PowerCardState class.
*
* @return selectedCard as a PowerCard object.
*/
public PowerCard getSelectedCard() {
return this.selectedCard;
}
/**
* This method will be used to return selectedPieces attribute of PowerCardState class.
*
* @return selectedPieces as a Set of Piece objects.
*/
public Set<Piece> getSelectedPieces() {
return this.selectedPieces;
}
/**
* This method will be used to set currentState attribute of PowerCardState class to the given state parameter.
* In Addition, the currentState will be exited, changed and entered.
*
* @param state as the new currentState attribute as a PowerCardAutomatonState object.
*/
public void setCurrentState(PowerCardAutomatonState state) {
if (this.currentState != null) {
this.currentState.exit();
}
this.currentState = state;
this.currentState.enter();
}
/**
* This method will be used to set selectedCard attribute of PowerCardState class to the given selectedCard
* parameter.
*
* @param selectedCard as the new selectedCard attribute as a PowerCard object.
*/
public void setSelectedCard(PowerCard selectedCard) {
this.selectedCard = selectedCard;
}
}

View File

@@ -1,5 +1,6 @@
package pp.mdga.server.automaton.game.turn;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.TurnState;
import pp.mdga.server.automaton.game.turn.rolldice.FirstRollState;
@@ -45,6 +46,18 @@ public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited RollDiceState state.");
}
/**
* This method will be called whenever the server received a RequestDieMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a RequestDieMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(RequestDieMessage msg, int from) {
this.currentState.received(msg, from);
}
/**
* This method will be used to return currentState attribute of RollDiceState class.
*

View File

@@ -0,0 +1,125 @@
package pp.mdga.server.automaton.game.turn.powercard;
import pp.mdga.game.card.HiddenCard;
import pp.mdga.game.card.ShieldCard;
import pp.mdga.game.card.SwapCard;
import pp.mdga.game.card.TurboCard;
import pp.mdga.message.client.SelectCardMessage;
import pp.mdga.message.server.IncorrectRequestMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.PowerCardState;
import pp.mdga.visitor.Visitor;
public class ChoosePowerCardState extends PowerCardAutomatonState implements Visitor {
/**
* Constructs a server state of the specified game logic.
*
* @param powerCardAutomaton as the automaton of the turn state as a PowerCardState object.
* @param logic the game logic
*/
public ChoosePowerCardState(PowerCardState powerCardAutomaton, ServerGameLogic logic) {
super(powerCardAutomaton, logic);
}
/**
* This method will be used whenever this state will be entered.
*/
@Override
public void enter() {
}
/**
* This method will be used whenever this state will be exited.
*/
@Override
public void exit() {
}
@Override
public void received(SelectCardMessage msg, int from) {
if (this.powerCardAutomaton.getVisitor().getCards().contains(msg.getCard())) {
this.powerCardAutomaton.setSelectedCard(msg.getCard());
msg.getCard().accept(this);
} else {
this.logic.getServerSender().send(from, new IncorrectRequestMessage(2));
}
}
/**
* This method will be used to change the state of the power card automaton depending on the given card parameter.
*
* @param card as a ShieldCard object.
*/
private void changeState(ShieldCard card) {
this.powerCardAutomaton.setCurrentState(this.powerCardAutomaton.getShieldCardState());
}
/**
* This method will be used to change the state of the power card automaton depending on the given card parameter.
*
* @param card as a SwapCard object.
*/
private void changeState(SwapCard card) {
this.powerCardAutomaton.setCurrentState(this.powerCardAutomaton.getSwapCardState());
}
/**
* This method will be used to change the state of the power card automaton depending on the given card parameter.
*
* @param card as a TurboCard object.
*/
private void changeState(TurboCard card) {
this.powerCardAutomaton.setCurrentState(this.powerCardAutomaton.getTurboCardState());
}
/**
* This method will be used to change the state of the power card automaton depending on the given card parameter.
*
* @param card as a TurboCard object.
*/
private void changeState(HiddenCard card) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new IncorrectRequestMessage(5));
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a TurboCard object.
*/
@Override
public void visit(TurboCard card) {
this.changeState(card);
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a SwapCard object.
*/
@Override
public void visit(SwapCard card) {
this.changeState(card);
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a ShieldCard oblect
*/
@Override
public void visit(ShieldCard card) {
this.changeState(card);
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a HiddenCard object.
*/
@Override
public void visit(HiddenCard card) {
this.changeState(card);
}
}

View File

@@ -0,0 +1,23 @@
package pp.mdga.server.automaton.game.turn.powercard;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.ServerState;
import pp.mdga.server.automaton.game.turn.PowerCardState;
public abstract class PowerCardAutomatonState extends ServerState {
/**
* Create PowerCardAutomatonState attributes.
*/
protected final PowerCardState powerCardAutomaton;
/**
* Constructs a server state of the specified game logic.
*
* @param powerCardAutomaton as the automaton of the turn state as a PowerCardState object.
* @param logic the game logic
*/
public PowerCardAutomatonState(PowerCardState powerCardAutomaton, ServerGameLogic logic) {
super(logic);
this.powerCardAutomaton = powerCardAutomaton;
}
}

View File

@@ -0,0 +1,64 @@
package pp.mdga.server.automaton.game.turn.powercard;
import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.game.ShieldState;
import pp.mdga.message.client.SelectedPiecesMessage;
import pp.mdga.message.server.IncorrectRequestMessage;
import pp.mdga.message.server.PlayCardMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.PowerCardState;
import java.util.ArrayList;
public class ShieldCardState extends PowerCardAutomatonState {
/**
* Constructs a server state of the specified game logic.
*
* @param powerCardAutomaton as the automaton of the turn state as a PowerCardState object.
* @param logic the game logic
*/
public ShieldCardState(PowerCardState powerCardAutomaton, ServerGameLogic logic) {
super(powerCardAutomaton, logic);
}
/**
* This method will be used whenever this state will be entered.
*/
@Override
public void enter() {
}
/**
* This method will be used whenever this state will be exited.
*/
@Override
public void exit() {
}
/**
* This method will be called whenever the server received a SelectedPiecesMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a SelectedPiecesMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(SelectedPiecesMessage msg, int from) {
if (msg.getPieces().size() == 1 && this.powerCardAutomaton.getVisitor().getShieldPieces().contains(msg.getPieces().get(0))) {
this.powerCardAutomaton.addSelectedPiece(msg.getPieces().get(0));
for (Piece piece : this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor()).getPieces()) {
if (piece == msg.getPieces().get(0)) {
piece.setShield(ShieldState.ACTIVE);
}
}
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new PlayCardMessage(this.powerCardAutomaton.getSelectedCard(), new ArrayList<>(), this.logic.getGame().getDiceModifier()));
this.powerCardAutomaton.getTurnAutomaton().setCurrentState(this.powerCardAutomaton.getTurnAutomaton().getPlayPowerCardState());
}
else {
this.logic.getServerSender().send(from, new IncorrectRequestMessage(3));
}
}
}

View File

@@ -0,0 +1,60 @@
package pp.mdga.server.automaton.game.turn.powercard;
import pp.mdga.game.Piece;
import pp.mdga.message.client.SelectedPiecesMessage;
import pp.mdga.message.server.IncorrectRequestMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.PowerCardState;
public class SwapCardState extends PowerCardAutomatonState {
/**
* Constructs a server state of the specified game logic.
*
* @param powerCardAutomaton as the automaton of the turn state as a PowerCardState object.
* @param logic the game logic
*/
public SwapCardState(PowerCardState powerCardAutomaton, ServerGameLogic logic) {
super(powerCardAutomaton, logic);
}
/**
* This method will be used whenever this state will be entered.
*/
@Override
public void enter() {
}
/**
* This method will be used whenever this state will be exited.
*/
@Override
public void exit() {
}
/**
* This method will be called whenever the server received a SelectedPiecesMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a SelectedPiecesMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(SelectedPiecesMessage msg, int from) {
if (msg.getPieces().size() == 2) {
Piece first = msg.getPieces().get(0);
Piece second = msg.getPieces().get(1);
if ((this.powerCardAutomaton.getVisitor().getSwapOwnPieces().contains(first)
&& this.powerCardAutomaton.getVisitor().getSwapOtherPieces().contains(second))
|| (this.powerCardAutomaton.getVisitor().getSwapOwnPieces().contains(second)
&& this.powerCardAutomaton.getVisitor().getSwapOtherPieces().contains(first))) {
this.powerCardAutomaton.addSelectedPiece(first);
this.powerCardAutomaton.addSelectedPiece(second);
this.powerCardAutomaton.getTurnAutomaton().setCurrentState(this.powerCardAutomaton.getTurnAutomaton().getPlayPowerCardState());
}
} else {
this.logic.getServerSender().send(from, new IncorrectRequestMessage(4));
}
}
}

View File

@@ -0,0 +1,51 @@
package pp.mdga.server.automaton.game.turn.powercard;
import pp.mdga.message.client.SelectedPiecesMessage;
import pp.mdga.message.server.PlayCardMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.PowerCardState;
import java.util.ArrayList;
public class TurboCardState extends PowerCardAutomatonState {
/**
* Constructs a server state of the specified game logic.
*
* @param powerCardAutomaton as the automaton of the turn state as a PowerCardState object.
* @param logic the game logic
*/
public TurboCardState(PowerCardState powerCardAutomaton, ServerGameLogic logic) {
super(powerCardAutomaton, logic);
}
/**
* This method will be used whenever this state will be entered.
*/
@Override
public void enter() {
this.logic.getGame().getDie().modify();
this.logic.getGame().setDiceModifier(this.logic.getGame().getDiceModifier());
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new PlayCardMessage(this.powerCardAutomaton.getSelectedCard(), new ArrayList<>(), this.logic.getGame().getDiceModifier()));
this.powerCardAutomaton.getTurnAutomaton().setCurrentState(this.powerCardAutomaton.getTurnAutomaton().getPlayPowerCardState());
}
/**
* This method will be used whenever this state will be exited.
*/
@Override
public void exit() {
}
/**
* This method will be called whenever the server received a SelectedPiecesMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a SelectedPiecesMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(SelectedPiecesMessage msg, int from) {
// ToDo: We can use this method to catch irregular client messages.
}
}

View File

@@ -1,8 +1,17 @@
package pp.mdga.server.automaton.game.turn.rolldice;
import pp.mdga.Resources;
import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.message.server.DiceAgainMessage;
import pp.mdga.message.server.DieMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.RollDiceState;
import java.util.ArrayList;
import java.util.List;
public class FirstRollState extends RollDiceAutomatonState {
/**
* Create FirstRollState constants.
@@ -28,4 +37,37 @@ public void enter() {
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited FirstRollState state.");
}
/**
* This method will be called whenever the server received a RequestDieMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a RequestDieMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(RequestDieMessage msg, int from) {
List<Piece> moveablePieces = new ArrayList<>();
for (Piece piece : this.rollDiceAutomaton.getTurnAutomaton().getPlayer().getPieces()) {
if (piece.getState() == PieceState.HOME || piece.getState() == PieceState.ACTIVE) {
moveablePieces.add(piece);
}
}
int roll = this.logic.getGame().getDie().shuffle();
this.logic.getGame().setDiceEyes(roll);
if (!moveablePieces.isEmpty()) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DieMessage(roll));
this.rollDiceAutomaton.getTurnAutomaton().setCurrentState(this.rollDiceAutomaton.getTurnAutomaton().getChoosePieceState());
} else {
if (roll == Resources.MAX_EYES) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DieMessage(roll));
this.rollDiceAutomaton.getTurnAutomaton().setCurrentState(this.rollDiceAutomaton.getTurnAutomaton().getChoosePieceState());
} else {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DiceAgainMessage());
this.rollDiceAutomaton.setCurrentState(this.rollDiceAutomaton.getSecondRollState());
}
}
}
}

View File

@@ -1,5 +1,8 @@
package pp.mdga.server.automaton.game.turn.rolldice;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.message.server.DiceAgainMessage;
import pp.mdga.message.server.DieMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.RollDiceState;
@@ -28,4 +31,23 @@ public void enter() {
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited SecondRollState state.");
}
/**
* This method will be called whenever the server received a RequestDieMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a RequestDieMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(RequestDieMessage msg, int from) {
int roll = this.logic.getGame().getDie().shuffle();
if (roll == 6) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DieMessage(roll));
this.rollDiceAutomaton.getTurnAutomaton().setCurrentState(this.rollDiceAutomaton.getTurnAutomaton().getChoosePieceState());
} else {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DiceAgainMessage());
this.rollDiceAutomaton.setCurrentState(this.rollDiceAutomaton.getThirdRollState());
}
}
}

View File

@@ -1,5 +1,11 @@
package pp.mdga.server.automaton.game.turn.rolldice;
import pp.mdga.message.client.RequestDieMessage;
import pp.mdga.message.server.ActivePlayerMessage;
import pp.mdga.message.server.CeremonyMessage;
import pp.mdga.message.server.DieMessage;
import pp.mdga.message.server.NoTurnMessage;
import pp.mdga.message.server.SpectatorMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.automaton.game.turn.RollDiceState;
@@ -28,4 +34,34 @@ public void enter() {
public void exit() {
LOGGER.log(System.Logger.Level.DEBUG, "Entered ThirdRollState state.");
}
/**
* This method will be called whenever the server received a RequestDieMessage message.
* It will also get the client id of the player who send this message.
*
* @param msg as the message which was sent by the player as a RequestDieMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(RequestDieMessage msg, int from) {
int roll = this.logic.getGame().getDie().shuffle();
if (roll == 6) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new DieMessage(roll));
this.rollDiceAutomaton.getTurnAutomaton().setCurrentState(this.rollDiceAutomaton.getTurnAutomaton().getChoosePieceState());
} else {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new NoTurnMessage());
if (this.rollDiceAutomaton.getTurnAutomaton().getPlayer().isFinished()) {
this.logic.getServerSender().send(this.logic.getGame().getActivePlayerId(), new SpectatorMessage());
if (this.logic.getGame().getNumberOfActivePlayers() == 1) {
this.logic.getServerSender().broadcast(new CeremonyMessage());
} else {
this.logic.getGame().setActiveColor(this.logic.getGame().getActiveColor().next());
this.logic.getServerSender().broadcast(new ActivePlayerMessage(this.logic.getGame().getActiveColor()));
this.rollDiceAutomaton.getTurnAutomaton().getGameAutomaton().setCurrentState(this.rollDiceAutomaton.getTurnAutomaton().getGameAutomaton().getAnimationState());
}
}
}
}
}

View File

@@ -0,0 +1,147 @@
package pp.mdga.visitor;
import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.game.card.HiddenCard;
import pp.mdga.game.card.PowerCard;
import pp.mdga.game.card.ShieldCard;
import pp.mdga.game.card.SwapCard;
import pp.mdga.game.card.TurboCard;
import pp.mdga.server.ServerGameLogic;
import java.util.ArrayList;
import java.util.List;
/**
* This class represents the visitor for all types of power cards.
*/
public class ServerCardVisitor implements Visitor {
/**
* Create ServerCardVisitor attributes
*/
private final List<PowerCard> cards = new ArrayList<>();
private final List<Piece> shieldPieces = new ArrayList<>();
private final List<Piece> swapOwnPieces = new ArrayList<>();
private final List<Piece> swapOtherPieces = new ArrayList<>();
private final ServerGameLogic logic;
/**
* Constructor.
*
* @param logic as the logic of this server which holds the model as a ServerGameLogic object.
*/
public ServerCardVisitor(ServerGameLogic logic) {
this.logic = logic;
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a TurboCard object.
*/
@Override
public void visit(TurboCard card) {
this.cards.add(card);
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a SwapCard object.
*/
@Override
public void visit(SwapCard card) {
List<Piece> possibleOwnPieces = new ArrayList<>();
for (Piece piece : this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor()).getPieces()) {
if (piece.getState() == PieceState.ACTIVE) {
if (!possibleOwnPieces.contains(piece)) {
possibleOwnPieces.add(piece);
}
}
}
List<Piece> possibleOtherPieces = new ArrayList<>();
for (var player : this.logic.getGame().getPlayers().values()) {
if (player != this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor())) {
for (Piece piece : player.getPieces()) {
if (piece.getState() == PieceState.ACTIVE) {
if (!possibleOtherPieces.contains(piece)) {
possibleOtherPieces.add(piece);
}
}
}
}
}
if (!possibleOtherPieces.isEmpty() && !possibleOwnPieces.isEmpty()) {
this.swapOwnPieces.addAll(possibleOtherPieces);
this.swapOtherPieces.addAll(possibleOwnPieces);
this.cards.add(card);
}
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a ShieldCard object.
*/
@Override
public void visit(ShieldCard card) {
for (Piece piece : this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor()).getPieces()) {
if (piece.getState() == PieceState.ACTIVE) {
if (!this.shieldPieces.contains(piece)) {
this.shieldPieces.add(piece);
}
if (!this.cards.contains(card)) {
this.cards.add(card);
}
}
}
}
/**
* This method will be used to visit the given card parameter.
*
* @param card as a HiddenCard object.
*/
@Override
public void visit(HiddenCard card) {
}
/**
* This method will be used to return cards attribute of ServerCardVisitor class.
*
* @return cards as a List of PowerCard objects.
*/
public List<PowerCard> getCards() {
return this.cards;
}
/**
* This method will be used to return shieldPieces attribute of ServerCardVisitor class.
*
* @return shieldPieces as a List of Piece objects.
*/
public List<Piece> getShieldPieces() {
return this.shieldPieces;
}
/**
* This method will be used to return swapOwnPieces attribute of ServerCardVisitor class.
*
* @return swapOwnPieces as a List of Piece objects.
*/
public List<Piece> getSwapOwnPieces() {
return this.swapOwnPieces;
}
/**
* This method will be used to return swapOtherPieces attribute of ServerCardVisitor class.
*
* @return swapOtherPieces as a List of Piece objects.
*/
public List<Piece> getSwapOtherPieces() {
return this.swapOtherPieces;
}
}

View File

@@ -0,0 +1,39 @@
package pp.mdga.visitor;
import pp.mdga.game.card.HiddenCard;
import pp.mdga.game.card.ShieldCard;
import pp.mdga.game.card.SwapCard;
import pp.mdga.game.card.TurboCard;
/**
* This interface will be used to realize the visitor pattern inside the application.
*/
public interface Visitor {
/**
* This method will be used to visit the given card parameter.
*
* @param card as a TurboCard object.
*/
void visit(TurboCard card);
/**
* This method will be used to visit the given card parameter.
*
* @param card as a SwapCard object.
*/
void visit(SwapCard card);
/**
* This method will be used to visit the given card parameter.
*
* @param card as a ShieldCard oblect
*/
void visit(ShieldCard card);
/**
* This method will be used to visit the given card parameter.
*
* @param card as a HiddenCard object.
*/
void visit(HiddenCard card);
}