Merge branch 'dev/client' into dev/client_beck

This commit is contained in:
Cedric Beck
2024-12-01 21:55:37 +01:00
23 changed files with 491 additions and 200 deletions

View File

@@ -59,8 +59,8 @@ public static void main(String[] args) {
AppSettings settings = new AppSettings(true); AppSettings settings = new AppSettings(true);
settings.setSamples(128); settings.setSamples(128);
settings.setCenterWindow(true); settings.setCenterWindow(true);
settings.setWidth(1800); settings.setWidth(1920);
settings.setHeight(900); settings.setHeight(1080);
settings.setVSync(false); settings.setVSync(false);
MdgaApp app = new MdgaApp(); MdgaApp app = new MdgaApp();
@@ -91,7 +91,7 @@ public void simpleInitApp() {
gameView = new GameView(this); gameView = new GameView(this);
ceremonyView = new CeremonyView(this); ceremonyView = new CeremonyView(this);
enter(MdgaState.GAME); enter(MdgaState.MAIN);
} }
/** /**

View File

@@ -127,6 +127,7 @@ public void setReady(boolean ready) {
test++; test++;
if(test > 2) { if(test > 2) {
testColor = null;
test = 0; test = 0;
enter(MdgaState.GAME); enter(MdgaState.GAME);
} }

View File

@@ -13,6 +13,7 @@
import java.io.IOException; import java.io.IOException;
import java.lang.System.Logger; import java.lang.System.Logger;
import java.lang.System.Logger.Level; import java.lang.System.Logger.Level;
import java.sql.Connection;
import java.util.Map; import java.util.Map;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
@@ -25,6 +26,7 @@ public class MdgaServer implements MessageListener<HostedConnection>, Connection
private static final Logger LOGGER = System.getLogger(MdgaServer.class.getName()); private static final Logger LOGGER = System.getLogger(MdgaServer.class.getName());
private Server myServer; private Server myServer;
private static int port;
private final ServerGameLogic logic; private final ServerGameLogic logic;
private final BlockingQueue<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>(); private final BlockingQueue<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>();
@@ -40,37 +42,38 @@ public class MdgaServer implements MessageListener<HostedConnection>, Connection
} }
/** /**
* Starts the Battleships server. * Constructor.
*
* @param port as the port for this server
*/ */
public static void main(String[] args) { public MdgaServer(int port) {
new MdgaServer().run(); MdgaServer.port = port;
}
/**
* Creates a new MdgaServer.
*/
public MdgaServer() {
LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS
logic = new ServerGameLogic(this, new Game()); logic = new ServerGameLogic(this, new Game());
} }
/**
*
*/
public void run() { public void run() {
startServer(); startServer();
this.connectionAdded(myServer, myServer.getConnection(0));
while (true) while (true)
processNextMessage(); processNextMessage();
} }
/**
*
*/
private void startServer() { private void startServer() {
try { try {
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
myServer = Network.createServer(1234); myServer = Network.createServer(port);
initializeSerializables(); initializeSerializables();
myServer.start(); myServer.start();
registerListeners(); registerListeners();
LOGGER.log(Level.INFO, "Server started: {0}", myServer.isRunning()); //NON-NLS LOGGER.log(Level.INFO, "Server started: {0}", myServer.isRunning()); //NON-NLS
} catch (IOException e) { }
catch (IOException e) {
LOGGER.log(Level.ERROR, "Couldn't start server: {0}", e.getMessage()); //NON-NLS LOGGER.log(Level.ERROR, "Couldn't start server: {0}", e.getMessage()); //NON-NLS
exit(1); exit(1);
} }
@@ -154,8 +157,30 @@ private void registerListeners() {
myServer.addConnectionListener(this); myServer.addConnectionListener(this);
} }
/**
* This method will be used to receive network messages from the given source parameter.
* It will check if the given message parameter is a ClientMessage object. If yes it will call the messageReceived
* method with the casted ClientMessage object.
*
* @param source as the connection which sends the message as a HostedConnection object.
* @param message as the received message as a Message object.
*/
@Override
public void messageReceived(HostedConnection source, Message message) {
if (message instanceof ClientMessage) {
this.messageReceived(source, (ClientMessage) message);
}
}
public void messageReceived(HostedConnection source, ClientMessage message) { /**
* This method will be used to received network messages from the given source parameter.
* It will add the given message parameter to the pendingMessage attribute of MdgaServer after creating
* a ReceivedMessage object with it and its id.
*
* @param source as the connection which sends the message as a HostedConnection object.
* @param message as the received message as a Message object.
*/
private void messageReceived(HostedConnection source, ClientMessage message) {
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
pendingMessages.add(new ReceivedMessage(message, source.getId())); pendingMessages.add(new ReceivedMessage(message, source.getId()));
} }
@@ -163,8 +188,6 @@ public void messageReceived(HostedConnection source, ClientMessage message) {
@Override @Override
public void connectionAdded(Server server, HostedConnection hostedConnection) { public void connectionAdded(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
// ToDo: Synchronize data between server and client.
logic.getGame().addPlayer(hostedConnection.getId(), new Player());
} }
@Override @Override
@@ -203,6 +226,7 @@ public void exit(int exitValue) { //NON-NLS
* @param id the connection id * @param id the connection id
* @param message the message * @param message the message
*/ */
@Override
public void send(int id, ServerMessage message) { public void send(int id, ServerMessage message) {
if (myServer == null || !myServer.isRunning()) { if (myServer == null || !myServer.isRunning()) {
LOGGER.log(Level.ERROR, "no server running when trying to send {0}", message); //NON-NLS LOGGER.log(Level.ERROR, "no server running when trying to send {0}", message); //NON-NLS
@@ -221,15 +245,20 @@ public void send(int id, ServerMessage message) {
* *
* @param message as the message which will be sent to all players as a ServerMessage. * @param message as the message which will be sent to all players as a ServerMessage.
*/ */
@Override
public void broadcast(ServerMessage message) { public void broadcast(ServerMessage message) {
for (Map.Entry<Integer, Player> entry: this.logic.getGame().getPlayers().entrySet()) { for (Map.Entry<Integer, Player> entry: this.logic.getGame().getPlayers().entrySet()) {
this.send(entry.getKey(), message); this.send(entry.getKey(), message);
} }
} }
//TODO: /**
* This method will be used to diconenect the client depending on the given id parameter.
*
* @param id as the connection id of the client as an Integer.
*/
@Override @Override
public void messageReceived(HostedConnection source, Message m) { public void disconnectClient(int id) {
this.myServer.getConnection(id).close("");
} }
} }

View File

@@ -58,6 +58,12 @@ public void setPieceOnBoard(int index, Piece piece) {
infield[index].setOccupant(piece); infield[index].setOccupant(piece);
} }
/**
* This method returns the index of a specific piece on the board
*
* @param piece the piece to be searched for
* @return the index of the piece
*/
public int getInfieldIndexOfPiece(Piece piece) { public int getInfieldIndexOfPiece(Piece piece) {
for (int i = 0; i < infield.length; i++) { for (int i = 0; i < infield.length; i++) {
if (infield[i].getOccupant() == piece) { if (infield[i].getOccupant() == piece) {

View File

@@ -1,8 +1,23 @@
package pp.mdga.game; package pp.mdga.game;
/**
* Enum representing the different types of bonus cards.
*/
public enum BonusCard { public enum BonusCard {
/**
* The hidden bonus card.
*/
HIDDEN, HIDDEN,
/**
* The shield bonus card.
*/
SHIELD, SHIELD,
/**
* The turbo bonus card.
*/
TURBO, TURBO,
/**
* The swap bonus card.
*/
SWAP SWAP
} }

View File

@@ -1,20 +1,7 @@
package pp.mdga.game; package pp.mdga.game;
public class BonusNode extends Node {
public BonusNode() {}
/** /**
* This method is used to set a new Occupant * This class represents a BonusNode
*
* @param occupant the new occupant of the node
*/ */
@Override public class BonusNode extends Node {
public void setOccupant(Piece occupant) {
if (occupant.isSuppressed()){
occupant.setShield(ShieldState.NONE);
}
//Draw Card Event
this.occupant = occupant;
}
} }

View File

@@ -26,16 +26,33 @@ public enum Color {
*/ */
NONE; NONE;
/**
* This method will be used to return a Color enumeration depending on the given index parameter.
*
* @param index as the index of the color inside the values as an Integer.
* @return a Color enumeration.
*/
public static Color getColorByIndex(int index) {
if (index < 0 || index >= values().length) {
throw new IllegalArgumentException("");
}
return values()[index];
}
/** /**
* This method will be used to calculate the next color inside the sequence. * This method will be used to calculate the next color inside the sequence.
* *
* @return color as a Color Enumeration. * @return color as a Color Enumeration.
*/ */
public Color next() { public Color next() {
if (this.next() == NONE) { Color[] colors = values();
return AIRFORCE; int nextIndex = (this.ordinal() + 1) % colors.length;
if (colors[nextIndex] == NONE) {
nextIndex = (nextIndex + 1) % colors.length;
} }
return values()[(ordinal() + 1) % values().length]; return colors[nextIndex];
} }
} }

View File

@@ -0,0 +1,133 @@
package pp.mdga.game;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
/**
* This class represents a simple die with the possibilities to shuffle and return the roll result.
* An important fact, the sum of ZERO_EYE, NORMAL_EYE and DOUBLE_EYE have to be 1.0;
*/
public class Die {
/**
* The maximum number of eyes on the die.
*/
public static final int MAXIMUM_EYES = 6;
/**
* The probability of rolling a zero eye.
*/
public static final double ZERO_EYE = 0.2;
/**
* The probability of rolling a normal eye.
*/
public static final double NORMAL_EYE = 0.2;
/**
* The probability of rolling a double eye.
*/
public static final double DOUBLE_EYE = 0.6;
/**
* The modifier applied to the die roll.
*/
private int dieModifier = 1;
/**
* The random number generator used for die rolls.
*/
private final RandomGenerator random;
/**
* The result of the last die roll.
*/
private int lastNumberOfDice;
/**
* This constructor is used to create a new Die object with a random seed.
*/
public Die() {
this.random = RandomGeneratorFactory.of("Random").create();
}
/**
* This constructor is used to create a new Die object with a given seed.
*
* @param seed the seed for the random number generator
*/
public Die(long seed) {
this.random = RandomGeneratorFactory.of("Random").create(seed);
}
/**
* This method will be used to return a random number generated by the random attribute of Die class.
*
* @return lastNumberOfDice as an Integer.
*/
public int shuffle() {
this.lastNumberOfDice = this.random.nextInt(MAXIMUM_EYES) + 1;
return this.lastNumberOfDice;
}
/**
* This method will be used to return the given roll parameter.
* It will be used for test cases.
*
* @param roll as the roll result which will be returned as an Integer.
* @return lastNumberOfDice as an Integer.
*/
public int shuffle(int roll) {
this.lastNumberOfDice = roll;
return this.lastNumberOfDice;
}
/**
* This method will be used to modify the value of dieModifier attribute of Die class.
*/
public void modify() {
float randomFloat = this.random.nextFloat();
if (randomFloat < ZERO_EYE) {
this.dieModifier = 0;
} else if (ZERO_EYE <= randomFloat && randomFloat < ZERO_EYE + NORMAL_EYE) {
this.dieModifier = 1;
} else if (ZERO_EYE + NORMAL_EYE <= randomFloat && randomFloat < ZERO_EYE + NORMAL_EYE + DOUBLE_EYE) {
this.dieModifier = 2;
}
}
/**
* This method will be used to reset all values of the Die class to its origins.
*/
public void reset() {
this.dieModifier = 1;
}
/**
* This method will be used to return dieModifier attribute of Die class.
*
* @return dieModifier as an Integer.
*/
public int getDieModifier() {
return this.dieModifier;
}
/**
* This method will be used return lastNumberOfDice attribute of Die class.
*
* @return lastNumberOfDice as an Integer.
*/
public int getLastNumberOfDice() {
return this.lastNumberOfDice;
}
/**
* This method will be used to set dieModifier attribute of Die class to the given dieModifier parameter.
*
* @param dieModifier as the new value of dieModifier as an Integer.
*/
public void setDieModifier(int dieModifier) {
this.dieModifier = dieModifier;
}
}

View File

@@ -9,25 +9,43 @@
*/ */
public class Game { public class Game {
/** /**
* Constants. * The number of turbo cards available in the game.
*/ */
public static final int AMOUNT_OF_TURBO_CARDS = 16; public static final int AMOUNT_OF_TURBO_CARDS = 16;
public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
/** /**
* Attributes. * The number of shield and swap cards available in the game.
*/ */
public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
// The modifier applied to the dice roll.
private int diceModifier = 1; private int diceModifier = 1;
// The number of eyes shown on the dice.
private int diceEyes; private int diceEyes;
// A map of player IDs to Player objects.
private Map<Integer, Player> players = new HashMap<>(); private Map<Integer, Player> players = new HashMap<>();
// The statistics of the game.
private Statistic gameStatistics; private Statistic gameStatistics;
// The pile of bonus cards available for drawing.
private List<BonusCard> drawPile; private List<BonusCard> drawPile;
// The pile of bonus cards that have been discarded.
private List<BonusCard> discardPile = new ArrayList<>(); private List<BonusCard> discardPile = new ArrayList<>();
// The game board.
private Board board; private Board board;
// The die used in the game.
private Die die;
// The color of the active player.
private Color activeColor; private Color activeColor;
private final ArrayList<Observer> observers = new ArrayList<>();
private boolean allRanked = false; // A flag indicating whether all players are ready.
private boolean movablePieces = false;
private boolean allReady = false; private boolean allReady = false;
/** /**
@@ -66,27 +84,10 @@ public void removePlayer(int id) {
} }
/** /**
* This method adds an observer to the game. * This method updates the active state of a player.
* *
* @param observer the observer to be added * @param id the id of the player
*/ * @param active the new active state
public void addObserver(Observer observer) {
observers.add(observer);
}
/**
* This method removes an observer from the game.
*
* @param observer the observer to be removed
*/
public void removeObserver(Observer observer) {
observers.remove(observer);
}
/**
*
* @param id
* @param active
*/ */
public void updatePlayerActiveState(int id, boolean active) { public void updatePlayerActiveState(int id, boolean active) {
this.players.get(id).setActive(active); this.players.get(id).setActive(active);
@@ -150,15 +151,6 @@ public Piece getPieceThroughUUID(UUID pieceId){
return null; return null;
} }
/**
* This method notifies the observers.
*/
public void notifyObservers() {
for (Observer observer : new ArrayList<>(observers)) {
//TODO: observer.update();
}
}
/** /**
* This method returns the dice modifier. * This method returns the dice modifier.
* *
@@ -222,6 +214,15 @@ public Board getBoard() {
return board; return board;
} }
/**
* This method will be used to return die attribute of Game class.
*
* @return die as a Die object.
*/
public Die getDie() {
return this.die;
}
/** /**
* This method returns the active color. * This method returns the active color.
* *
@@ -231,15 +232,6 @@ public Color getActiveColor() {
return activeColor; return activeColor;
} }
/**
* This method returns whether the game is interrupted.
*
* @return true if the game is interrupted, false otherwise
*/
public Boolean getMovablePieces() {
return movablePieces;
}
/** /**
* This method sets the dice modifier. * This method sets the dice modifier.
* *
@@ -312,35 +304,6 @@ public void setActiveColor(Color activeColor) {
this.activeColor = activeColor; this.activeColor = activeColor;
} }
/**
* This method sets the game interruption state.
*
* @param movablePieces the new game interruption state
*/
public void setMovablePieces(Boolean movablePieces) {
this.movablePieces = movablePieces;
if (Boolean.FALSE.equals(movablePieces)) notifyObservers();
}
/**
* This method returns whether all players have ranked.
*
* @return true if all players have ranked, false otherwise
*/
public Boolean allRanked() {
return allRanked;
}
/**
* This method sets whether all players have ranked.
*
* @param allRanked the new all ranked state
*/
public void setAllRanked(Boolean allRanked) {
this.allRanked = allRanked;
if (Boolean.TRUE.equals(allRanked)) notifyObservers();
}
/** /**
* This method returns the all ready state. * This method returns the all ready state.
* *
@@ -357,6 +320,5 @@ public Boolean allReady() {
*/ */
public void setAllReady(Boolean allReady) { public void setAllReady(Boolean allReady) {
this.allReady = allReady; this.allReady = allReady;
if (Boolean.TRUE.equals(allReady)) notifyObservers();
} }
} }

View File

@@ -1,6 +1,7 @@
package pp.mdga.game; package pp.mdga.game;
/**
* Represents a home node.
*/
public class HomeNode extends Node { public class HomeNode extends Node {
public HomeNode() {}
} }

View File

@@ -6,8 +6,6 @@
public class Node { public class Node {
protected Piece occupant; protected Piece occupant;
public Node() {}
/** /**
* This method is used to get an occupant of the Node. * This method is used to get an occupant of the Node.
* *

View File

@@ -6,9 +6,24 @@
* This class will be used to hold all Piece relevant data. * This class will be used to hold all Piece relevant data.
*/ */
public class Piece { public class Piece {
/**
* The shield state of the piece.
*/
private ShieldState shield; private ShieldState shield;
/**
* The current state of the piece.
*/
private PieceState state; private PieceState state;
/**
* The color of the piece.
*/
private final Color color; private final Color color;
/**
* The unique identifier of the piece.
*/
private final UUID uuid = UUID.randomUUID(); private final UUID uuid = UUID.randomUUID();
/** /**

View File

@@ -1,8 +1,23 @@
package pp.mdga.game; package pp.mdga.game;
/**
* Represents the state of a piece.
*/
public enum PieceState { public enum PieceState {
/**
* The piece is active.
*/
ACTIVE, ACTIVE,
/**
* The piece is waiting.
*/
WAITING, WAITING,
/**
* The piece is in the home.
*/
HOME, HOME,
/**
* The piece is finished.
*/
HOMEFINISHED HOMEFINISHED
} }

View File

@@ -6,11 +6,34 @@
* This class will be used to handle general PlayerData * This class will be used to handle general PlayerData
*/ */
public class Player { public class Player {
/**
* The name of the player.
*/
private String name; private String name;
/**
* The statistics of the player.
*/
private Statistic playerStatistic; private Statistic playerStatistic;
/**
* The hand cards of the player.
*/
private ArrayList<BonusCard> handCards; private ArrayList<BonusCard> handCards;
/**
* The color of the player.
*/
private Color color = Color.NONE; private Color color = Color.NONE;
/**
* Indicates if the player is ready.
*/
private boolean isReady; private boolean isReady;
/**
* Indicates if the player is active.
*/
private boolean active = true; private boolean active = true;
/** /**
@@ -72,7 +95,7 @@ public ArrayList<BonusCard> getHandCards() {
* *
* @param card the card to be added to the players hand * @param card the card to be added to the players hand
*/ */
public void addHandCards(BonusCard card) { public void addHandCard(BonusCard card) {
handCards.add(card); handCards.add(card);
} }

View File

@@ -4,9 +4,24 @@
* This class is used to represent PlayerData related to the board * This class is used to represent PlayerData related to the board
*/ */
public class PlayerData { public class PlayerData {
/**
* An array of HomeNode objects representing the home nodes of the player.
*/
private HomeNode[] homeNodes; private HomeNode[] homeNodes;
/**
* The index of the start node for the player.
*/
private int startNodeIndex; private int startNodeIndex;
/**
* An array of Piece objects representing the waiting area of the player.
*/
private Piece[] waitingArea; private Piece[] waitingArea;
/**
* An array of Piece objects representing all the pieces of the player.
*/
private Piece[] pieces; private Piece[] pieces;
/** /**

View File

@@ -1,7 +1,19 @@
package pp.mdga.game; package pp.mdga.game;
/**
* Represents the state of a piece's shield.
*/
public enum ShieldState { public enum ShieldState {
/**
* The shield is not active.
*/
NONE, NONE,
/**
* The shield is active.
*/
ACTIVE, ACTIVE,
/**
* The shield is suppressed, when the piece is on a start node.
*/
SUPPRESSED SUPPRESSED
} }

View File

@@ -1,24 +1,21 @@
package pp.mdga.game; package pp.mdga.game;
/**
* Represents a start node.
*/
public class StartNode extends Node { public class StartNode extends Node {
/**
public StartNode(Color color) { * The color of the node.
this.color = color; */
}
private Color color; private Color color;
/** /**
* This method is used to set a new Occupant * Creates a new start node with the given color.
* *
* @param occupant the new occupant of the node * @param color the color of the node
*/ */
@Override public StartNode(Color color) {
public void setOccupant(Piece occupant) { this.color = color;
if (occupant.isShielded()){
occupant.setShield(ShieldState.SUPPRESSED);
}
this.occupant = occupant;
} }
/** /**

View File

@@ -4,13 +4,39 @@
* This class will be used to store Statistics during the Game; * This class will be used to store Statistics during the Game;
*/ */
public class Statistic { public class Statistic {
/**
* The number of cards played.
*/
private int cardsPlayed; private int cardsPlayed;
/**
* The number of pieces thrown. (enemy)
*/
private int piecesThrown; private int piecesThrown;
/**
* The number of pieces being thrown. (own pieces)
*/
private int piecesBeingThrown; private int piecesBeingThrown;
/**
* The number of times a 6 was diced.
*/
private int diced6; private int diced6;
/**
* The number of nodes traveled.
*/
private int traveledNodes; private int traveledNodes;
/**
* The number of bonus nodes activated.
*/
private int activatedBonusNodes; private int activatedBonusNodes;
/**
* Constructs a new Statistic object with all values initialized to 0.
*/
public Statistic() { public Statistic() {
cardsPlayed = 0; cardsPlayed = 0;
piecesThrown = 0; piecesThrown = 0;

View File

@@ -23,6 +23,15 @@ public StartGameMessage() {
forceStartGame = false; forceStartGame = false;
} }
/**
* Gets whether the game should be force started.
*
* @return whether the game should be force started
*/
public boolean isForceStartGame() {
return forceStartGame;
}
/** /**
* Returns a string representation of this message. * Returns a string representation of this message.
* *

View File

@@ -13,21 +13,14 @@ public class LobbyPlayerLeaveMessage extends ServerMessage {
*/ */
private final int id; private final int id;
/**
* The color associated with the player leaving the lobby.
*/
private final Color color;
/** /**
* Constructs a new LobbyPlayerLeave instance with the specified player name and color. * Constructs a new LobbyPlayerLeave instance with the specified player name and color.
* *
* @param id the id of the player leaving the lobby * @param id the id of the player leaving the lobby.
* @param color the color associated with the player leaving the lobby
*/ */
public LobbyPlayerLeaveMessage(int id, Color color) { public LobbyPlayerLeaveMessage(int id) {
super(); super();
this.id = id; this.id = id;
this.color = color;
} }
/** /**
@@ -35,7 +28,6 @@ public LobbyPlayerLeaveMessage(int id, Color color) {
*/ */
private LobbyPlayerLeaveMessage() { private LobbyPlayerLeaveMessage() {
id = 0; id = 0;
color = null;
} }
/** /**
@@ -47,15 +39,6 @@ public int getId() {
return id; return id;
} }
/**
* Returns the color associated with the player leaving the lobby.
*
* @return the color associated with the player leaving the lobby
*/
public Color getColor() {
return color;
}
/** /**
* Accepts a visitor to process this message. * Accepts a visitor to process this message.
* *

View File

@@ -21,4 +21,11 @@ public interface ServerSender {
* @param message as the message which will be sent to all players as a ServerMessage. * @param message as the message which will be sent to all players as a ServerMessage.
*/ */
void broadcast(ServerMessage message); void broadcast(ServerMessage message);
/**
* This method will be used to disconnect the client depending on the given id parameter.
*
* @param id as the connection id of the client as an Integer.
*/
void disconnectClient(int id);
} }

View File

@@ -1,13 +1,16 @@
package pp.mdga.server.automaton; package pp.mdga.server.automaton;
import pp.mdga.game.Color;
import pp.mdga.game.Player;
import pp.mdga.message.client.*; import pp.mdga.message.client.*;
import pp.mdga.message.server.ServerStartGameMessage; import pp.mdga.message.server.*;
import pp.mdga.message.server.UpdateReadyMessage;
import pp.mdga.message.server.UpdateTSKMessage;
import pp.mdga.server.ServerGameLogic; import pp.mdga.server.ServerGameLogic;
import java.util.Map;
/** /**
* * This class represents the lobby state of the server.
* It will handle all join and disconnect messages, as well the selection of the color of the player.
*/ */
public class LobbyState extends ServerState { public class LobbyState extends ServerState {
/** /**
@@ -36,53 +39,90 @@ public void exit() {
} }
/** /**
* * @param msg as the message which was sent by the player as a JoinedLobbyMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(JoinedLobbyMessage msg, int from) {
Player player = new Player(msg.getName());
this.logic.getGame().addPlayer(from, player);
this.logic.getServerSender().broadcast(new LobbyPlayerJoinedMessage(from, player));
}
/**
* @param msg as the message which was sent by the player as a SelectTSK object. * @param msg as the message which was sent by the player as a SelectTSK object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
@Override @Override
public void received(SelectTSKMessage msg, int from) { public void received(SelectTSKMessage msg, int from) {
for (Map.Entry<Integer, Player> entry : this.logic.getGame().getPlayers().entrySet()) {
if (entry.getValue().getColor() == msg.getColor()) {
return;
}
}
this.logic.getGame().getPlayerById(from).setColor(msg.getColor());
this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor())); this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor()));
} }
/** /**
*
* @param msg as the message which was sent by the player as a DeselectTSK object. * @param msg as the message which was sent by the player as a DeselectTSK object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
@Override @Override
public void received(DeselectTSKMessage msg, int from) { public void received(DeselectTSKMessage msg, int from) {
this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, msg.getColor())); this.logic.getGame().getPlayerById(from).setColor(Color.NONE);
this.logic.getServerSender().broadcast(new UpdateTSKMessage(from, Color.NONE));
} }
/** /**
*
* @param msg as the message which was sent by the player as a LobbyReady object. * @param msg as the message which was sent by the player as a LobbyReady object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
@Override @Override
public void received(LobbyReadyMessage msg, int from) { public void received(LobbyReadyMessage msg, int from) {
this.logic.getGame().getPlayerById(from).setReady(true);
this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, true)); this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, true));
for (Map.Entry<Integer, Player> entry : this.logic.getGame().getPlayers().entrySet()) {
if (!entry.getValue().isActive()) {
return;
}
}
this.logic.getGame().setAllReady(true);
} }
/** /**
*
* @param msg as the message which was sent by the player as a LobbyNotReady object. * @param msg as the message which was sent by the player as a LobbyNotReady object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
@Override @Override
public void received(LobbyNotReadyMessage msg, int from) { public void received(LobbyNotReadyMessage msg, int from) {
this.logic.getGame().getPlayerById(from).setReady(false);
this.logic.getGame().setAllReady(false);
this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, false)); this.logic.getServerSender().broadcast(new UpdateReadyMessage(from, false));
} }
/** /**
* *
* @param msg as the message which was sent by the player as a LeaveGameMessage object.
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(LeaveGameMessage msg, int from) {
this.logic.getGame().removePlayer(from);
this.logic.getServerSender().broadcast(new LobbyPlayerLeaveMessage(from));
this.logic.getServerSender().disconnectClient(from);
}
/**
* @param msg as the message which was sent by the player as a ForceStartGame object. * @param msg as the message which was sent by the player as a ForceStartGame object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
@Override @Override
public void received(StartGameMessage msg, int from) { public void received(StartGameMessage msg, int from) {
if (msg.isForceStartGame() || this.logic.getGame().allReady()) {
this.logic.getServerSender().broadcast(new ServerStartGameMessage()); this.logic.getServerSender().broadcast(new ServerStartGameMessage());
this.logic.setCurrentState(this.logic.getGameState()); this.logic.setCurrentState(this.logic.getGameState());
} }
} }
}

View File

@@ -33,19 +33,19 @@ public ServerState(ServerGameLogic logic) {
public abstract void exit(); public abstract void exit();
/** /**
* This method will be called whenever the server received an AnimationEnd message. * 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. * 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 AnimationEnd object. * @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. * @param from as the client id of the player as an Integer.
*/ */
public void received(AnimationEndMessage msg, int from) {} public void received(AnimationEndMessage msg, int from) {}
/** /**
* This method will be called whenever the server received an DeselectTSK message. * This method will be called whenever the server received a DeselectTSKMessage message.
* It will also get the client id of the player who send this 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 DeselectTSK object. * @param msg as the message which was sent by the player as a DeselectTSKMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(DeselectTSKMessage msg, int from) {} public void received(DeselectTSKMessage msg, int from) {}
@@ -60,136 +60,136 @@ public void received(DeselectTSKMessage msg, int from) {}
public void received(StartGameMessage msg, int from) {} public void received(StartGameMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a JoinServer message. * This method will be called whenever the server received a JoinedLobbyMessage message.
* It will also get the client id of the player who send this 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 JoinServer object. * @param msg as the message which was sent by the player as a JoinedLobbyMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(JoinedLobbyMessage msg, int from) {} public void received(JoinedLobbyMessage msg, int from) {}
/** /**
* This method will be called whenever the server received an LeaveGame message. * This method will be called whenever the server received an LeaveGameMessage message.
* It will also get the client id of the player who send this 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 LeaveGame object. * @param msg as the message which was sent by the player as a LeaveGameMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(LeaveGameMessage msg, int from) {} public void received(LeaveGameMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a LobbyReady message. * This method will be called whenever the server received a LobbyReadyMessage message.
* It will also get the client id of the player who send this 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 LobbyReady object. * @param msg as the message which was sent by the player as a LobbyReadyMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(LobbyReadyMessage msg, int from) {} public void received(LobbyReadyMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a LobbyNotReady message. * This method will be called whenever the server received a LobbyNotReadyMessage message.
* It will also get the client id of the player who send this 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 LobbyNotReady object. * @param msg as the message which was sent by the player as a LobbyNotReadyMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(LobbyNotReadyMessage msg, int from) {} public void received(LobbyNotReadyMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a Disconnected message. * This method will be called whenever the server received a DisconnectedMessage message.
* It will also get the client id of the player who send this 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 Disconnected object. * @param msg as the message which was sent by the player as a DisconnectedMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(DisconnectedMessage msg, int from) {} public void received(DisconnectedMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a Briefing message. * This method will be called whenever the server received a RequestBriefingMessage message.
* It will also get the client id of the player who send this 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 Briefing object. * @param msg as the message which was sent by the player as a RequestBriefingMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(RequestBriefingMessage msg, int from) {} public void received(RequestBriefingMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a Die message. * 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. * 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 Die object. * @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. * @param from as the client id of the player as an Integer.
*/ */
public void received(RequestDieMessage msg, int from) {} public void received(RequestDieMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a RequestMove message. * This method will be called whenever the server received a RequestMoveMessage message.
* It will also get the client id of the player who send this 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 RequestMove object. * @param msg as the message which was sent by the player as a RequestMoveMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(RequestMoveMessage msg, int from) {} public void received(RequestMoveMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a PlayCard message. * This method will be called whenever the server received a RequestPlayCardMessage message.
* It will also get the client id of the player who send this 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 PlayCard object. * @param msg as the message which was sent by the player as a RequestPlayCardMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(RequestPlayCardMessage msg, int from) {} public void received(RequestPlayCardMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a SelectCard message. * This method will be called whenever the server received a SelectCardMessage message.
* It will also get the client id of the player who send this 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 SelectCard object. * @param msg as the message which was sent by the player as a SelectCardMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(SelectCardMessage msg, int from) {} public void received(SelectCardMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a SelectTSK message. * This method will be called whenever the server received a SelectTSKMessage message.
* It will also get the client id of the player who send this 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 SelectTSK object. * @param msg as the message which was sent by the player as a SelectTSKMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(SelectTSKMessage msg, int from) {} public void received(SelectTSKMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a ForceContinueGame message. * This method will be called whenever the server received a ForceContinueGameMessage message.
* It will also get the client id of the player who send this 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 ForceContinueGame object. * @param msg as the message which was sent by the player as a ForceContinueGameMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(ForceContinueGameMessage msg, int from) {} public void received(ForceContinueGameMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a ClientStartGame message. * This method will be called whenever the server received a ClientStartGameMessage message.
* It will also get the client id of the player who send this 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 ClientStartGame object. * @param msg as the message which was sent by the player as a ClientStartGameMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(ClientStartGameMessage msg, int from) {} public void received(ClientStartGameMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a NoPowerCard message. * This method will be called whenever the server received a NoPowerCardMessage message.
* It will also get the client id of the player who send this 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 NoPowerCard object. * @param msg as the message which was sent by the player as a NoPowerCardMessage object.
* @param from as the client id of the player as an Integer. * @param from as the client id of the player as an Integer.
*/ */
public void received(NoPowerCardMessage msg, int from) {} public void received(NoPowerCardMessage msg, int from) {}
/** /**
* This method will be called whenever the server received a SelectedPieces message. * 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. * 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 SelectedPieces object. * @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. * @param from as the client id of the player as an Integer.
*/ */
public void received(SelectedPiecesMessage msg, int from) {} public void received(SelectedPiecesMessage msg, int from) {}